diff options
author | James Booth <boothj5@gmail.com> | 2013-12-19 21:05:39 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2013-12-19 21:05:39 +0000 |
commit | d56f6dc39b002e16966354b0ad98cf7801260e0c (patch) | |
tree | 07480d1c9e14084dbfc5345a00fc002419ef50d9 | |
parent | b27c5d0f5baa7161231642b14106db2e96065f95 (diff) | |
download | profani-tty-d56f6dc39b002e16966354b0ad98cf7801260e0c.tar.gz |
Added mocks and stubs for cons_show
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/ui/console.c | 5 | ||||
-rw-r--r-- | src/ui/ui.h | 2 | ||||
-rw-r--r-- | tests/test_cmd_account.c | 195 | ||||
-rw-r--r-- | tests/test_cmd_account.h | 2 | ||||
-rw-r--r-- | tests/test_cmd_connect.c | 28 | ||||
-rw-r--r-- | tests/test_cmd_rooms.c | 4 | ||||
-rw-r--r-- | tests/testsuite.c | 2 | ||||
-rw-r--r-- | tests/ui/mock_ui.c | 51 | ||||
-rw-r--r-- | tests/ui/mock_ui.h | 14 |
10 files changed, 183 insertions, 122 deletions
diff --git a/Makefile.am b/Makefile.am index 2148fd3f..f3634606 100644 --- a/Makefile.am +++ b/Makefile.am @@ -61,7 +61,7 @@ test_sources = \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ tests/xmpp/mock_xmpp.h tests/xmpp/mock_xmpp.c \ - tests/ui/mock_ui.c \ + tests/ui/mock_ui.h tests/ui/mock_ui.c \ tests/log/mock_log.c \ tests/config/mock_accounts.c \ tests/test_autocomplete.c \ diff --git a/src/ui/console.c b/src/ui/console.c index 00e891db..43313dd1 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -85,8 +85,8 @@ cons_debug(const char * const msg, ...) } } -void -cons_show(const char * const msg, ...) +static void +_cons_show(const char * const msg, ...) { ProfWin *console = wins_get_console(); va_list arg; @@ -99,6 +99,7 @@ cons_show(const char * const msg, ...) va_end(arg); wins_refresh_console(); } +void (*cons_show)(const char * const, ...) = _cons_show; void cons_show_error(const char * const msg, ...) diff --git a/src/ui/ui.h b/src/ui/ui.h index f6bb98ac..539146b7 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -149,7 +149,7 @@ void title_bar_set_typing(gboolean is_typing); void title_bar_draw(void); // console window actions -void cons_show(const char * const msg, ...); +void (*cons_show)(const char * const msg, ...); void cons_about(void); void cons_help(void); void cons_basic_help(void); diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index b44b2ea1..0ef3c0ee 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -10,18 +10,20 @@ #include "xmpp/mock_xmpp.h" #include "ui/ui.h" +#include "ui/mock_ui.h" #include "command/commands.h" void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { NULL }; mock_connection_status(JABBER_DISCONNECTED); - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -75,11 +77,12 @@ void cmd_account_list_shows_accounts(void **state) void cmd_account_show_shows_usage_when_no_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "show", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -89,14 +92,15 @@ void cmd_account_show_shows_usage_when_no_arg(void **state) void cmd_account_show_shows_message_when_account_does_not_exist(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "show", "account_name", NULL }; expect_any(accounts_get_account, name); will_return(accounts_get_account, NULL); - expect_string(cons_show, output, "No such account."); - expect_string(cons_show, output, ""); + expect_cons_show("No such account."); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -104,7 +108,7 @@ void cmd_account_show_shows_message_when_account_does_not_exist(void **state) free(help); } -void cmd_account_show_shows_message_when_account_exists(void **state) +void cmd_account_show_shows_account_when_exists(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "show", "account_name", NULL }; @@ -125,11 +129,12 @@ void cmd_account_show_shows_message_when_account_exists(void **state) void cmd_account_add_shows_usage_when_no_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "add", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -139,13 +144,12 @@ void cmd_account_add_shows_usage_when_no_arg(void **state) void cmd_account_add_adds_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "add", "new_account", NULL }; expect_string(accounts_add, jid, "new_account"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -154,13 +158,14 @@ void cmd_account_add_adds_account(void **state) void cmd_account_add_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "add", "new_account", NULL }; expect_any(accounts_add, jid); - expect_string(cons_show, output, "Account created.");; - expect_string(cons_show, output, ""); + expect_cons_show("Account created.");; + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -170,11 +175,12 @@ void cmd_account_add_shows_message(void **state) void cmd_account_enable_shows_usage_when_no_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "enable", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -184,14 +190,13 @@ void cmd_account_enable_shows_usage_when_no_arg(void **state) void cmd_account_enable_enables_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "enable", "account_name", NULL }; expect_string(accounts_enable, name, "account_name"); will_return(accounts_enable, TRUE); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -200,14 +205,15 @@ void cmd_account_enable_enables_account(void **state) void cmd_account_enable_shows_message_when_enabled(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "enable", "account_name", NULL }; expect_any(accounts_enable, name); will_return(accounts_enable, TRUE); - expect_string(cons_show, output, "Account enabled."); - expect_string(cons_show, output, ""); + expect_cons_show("Account enabled."); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -217,14 +223,15 @@ void cmd_account_enable_shows_message_when_enabled(void **state) void cmd_account_enable_shows_message_when_account_doesnt_exist(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "enable", "account_name", NULL }; expect_any(accounts_enable, name); will_return(accounts_enable, FALSE); - expect_string(cons_show, output, "No such account: account_name"); - expect_string(cons_show, output, ""); + expect_cons_show("No such account: account_name"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -234,11 +241,12 @@ void cmd_account_enable_shows_message_when_account_doesnt_exist(void **state) void cmd_account_disable_shows_usage_when_no_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "disable", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -248,14 +256,13 @@ void cmd_account_disable_shows_usage_when_no_arg(void **state) void cmd_account_disable_disables_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "disable", "account_name", NULL }; expect_string(accounts_disable, name, "account_name"); will_return(accounts_disable, TRUE); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -264,14 +271,15 @@ void cmd_account_disable_disables_account(void **state) void cmd_account_disable_shows_message_when_disabled(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "disable", "account_name", NULL }; expect_any(accounts_disable, name); will_return(accounts_disable, TRUE); - expect_string(cons_show, output, "Account disabled."); - expect_string(cons_show, output, ""); + expect_cons_show("Account disabled."); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -281,14 +289,15 @@ void cmd_account_disable_shows_message_when_disabled(void **state) void cmd_account_disable_shows_message_when_account_doesnt_exist(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "disable", "account_name", NULL }; expect_any(accounts_disable, name); will_return(accounts_disable, FALSE); - expect_string(cons_show, output, "No such account: account_name"); - expect_string(cons_show, output, ""); + expect_cons_show("No such account: account_name"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -298,11 +307,12 @@ void cmd_account_disable_shows_message_when_account_doesnt_exist(void **state) void cmd_account_rename_shows_usage_when_no_args(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "rename", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -312,11 +322,12 @@ void cmd_account_rename_shows_usage_when_no_args(void **state) void cmd_account_rename_shows_usage_when_one_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "rename", "original_name", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -326,6 +337,7 @@ void cmd_account_rename_shows_usage_when_one_arg(void **state) void cmd_account_rename_renames_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "rename", "original_name", "new_name", NULL }; @@ -333,8 +345,6 @@ void cmd_account_rename_renames_account(void **state) expect_string(accounts_rename, new_name, "new_name"); will_return(accounts_rename, TRUE); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -343,6 +353,7 @@ void cmd_account_rename_renames_account(void **state) void cmd_account_rename_shows_message_when_renamed(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "rename", "original_name", "new_name", NULL }; @@ -350,8 +361,8 @@ void cmd_account_rename_shows_message_when_renamed(void **state) expect_any(accounts_rename, new_name); will_return(accounts_rename, TRUE); - expect_string(cons_show, output, "Account renamed."); - expect_string(cons_show, output, ""); + expect_cons_show("Account renamed."); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -361,6 +372,7 @@ void cmd_account_rename_shows_message_when_renamed(void **state) void cmd_account_rename_shows_message_when_not_renamed(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "rename", "original_name", "new_name", NULL }; @@ -368,8 +380,8 @@ void cmd_account_rename_shows_message_when_not_renamed(void **state) expect_any(accounts_rename, new_name); will_return(accounts_rename, FALSE); - expect_string(cons_show, output, "Either account original_name doesn't exist, or account new_name already exists."); - expect_string(cons_show, output, ""); + expect_cons_show("Either account original_name doesn't exist, or account new_name already exists."); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -379,11 +391,12 @@ void cmd_account_rename_shows_message_when_not_renamed(void **state) void cmd_account_set_shows_usage_when_no_args(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "set", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -393,11 +406,12 @@ void cmd_account_set_shows_usage_when_no_args(void **state) void cmd_account_set_shows_usage_when_one_arg(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "set", "a_account", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -407,11 +421,12 @@ void cmd_account_set_shows_usage_when_one_arg(void **state) void cmd_account_set_shows_usage_when_two_args(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "set", "a_account", "a_property", NULL }; - expect_string(cons_show, output, "Usage: some usage"); + expect_cons_show("Usage: some usage"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -421,14 +436,13 @@ void cmd_account_set_shows_usage_when_two_args(void **state) void cmd_account_set_checks_account_exists(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; expect_string(accounts_account_exists, account_name, "a_account"); will_return(accounts_account_exists, FALSE); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -437,14 +451,15 @@ void cmd_account_set_checks_account_exists(void **state) void cmd_account_set_shows_message_when_account_doesnt_exist(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, FALSE); - expect_string(cons_show, output, "Account a_account doesn't exist"); - expect_string(cons_show, output, ""); + expect_cons_show("Account a_account doesn't exist"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -454,13 +469,14 @@ void cmd_account_set_shows_message_when_account_doesnt_exist(void **state) void cmd_account_set_jid_shows_message_for_malformed_jid(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "jid", "@malformed", NULL }; expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); - expect_string(cons_show, output, "Malformed jid: @malformed"); + expect_cons_show("Malformed jid: @malformed"); gboolean result = cmd_account(args, *help); assert_true(result); @@ -470,6 +486,7 @@ void cmd_account_set_jid_shows_message_for_malformed_jid(void **state) void cmd_account_set_jid_sets_barejid(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; @@ -479,13 +496,12 @@ void cmd_account_set_jid_sets_barejid(void **state) expect_string(accounts_set_jid, account_name, "a_account"); expect_string(accounts_set_jid, value, "a_local@a_domain"); - expect_string(cons_show, output, "Updated jid for account a_account: a_local@a_domain"); + expect_cons_show("Updated jid for account a_account: a_local@a_domain"); expect_any(accounts_set_resource, account_name); expect_any(accounts_set_resource, value); - expect_any(cons_show, output); - expect_string(cons_show, output, ""); + expect_cons_show_calls(2); gboolean result = cmd_account(args, *help); assert_true(result); @@ -495,6 +511,7 @@ void cmd_account_set_jid_sets_barejid(void **state) void cmd_account_set_jid_sets_resource(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; @@ -504,13 +521,13 @@ void cmd_account_set_jid_sets_resource(void **state) expect_any(accounts_set_jid, account_name); expect_any(accounts_set_jid, value); - expect_any(cons_show, output); + expect_cons_show_calls(1); expect_string(accounts_set_resource, account_name, "a_account"); expect_string(accounts_set_resource, value, "a_resource"); - expect_string(cons_show, output, "Updated resource for account a_account: a_resource"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated resource for account a_account: a_resource"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -520,6 +537,7 @@ void cmd_account_set_jid_sets_resource(void **state) void cmd_account_set_server_sets_server(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "server", "a_server", NULL }; @@ -529,8 +547,6 @@ void cmd_account_set_server_sets_server(void **state) expect_string(accounts_set_server, account_name, "a_account"); expect_string(accounts_set_server, value, "a_server"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -539,6 +555,7 @@ void cmd_account_set_server_sets_server(void **state) void cmd_account_set_server_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "server", "a_server", NULL }; @@ -548,8 +565,8 @@ void cmd_account_set_server_shows_message(void **state) expect_any(accounts_set_server, account_name); expect_any(accounts_set_server, value); - expect_string(cons_show, output, "Updated server for account a_account: a_server"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated server for account a_account: a_server"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -559,6 +576,7 @@ void cmd_account_set_server_shows_message(void **state) void cmd_account_set_resource_sets_resource(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "resource", "a_resource", NULL }; @@ -568,8 +586,6 @@ void cmd_account_set_resource_sets_resource(void **state) expect_string(accounts_set_resource, account_name, "a_account"); expect_string(accounts_set_resource, value, "a_resource"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -578,6 +594,7 @@ void cmd_account_set_resource_sets_resource(void **state) void cmd_account_set_resource_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "resource", "a_resource", NULL }; @@ -587,8 +604,8 @@ void cmd_account_set_resource_shows_message(void **state) expect_any(accounts_set_resource, account_name); expect_any(accounts_set_resource, value); - expect_string(cons_show, output, "Updated resource for account a_account: a_resource"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated resource for account a_account: a_resource"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -598,6 +615,7 @@ void cmd_account_set_resource_shows_message(void **state) void cmd_account_set_password_sets_password(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "password", "a_password", NULL }; @@ -607,8 +625,6 @@ void cmd_account_set_password_sets_password(void **state) expect_string(accounts_set_password, account_name, "a_account"); expect_string(accounts_set_password, value, "a_password"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -617,6 +633,7 @@ void cmd_account_set_password_sets_password(void **state) void cmd_account_set_password_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "password", "a_password", NULL }; @@ -626,8 +643,8 @@ void cmd_account_set_password_shows_message(void **state) expect_any(accounts_set_password, account_name); expect_any(accounts_set_password, value); - expect_string(cons_show, output, "Updated password for account a_account"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated password for account a_account"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -637,6 +654,7 @@ void cmd_account_set_password_shows_message(void **state) void cmd_account_set_muc_sets_muc(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "muc", "a_muc", NULL }; @@ -646,8 +664,6 @@ void cmd_account_set_muc_sets_muc(void **state) expect_string(accounts_set_muc_service, account_name, "a_account"); expect_string(accounts_set_muc_service, value, "a_muc"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -656,6 +672,7 @@ void cmd_account_set_muc_sets_muc(void **state) void cmd_account_set_muc_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "muc", "a_muc", NULL }; @@ -665,8 +682,8 @@ void cmd_account_set_muc_shows_message(void **state) expect_any(accounts_set_muc_service, account_name); expect_any(accounts_set_muc_service, value); - expect_string(cons_show, output, "Updated muc service for account a_account: a_muc"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated muc service for account a_account: a_muc"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -676,6 +693,7 @@ void cmd_account_set_muc_shows_message(void **state) void cmd_account_set_nick_sets_nick(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "nick", "a_nick", NULL }; @@ -685,8 +703,6 @@ void cmd_account_set_nick_sets_nick(void **state) expect_string(accounts_set_muc_nick, account_name, "a_account"); expect_string(accounts_set_muc_nick, value, "a_nick"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -695,6 +711,7 @@ void cmd_account_set_nick_sets_nick(void **state) void cmd_account_set_nick_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "nick", "a_nick", NULL }; @@ -704,8 +721,8 @@ void cmd_account_set_nick_shows_message(void **state) expect_any(accounts_set_muc_nick, account_name); expect_any(accounts_set_muc_nick, value); - expect_string(cons_show, output, "Updated muc nick for account a_account: a_nick"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated muc nick for account a_account: a_nick"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -715,14 +732,15 @@ void cmd_account_set_nick_shows_message(void **state) void cmd_account_set_status_shows_message_when_invalid_status(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "status", "bad_status", NULL }; expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); - expect_string(cons_show, output, "Invalid status: bad_status"); - expect_string(cons_show, output, ""); + expect_cons_show("Invalid status: bad_status"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -732,6 +750,7 @@ void cmd_account_set_status_shows_message_when_invalid_status(void **state) void cmd_account_set_status_sets_status_when_valid(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "status", "away", NULL }; @@ -741,8 +760,6 @@ void cmd_account_set_status_sets_status_when_valid(void **state) expect_string(accounts_set_login_presence, account_name, "a_account"); expect_string(accounts_set_login_presence, value, "away"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -751,6 +768,7 @@ void cmd_account_set_status_sets_status_when_valid(void **state) void cmd_account_set_status_sets_status_when_last(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "status", "last", NULL }; @@ -760,8 +778,6 @@ void cmd_account_set_status_sets_status_when_last(void **state) expect_string(accounts_set_login_presence, account_name, "a_account"); expect_string(accounts_set_login_presence, value, "last"); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -770,6 +786,7 @@ void cmd_account_set_status_sets_status_when_last(void **state) void cmd_account_set_status_shows_message_when_set_valid(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "status", "away", NULL }; @@ -779,8 +796,8 @@ void cmd_account_set_status_shows_message_when_set_valid(void **state) expect_any(accounts_set_login_presence, account_name); expect_any(accounts_set_login_presence, value); - expect_string(cons_show, output, "Updated login status for account a_account: away"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated login status for account a_account: away"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -790,6 +807,7 @@ void cmd_account_set_status_shows_message_when_set_valid(void **state) void cmd_account_set_status_shows_message_when_set_last(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "status", "last", NULL }; @@ -799,8 +817,8 @@ void cmd_account_set_status_shows_message_when_set_last(void **state) expect_any(accounts_set_login_presence, account_name); expect_any(accounts_set_login_presence, value); - expect_string(cons_show, output, "Updated login status for account a_account: last"); - expect_string(cons_show, output, ""); + expect_cons_show("Updated login status for account a_account: last"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -810,14 +828,15 @@ void cmd_account_set_status_shows_message_when_set_last(void **state) void cmd_account_set_invalid_presence_string_priority_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "blah", "10", NULL }; expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); - expect_string(cons_show, output, "Invalid property: blah"); - expect_string(cons_show, output, ""); + expect_cons_show("Invalid property: blah"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -827,14 +846,15 @@ void cmd_account_set_invalid_presence_string_priority_shows_message(void **state void cmd_account_set_last_priority_shows_message(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "last", "10", NULL }; expect_any(accounts_account_exists, account_name); will_return(accounts_account_exists, TRUE); - expect_string(cons_show, output, "Invalid property: last"); - expect_string(cons_show, output, ""); + expect_cons_show("Invalid property: last"); + expect_cons_show(""); gboolean result = cmd_account(args, *help); assert_true(result); @@ -844,6 +864,7 @@ void cmd_account_set_last_priority_shows_message(void **state) void cmd_account_set_online_priority_sets_preference(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "online", "10", NULL }; @@ -855,8 +876,6 @@ void cmd_account_set_online_priority_sets_preference(void **state) mock_connection_status(JABBER_DISCONNECTED); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -865,6 +884,7 @@ void cmd_account_set_online_priority_sets_preference(void **state) void cmd_account_set_chat_priority_sets_preference(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "chat", "10", NULL }; @@ -876,8 +896,6 @@ void cmd_account_set_chat_priority_sets_preference(void **state) mock_connection_status(JABBER_DISCONNECTED); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -886,6 +904,7 @@ void cmd_account_set_chat_priority_sets_preference(void **state) void cmd_account_set_away_priority_sets_preference(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "away", "10", NULL }; @@ -897,8 +916,6 @@ void cmd_account_set_away_priority_sets_preference(void **state) mock_connection_status(JABBER_DISCONNECTED); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -907,6 +924,7 @@ void cmd_account_set_away_priority_sets_preference(void **state) void cmd_account_set_xa_priority_sets_preference(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "xa", "10", NULL }; @@ -918,8 +936,6 @@ void cmd_account_set_xa_priority_sets_preference(void **state) mock_connection_status(JABBER_DISCONNECTED); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); @@ -928,6 +944,7 @@ void cmd_account_set_xa_priority_sets_preference(void **state) void cmd_account_set_dnd_priority_sets_preference(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "set", "a_account", "dnd", "10", NULL }; @@ -939,8 +956,6 @@ void cmd_account_set_dnd_priority_sets_preference(void **state) mock_connection_status(JABBER_DISCONNECTED); - expect_any_count(cons_show, output, 2); - gboolean result = cmd_account(args, *help); assert_true(result); diff --git a/tests/test_cmd_account.h b/tests/test_cmd_account.h index 7333ad05..8d63dcbc 100644 --- a/tests/test_cmd_account.h +++ b/tests/test_cmd_account.h @@ -3,7 +3,7 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state); void cmd_account_list_shows_accounts(void **state); void cmd_account_show_shows_usage_when_no_arg(void **state); void cmd_account_show_shows_message_when_account_does_not_exist(void **state); -void cmd_account_show_shows_message_when_account_exists(void **state); +void cmd_account_show_shows_account_when_exists(void **state); void cmd_account_add_shows_usage_when_no_arg(void **state); void cmd_account_add_adds_account(void **state); void cmd_account_add_shows_message(void **state); diff --git a/tests/test_cmd_connect.c b/tests/test_cmd_connect.c index b0d1f3c9..f4545fb2 100644 --- a/tests/test_cmd_connect.c +++ b/tests/test_cmd_connect.c @@ -10,6 +10,7 @@ #include "xmpp/mock_xmpp.h" #include "ui/ui.h" +#include "ui/mock_ui.h" #include "command/commands.h" @@ -48,11 +49,12 @@ static jabber_conn_status_t _mock_jabber_connect_with_account_result_check(const static void test_with_connection_status(jabber_conn_status_t status) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); mock_connection_status(status); - expect_string(cons_show, output, "You are either connected already, or a login is in process."); + expect_cons_show("You are either connected already, or a login is in process."); gboolean result = cmd_connect(NULL, *help); assert_true(result); @@ -82,6 +84,7 @@ void cmd_connect_shows_message_when_undefined(void **state) void cmd_connect_when_no_account(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", NULL }; @@ -92,7 +95,7 @@ void cmd_connect_when_no_account(void **state) will_return(ui_ask_password, strdup("password")); - expect_string(cons_show, output, "Connecting as user@server.org"); + expect_cons_show("Connecting as user@server.org"); jabber_connect_with_details = _mock_jabber_connect_with_details_no_altdomain; expect_string(_mock_jabber_connect_with_details_no_altdomain, jid, "user@server.org"); @@ -107,6 +110,7 @@ void cmd_connect_when_no_account(void **state) void cmd_connect_with_altdomain_when_provided(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "altdomain" }; @@ -117,8 +121,6 @@ void cmd_connect_with_altdomain_when_provided(void **state) will_return(ui_ask_password, strdup("password")); - expect_any(cons_show, output); - jabber_connect_with_details = _mock_jabber_connect_with_details_altdomain; expect_string(_mock_jabber_connect_with_details_altdomain, altdomain, "altdomain"); will_return(_mock_jabber_connect_with_details_altdomain, JABBER_CONNECTING); @@ -131,6 +133,7 @@ void cmd_connect_with_altdomain_when_provided(void **state) void cmd_connect_fail_message(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", NULL }; @@ -141,8 +144,6 @@ void cmd_connect_fail_message(void **state) will_return(ui_ask_password, strdup("password")); - expect_any(cons_show, output); - jabber_connect_with_details = _mock_jabber_connect_with_details_result; will_return(_mock_jabber_connect_with_details_result, JABBER_DISCONNECTED); @@ -156,6 +157,7 @@ void cmd_connect_fail_message(void **state) void cmd_connect_lowercases_argument(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "USER@server.ORG", NULL }; @@ -166,8 +168,6 @@ void cmd_connect_lowercases_argument(void **state) will_return(ui_ask_password, strdup("password")); - expect_any(cons_show, output); - jabber_connect_with_details = _mock_jabber_connect_with_details_result; will_return(_mock_jabber_connect_with_details_result, JABBER_CONNECTING); @@ -179,6 +179,7 @@ void cmd_connect_lowercases_argument(void **state) void cmd_connect_asks_password_when_not_in_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = malloc(sizeof(ProfAccount)); @@ -193,8 +194,6 @@ void cmd_connect_asks_password_when_not_in_account(void **state) will_return(ui_ask_password, strdup("password")); - expect_any(cons_show, output); - jabber_connect_with_account = _mock_jabber_connect_with_account_result; will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING); @@ -209,6 +208,7 @@ void cmd_connect_asks_password_when_not_in_account(void **state) void cmd_connect_shows_message_when_connecting_with_account(void **state) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = malloc(sizeof(ProfAccount)); @@ -222,7 +222,7 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state) will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); - expect_string(cons_show, output, "Connecting with account jabber_org as user@jabber.org/laptop"); + expect_cons_show("Connecting with account jabber_org as user@jabber.org/laptop"); jabber_connect_with_account = _mock_jabber_connect_with_account_result; will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING); @@ -238,6 +238,7 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state) void cmd_connect_connects_with_account(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = malloc(sizeof(ProfAccount)); @@ -251,8 +252,6 @@ void cmd_connect_connects_with_account(void **state) will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); - expect_any(cons_show, output); - jabber_connect_with_account = _mock_jabber_connect_with_account_result_check; expect_memory(_mock_jabber_connect_with_account_result_check, account, account, sizeof(ProfAccount)); will_return(_mock_jabber_connect_with_account_result_check, JABBER_CONNECTING); @@ -268,6 +267,7 @@ void cmd_connect_connects_with_account(void **state) void cmd_connect_frees_account_after_connecting(void **state) { + stub_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = malloc(sizeof(ProfAccount)); @@ -279,8 +279,6 @@ void cmd_connect_frees_account_after_connecting(void **state) will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); - expect_any(cons_show, output); - jabber_connect_with_account = _mock_jabber_connect_with_account_result; will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING); diff --git a/tests/test_cmd_rooms.c b/tests/test_cmd_rooms.c index a31f67f3..01aeecbe 100644 --- a/tests/test_cmd_rooms.c +++ b/tests/test_cmd_rooms.c @@ -9,15 +9,17 @@ #include "xmpp/mock_xmpp.h" #include "ui/ui.h" +#include "ui/mock_ui.h" #include "command/commands.h" static void test_with_connection_status(jabber_conn_status_t status) { + mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); mock_connection_status(status); - expect_string(cons_show, output, "You are not currently connected."); + expect_cons_show("You are not currently connected."); gboolean result = cmd_rooms(NULL, *help); assert_true(result); diff --git a/tests/testsuite.c b/tests/testsuite.c index bb3fdbbc..2e8c2e30 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -194,7 +194,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_account_list_shows_accounts), unit_test(cmd_account_show_shows_usage_when_no_arg), unit_test(cmd_account_show_shows_message_when_account_does_not_exist), - unit_test(cmd_account_show_shows_message_when_account_exists), + unit_test(cmd_account_show_shows_account_when_exists), unit_test(cmd_account_add_shows_usage_when_no_arg), unit_test(cmd_account_add_adds_account), unit_test(cmd_account_add_shows_message), diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index 6e3e190c..c70fbcf4 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -1,5 +1,5 @@ /* - * mock_ui.h + * mock_ui.c * * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com> * @@ -28,6 +28,46 @@ char output[256]; +static +void _mock_cons_show(const char * const msg, ...) +{ + va_list args; + va_start(args, msg); + vsnprintf(output, sizeof(output), msg, args); + check_expected(output); + va_end(args); +} + +static +void _stub_cons_show(const char * const msg, ...) +{ +} + +void +mock_cons_show(void) +{ + cons_show = _mock_cons_show; + +} + +void +stub_cons_show(void) +{ + cons_show = _stub_cons_show; +} + +void +expect_cons_show(char *output) +{ + expect_string(_mock_cons_show, output, output); +} + +void +expect_cons_show_calls(int n) +{ + expect_any_count(_mock_cons_show, output, n); +} + // ui startup and control void ui_init(void) {} void ui_load_colours(void) {} @@ -196,15 +236,6 @@ void title_bar_set_typing(gboolean is_typing) {} void title_bar_draw(void) {} // console window actions -void cons_show(const char * const msg, ...) -{ - va_list args; - va_start(args, msg); - vsnprintf(output, sizeof(output), msg, args); - check_expected(output); - va_end(args); -} - void cons_about(void) {} void cons_help(void) {} void cons_basic_help(void) {} diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h new file mode 100644 index 00000000..76d15614 --- /dev/null +++ b/tests/ui/mock_ui.h @@ -0,0 +1,14 @@ +#ifndef MOCK_UI_H +#define MICK_UI_H + +#include <glib.h> +#include <setjmp.h> +#include <cmocka.h> + +void stub_cons_show(void); + +void mock_cons_show(void); +void expect_cons_show(char *output); +void expect_cons_show_calls(int n); + +#endif |