about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-01-23 22:29:53 +0000
committerJames Booth <boothj5@gmail.com>2014-01-23 22:29:53 +0000
commit8dbe300d72e3bdaba672b4a7027ab0f2fb431862 (patch)
tree521260cc8995af389f26d96ecf634c6fcaae98ba /tests
parent8ba2d2694756da38b680befff88fdaea1b5ef8bf (diff)
downloadprofani-tty-8dbe300d72e3bdaba672b4a7027ab0f2fb431862.tar.gz
Added /alias command, writing aliases to [alias] group in profrc
Diffstat (limited to 'tests')
-rw-r--r--tests/test_cmd_alias.c150
-rw-r--r--tests/test_cmd_alias.h8
-rw-r--r--tests/testsuite.c22
-rw-r--r--tests/ui/mock_ui.c19
-rw-r--r--tests/ui/mock_ui.h3
5 files changed, 202 insertions, 0 deletions
diff --git a/tests/test_cmd_alias.c b/tests/test_cmd_alias.c
new file mode 100644
index 00000000..bdb16ede
--- /dev/null
+++ b/tests/test_cmd_alias.c
@@ -0,0 +1,150 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+
+#include "xmpp/xmpp.h"
+#include "xmpp/mock_xmpp.h"
+
+#include "ui/ui.h"
+#include "ui/mock_ui.h"
+
+#include "config/preferences.h"
+
+#include "command/commands.h"
+
+void cmd_alias_add_shows_usage_when_no_args(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "add", NULL };
+
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_alias_add_shows_usage_when_no_value(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "add", "alias", NULL };
+
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_alias_remove_shows_usage_when_no_args(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "remove", NULL };
+
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_alias_show_usage_when_invalid_subcmd(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "blah", NULL };
+
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_alias_add_adds_alias(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "add", "hc", "/help commands", NULL };
+
+    expect_cons_show("Command alias added /hc -> /help commands");
+
+    gboolean result = cmd_alias(args, *help);
+
+    char *returned_val = prefs_get_alias("hc");
+
+    assert_true(result);
+    assert_string_equal("/help commands", returned_val);
+
+    free(help);
+}
+
+void cmd_alias_remove_removes_alias(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "remove", "hn", NULL };
+
+    prefs_add_alias("hn", "/help navigation");
+
+    expect_cons_show("Command alias removed -> /hn");
+
+    gboolean result = cmd_alias(args, *help);
+
+    char *returned_val = prefs_get_alias("hn");
+
+    assert_true(result);
+    assert_null(returned_val);
+
+    free(help);
+}
+
+void cmd_alias_remove_shows_message_when_no_alias(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "remove", "hn", NULL };
+
+    expect_cons_show("No such command alias /hn");
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_alias_list_shows_all_aliases(void **state)
+{
+    mock_cons_show_aliases();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "list", NULL };
+
+    prefs_add_alias("vy", "/vercheck on");
+    prefs_add_alias("q", "/quit");
+    prefs_add_alias("hn", "/help navigation");
+    prefs_add_alias("hc", "/help commands");
+    prefs_add_alias("vn", "/vercheck off");
+
+    // write a custom checker to check the correct list is passed
+    expect_cons_show_aliases();
+
+    gboolean result = cmd_alias(args, *help);
+    assert_true(result);
+
+    free(help);
+}
diff --git a/tests/test_cmd_alias.h b/tests/test_cmd_alias.h
new file mode 100644
index 00000000..1f2df5cd
--- /dev/null
+++ b/tests/test_cmd_alias.h
@@ -0,0 +1,8 @@
+void cmd_alias_add_shows_usage_when_no_args(void **state);
+void cmd_alias_add_shows_usage_when_no_value(void **state);
+void cmd_alias_remove_shows_usage_when_no_args(void **state);
+void cmd_alias_show_usage_when_invalid_subcmd(void **state);
+void cmd_alias_add_adds_alias(void **state);
+void cmd_alias_remove_removes_alias(void **state);
+void cmd_alias_remove_shows_message_when_no_alias(void **state);
+void cmd_alias_list_shows_all_aliases(void **state);
diff --git a/tests/testsuite.c b/tests/testsuite.c
index 94a06a87..af42fe7c 100644
--- a/tests/testsuite.c
+++ b/tests/testsuite.c
@@ -21,6 +21,7 @@
 #include "test_roster_list.h"
 #include "test_preferences.h"
 #include "test_server_events.h"
+#include "test_cmd_alias.h"
 
 #define PROF_RUN_TESTS(name) fprintf(stderr, "\n-> Running %s\n", #name); \
     fflush(stderr); \
@@ -390,6 +391,26 @@ int main(int argc, char* argv[]) {
 //            delete_config_file),
     };
 
+    const UnitTest cmd_alias_tests[] = {
+        unit_test(cmd_alias_add_shows_usage_when_no_args),
+        unit_test(cmd_alias_add_shows_usage_when_no_value),
+        unit_test(cmd_alias_remove_shows_usage_when_no_args),
+        unit_test(cmd_alias_show_usage_when_invalid_subcmd),
+        unit_test_setup_teardown(cmd_alias_add_adds_alias,
+            create_config_file,
+            delete_config_file),
+        unit_test_setup_teardown(cmd_alias_remove_removes_alias,
+            create_config_file,
+            delete_config_file),
+        unit_test_setup_teardown(cmd_alias_remove_shows_message_when_no_alias,
+            create_config_file,
+            delete_config_file),
+        unit_test_setup_teardown(cmd_alias_list_shows_all_aliases,
+            create_config_file,
+            delete_config_file),
+    };
+
+
     int bak, new;
     fflush(stdout);
     bak = dup(1);
@@ -413,6 +434,7 @@ int main(int argc, char* argv[]) {
     PROF_RUN_TESTS(cmd_statuses_tests);
     PROF_RUN_TESTS(preferences_tests);
     PROF_RUN_TESTS(server_events_tests);
+    PROF_RUN_TESTS(cmd_alias_tests);
 
     fflush(stdout);
     dup2(bak, 1);
diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c
index cd6c62ff..e03b77c5 100644
--- a/tests/ui/mock_ui.c
+++ b/tests/ui/mock_ui.c
@@ -69,6 +69,12 @@ void _mock_cons_show_account(ProfAccount *account)
 }
 
 static
+void _mock_cons_show_aliases(GList *aliases)
+{
+    check_expected(aliases);
+}
+
+static
 void _mock_cons_show_account_list(gchar **accounts)
 {
     check_expected(accounts);
@@ -123,6 +129,12 @@ mock_cons_show_account(void)
 }
 
 void
+mock_cons_show_aliases(void)
+{
+    cons_show_aliases = _mock_cons_show_aliases;
+}
+
+void
 mock_cons_show_account_list(void)
 {
     cons_show_account_list = _mock_cons_show_account_list;
@@ -190,6 +202,13 @@ expect_cons_show_contact_online(PContact contact, Resource *resource, GDateTime
 }
 
 void
+expect_cons_show_aliases()
+{
+    // write a custom checker for the list
+    expect_any(_mock_cons_show_aliases, aliases);
+}
+
+void
 mock_ui_ask_password_returns(char *password)
 {
     will_return(_mock_ui_ask_password, strdup(password));
diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h
index 6f6d34d7..b0a5a80d 100644
--- a/tests/ui/mock_ui.h
+++ b/tests/ui/mock_ui.h
@@ -21,6 +21,9 @@ void expect_cons_show_error(char *output);
 void mock_cons_show_account(void);
 void expect_cons_show_account(ProfAccount *account);
 
+void mock_cons_show_aliases(void);
+void expect_cons_show_aliases(void);
+
 void mock_cons_show_account_list(void);
 void expect_cons_show_account_list(gchar **accounts);