aboutsummaryrefslogtreecommitdiff
path: root/test/xml/ParserTest.cpp
blob: 8edebeb63885bf37e4797d6da36eb279842f5f22 (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
#include "cru/xml/XmlNode.h"
#include "cru/xml/XmlParser.h"

#include <gtest/gtest.h>

using namespace cru::xml;

TEST(CruXmlParserTest, Simple) {
  XmlParser parser(u"<root></root>");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetTag(), u"root");
  ASSERT_EQ(n->GetAttributes().empty(), true);
  ASSERT_EQ(n->GetChildCount(), 0);
  delete n;
}

TEST(CruXmlParserTest, SimpleWithAttribute) {
  XmlParser parser(u"<root a1=\"v1\" a2=\"v2\"></root>");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetTag(), u"root");
  ASSERT_EQ(n->GetAttributeValue(u"a1"), u"v1");
  ASSERT_EQ(n->GetAttributeValue(u"a2"), u"v2");
  ASSERT_EQ(n->GetChildCount(), 0);
  delete n;
}

TEST(CruXmlParserTest, SimpleSelfClosing) {
  XmlParser parser(u"<root a1=\"v1\" a2=\"v2\"/>");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetTag(), u"root");
  ASSERT_EQ(n->GetAttributeValue(u"a1"), u"v1");
  ASSERT_EQ(n->GetAttributeValue(u"a2"), u"v2");
  ASSERT_EQ(n->GetChildCount(), 0);
  delete n;
}

TEST(CruXmlParserTest, NestedElement) {
  XmlParser parser(
      u"<root><c1><d1></d1></c1><c2><d2></d2><d3></d3></c2></root>");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetChildren().size(), 2);
  ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetTag(), u"c1");
  ASSERT_EQ(n->GetChildAt(1)->AsElement()->GetTag(), u"c2");
  ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetChildCount(), 1);
  ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetChildAt(0)->AsElement()->GetTag(),
            u"d1");
  ASSERT_EQ(n->GetChildAt(1)->AsElement()->GetChildCount(), 2);
  ASSERT_EQ(n->GetChildAt(1)->AsElement()->GetChildAt(0)->AsElement()->GetTag(),
            u"d2");
  ASSERT_EQ(n->GetChildAt(1)->AsElement()->GetChildAt(1)->AsElement()->GetTag(),
            u"d3");
  delete n;
}

TEST(CruXmlParserTest, SimpleText) {
  XmlParser parser(u"<root>text</root>");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetChildCount(), 1);
  ASSERT_EQ(n->GetChildAt(0)->AsText()->GetText(), u"text");
  delete n;
}

TEST(CruXmlParserTest, Whitespace) {
  XmlParser parser(u"\t\t<root>\n\t\t\ttext test\n\t\t</root>\t\t");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetChildCount(), 1);
  ASSERT_EQ(n->GetChildAt(0)->AsText()->GetText(), u"text test");
  delete n;
}

TEST(CruXmlParserTest, Comment) {
  XmlParser parser(u"<!-- comment -->");
  auto n = parser.Parse();
  ASSERT_TRUE(n->IsCommentNode());
  ASSERT_EQ(n->AsComment()->GetText(), u"comment");
  delete n;
}

TEST(CruXmlParserTest, Complex) {
  XmlParser parser(
      uR"(
<root a1="v1">
  <c1>
    <d1>
    </d1>
  </c1>
  <c2 a2="v2" a3="v3">
    t1
    <d2 a4="v4"> t2 </d2>
    text test
    <d3></d3>
    t2
  </c2>
  <!-- comment -->
</root>
  )");
  auto n = parser.Parse();
  ASSERT_EQ(n->GetAttributeValue(u"a1"), u"v1");
  ASSERT_EQ(n->GetChildCount(), 3);
  ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetTag(), u"c1");
  ASSERT_EQ(n->GetChildAt(0)->AsElement()->GetChildCount(), 1);
  auto c2 = n->GetChildAt(1)->AsElement();
  ASSERT_EQ(c2->GetTag(), u"c2");
  ASSERT_EQ(c2->GetAttributeValue(u"a2"), u"v2");
  ASSERT_EQ(c2->GetAttributeValue(u"a3"), u"v3");
  ASSERT_EQ(c2->GetChildAt(0)->AsText()->GetText(), u"t1");
  auto d2 = c2->GetChildAt(1)->AsElement();
  ASSERT_EQ(d2->GetTag(), u"d2");
  ASSERT_EQ(d2->GetAttributeValue(u"a4"), u"v4");
  ASSERT_EQ(c2->GetChildAt(2)->AsText()->GetText(), u"text test");
  ASSERT_EQ(c2->GetChildAt(3)->AsElement()->GetTag(), u"d3");
  ASSERT_EQ(c2->GetChildAt(4)->AsText()->GetText(), u"t2");
  ASSERT_TRUE(n->GetChildAt(2)->IsCommentNode());
  ASSERT_EQ(n->GetChildAt(2)->AsComment()->GetText(), u"comment");
  delete n;
}