1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <glib.h>
8 
9 #include "xmpp/xmpp.h"
10 
11 #include "ui/ui.h"
12 #include "ui/stub_ui.h"
13 
14 #include "config/preferences.h"
15 
16 #include "command/cmd_defs.h"
17 #include "command/cmd_funcs.h"
18 #include "command/cmd_ac.h"
19 
20 #define CMD_ALIAS "/alias"
21 
22 void
cmd_alias_add_shows_usage_when_no_args(void ** state)23 cmd_alias_add_shows_usage_when_no_args(void** state)
24 {
25     gchar* args[] = { "add", NULL };
26 
27     expect_string(cons_bad_cmd_usage, cmd, CMD_ALIAS);
28 
29     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
30     assert_true(result);
31 }
32 
33 void
cmd_alias_add_shows_usage_when_no_value(void ** state)34 cmd_alias_add_shows_usage_when_no_value(void** state)
35 {
36     gchar* args[] = { "add", "alias", NULL };
37 
38     expect_string(cons_bad_cmd_usage, cmd, CMD_ALIAS);
39 
40     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
41     assert_true(result);
42 }
43 
44 void
cmd_alias_remove_shows_usage_when_no_args(void ** state)45 cmd_alias_remove_shows_usage_when_no_args(void** state)
46 {
47     gchar* args[] = { "remove", NULL };
48 
49     expect_string(cons_bad_cmd_usage, cmd, CMD_ALIAS);
50 
51     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
52     assert_true(result);
53 }
54 
55 void
cmd_alias_show_usage_when_invalid_subcmd(void ** state)56 cmd_alias_show_usage_when_invalid_subcmd(void** state)
57 {
58     gchar* args[] = { "blah", NULL };
59 
60     expect_string(cons_bad_cmd_usage, cmd, CMD_ALIAS);
61 
62     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
63     assert_true(result);
64 }
65 
66 void
cmd_alias_add_adds_alias(void ** state)67 cmd_alias_add_adds_alias(void** state)
68 {
69     gchar* args[] = { "add", "hc", "/help commands", NULL };
70 
71     expect_cons_show("Command alias added /hc -> /help commands");
72 
73     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
74     assert_true(result);
75 
76     char* returned_val = prefs_get_alias("hc");
77     assert_string_equal("/help commands", returned_val);
78 
79     free(returned_val);
80 }
81 
82 void
cmd_alias_add_shows_message_when_exists(void ** state)83 cmd_alias_add_shows_message_when_exists(void** state)
84 {
85     gchar* args[] = { "add", "hc", "/help commands", NULL };
86 
87     cmd_init();
88     prefs_add_alias("hc", "/help commands");
89     cmd_ac_add("/hc");
90 
91     expect_cons_show("Command or alias '/hc' already exists.");
92 
93     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
94     assert_true(result);
95 }
96 
97 void
cmd_alias_remove_removes_alias(void ** state)98 cmd_alias_remove_removes_alias(void** state)
99 {
100     gchar* args[] = { "remove", "hn", NULL };
101 
102     prefs_add_alias("hn", "/help navigation");
103 
104     expect_cons_show("Command alias removed -> /hn");
105 
106     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
107     assert_true(result);
108 
109     char* returned_val = prefs_get_alias("hn");
110     assert_null(returned_val);
111 
112     free(returned_val);
113 }
114 
115 void
cmd_alias_remove_shows_message_when_no_alias(void ** state)116 cmd_alias_remove_shows_message_when_no_alias(void** state)
117 {
118     gchar* args[] = { "remove", "hn", NULL };
119 
120     expect_cons_show("No such command alias /hn");
121 
122     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
123     assert_true(result);
124 }
125 
126 void
cmd_alias_list_shows_all_aliases(void ** state)127 cmd_alias_list_shows_all_aliases(void** state)
128 {
129     gchar* args[] = { "list", NULL };
130 
131     prefs_add_alias("vy", "/vercheck on");
132     prefs_add_alias("q", "/quit");
133     prefs_add_alias("hn", "/help navigation");
134     prefs_add_alias("hc", "/help commands");
135     prefs_add_alias("vn", "/vercheck off");
136 
137     // write a custom checker to check the correct list is passed
138     expect_any(cons_show_aliases, aliases);
139 
140     gboolean result = cmd_alias(NULL, CMD_ALIAS, args);
141     assert_true(result);
142 }
143