diff options
author | James Booth <boothj5@gmail.com> | 2015-01-20 00:09:47 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2015-01-20 00:09:47 +0000 |
commit | 2ed78fe5afaa0197de65a0edbf2423b5d7fe9792 (patch) | |
tree | 5384f57520420ded396496aef313d8f2b7a1e1d3 /tests | |
parent | 0e8092afef2e6172fc1fc27cd5a7e4f216456095 (diff) | |
download | profani-tty-2ed78fe5afaa0197de65a0edbf2423b5d7fe9792.tar.gz |
Extracted keyhandler for printable characters
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_keyhandlers.c | 150 | ||||
-rw-r--r-- | tests/test_keyhandlers.h | 11 | ||||
-rw-r--r-- | tests/testsuite.c | 10 |
3 files changed, 171 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 diff --git a/tests/test_keyhandlers.h b/tests/test_keyhandlers.h new file mode 100644 index 00000000..142a05ef --- /dev/null +++ b/tests/test_keyhandlers.h @@ -0,0 +1,11 @@ +void append_non_wide_to_empty(void **state); +void append_wide_to_empty(void **state); + +void append_non_wide_to_non_wide(void **state); +void append_wide_to_non_wide(void **state); + +void append_non_wide_to_wide(void **state); +void append_wide_to_wide(void **state); + +void append_no_wide_when_overrun(void **state); +void append_wide_when_overrun(void **state); \ No newline at end of file diff --git a/tests/testsuite.c b/tests/testsuite.c index cf511c59..4964d99d 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -35,6 +35,7 @@ #include "test_cmd_win.h" #include "test_cmd_disconnect.h" #include "test_form.h" +#include "test_keyhandlers.h" int main(int argc, char* argv[]) { const UnitTest all_tests[] = { @@ -622,6 +623,15 @@ int main(int argc, char* argv[]) { unit_test(remove_text_multi_value_removes_when_many), unit_test(clears_chat_sessions), + + unit_test(append_non_wide_to_empty), + unit_test(append_wide_to_empty), + unit_test(append_non_wide_to_non_wide), + unit_test(append_wide_to_non_wide), + unit_test(append_non_wide_to_wide), + unit_test(append_wide_to_wide), + unit_test(append_no_wide_when_overrun), + unit_test(append_wide_when_overrun), }; return run_tests(all_tests); |