dte test coverage


Directory: ./
File: test/dump.c
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 44 47 93.6%
Functions: 1 1 100.0%
Branches: 15 20 75.0%

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 (47→9) taken 14 times.
✓ Branch 1 (47→48) 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 (13→14) not taken.
✓ Branch 1 (13→15) 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 (44→18) taken 562 times.
✓ Branch 1 (44→45) taken 14 times.
576 for (size_t pos = 0, len = str.len; pos < len; ) {
67 562 bool check_parse = !!(handler->flags & SHOW_DTERC);
68 562 bool check_name = handlers[i].check_name;
69 562 EXPECT_TRUE(!check_name || check_parse);
70
71 562 const char *line = buf_next_line(str.buffer, &pos, len);
72 562 ASSERT_NONNULL(line);
73
4/4
✓ Branch 0 (21→22) taken 550 times.
✓ Branch 1 (21→23) taken 12 times.
✓ Branch 2 (22→23) taken 118 times.
✓ Branch 3 (22→24) taken 432 times.
562 if (line[0] == '\0' || line[0] == '#' || !check_parse) {
74 130 continue;
75 }
76
77 432 PointerArray arr = PTR_ARRAY_INIT;
78 432 CommandParseError parse_err = parse_commands(&runner, &arr, line);
79 432 EXPECT_EQ(parse_err, CMDERR_NONE);
80 432 EXPECT_TRUE(arr.count >= 2);
81
2/4
✓ Branch 0 (27→28) taken 432 times.
✗ Branch 1 (27→29) not taken.
✗ Branch 2 (28→29) not taken.
✓ Branch 3 (28→30) taken 432 times.
432 if (parse_err != CMDERR_NONE || arr.count < 2) {
82 continue;
83 }
84
85
2/2
✓ Branch 0 (30→31) taken 342 times.
✓ Branch 1 (30→32) taken 90 times.
432 if (check_name) {
86 342 EXPECT_STREQ(arr.ptrs[0], name);
87 }
88
89 432 const Command *cmd = cmds->lookup(arr.ptrs[0]);
90 432 EXPECT_NONNULL(cmd);
91
1/2
✗ Branch 0 (34→35) not taken.
✓ Branch 1 (34→36) taken 432 times.
432 if (!cmd) {
92 continue;
93 }
94
95 432 CommandArgs a = cmdargs_new((char**)arr.ptrs + 1);
96 432 ArgParseError arg_err = do_parse_args(cmd, &a);
97 432 EXPECT_EQ(arg_err, ARGERR_NONE);
98
1/2
✗ Branch 0 (38→39) not taken.
✓ Branch 1 (38→40) taken 432 times.
432 if (arg_err != ARGERR_NONE) {
99 continue;
100 }
101 432 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