11 #include <CUnit/CUnit.h>
13 #include "highlight.h"
14 #include "string_operations.h"
17 void _callConvertToAbsolutePositions(
char* text,
char*
search, GArray* diffMatchInfo) {
18 char* testText = g_strdup(text);
19 char* testSearch = g_strdup(
search);
21 GArray* textTokens = tokenize(testText,
"." );
22 GArray* searchTokens = tokenize(testSearch,
"." );
24 convertToAbsolutePositions(diffMatchInfo, textTokens, searchTokens);
26 tokens_free(textTokens);
27 tokens_free(searchTokens);
32 void _appendToDiffMatchInfo(GArray* diffMatchInfo,
33 size_t textPosition,
size_t textCount,
size_t searchPosition,
37 toAppend.diffType =
"a";
38 toAppend.search = (
DiffPoint){.start = searchPosition, .length = searchCount};
39 toAppend.text = (
DiffPoint){.start = textPosition, .length = textCount};
41 g_array_append_val(diffMatchInfo, toAppend);
45 int _CU_ASSERT_EQUAL(
size_t actual,
size_t expected,
char * error) {
46 CU_ASSERT_EQUAL(actual, expected);
47 if (actual != expected)
48 printf(error, actual, expected);
49 return actual == expected;
52 void _assertDiffMatchInfo(GArray* diffMatchInfo, GArray* expectedDiffMatchInfo) {
53 CU_ASSERT_EQUAL(diffMatchInfo->len, expectedDiffMatchInfo->len );
54 if (diffMatchInfo->len == expectedDiffMatchInfo->len) {
55 for (
size_t i = 0; i < diffMatchInfo->len; i++) {
58 _CU_ASSERT_EQUAL(extracted.search.start, expected.search.start,
"ss %zu != %zu\n");
59 _CU_ASSERT_EQUAL(extracted.search.length, expected.search.length,
"sl %zu != %zu\n");
60 _CU_ASSERT_EQUAL(extracted.text.start, expected.text.start,
"ts %zu != %zu\n");
61 _CU_ASSERT_EQUAL(extracted.text.length, expected.text.length,
"tl %zu != %zu\n");
66 void test_convertToAbsolute() {
67 GArray* diffMatchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
68 GArray* expectedDiffMatchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
70 char* text =
"A.a.bcd.e.f.";
71 char*
search =
"...a.bc.e.f.";
73 _appendToDiffMatchInfo(diffMatchInfo, 1, 3, 1, 2);
74 _appendToDiffMatchInfo(expectedDiffMatchInfo, 2, 7, 5, 4);
76 _appendToDiffMatchInfo(diffMatchInfo, 4, 0, 0, 2);
77 _appendToDiffMatchInfo(expectedDiffMatchInfo, 10, 0, 3, 4);
79 _appendToDiffMatchInfo(diffMatchInfo, 0, 0, 0, 1);
80 _appendToDiffMatchInfo(expectedDiffMatchInfo, 0, 0, 3, 1);
82 _callConvertToAbsolutePositions(text,
search, diffMatchInfo);
84 _assertDiffMatchInfo(diffMatchInfo, expectedDiffMatchInfo);
86 g_array_free(diffMatchInfo, TRUE);
87 g_array_free(expectedDiffMatchInfo, TRUE);
90 void test_getFullHighlightFor() {
91 char* text = g_strdup(
"...a.aa..b.a.c");
93 GArray* tokens = tokenize(text,
".");
95 DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 3);
97 _CU_ASSERT_EQUAL(fullHighlight.start, 5,
"start %zu!=%zu\n");
98 _CU_ASSERT_EQUAL(fullHighlight.length, 7,
"length %zu!=%zu\n");
104 void test_getFullHighlightFor_2() {
105 char* text = g_strdup(
"...a.aa..b.a.c");
107 GArray* tokens = tokenize(text,
".");
109 DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 0);
111 _CU_ASSERT_EQUAL(fullHighlight.start, 5,
"start %zu!=%zu\n");
112 _CU_ASSERT_EQUAL(fullHighlight.length, 0,
"length %zu!=%zu\n");
118 CU_TestInfo highlight_testcases[] = {
119 {
"Testing conversion to absolute positions:", test_convertToAbsolute},
120 {
"Testing extracting full highlight:", test_getFullHighlightFor},
121 {
"Testing extracting full highlight with empty:", test_getFullHighlightFor_2},