1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Use will_return instead of mock values

This commit is contained in:
James Booth 2014-12-23 21:42:01 +00:00
parent 69fe6c4d21
commit 8c01021ab9
11 changed files with 30 additions and 125 deletions

View File

@ -60,11 +60,11 @@ tests_sources = \
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
src/server_events.c src/server_events.h \
tests/xmpp/stub_xmpp.c tests/xmpp/stub_xmpp.h \
tests/xmpp/stub_xmpp.c \
tests/otr/stub_otr.c \
tests/ui/stub_ui.c tests/ui/stub_ui.h \
tests/config/stub_accounts.c \
tests/ui/stub_ui.c \
tests/log/stub_log.c \
tests/config/stub_accounts.c \
tests/helpers.c tests/helpers.h \
tests/test_cmd_account.c tests/test_cmd_account.h \
tests/testsuite.c

View File

@ -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)

View File

@ -1,2 +0,0 @@
void reset_account_mocks(void);
void mock_accounts_get_account(ProfAccount *given_account);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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),

View File

@ -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, ...) {}

View File

@ -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);

View File

@ -1,25 +1,10 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "xmpp/xmpp.h"
// 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
// 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)

View File

@ -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);