aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-11 14:19:39 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-11 14:31:37 -0400
commitd3ec525b7d6ecd8de064d3f3a54d112769d1d3b3 (patch)
tree935b51f95afb513604c82c9e1efca223af60c109 /tests
parentFix git clone link in readme (diff)
downloadcli-d3ec525b7d6ecd8de064d3f3a54d112769d1d3b3.zip
Add unicode support to justify
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt9
-rw-r--r--tests/justify.bats39
-rw-r--r--tests/test_linebuffer.c10
-rw-r--r--tests/test_utf8.c118
4 files changed, 170 insertions, 6 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a0a8afa..fb75bc6 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,14 +1,19 @@
-set(TEST_LINEBUFFER_SOURCES test_linebuffer.c ../src/linebuffer.c)
+set(TEST_LINEBUFFER_SOURCES test_linebuffer.c ../src/linebuffer.c ../src/utf8.c)
set(TEST_WORD_SOURCES test_word.c ../src/word.c)
+set(TEST_UTF8_SOURCES test_utf8.c ../src/utf8.c)
add_executable(test_linebuffer ${TEST_LINEBUFFER_SOURCES})
add_executable(test_word ${TEST_WORD_SOURCES})
+add_executable(test_utf8 ${TEST_UTF8_SOURCES})
add_test(NAME test_linebuffer COMMAND test_linebuffer)
add_test(NAME test_word COMMAND test_word)
+add_test(NAME test_utf8 COMMAND test_utf8)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CMOCKA REQUIRED cmocka)
+pkg_check_modules(ICU REQUIRED IMPORTED_TARGET icu-uc icu-i18n)
include_directories(${CMOCKA_INCLUDE_DIRS})
-target_link_libraries(test_linebuffer ${CMOCKA_LIBRARIES})
+target_link_libraries(test_linebuffer ${CMOCKA_LIBRARIES} PkgConfig::ICU)
target_link_libraries(test_word ${CMOCKA_LIBRARIES})
+target_link_libraries(test_utf8 ${CMOCKA_LIBRARIES} PkgConfig::ICU)
diff --git a/tests/justify.bats b/tests/justify.bats
index 806ea81..525c5d8 100644
--- a/tests/justify.bats
+++ b/tests/justify.bats
@@ -94,3 +94,42 @@ EOF
assert_success
assert_output ""
}
+
+@test "unicode text with accented characters is justified correctly" {
+ run "${JUSTIFY_BIN}" -w 30 <<EOF
+café résumé naïve Zürich another word
+EOF
+ assert_success
+ expected_output="café résumé naïve Zürich
+another word"
+ assert_output "$expected_output"
+}
+
+@test "unicode text with CJK characters is justified correctly" {
+ run "${JUSTIFY_BIN}" -w 20 <<EOF
+Hello 世界 test this is a longer line
+EOF
+ assert_success
+ expected_output="Hello 世界 test this
+is a longer line"
+ assert_output "$expected_output"
+}
+
+@test "mixed ASCII and unicode text wraps correctly" {
+ run "${JUSTIFY_BIN}" -w 25 <<EOF
+This is café and résumé text that should wrap properly
+EOF
+ assert_success
+ expected_output="This is café and résumé
+text that should wrap
+properly"
+ assert_output "$expected_output"
+}
+
+@test "unicode text on last line is left-aligned" {
+ run "${JUSTIFY_BIN}" <<EOF
+café résumé
+EOF
+ assert_success
+ assert_output "café résumé"
+}
diff --git a/tests/test_linebuffer.c b/tests/test_linebuffer.c
index ae233cc..6d84780 100644
--- a/tests/test_linebuffer.c
+++ b/tests/test_linebuffer.c
@@ -10,17 +10,19 @@
#include <string.h>
/**
- * GIVEN a specific buffer capacity
+ * GIVEN a specific line width
* WHEN we attempt to create a LineBuffer
* THEN we expect it to be created successfully
* with the expected internal state
*/
void test_linebuffer_create(void **state) {
- const int buffer_capacity = 50;
- LineBuffer *lb = linebuffer_create(buffer_capacity);
+ const int line_width = 50;
+ LineBuffer *lb = linebuffer_create(line_width);
assert_non_null(lb);
- assert_int_equal(lb->_buffer_capacity, buffer_capacity);
+ assert_int_equal(lb->_line_width, line_width);
+ assert_int_equal(lb->_buffer_capacity, line_width * 4);
assert_int_equal(lb->_buffer_length, 0);
+ assert_int_equal(lb->_display_width, 0);
assert_int_equal(lb->_num_words, 0);
linebuffer_destroy(lb);
}
diff --git a/tests/test_utf8.c b/tests/test_utf8.c
new file mode 100644
index 0000000..623b607
--- /dev/null
+++ b/tests/test_utf8.c
@@ -0,0 +1,118 @@
+#include "utf8.h"
+// clang-format off
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+// clang-format on
+
+/**
+ * GIVEN an ASCII string
+ * WHEN we calculate the display width
+ * THEN we expect the width to equal the byte length
+ */
+void test_utf8_display_width_ascii(void **state) {
+ const char *input = "hello";
+ size_t expected = 5;
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN a string with accented characters (combining marks)
+ * WHEN we calculate the display width
+ * THEN we expect the width to be less than the byte length
+ */
+void test_utf8_display_width_accented(void **state) {
+ const char *input = "café"; // é is 2 bytes in UTF-8
+ size_t expected = 4; // 4 visible characters
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN a string with East Asian wide characters
+ * WHEN we calculate the display width
+ * THEN we expect the width to be double the character count
+ */
+void test_utf8_display_width_cjk(void **state) {
+ const char *input = "日本語"; // 3 Japanese characters
+ size_t expected = 6; // Each CJK character is 2 columns wide
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN a string with mixed ASCII and UTF-8 characters
+ * WHEN we calculate the display width
+ * THEN we expect the correct combined width
+ */
+void test_utf8_display_width_mixed(void **state) {
+ const char *input = "Hello 世界"; // "Hello " (6) + "世界" (4)
+ size_t expected = 10; // 6 ASCII + 2 CJK (2 columns each)
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN an empty string
+ * WHEN we calculate the display width
+ * THEN we expect width of 0
+ */
+void test_utf8_display_width_empty(void **state) {
+ const char *input = "";
+ size_t expected = 0;
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN a NULL pointer
+ * WHEN we calculate the display width
+ * THEN we expect width of 0
+ */
+void test_utf8_display_width_null(void **state) {
+ size_t expected = 0;
+
+ size_t result = utf8_display_width(NULL);
+
+ assert_int_equal(result, expected);
+}
+
+/**
+ * GIVEN a string with various Unicode characters
+ * WHEN we calculate the display width
+ * THEN we expect the correct width
+ */
+void test_utf8_display_width_various_unicode(void **state) {
+ const char *input = "Zürich"; // ü is 2 bytes in UTF-8
+ size_t expected = 6; // 6 visible characters
+
+ size_t result = utf8_display_width(input);
+
+ assert_int_equal(result, expected);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_utf8_display_width_ascii),
+ cmocka_unit_test(test_utf8_display_width_accented),
+ cmocka_unit_test(test_utf8_display_width_cjk),
+ cmocka_unit_test(test_utf8_display_width_mixed),
+ cmocka_unit_test(test_utf8_display_width_empty),
+ cmocka_unit_test(test_utf8_display_width_null),
+ cmocka_unit_test(test_utf8_display_width_various_unicode),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}