#include "../kit/xml.h"
#define KIT_TEST_FILE xml
#include "../kit/kit_test.h"
TEST("xml parse tag") {
is_handle_t is = IS_WRAP_STRING(SZ(" "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse tag with dash") {
is_handle_t is = IS_WRAP_STRING(SZ(" "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo-bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse tag not closed") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_ERROR_INTERNAL);
if (res.status == KIT_OK)
xml_destroy(&res.xml);
is_destroy(is);
}
TEST("xml parse tag text") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.text, SZ(" bar ")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse empty tag") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse tail") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.tail, SZ(" bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse empty tail") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.tail, SZ(" bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse property") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE_EQ(res.xml.properties.size, 1);
if (res.xml.properties.size == 1) {
REQUIRE(AR_EQUAL(res.xml.properties.values[0].name, SZ("bar")));
REQUIRE(AR_EQUAL(res.xml.properties.values[0].value, SZ("42")));
}
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse empty property") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE_EQ(res.xml.properties.size, 1);
if (res.xml.properties.size == 1) {
REQUIRE(AR_EQUAL(res.xml.properties.values[0].name, SZ("bar")));
REQUIRE(AR_EQUAL(res.xml.properties.values[0].value, SZ("42")));
}
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse child") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE_EQ(res.xml.children.size, 1);
if (res.xml.children.size == 1)
REQUIRE(AR_EQUAL(res.xml.children.values[0].tag, SZ("bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse child with text and tail") {
is_handle_t is = IS_WRAP_STRING(SZ("text tail"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.text, SZ("text")));
REQUIRE_EQ(res.xml.children.size, 1);
if (res.xml.children.size == 1) {
REQUIRE(AR_EQUAL(res.xml.children.values[0].tag, SZ("bar")));
REQUIRE(AR_EQUAL(res.xml.children.values[0].tail, SZ(" tail")));
}
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse declaration") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 1);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment before text") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.text, SZ(" bar ")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment after text") {
is_handle_t is = IS_WRAP_STRING(SZ("foo "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.text, SZ("foo ")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment between text") {
is_handle_t is = IS_WRAP_STRING(
SZ("foo bar"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.text, SZ("foo bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment tail") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment tail before text") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.tail, SZ(" bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment tail after text") {
is_handle_t is = IS_WRAP_STRING(SZ(" bar "));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE(AR_EQUAL(res.xml.tail, SZ(" bar ")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse comment tail between text") {
is_handle_t is = IS_WRAP_STRING(SZ("foo bar"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("tag")));
REQUIRE(AR_EQUAL(res.xml.tail, SZ("foo bar")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse escaped text") {
is_handle_t is = IS_WRAP_STRING(SZ("<foo>"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.text, SZ("")));
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse escaped quote property") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE_EQ(res.xml.properties.size, 1);
if (res.xml.properties.size == 1) {
REQUIRE(AR_EQUAL(res.xml.properties.values[0].name, SZ("bar")));
REQUIRE(
AR_EQUAL(res.xml.properties.values[0].value, SZ("&\"")));
}
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse escaped apostrophe property") {
is_handle_t is = IS_WRAP_STRING(SZ(""));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
REQUIRE_EQ(res.xml.is_declaration, 0);
REQUIRE(AR_EQUAL(res.xml.tag, SZ("foo")));
REQUIRE_EQ(res.xml.properties.size, 1);
if (res.xml.properties.size == 1) {
REQUIRE(AR_EQUAL(res.xml.properties.values[0].name, SZ("bar")));
REQUIRE(AR_EQUAL(res.xml.properties.values[0].value, SZ("&'")));
}
xml_destroy(&res.xml);
}
is_destroy(is);
}
TEST("xml parse invalid escape") {
is_handle_t is = IS_WRAP_STRING(SZ("&foobar;"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_ERROR_INTERNAL);
if (res.status == KIT_OK)
xml_destroy(&res.xml);
is_destroy(is);
}
TEST("xml full text") {
is_handle_t is = IS_WRAP_STRING(
SZ("foo text bar text tail"));
xml_parse_result_t res = xml_parse(is, NULL);
REQUIRE_EQ(res.status, KIT_OK);
if (res.status == KIT_OK) {
xml_text_t text = xml_full_text(&res.xml, NULL);
REQUIRE_EQ(text.status, KIT_OK);
REQUIRE(AR_EQUAL(text.text, SZ("foo text bar text tail")));
DA_DESTROY(text.text);
xml_destroy(&res.xml);
}
is_destroy(is);
}
#undef KIT_TEST_FILE