File size: 5,221 Bytes
6baed57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <string.h>
#include <stdlib.h>

/* External wrapper for the static function under test */
extern int test_htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag);

void setUp(void) {
    /* Setup code here, or leave empty */
}

void tearDown(void) {
    /* Cleanup code here, or leave empty */
}

/* Positive tests: well-known auto-close pairs */

/* Starting a new <p> closes a previous <p> */
void test_htmlCheckAutoClose_p_closes_p(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"p",
                                                     (const xmlChar *)"p"));
}

/* Starting a new <li> closes a previous <li> */
void test_htmlCheckAutoClose_li_closes_li(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"li",
                                                     (const xmlChar *)"li"));
}

/* Starting a new <option> closes a previous <option> */
void test_htmlCheckAutoClose_option_closes_option(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"option",
                                                     (const xmlChar *)"option"));
}

/* Starting a new <tr> closes a previous <tr> */
void test_htmlCheckAutoClose_tr_closes_tr(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"tr",
                                                     (const xmlChar *)"tr"));
}

/* Starting a new <td> closes a previous <td> */
void test_htmlCheckAutoClose_td_closes_td(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"td",
                                                     (const xmlChar *)"td"));
}

/* Starting a new <th> closes a previous <th> */
void test_htmlCheckAutoClose_th_closes_th(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"th",
                                                     (const xmlChar *)"th"));
}

/* Cross-closures within table rows: td <-> th */
void test_htmlCheckAutoClose_td_closes_th_and_th_closes_td(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"td",
                                                     (const xmlChar *)"th"));
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"th",
                                                     (const xmlChar *)"td"));
}

/* Definition list cross-closures: dt <-> dd */
void test_htmlCheckAutoClose_dt_dd_cross_closures(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"dt",
                                                     (const xmlChar *)"dd"));
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"dd",
                                                     (const xmlChar *)"dt"));
}

/* Block-level element like <div> closes an open <p> */
void test_htmlCheckAutoClose_div_closes_p(void) {
    TEST_ASSERT_EQUAL_INT(1, test_htmlCheckAutoClose((const xmlChar *)"div",
                                                     (const xmlChar *)"p"));
}

/* Negative tests: non-auto-close cases */

/* Inline formatting elements typically don't auto-close themselves */
void test_htmlCheckAutoClose_b_does_not_close_b(void) {
    TEST_ASSERT_EQUAL_INT(0, test_htmlCheckAutoClose((const xmlChar *)"b",
                                                     (const xmlChar *)"b"));
}

/* Inline element <span> should not auto-close <p> */
void test_htmlCheckAutoClose_span_does_not_close_p(void) {
    TEST_ASSERT_EQUAL_INT(0, test_htmlCheckAutoClose((const xmlChar *)"span",
                                                     (const xmlChar *)"p"));
}

/* Unknown tags should not be in the auto-close table */
void test_htmlCheckAutoClose_unknown_tags_return_zero(void) {
    TEST_ASSERT_EQUAL_INT(0, test_htmlCheckAutoClose((const xmlChar *)"customnew",
                                                     (const xmlChar *)"customold"));
    TEST_ASSERT_EQUAL_INT(0, test_htmlCheckAutoClose((const xmlChar *)"custom",
                                                     (const xmlChar *)"p"));
}

/* Non-symmetric: starting <p> should not auto-close an open <div> */
void test_htmlCheckAutoClose_p_does_not_close_div(void) {
    TEST_ASSERT_EQUAL_INT(0, test_htmlCheckAutoClose((const xmlChar *)"p",
                                                     (const xmlChar *)"div"));
}

int main(void) {
    UNITY_BEGIN();

    RUN_TEST(test_htmlCheckAutoClose_p_closes_p);
    RUN_TEST(test_htmlCheckAutoClose_li_closes_li);
    RUN_TEST(test_htmlCheckAutoClose_option_closes_option);
    RUN_TEST(test_htmlCheckAutoClose_tr_closes_tr);
    RUN_TEST(test_htmlCheckAutoClose_td_closes_td);
    RUN_TEST(test_htmlCheckAutoClose_th_closes_th);
    RUN_TEST(test_htmlCheckAutoClose_td_closes_th_and_th_closes_td);
    RUN_TEST(test_htmlCheckAutoClose_dt_dd_cross_closures);
    RUN_TEST(test_htmlCheckAutoClose_div_closes_p);

    RUN_TEST(test_htmlCheckAutoClose_b_does_not_close_b);
    RUN_TEST(test_htmlCheckAutoClose_span_does_not_close_p);
    RUN_TEST(test_htmlCheckAutoClose_unknown_tags_return_zero);
    RUN_TEST(test_htmlCheckAutoClose_p_does_not_close_div);

    return UNITY_END();
}