From 8499b75fb011d72eeb5acbd85bcf41e4ee51e9a7 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sun, 17 Sep 2023 00:40:34 +0200 Subject: XML --- source/tests/_static.c | 1 + source/tests/input_buffer.test.c | 28 +++++++++--------- source/tests/xml.test.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 source/tests/xml.test.c (limited to 'source/tests') diff --git a/source/tests/_static.c b/source/tests/_static.c index 9e4451d..b406f48 100644 --- a/source/tests/_static.c +++ b/source/tests/_static.c @@ -18,4 +18,5 @@ #include "string_ref.test.c" #include "duration.test.c" #include "thread.test.c" +#include "xml.test.c" #include "bench.test.c" diff --git a/source/tests/input_buffer.test.c b/source/tests/input_buffer.test.c index 4712da9..237d60d 100644 --- a/source/tests/input_buffer.test.c +++ b/source/tests/input_buffer.test.c @@ -6,9 +6,9 @@ TEST("input buffer read once") { str_t text = { .size = 3, .values = "foo" }; is_handle_t in = IS_WRAP_STRING(text); - ib_handle_t first = IB_WRAP(in); + ib_t first = IB_WRAP(in); - ib_handle_t second = ib_read(first, 3); + ib_t second = ib_read(first, 3); REQUIRE(second.status == KIT_OK); REQUIRE(second.data.size == 3); @@ -23,10 +23,10 @@ TEST("input buffer read again") { str_t text = { .size = 6, .values = "foobar" }; str_t foo = { .size = 3, .values = "foo" }; is_handle_t in = IS_WRAP_STRING(text); - ib_handle_t first = IB_WRAP(in); + ib_t first = IB_WRAP(in); - ib_handle_t second = ib_read(first, 3); - ib_handle_t third = ib_read(first, 3); + ib_t second = ib_read(first, 3); + ib_t third = ib_read(first, 3); REQUIRE(AR_EQUAL(foo, second.data)); REQUIRE(AR_EQUAL(foo, third.data)); @@ -42,10 +42,10 @@ TEST("input buffer read twice") { str_t foo = { .size = 3, .values = "foo" }; str_t bar = { .size = 3, .values = "bar" }; is_handle_t in = IS_WRAP_STRING(text); - ib_handle_t first = IB_WRAP(in); + ib_t first = IB_WRAP(in); - ib_handle_t second = ib_read(first, 3); - ib_handle_t third = ib_read(second, 3); + ib_t second = ib_read(first, 3); + ib_t third = ib_read(second, 3); REQUIRE(AR_EQUAL(foo, second.data)); REQUIRE(AR_EQUAL(bar, third.data)); @@ -67,9 +67,9 @@ TEST("input buffer read integer once") { str_t text = { .size = 9, .values = "31415 foo" }; str_t num = { .size = 5, .values = "31415" }; is_handle_t in = IS_WRAP_STRING(text); - ib_handle_t first = IB_WRAP(in); + ib_t first = IB_WRAP(in); - ib_handle_t second = ib_read_while(first, is_integer_); + ib_t second = ib_while(first, is_integer_); REQUIRE(second.status == KIT_OK); REQUIRE(second.data.size == 5); @@ -85,11 +85,11 @@ TEST("input buffer read integer twice") { str_t num_0 = { .size = 3, .values = "314" }; str_t num_1 = { .size = 2, .values = "15" }; is_handle_t in = IS_WRAP_STRING(text); - ib_handle_t first = IB_WRAP(in); + ib_t first = IB_WRAP(in); - ib_handle_t second = ib_read_while(first, is_integer_); - ib_handle_t third = ib_read(second, 1); - ib_handle_t fourth = ib_read_while(third, is_integer_); + ib_t second = ib_while(first, is_integer_); + ib_t third = ib_read(second, 1); + ib_t fourth = ib_while(third, is_integer_); REQUIRE(fourth.status == KIT_OK); REQUIRE(second.data.size == 3); diff --git a/source/tests/xml.test.c b/source/tests/xml.test.c new file mode 100644 index 0000000..7d2151c --- /dev/null +++ b/source/tests/xml.test.c @@ -0,0 +1,61 @@ +#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(AR_EQUAL(res.xml.tag, SZ("foo"))); + 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(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(AR_EQUAL(res.xml.tag, SZ("foo"))); + xml_destroy(&res.xml); + } + + is_destroy(is); +} + +#undef KIT_TEST_FILE -- cgit v1.2.3