diff options
author | James Booth <boothj5@gmail.com> | 2014-12-23 21:42:01 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2014-12-23 21:42:01 +0000 |
commit | 8c01021ab9ae719c398e6c4a3e87ab98a5d6ac0e (patch) | |
tree | 6e5be41da643e09d10e3bdcc9951b7ab34da5d8e /tests | |
parent | 69fe6c4d21d885f1397172605e8babe639a945c5 (diff) | |
download | profani-tty-8c01021ab9ae719c398e6c4a3e87ab98a5d6ac0e.tar.gz |
Use will_return instead of mock values
Diffstat (limited to 'tests')
-rw-r--r-- | tests/config/stub_accounts.c | 25 | ||||
-rw-r--r-- | tests/config/stub_accounts.h | 2 | ||||
-rw-r--r-- | tests/helpers.c | 18 | ||||
-rw-r--r-- | tests/helpers.h | 5 | ||||
-rw-r--r-- | tests/test_cmd_account.c | 12 | ||||
-rw-r--r-- | tests/testsuite.c | 9 | ||||
-rw-r--r-- | tests/ui/stub_ui.c | 31 | ||||
-rw-r--r-- | tests/ui/stub_ui.h | 6 | ||||
-rw-r--r-- | tests/xmpp/stub_xmpp.c | 35 | ||||
-rw-r--r-- | tests/xmpp/stub_xmpp.h | 6 |
10 files changed, 27 insertions, 122 deletions
diff --git a/tests/config/stub_accounts.c b/tests/config/stub_accounts.c index 001be6d8..fe76f40c 100644 --- a/tests/config/stub_accounts.c +++ b/tests/config/stub_accounts.c @@ -1,24 +1,11 @@ +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + #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) {} @@ -47,7 +34,7 @@ gchar** accounts_get_list(void) ProfAccount* accounts_get_account(const char * const name) { - return account; + return (ProfAccount*)mock(); } gboolean accounts_enable(const char * const name) diff --git a/tests/config/stub_accounts.h b/tests/config/stub_accounts.h deleted file mode 100644 index 1510f389..00000000 --- a/tests/config/stub_accounts.h +++ /dev/null @@ -1,2 +0,0 @@ -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 0ae9a999..a6a473e4 100644 --- a/tests/helpers.c +++ b/tests/helpers.c @@ -11,24 +11,6 @@ #include "helpers.h" #include "config/preferences.h" -#include "ui/stub_ui.h" -#include "xmpp/stub_xmpp.h" -#include "config/stub_accounts.h" - -void -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) { setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1); diff --git a/tests/helpers.h b/tests/helpers.h index 6bd4bd49..17d8329c 100644 --- a/tests/helpers.h +++ b/tests/helpers.h @@ -4,7 +4,4 @@ void load_preferences(void **state); void close_preferences(void **state); void glist_set_cmp(GCompareFunc func); -int glist_contents_equal(const void *actual, const void *expected); - -void pre_test(void **state); -void post_test(void **state); +int glist_contents_equal(const void *actual, const void *expected); \ No newline at end of file diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c index e8246fc4..d1b90585 100644 --- a/tests/test_cmd_account.c +++ b/tests/test_cmd_account.c @@ -7,13 +7,11 @@ #include <glib.h> #include "xmpp/xmpp.h" -#include "xmpp/stub_xmpp.h" #include "ui/ui.h" #include "ui/stub_ui.h" #include "config/accounts.h" -#include "config/stub_accounts.h" #include "command/commands.h" @@ -23,7 +21,7 @@ void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) help->usage = "some usage"; gchar *args[] = { NULL }; - mock_connection_status(JABBER_DISCONNECTED); + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_cons_show("Usage: some usage"); @@ -41,11 +39,11 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state) TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL); gchar *args[] = { NULL }; - mock_connection_status(JABBER_CONNECTED); - mock_connection_account_name("account_name"); - mock_accounts_get_account(account); + will_return(jabber_get_connection_status, JABBER_CONNECTED); + will_return(jabber_get_account_name, "account_name"); + will_return(accounts_get_account, account); - expect_cons_show_account(account); + expect_memory(cons_show_account, account, account, sizeof(ProfAccount)); gboolean result = cmd_account(args, *help); assert_true(result); diff --git a/tests/testsuite.c b/tests/testsuite.c index 56de0109..2fce6be1 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -239,13 +239,8 @@ int main(int argc, char* argv[]) { unit_test(cmd_rooms_uses_account_default_when_no_arg), unit_test(cmd_rooms_arg_used_when_passed), */ - unit_test_setup_teardown(cmd_account_shows_usage_when_not_connected_and_no_args, - 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_usage_when_not_connected_and_no_args), + 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), diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index e83cd251..444de129 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -5,35 +5,20 @@ #include <cmocka.h> #include "ui/window.h" +#include "ui/ui.h" #include "tests/ui/stub_ui.h" // 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 expect_cons_show(char *expected) { - mock_cons_show = TRUE; 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) {} @@ -292,13 +277,11 @@ gboolean ui_win_has_unsaved_form(int num) void cons_show(const char * const msg, ...) { - if (mock_cons_show) { - va_list args; - va_start(args, msg); - vsnprintf(output, sizeof(output), msg, args); - check_expected(output); - va_end(args); - } + va_list args; + va_start(args, msg); + vsnprintf(output, sizeof(output), msg, args); + check_expected(output); + va_end(args); } void cons_about(void) {} @@ -315,9 +298,7 @@ void cons_show_otr_prefs(void) {} void cons_show_account(ProfAccount *account) { - if (mock_cons_show_account) { check_expected(account); - } } void cons_debug(const char * const msg, ...) {} diff --git a/tests/ui/stub_ui.h b/tests/ui/stub_ui.h index d71eaabc..dbddda9b 100644 --- a/tests/ui/stub_ui.h +++ b/tests/ui/stub_ui.h @@ -1,7 +1 @@ -#include <glib.h> - -#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 229a37b9..8a75b9ed 100644 --- a/tests/xmpp/stub_xmpp.c +++ b/tests/xmpp/stub_xmpp.c @@ -1,24 +1,9 @@ -#include "xmpp/xmpp.h" - -// mock state +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> -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 +#include "xmpp/xmpp.h" // connection functions void jabber_init(const int disable_tls) {} @@ -47,15 +32,9 @@ const char * jabber_get_domain(void) return NULL; } -void -mock_connection_status(jabber_conn_status_t given_connection_status) -{ - connection_status = given_connection_status; -} - jabber_conn_status_t jabber_get_connection_status(void) { - return connection_status; + return (jabber_conn_status_t)mock(); } char* jabber_get_presence_message(void) @@ -65,7 +44,7 @@ char* jabber_get_presence_message(void) char* jabber_get_account_name(void) { - return account_name; + return (char*)mock(); } GList * jabber_get_available_resources(void) diff --git a/tests/xmpp/stub_xmpp.h b/tests/xmpp/stub_xmpp.h deleted file mode 100644 index 21a0c8c2..00000000 --- a/tests/xmpp/stub_xmpp.h +++ /dev/null @@ -1,6 +0,0 @@ -#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 |