summaryrefslogtreecommitdiff
path: root/source/tests/xml.test.c
blob: 52ff7c01f2c2f9808984c15d045e099abc826db7 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#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("<foo> </foo>"));
  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("<foo>"));
  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("<foo> bar </foo>"));
  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("<foo />"));
  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 tail") {
  is_handle_t        is  = IS_WRAP_STRING(SZ("<foo></foo> 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("<foo /> 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("<foo bar=\"42\"></foo>"));
  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("<foo bar=\"42\" />"));
  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("<foo><bar></bar></foo>"));
  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);
}

#undef KIT_TEST_FILE