libxml / tests /tests_HTMLparser_htmlCreateDocParserCtxt.c
AryaWu's picture
Upload folder using huggingface_hub
6baed57 verified
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <stdlib.h>
#include <string.h>
/* Wrapper for the static function under test (provided in the module) */
extern htmlParserCtxtPtr test_htmlCreateDocParserCtxt(const xmlChar *str, const char *url, const char *encoding);
static void free_ctxt(htmlParserCtxtPtr ctxt) {
if (ctxt != NULL) {
xmlFreeParserCtxt(ctxt);
}
}
void setUp(void) {
/* Initialize libxml2 for each test to ensure a clean environment */
xmlInitParser();
}
void tearDown(void) {
/* Cleanup libxml2 globals after each test */
xmlCleanupParser();
}
void test_htmlCreateDocParserCtxt_null_str_returns_null(void) {
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(NULL, NULL, NULL);
TEST_ASSERT_NULL(ctxt);
}
void test_htmlCreateDocParserCtxt_simple_html_success(void) {
const xmlChar *str = BAD_CAST "<!DOCTYPE html><html><body>Hello</body></html>";
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(str, NULL, NULL);
TEST_ASSERT_NOT_NULL(ctxt);
/* A successful push of the input stream should result in at least one input */
TEST_ASSERT_TRUE_MESSAGE(ctxt->inputNr >= 1, "Expected at least one input on the stack");
free_ctxt(ctxt);
}
void test_htmlCreateDocParserCtxt_empty_string_success(void) {
const xmlChar *str = BAD_CAST "";
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(str, NULL, NULL);
TEST_ASSERT_NOT_NULL(ctxt);
TEST_ASSERT_TRUE_MESSAGE(ctxt->inputNr >= 1, "Expected at least one input on the stack for empty string");
free_ctxt(ctxt);
}
void test_htmlCreateDocParserCtxt_with_url_success(void) {
const xmlChar *str = BAD_CAST "Hi";
const char *url = "http://example.com/base/index.html";
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(str, url, NULL);
TEST_ASSERT_NOT_NULL(ctxt);
TEST_ASSERT_TRUE_MESSAGE(ctxt->inputNr >= 1, "Expected at least one input on the stack with URL provided");
free_ctxt(ctxt);
}
void test_htmlCreateDocParserCtxt_with_utf8_encoding_success(void) {
/* ASCII-only content to avoid ambiguity; explicit UTF-8 encoding */
const xmlChar *str = BAD_CAST "Just some text";
const char *encoding = "UTF-8";
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(str, NULL, encoding);
TEST_ASSERT_NOT_NULL(ctxt);
TEST_ASSERT_TRUE_MESSAGE(ctxt->inputNr >= 1, "Expected at least one input on the stack with UTF-8 encoding");
free_ctxt(ctxt);
}
void test_htmlCreateDocParserCtxt_with_iso_8859_1_encoding_success(void) {
/* ASCII-only content is valid in ISO-8859-1 as well */
const xmlChar *str = BAD_CAST "Plain ASCII content";
const char *encoding = "ISO-8859-1";
htmlParserCtxtPtr ctxt = test_htmlCreateDocParserCtxt(str, NULL, encoding);
TEST_ASSERT_NOT_NULL(ctxt);
TEST_ASSERT_TRUE_MESSAGE(ctxt->inputNr >= 1, "Expected at least one input on the stack with ISO-8859-1 encoding");
free_ctxt(ctxt);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlCreateDocParserCtxt_null_str_returns_null);
RUN_TEST(test_htmlCreateDocParserCtxt_simple_html_success);
RUN_TEST(test_htmlCreateDocParserCtxt_empty_string_success);
RUN_TEST(test_htmlCreateDocParserCtxt_with_url_success);
RUN_TEST(test_htmlCreateDocParserCtxt_with_utf8_encoding_success);
RUN_TEST(test_htmlCreateDocParserCtxt_with_iso_8859_1_encoding_success);
return UNITY_END();
}