about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-01 23:54:26 +0100
committerJames Booth <boothj5@gmail.com>2014-04-01 23:54:26 +0100
commitb19b881b97dccff4e5589b5585db27beea1683de (patch)
tree905823dd214fb3e5adcf6c5f8f7c5463378246e5
parent7113b979524d16a20048e2533cffa580a0b2d288 (diff)
downloadprofani-tty-b19b881b97dccff4e5589b5585db27beea1683de.tar.gz
Added missing files, refactored ui_switch_win to check win exists
-rw-r--r--src/command/commands.c6
-rw-r--r--src/ui/core.c11
-rw-r--r--src/ui/ui.h2
-rw-r--r--tests/test_cmd_win.c39
-rw-r--r--tests/test_cmd_win.h2
-rw-r--r--tests/ui/mock_ui.c26
-rw-r--r--tests/ui/mock_ui.h5
7 files changed, 56 insertions, 35 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 15676184..0ed51795 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -530,12 +530,10 @@ gboolean
 cmd_win(gchar **args, struct cmd_help_t help)
 {
     int num = atoi(args[0]);
-    if (ui_win_exists(num)) {
-        ui_switch_win(num);
-    } else {
+    gboolean switched = ui_switch_win(num);
+    if (switched == FALSE) {
         cons_show("Window %d does not exist.", num);
     }
-
     return TRUE;
 }
 
diff --git a/src/ui/core.c b/src/ui/core.c
index d069ba53..7b1e4d1e 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -531,12 +531,12 @@ _get_recipient_string(ProfWin *window)
     return result;
 }
 
-static void
+static gboolean
 _ui_switch_win(const int i)
 {
-    ui_current_page_off();
-    ProfWin *new_current = wins_get_by_num(i);
-    if (new_current != NULL) {
+    if (ui_win_exists(i)) {
+        ui_current_page_off();
+        ProfWin *new_current = wins_get_by_num(i);
         wins_set_current_by_num(i);
         ui_current_page_off();
 
@@ -554,6 +554,9 @@ _ui_switch_win(const int i)
             status_bar_active(i);
         }
         wins_update_virtual_current();
+        return TRUE;
+    } else {
+        return FALSE;
     }
 }
 
diff --git a/src/ui/ui.h b/src/ui/ui.h
index ddc1ca17..874ccee2 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -58,7 +58,7 @@ void (*ui_resize)(const int ch, const char * const input,
 GSList* (*ui_get_recipients)(void);
 void (*ui_handle_special_keys)(const wint_t * const ch, const char * const inp,
     const int size);
-void (*ui_switch_win)(const int i);
+gboolean (*ui_switch_win)(const int i);
 void (*ui_next_win)(void);
 void (*ui_previous_win)(void);
 void (*ui_gone_secure)(const char * const recipient, gboolean trusted);
diff --git a/tests/test_cmd_win.c b/tests/test_cmd_win.c
new file mode 100644
index 00000000..0ff109bc
--- /dev/null
+++ b/tests/test_cmd_win.c
@@ -0,0 +1,39 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+#include <glib.h>
+
+#include "ui/ui.h"
+#include "ui/mock_ui.h"
+
+#include "command/commands.h"
+
+void cmd_win_shows_message_when_win_doesnt_exist(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "3", NULL };
+
+    ui_switch_win_expect_and_return(3, FALSE);
+    expect_cons_show("Window 3 does not exist.");
+
+    gboolean result = cmd_win(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_win_switches_to_given_win_when_exists(void **state)
+{
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "12", NULL };
+
+    ui_switch_win_expect_and_return(12, TRUE);
+
+    gboolean result = cmd_win(args, *help);
+    assert_true(result);
+
+    free(help);
+}
diff --git a/tests/test_cmd_win.h b/tests/test_cmd_win.h
new file mode 100644
index 00000000..a0f61a6b
--- /dev/null
+++ b/tests/test_cmd_win.h
@@ -0,0 +1,2 @@
+void cmd_win_shows_message_when_win_doesnt_exist(void **state);
+void cmd_win_switches_to_given_win_when_exists(void **state);
diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c
index 03960e58..94f8e3c3 100644
--- a/tests/ui/mock_ui.c
+++ b/tests/ui/mock_ui.c
@@ -178,16 +178,10 @@ void _mock_cons_show_roster(GSList *list)
 }
 
 static
-gboolean _mock_ui_win_exists(int index)
-{
-    check_expected(index);
-    return (gboolean)mock();
-}
-
-static
-void _mock_ui_switch_win(const int i)
+gboolean _mock_ui_switch_win(const int i)
 {
     check_expected(i);
+    return (gboolean)mock();
 }
 
 // bind mocks and stubs
@@ -300,12 +294,6 @@ mock_cons_show_roster(void)
     cons_show_roster = _mock_cons_show_roster;
 }
 
-void
-mock_ui_win_exists(void)
-{
-    ui_win_exists = _mock_ui_win_exists;
-}
-
 // expectations
 
 void
@@ -458,15 +446,9 @@ cons_show_roster_expect(GSList *list)
 }
 
 void
-ui_win_exists_expect_and_return(int given_index, gboolean result)
-{
-    expect_value(_mock_ui_win_exists, index, given_index);
-    will_return(_mock_ui_win_exists, result);
-}
-
-void
-ui_switch_win_expect(int given_i)
+ui_switch_win_expect_and_return(int given_i, gboolean result)
 {
     ui_switch_win = _mock_ui_switch_win;
     expect_value(_mock_ui_switch_win, i, given_i);
+    will_return(_mock_ui_switch_win, result);
 }
diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h
index 197436b3..5b984615 100644
--- a/tests/ui/mock_ui.h
+++ b/tests/ui/mock_ui.h
@@ -64,9 +64,6 @@ void ui_room_join_expect(char *room);
 void mock_cons_show_roster(void);
 void cons_show_roster_expect(GSList *list);
 
-void mock_ui_win_exists(void);
-void ui_win_exists_expect_and_return(int given_index, gboolean result);
-
-void ui_switch_win_expect(int given_i);
+void ui_switch_win_expect_and_return(int given_i, gboolean result);
 
 #endif