about summary refs log tree commit diff stats
path: root/tests/test_keyhandlers.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-01-20 00:09:47 +0000
committerJames Booth <boothj5@gmail.com>2015-01-20 00:09:47 +0000
commit2ed78fe5afaa0197de65a0edbf2423b5d7fe9792 (patch)
tree5384f57520420ded396496aef313d8f2b7a1e1d3 /tests/test_keyhandlers.c
parent0e8092afef2e6172fc1fc27cd5a7e4f216456095 (diff)
downloadprofani-tty-2ed78fe5afaa0197de65a0edbf2423b5d7fe9792.tar.gz
Extracted keyhandler for printable characters
Diffstat (limited to 'tests/test_keyhandlers.c')
-rw-r--r--tests/test_keyhandlers.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/tests/test_keyhandlers.c b/tests/test_keyhandlers.c
new file mode 100644
index 00000000..e01133e1
--- /dev/null
+++ b/tests/test_keyhandlers.c
@@ -0,0 +1,150 @@
+#include "ui/keyhandlers.h"
+#include "ui/inputwin.h"
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <locale.h>
+
+static char line[INP_WIN_MAX];
+
+void append_non_wide_to_empty(void **state)
+{
+    setlocale(LC_ALL, "");
+    line[0] = '\0';
+    int line_utf8_pos = 0;
+    int col = 0;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'a', 80);
+
+    assert_string_equal("a", line);
+    assert_int_equal(line_utf8_pos, 1);
+    assert_int_equal(col, 1);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_wide_to_empty(void **state)
+{
+    setlocale(LC_ALL, "");
+    line[0] = '\0';
+    int line_utf8_pos = 0;
+    int col = 0;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);
+
+    assert_string_equal("四", line);
+    assert_int_equal(line_utf8_pos, 1);
+    assert_int_equal(col, 2);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_non_wide_to_non_wide(void **state)
+{
+    setlocale(LC_ALL, "");
+    strncpy(line, "a", 1);
+    line[1] = '\0';
+    int line_utf8_pos = 1;
+    int col = 1;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);
+
+    assert_string_equal("ab", line);
+    assert_int_equal(line_utf8_pos, 2);
+    assert_int_equal(col, 2);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_wide_to_non_wide(void **state)
+{
+    setlocale(LC_ALL, "");
+    strncpy(line, "a", 1);
+    line[1] = '\0';
+    int line_utf8_pos = 1;
+    int col = 1;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);
+
+    assert_string_equal("a四", line);
+    assert_int_equal(line_utf8_pos, 2);
+    assert_int_equal(col, 3);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_non_wide_to_wide(void **state)
+{
+    setlocale(LC_ALL, "");
+    g_utf8_strncpy(line, "四", 1);
+    line[strlen(line)] = '\0';
+    int line_utf8_pos = 1;
+    int col = 2;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);
+
+    assert_string_equal("四b", line);
+    assert_int_equal(line_utf8_pos, 2);
+    assert_int_equal(col, 3);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_wide_to_wide(void **state)
+{
+    setlocale(LC_ALL, "");
+    g_utf8_strncpy(line, "四", 1);
+    line[strlen(line)] = '\0';
+    int line_utf8_pos = 1;
+    int col = 2;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 80);
+
+    assert_string_equal("四三", line);
+    assert_int_equal(line_utf8_pos, 2);
+    assert_int_equal(col, 4);
+    assert_int_equal(pad_start, 0);
+}
+
+void append_no_wide_when_overrun(void **state)
+{
+    setlocale(LC_ALL, "");
+    g_utf8_strncpy(line, "0123456789四1234567", 18);
+    line[strlen(line)] = '\0';
+    int line_utf8_pos = 18;
+    int col = 19;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
+
+    assert_string_equal("0123456789四1234567zzz", line);
+    assert_int_equal(line_utf8_pos, 21);
+    assert_int_equal(col, 22);
+    assert_int_equal(pad_start, 3);
+}
+
+void append_wide_when_overrun(void **state)
+{
+    setlocale(LC_ALL, "");
+    g_utf8_strncpy(line, "0123456789四1234567", 18);
+    line[strlen(line)] = '\0';
+    int line_utf8_pos = 18;
+    int col = 19;
+    int pad_start = 0;
+
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
+    key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
+
+    assert_string_equal("0123456789四1234567三三三", line);
+    assert_int_equal(line_utf8_pos, 21);
+    assert_int_equal(col, 25);
+    assert_int_equal(pad_start, 6);
+}
\ No newline at end of file