File size: 4,019 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
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <libxml/xmlerror.h>
#include <string.h>
#include <stdlib.h>

/* Global wrapper for the static function under test (provided in the source module). */
extern void test_htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
                              const char *msg, const xmlChar *str1, const xmlChar *str2);

static htmlParserCtxtPtr new_ctx(void) {
    htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
    TEST_ASSERT_NOT_NULL_MESSAGE(ctxt, "Failed to create HTML parser context");
    return ctxt;
}

static void free_ctx(htmlParserCtxtPtr ctxt) {
    if (ctxt)
        htmlFreeParserCtxt(ctxt);
}

void setUp(void) {
    /* No global setup needed */
}

void tearDown(void) {
    /* No global teardown needed */
}

void test_htmlParseErr_sets_error_fields_basic(void) {
    htmlParserCtxtPtr ctxt = new_ctx();

    const char *msg = "Simple error";
    const xmlChar *s1 = (const xmlChar *)"s1";
    const xmlChar *s2 = (const xmlChar *)"s2";
    xmlParserErrors code = XML_ERR_INTERNAL_ERROR;

    test_htmlParseErr(ctxt, code, msg, s1, s2);

    const xmlErrorPtr err = xmlCtxtGetLastError((xmlParserCtxtPtr)ctxt);
    TEST_ASSERT_NOT_NULL(err);
    TEST_ASSERT_EQUAL_INT(XML_FROM_HTML, err->domain);
    TEST_ASSERT_EQUAL_INT(code, err->code);
    TEST_ASSERT_EQUAL_INT(XML_ERR_ERROR, err->level);
    TEST_ASSERT_EQUAL_PTR(s1, err->str1);
    TEST_ASSERT_EQUAL_PTR(s2, err->str2);
    TEST_ASSERT_NOT_NULL(err->message);
    /* Allow potential trailing newline or extra info by checking prefix */
    TEST_ASSERT_EQUAL_INT_MESSAGE(0, strncmp(err->message, msg, strlen(msg)),
                                  "Error message prefix mismatch");
    TEST_ASSERT_EQUAL_INT(0, err->int2); /* htmlParseErr passes int2 as 0 */

    free_ctx(ctxt);
}

void test_htmlParseErr_handles_null_strings(void) {
    htmlParserCtxtPtr ctxt = new_ctx();

    const char *msg = "No args";
    test_htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY, msg, NULL, NULL);

    const xmlErrorPtr err = xmlCtxtGetLastError((xmlParserCtxtPtr)ctxt);
    TEST_ASSERT_NOT_NULL(err);
    TEST_ASSERT_EQUAL_INT(XML_FROM_HTML, err->domain);
    TEST_ASSERT_EQUAL_INT(XML_ERR_DOCUMENT_EMPTY, err->code);
    TEST_ASSERT_EQUAL_INT(XML_ERR_ERROR, err->level);
    TEST_ASSERT_NULL(err->str1);
    TEST_ASSERT_NULL(err->str2);
    TEST_ASSERT_NOT_NULL(err->message);
    TEST_ASSERT_EQUAL_INT(0, strncmp(err->message, msg, strlen(msg)));
    TEST_ASSERT_EQUAL_INT(0, err->int2);

    free_ctx(ctxt);
}

void test_htmlParseErr_formats_message_with_strs(void) {
    htmlParserCtxtPtr ctxt = new_ctx();

    const char *msg = "Fmt: %s-%s";
    const xmlChar *s1 = (const xmlChar *)"A";
    const xmlChar *s2 = (const xmlChar *)"B";

    test_htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, msg, s1, s2);

    const xmlErrorPtr err = xmlCtxtGetLastError((xmlParserCtxtPtr)ctxt);
    TEST_ASSERT_NOT_NULL(err);
    TEST_ASSERT_EQUAL_INT(XML_FROM_HTML, err->domain);
    TEST_ASSERT_EQUAL_INT(XML_ERR_ENTITYREF_SEMICOL_MISSING, err->code);
    TEST_ASSERT_EQUAL_INT(XML_ERR_ERROR, err->level);
    TEST_ASSERT_EQUAL_PTR(s1, err->str1);
    TEST_ASSERT_EQUAL_PTR(s2, err->str2);
    TEST_ASSERT_NOT_NULL(err->message);
    TEST_ASSERT_NOT_NULL_MESSAGE(strstr(err->message, "Fmt: A-B"),
                                 "Formatted message not found");
    TEST_ASSERT_EQUAL_INT(0, err->int2);

    free_ctx(ctxt);
}

void test_htmlParseErr_null_context_does_not_crash(void) {
    /* There is no direct assertion here; test ensures no crash. */
    test_htmlParseErr(NULL, XML_ERR_INTERNAL_ERROR, "Should not crash", NULL, NULL);
    TEST_PASS_MESSAGE("Calling with NULL context did not crash");
}

int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_htmlParseErr_sets_error_fields_basic);
    RUN_TEST(test_htmlParseErr_handles_null_strings);
    RUN_TEST(test_htmlParseErr_formats_message_with_strs);
    RUN_TEST(test_htmlParseErr_null_context_does_not_crash);
    return UNITY_END();
}