diff --git a/tests/config/stub_accounts.c b/tests/config/stub_accounts.c index d2333fee..001be6d8 100644 --- a/tests/config/stub_accounts.c +++ b/tests/config/stub_accounts.c @@ -1,6 +1,24 @@ #include "common.h" #include "config/account.h" +// mock state + +static ProfAccount *account = NULL; + +void +reset_account_mocks(void) +{ + account = NULL; +} + +void +mock_accounts_get_account(ProfAccount *given_account) +{ + account = given_account; +} + +// stubs + void accounts_load(void) {} void accounts_close(void) {} @@ -29,7 +47,7 @@ gchar** accounts_get_list(void) ProfAccount* accounts_get_account(const char * const name) { - return NULL; + return account; } gboolean accounts_enable(const char * const name) diff --git a/tests/config/stub_accounts.h b/tests/config/stub_accounts.h new file mode 100644 index 00000000..1510f389 --- /dev/null +++ b/tests/config/stub_accounts.h @@ -0,0 +1,2 @@ +void reset_account_mocks(void); +void mock_accounts_get_account(ProfAccount *given_account); \ No newline at end of file diff --git a/tests/helpers.c b/tests/helpers.c index 6a7b2faa..0ae9a999 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -13,12 +13,20 @@ #include "ui/stub_ui.h" #include "xmpp/stub_xmpp.h" +#include "config/stub_accounts.h" void -reset_mocks(void **state) +pre_test(void **state) { reset_ui_mocks(); reset_xmpp_mocks(); + reset_account_mocks(); +} + +void +post_test(void **state) +{ + // nothing } void create_config_dir(void **state) diff --git a/tests/helpers.h b/tests/helpers.h index be640279..6bd4bd49 100644 --- a/tests/helpers.h +++ b/tests/helpers.h @@ -6,4 +6,5 @@ void close_preferences(void **state); void glist_set_cmp(GCompareFunc func); int glist_contents_equal(const void *actual, const void *expected); -void reset_mocks(void **state); +void pre_test(void **state); +void post_test(void **state); diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index 8610ccc4..e8246fc4 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -13,6 +13,7 @@ #include "ui/stub_ui.h" #include "config/accounts.h" +#include "config/stub_accounts.h" #include "command/commands.h" @@ -32,11 +33,9 @@ void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) free(help); } -/* + void cmd_account_shows_account_when_connected_and_no_args(void **state) { - mock_cons_show_account(); - mock_accounts_get_account(); CommandHelp *help = malloc(sizeof(CommandHelp)); ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); @@ -44,8 +43,7 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state) mock_connection_status(JABBER_CONNECTED); mock_connection_account_name("account_name"); - - accounts_get_account_return(account); + mock_accounts_get_account(account); expect_cons_show_account(account); @@ -54,7 +52,7 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state) free(help); } - +/* void cmd_account_list_shows_accounts(void **state) { mock_cons_show_account_list(); diff --git a/tests/testsuite.c b/tests/testsuite.c index a9904301..56de0109 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -240,10 +240,13 @@ int main(int argc, char* argv[]) { unit_test(cmd_rooms_arg_used_when_passed), */ unit_test_setup_teardown(cmd_account_shows_usage_when_not_connected_and_no_args, - reset_mocks, - reset_mocks), + pre_test, + post_test), + + unit_test_setup_teardown(cmd_account_shows_account_when_connected_and_no_args, + pre_test, + post_test), /* - unit_test(cmd_account_shows_account_when_connected_and_no_args), 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), diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index 673db33f..e83cd251 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -11,11 +11,13 @@ // mock state static gboolean mock_cons_show = FALSE; +static gboolean mock_cons_show_account = FALSE; static char output[256]; void reset_ui_mocks(void) { mock_cons_show = FALSE; + mock_cons_show_account = FALSE; } void @@ -25,6 +27,13 @@ expect_cons_show(char *expected) expect_string(cons_show, output, expected); } +void +expect_cons_show_account(ProfAccount *account) +{ + mock_cons_show_account = TRUE; + expect_memory(cons_show_account, account, account, sizeof(ProfAccount)); +} + // stubs void ui_init(void) {} @@ -303,7 +312,14 @@ void cons_show_log_prefs(void) {} void cons_show_presence_prefs(void) {} void cons_show_connection_prefs(void) {} void cons_show_otr_prefs(void) {} -void cons_show_account(ProfAccount *account) {} + +void cons_show_account(ProfAccount *account) +{ + if (mock_cons_show_account) { + check_expected(account); + } +} + void cons_debug(const char * const msg, ...) {} void cons_show_time(void) {} void cons_show_word(const char * const word) {} diff --git a/tests/ui/stub_ui.h b/tests/ui/stub_ui.h index 02e3f4fa..d71eaabc 100644 --- a/tests/ui/stub_ui.h +++ b/tests/ui/stub_ui.h @@ -1,4 +1,7 @@ #include +#include "config/account.h" + void expect_cons_show(char *expected); +void expect_cons_show_account(ProfAccount *account); void reset_ui_mocks(void); diff --git a/tests/xmpp/stub_xmpp.c b/tests/xmpp/stub_xmpp.c index b087b96f..229a37b9 100644 --- a/tests/xmpp/stub_xmpp.c +++ b/tests/xmpp/stub_xmpp.c @@ -3,11 +3,19 @@ // mock state static jabber_conn_status_t connection_status = JABBER_CONNECTED; +static char *account_name = NULL; void reset_xmpp_mocks(void) { connection_status = JABBER_CONNECTED; + account_name = NULL; +} + +void +mock_connection_account_name(char *given_account_name) +{ + account_name = given_account_name; } // stubs @@ -57,7 +65,7 @@ char* jabber_get_presence_message(void) char* jabber_get_account_name(void) { - return NULL; + return account_name; } GList * jabber_get_available_resources(void) diff --git a/tests/xmpp/stub_xmpp.h b/tests/xmpp/stub_xmpp.h index 875e87b5..21a0c8c2 100644 --- a/tests/xmpp/stub_xmpp.h +++ b/tests/xmpp/stub_xmpp.h @@ -1,4 +1,6 @@ #include "xmpp/xmpp.h" void mock_connection_status(jabber_conn_status_t given_connection_status); +void mock_connection_account_name(char *given_account_name); + void reset_xmpp_mocks(void); \ No newline at end of file