21 #include <CUnit/CUnit.h> 23 #include "highlight.h" 24 #include "string_operations.h" 27 void _callConvertToAbsolutePositions(
char* text,
char*
search, GArray* diffMatchInfo) {
28 char* testText = g_strdup(text);
29 char* testSearch = g_strdup(search);
31 GArray* textTokens = tokenize(testText,
"." );
32 GArray* searchTokens = tokenize(testSearch,
"." );
34 convertToAbsolutePositions(diffMatchInfo, textTokens, searchTokens);
36 tokens_free(textTokens);
37 tokens_free(searchTokens);
42 void _appendToDiffMatchInfo(GArray* diffMatchInfo,
43 size_t textPosition,
size_t textCount,
size_t searchPosition,
47 toAppend.diffType =
"a";
48 toAppend.search = (
DiffPoint){.start = searchPosition, .length = searchCount};
49 toAppend.text = (
DiffPoint){.start = textPosition, .length = textCount};
51 g_array_append_val(diffMatchInfo, toAppend);
55 int _CU_ASSERT_EQUAL(
size_t actual,
size_t expected,
char * error) {
56 CU_ASSERT_EQUAL(actual, expected);
57 if (actual != expected)
58 printf(error, actual, expected);
59 return actual == expected;
62 void _assertDiffMatchInfo(GArray* diffMatchInfo, GArray* expectedDiffMatchInfo) {
63 CU_ASSERT_EQUAL(diffMatchInfo->len, expectedDiffMatchInfo->len );
64 if (diffMatchInfo->len == expectedDiffMatchInfo->len) {
65 for (
size_t i = 0; i < diffMatchInfo->len; i++) {
68 _CU_ASSERT_EQUAL(extracted.search.start, expected.search.start,
"ss %zu != %zu\n");
69 _CU_ASSERT_EQUAL(extracted.search.length, expected.search.length,
"sl %zu != %zu\n");
70 _CU_ASSERT_EQUAL(extracted.text.start, expected.text.start,
"ts %zu != %zu\n");
71 _CU_ASSERT_EQUAL(extracted.text.length, expected.text.length,
"tl %zu != %zu\n");
76 void test_convertToAbsolute() {
77 GArray* diffMatchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
78 GArray* expectedDiffMatchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
80 char* text =
"A.a.bcd.e.f.";
81 char*
search =
"...a.bc.e.f.";
83 _appendToDiffMatchInfo(diffMatchInfo, 1, 3, 1, 2);
84 _appendToDiffMatchInfo(expectedDiffMatchInfo, 2, 7, 5, 4);
86 _appendToDiffMatchInfo(diffMatchInfo, 4, 0, 0, 2);
87 _appendToDiffMatchInfo(expectedDiffMatchInfo, 10, 0, 3, 4);
89 _appendToDiffMatchInfo(diffMatchInfo, 0, 0, 0, 1);
90 _appendToDiffMatchInfo(expectedDiffMatchInfo, 0, 0, 3, 1);
92 _callConvertToAbsolutePositions(text, search, diffMatchInfo);
94 _assertDiffMatchInfo(diffMatchInfo, expectedDiffMatchInfo);
96 g_array_free(diffMatchInfo, TRUE);
97 g_array_free(expectedDiffMatchInfo, TRUE);
100 void test_getFullHighlightFor() {
101 char* text = g_strdup(
"...a.aa..b.a.c");
103 GArray* tokens = tokenize(text,
".");
105 DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 3);
107 _CU_ASSERT_EQUAL(fullHighlight.start, 5,
"start %zu!=%zu\n");
108 _CU_ASSERT_EQUAL(fullHighlight.length, 7,
"length %zu!=%zu\n");
114 void test_getFullHighlightFor_2() {
115 char* text = g_strdup(
"...a.aa..b.a.c");
117 GArray* tokens = tokenize(text,
".");
119 DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 0);
121 _CU_ASSERT_EQUAL(fullHighlight.start, 5,
"start %zu!=%zu\n");
122 _CU_ASSERT_EQUAL(fullHighlight.length, 0,
"length %zu!=%zu\n");
128 CU_TestInfo highlight_testcases[] = {
129 {
"Testing conversion to absolute positions:", test_convertToAbsolute},
130 {
"Testing extracting full highlight:", test_getFullHighlightFor},
131 {
"Testing extracting full highlight with empty:", test_getFullHighlightFor_2},