Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "test.h" | ||
2 | #include "bind.h" | ||
3 | #include "command/args.h" | ||
4 | #include "command/parse.h" | ||
5 | #include "commands.h" | ||
6 | #include "compiler.h" | ||
7 | #include "config.h" | ||
8 | #include "editor.h" | ||
9 | #include "filetype.h" | ||
10 | #include "frame.h" | ||
11 | #include "options.h" | ||
12 | #include "show.h" | ||
13 | #include "syntax/color.h" | ||
14 | #include "util/str-util.h" | ||
15 | |||
16 | static const struct { | ||
17 | const char name[9]; | ||
18 | bool check_name; | ||
19 | } handlers[] = { | ||
20 | {"alias", true}, | ||
21 | {"bind", true}, | ||
22 | {"buffer", false}, | ||
23 | {"cursor", true}, | ||
24 | {"def-mode", true}, | ||
25 | {"errorfmt", true}, | ||
26 | {"ft", true}, | ||
27 | {"hi", true}, | ||
28 | {"include", false}, | ||
29 | {"macro", false}, | ||
30 | {"open", false}, | ||
31 | {"option", false}, | ||
32 | {"set", true}, | ||
33 | {"wsplit", false}, | ||
34 | }; | ||
35 | |||
36 | 1 | static void test_dump_handlers(TestContext *ctx) | |
37 | { | ||
38 | 1 | EditorState *e = ctx->userdata; | |
39 | 1 | ASSERT_NONNULL(e); | |
40 | 1 | ASSERT_NONNULL(e->window); | |
41 | 1 | ASSERT_NONNULL(e->view); | |
42 | 1 | ASSERT_NONNULL(e->buffer); | |
43 | 1 | const CommandRunner runner = normal_mode_cmdrunner(e); | |
44 | 1 | const CommandSet *cmds = runner.cmds; | |
45 | 1 | ASSERT_NONNULL(cmds); | |
46 | 1 | ASSERT_NONNULL(cmds->lookup); | |
47 | |||
48 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
|
15 | for (size_t i = 0; i < ARRAYLEN(handlers); i++) { |
49 | 14 | const char *name = handlers[i].name; | |
50 | 14 | const ShowHandler *handler = lookup_show_handler(name); | |
51 | 14 | ASSERT_NONNULL(handler); | |
52 | 14 | ASSERT_NONNULL(handler->dump); | |
53 | 14 | String str = handler->dump(e); | |
54 | |||
55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (str.len == 0) { |
56 | // Returning an empty string is fine in general, but none of | ||
57 | // the handlers being tested here are expected to do so | ||
58 | − | TEST_FAIL("'show %s' handler returned an empty String", name); | |
59 | } else { | ||
60 | 14 | test_pass(ctx); | |
61 | // The last line of generated text must end with a newline | ||
62 | // (see comment in get_delim_str()) | ||
63 | 14 | ASSERT_EQ(str.buffer[str.len - 1], '\n'); | |
64 | } | ||
65 | |||
66 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 14 times.
|
566 | for (size_t pos = 0, len = str.len; pos < len; ) { |
67 | 552 | bool check_parse = !!(handler->flags & SHOW_DTERC); | |
68 | 552 | bool check_name = handlers[i].check_name; | |
69 | 552 | EXPECT_TRUE(!check_name || check_parse); | |
70 | |||
71 | 552 | const char *line = buf_next_line(str.buffer, &pos, len); | |
72 | 552 | ASSERT_NONNULL(line); | |
73 |
4/4✓ Branch 0 taken 540 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 430 times.
|
552 | if (line[0] == '\0' || line[0] == '#' || !check_parse) { |
74 | 122 | continue; | |
75 | } | ||
76 | |||
77 | 430 | PointerArray arr = PTR_ARRAY_INIT; | |
78 | 430 | CommandParseError parse_err = parse_commands(&runner, &arr, line); | |
79 | 430 | EXPECT_EQ(parse_err, CMDERR_NONE); | |
80 | 430 | EXPECT_TRUE(arr.count >= 2); | |
81 |
2/4✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 430 times.
|
430 | if (parse_err != CMDERR_NONE || arr.count < 2) { |
82 | ✗ | continue; | |
83 | } | ||
84 | |||
85 |
2/2✓ Branch 0 taken 342 times.
✓ Branch 1 taken 88 times.
|
430 | if (check_name) { |
86 | 342 | EXPECT_STREQ(arr.ptrs[0], name); | |
87 | } | ||
88 | |||
89 | 430 | const Command *cmd = cmds->lookup(arr.ptrs[0]); | |
90 | 430 | EXPECT_NONNULL(cmd); | |
91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
|
430 | if (!cmd) { |
92 | ✗ | continue; | |
93 | } | ||
94 | |||
95 | 430 | CommandArgs a = cmdargs_new((char**)arr.ptrs + 1); | |
96 | 430 | ArgParseError arg_err = do_parse_args(cmd, &a); | |
97 | 430 | EXPECT_EQ(arg_err, ARGERR_NONE); | |
98 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 430 times.
|
430 | if (arg_err != ARGERR_NONE) { |
99 | ✗ | continue; | |
100 | } | ||
101 | 430 | ptr_array_free(&arr); | |
102 | } | ||
103 | 14 | string_free(&str); | |
104 | } | ||
105 | 1 | } | |
106 | |||
107 | static const TestEntry tests[] = { | ||
108 | TEST(test_dump_handlers), | ||
109 | }; | ||
110 | |||
111 | const TestGroup dump_tests = TEST_GROUP(tests); | ||
112 |