libxml / tests /tests_HTMLparser_LIBXML_ATTR_FORMAT.c
AryaWu's picture
Upload folder using huggingface_hub
6baed57 verified
#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();
}