dte test coverage


Directory: ./
File: test/bind.c
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 63 63 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #include "test.h"
2 #include "bind.h"
3 #include "block.h"
4 #include "buffer.h"
5 #include "change.h"
6 #include "command/args.h"
7 #include "commands.h"
8 #include "editor.h"
9 #include "util/str-util.h"
10
11 1 static void test_add_binding(TestContext *ctx)
12 {
13 1 EditorState *e = ctx->userdata;
14 // TODO: Use a temporary IntMap, instead of normal mode bindings?
15 1 IntMap *bindings = &e->normal_mode->key_bindings;
16 1 KeyCode key = MOD_CTRL | MOD_SHIFT | KEY_F12;
17 1 const CachedCommand *bind = lookup_binding(bindings, key);
18 1 EXPECT_NULL(bind);
19
20 1 const Command *insert_cmd = find_normal_command("insert");
21 1 ASSERT_NONNULL(insert_cmd);
22
23 1 static const char cmd_str[] = "insert xyz";
24 1 CommandRunner runner = normal_mode_cmdrunner(e);
25 1 CachedCommand *cc = cached_command_new(&runner, cmd_str);
26 1 ASSERT_NONNULL(cc);
27
28 1 add_binding(bindings, key, cc);
29 1 bind = lookup_binding(bindings, key);
30 1 ASSERT_NONNULL(bind);
31 1 EXPECT_PTREQ(bind->cmd, insert_cmd);
32 1 EXPECT_EQ(bind->a.nr_args, 1);
33 1 EXPECT_EQ(bind->a.nr_flags, 0);
34 1 EXPECT_STREQ(bind->cmd_str, cmd_str);
35
36 1 remove_binding(bindings, key);
37 1 EXPECT_NULL(lookup_binding(bindings, key));
38 1 }
39
40 1 static void test_handle_binding(TestContext *ctx)
41 {
42 1 const Command *insert = find_normal_command("insert");
43 1 ASSERT_NONNULL(insert);
44
45 1 EditorState *e = ctx->userdata;
46 1 EXPECT_TRUE(handle_normal_command(e, "open; bind C-S-F11 'insert -m zzz'", false));
47
48 // Bound command should be cached
49 1 const ModeHandler *mode = e->normal_mode;
50 1 const IntMap *bindings = &mode->key_bindings;
51 1 KeyCode key = MOD_CTRL | MOD_SHIFT | KEY_F11;
52 1 const CachedCommand *binding = lookup_binding(bindings, key);
53 1 ASSERT_NONNULL(binding);
54 1 EXPECT_PTREQ(binding->cmd, insert);
55 1 EXPECT_EQ(binding->a.nr_flags, 1);
56 1 EXPECT_EQ(binding->a.nr_args, 1);
57 1 EXPECT_STREQ(binding->a.args[0], "zzz");
58 1 EXPECT_NULL(binding->a.args[1]);
59 1 EXPECT_EQ(binding->a.flags[0], 'm');
60 1 EXPECT_EQ(binding->a.flags[1], '\0');
61 1 EXPECT_TRUE(cmdargs_has_flag(&binding->a, 'm'));
62
63 1 ASSERT_TRUE(handle_binding(e, mode, key));
64 1 const Block *block = BLOCK(e->buffer->blocks.next);
65 1 ASSERT_NONNULL(block);
66 1 EXPECT_MEMEQ(block->data, block->size, "zzz\n", 4);
67 1 EXPECT_EQ(block->nl, 1);
68
69 1 ASSERT_TRUE(handle_binding(e, mode, KEY_BACKSPACE));
70 1 EXPECT_MEMEQ(block->data, block->size, "zz\n", 3);
71 1 EXPECT_EQ(block->nl, 1);
72
73 1 ASSERT_TRUE(handle_binding(e, mode, MOD_CTRL | KEY_BACKSPACE));
74 1 EXPECT_MEMEQ(block->data, block->size, "\n", 1);
75 1 EXPECT_EQ(block->nl, 1);
76
77 1 View *view = e->view;
78 1 ASSERT_NONNULL(view);
79 1 EXPECT_TRUE(undo(view));
80 1 ASSERT_EQ(block->size, 3);
81 1 EXPECT_TRUE(undo(view));
82 1 ASSERT_EQ(block->size, 4);
83 1 EXPECT_TRUE(undo(view));
84 1 EXPECT_EQ(block->size, 0);
85 1 EXPECT_EQ(block->nl, 0);
86 1 EXPECT_FALSE(undo(view));
87 1 EXPECT_TRUE(handle_normal_command(e, "close", false));
88 1 }
89
90 static const TestEntry tests[] = {
91 TEST(test_add_binding),
92 TEST(test_handle_binding),
93 };
94
95 const TestGroup bind_tests = TEST_GROUP(tests);
96