diff --git a/src/tests.c b/src/tests.c index 0eed7ae..35e1737 100644 --- a/src/tests.c +++ b/src/tests.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -528,31 +529,159 @@ static void test_wm_keybind_str_to_mask_keysym(void **state) KeySym *ret; - ret = wm_keybind_str_to_mask_keysym(keybind1); + ret = wm_keybind_string_to_mask_keysym(keybind1); assert_int_equal(ret[0], Mod4Mask | ShiftMask); assert_int_equal(ret[1], XK_h); free(ret); - ret = wm_keybind_str_to_mask_keysym(keybind2); + ret = wm_keybind_string_to_mask_keysym(keybind2); assert_int_equal(ret[0], Mod1Mask); assert_int_equal(ret[1], XK_1); free(ret); - ret = wm_keybind_str_to_mask_keysym(keybind3); + ret = wm_keybind_string_to_mask_keysym(keybind3); assert_int_equal(ret[0], Mod4Mask | Mod1Mask | ShiftMask); assert_int_equal(ret[1], XK_j); free(ret); - ret = wm_keybind_str_to_mask_keysym(keybind4); + ret = wm_keybind_string_to_mask_keysym(keybind4); assert_int_equal(ret[0], LockMask); assert_int_equal(ret[1], XK_k); free(ret); } +static void test_wm_config_parse_bind_command(void **state) +{ + char line1[] = "keybind Windows+h focus left"; + + ConfigFile file; + Config config; + ConfigCommand command; + + char tmp[] = "/tmp/XXXXXX"; + mkstemp(tmp); + + wm_configfile_init(&file, tmp); + wm_configcommand_init(&command); + + ParserResult res = wm_configfile_parse_line(&file, &config, line1, &command); + + assert_true(res.success); + + Keybind keybind = wm_config_parse_bind_command(&command); + + assert_ptr_equal(keybind.function, &wm_kb_focus_dir); + assert_ptr_equal(keybind.args.i, LEFT); + + wm_configfile_free(&file); + wm_configcommand_free(&command); + + char line2[] = "keybind Alt+j move down"; + + file = (ConfigFile) {0}; + config = (Config) {0}; + command = (ConfigCommand) {0}; + + wm_configfile_init(&file, tmp); + wm_configcommand_init(&command); + + res = wm_configfile_parse_line(&file, &config, line2, &command); + + assert_true(res.success); + + keybind = wm_config_parse_bind_command(&command); + + assert_ptr_equal(keybind.function, &wm_kb_move_dir); + assert_ptr_equal(keybind.args.i, DOWN); + + wm_configfile_free(&file); + wm_configcommand_free(&command); + + char line3[] = "keybind Alt+1 switch_ws 1"; + + file = (ConfigFile) {0}; + config = (Config) {0}; + command = (ConfigCommand) {0}; + + wm_configfile_init(&file, tmp); + wm_configcommand_init(&command); + + res = wm_configfile_parse_line(&file, &config, line3, &command); + + assert_true(res.success); + + keybind = wm_config_parse_bind_command(&command); + + assert_ptr_equal(keybind.function, &wm_kb_switch_ws); + assert_ptr_equal(keybind.args.i, 1); + + wm_configfile_free(&file); + wm_configcommand_free(&command); + unlink(tmp); +} + +static void test_wm_config(void **state) +{ + char configstr[] = + "border_color #112233\n" + "focused_border_color #445566\n" + "border_width 1\n" + "keybind Windows+Alt+Shift+h focus left\n" + "focus_on_motion true\n"; + + char tmp[] = "/tmp/XXXXXX"; + mkstemp(tmp); + + int fd = open(tmp, O_CREAT | O_TRUNC | O_RDWR, 0664); + if (fd < 0) { + perror("open"); + fail(); + } + + int ret = write(fd, configstr, sizeof(configstr)); + + assert_int_equal(ret, sizeof(configstr)); + + ret = close(fd); + if (fd < 0) { + perror("close"); + fail(); + } + + ConfigFile file = (ConfigFile) {0}; + Config config = (Config) {0}; + + wm_cfg_init_def(&config); + + wm_configfile_init(&file, tmp); + wm_configfile_read(&file, &config); + + assert_string_equal(config.border_col, "#112233"); + assert_string_equal(config.focused_border_col, "#445566"); + assert_int_equal(config.border_width, 1); + + bool found_keybind = false; + for (size_t i = 0; i < config.kb_count; i++) { + if (config.keybinds[i].mask == (Mod4Mask | Mod1Mask | ShiftMask) && + config.keybinds[i].keysym == XK_h && + config.keybinds[i].function == &wm_kb_focus_dir && + config.keybinds[i].args.i == LEFT) + found_keybind = true; + } + + assert_true(found_keybind); + assert_true(config.focus_on_motion); + + wm_configfile_free(&file); + wm_keybinds_free(&config); + free(config.keybinds); + unlink(tmp); +} + static void test_wm_postorder_traversal(void **state) { TreeNode *node1 = (TreeNode*)*state; @@ -1059,6 +1188,8 @@ int main(void) cmocka_unit_test(test_wm_read_log), cmocka_unit_test(test_wm_logentries_calculate_distances), cmocka_unit_test(test_wm_keybind_str_to_mask_keysym), + cmocka_unit_test(test_wm_config_parse_bind_command), + cmocka_unit_test(test_wm_config), }; const struct CMUnitTest test_group2[] = {