File: structured_data.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (187 lines) | stat: -rw-r--r-- 5,408 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*******************************************************************\

Module: Unit tests for the structed_datat related classes

Author: Thomas Kiley

\*******************************************************************/

#include <testing-utils/use_catch.h>

#include <util/structured_data.h>
#include <util/xml.h>

TEST_CASE("label", "[core][util][structured_data][label]")
{
  const cbmc_invariants_should_throwt invariant_throw_during_tests{};

  SECTION("Single element")
  {
    labelt empty_label({"hello"});
    REQUIRE(empty_label.camel_case() == "hello");
    REQUIRE(empty_label.kebab_case() == "hello");
    REQUIRE(empty_label.snake_case() == "hello");
    REQUIRE(empty_label.pretty() == "Hello");
  }
  SECTION("Two elements")
  {
    labelt empty_label({"hello", "goodbye"});
    REQUIRE(empty_label.camel_case() == "helloGoodbye");
    REQUIRE(empty_label.kebab_case() == "hello-goodbye");
    REQUIRE(empty_label.snake_case() == "hello_goodbye");
    REQUIRE(empty_label.pretty() == "Hello goodbye");
  }
  SECTION("Valid labels")
  {
    labelt numbered_label({"hello", "1"});
    REQUIRE(numbered_label.camel_case() == "hello1");
    REQUIRE(numbered_label.kebab_case() == "hello-1");
    REQUIRE(numbered_label.snake_case() == "hello_1");
    REQUIRE(numbered_label.pretty() == "Hello 1");
  }
  SECTION("Invalid components")
  {
    REQUIRE_THROWS_AS(labelt{{}}, invariant_failedt);
    std::vector<std::string> starts_with_upper{"Hello"};
    REQUIRE_THROWS_AS(labelt{starts_with_upper}, invariant_failedt);
    std::vector<std::string> contains_upper{"Hello"};
    REQUIRE_THROWS_AS(labelt{contains_upper}, invariant_failedt);
    REQUIRE_THROWS_AS(labelt{{""}}, invariant_failedt);
  }
}

TEST_CASE("Label equality", "[core][util][structured_data][label]")
{
  labelt single_label({"a"});
  labelt b_label({"b"});
  labelt multi_label({"b", "c", "d"});
  labelt multi_label2({"b", "d", "d"});

  REQUIRE(single_label < b_label);
  REQUIRE(single_label < multi_label);
  REQUIRE(single_label < multi_label2);

  REQUIRE(b_label < multi_label);
  REQUIRE(b_label < multi_label2);

  REQUIRE(multi_label < multi_label2);
}

TEST_CASE("Convert structured_datat", "[core][util][structured_data]")
{
  GIVEN("Empty structured data")
  {
    structured_datat empty_data{{}};

    THEN("Empty json object")
    {
      REQUIRE(to_json(empty_data) == json_nullt());
    }

    THEN("Empty xml object")
    {
      REQUIRE(to_xml(empty_data).elements.empty());
      REQUIRE(to_xml(empty_data).data.empty());
    }

    THEN("Empty pretty object")
    {
      REQUIRE(to_pretty(empty_data) == "");
    }
  }
  GIVEN("Single node XML")
  {
    const labelt label = labelt{{"node", "name"}};
    const structured_data_entryt entry =
      structured_data_entryt::data_node(json_stringt{"nodeValue"});
    structured_datat single_node{{{label, entry}}};

    THEN("Appropriate json object")
    {
      json_objectt expected;
      expected["nodeName"] = json_stringt{"nodeValue"};
      REQUIRE(to_json(single_node) == expected);
    }

    THEN("Appropriate xml object")
    {
      xmlt expected_xml{"node-name"};
      expected_xml.data = "nodeValue";
      auto result = to_xml(single_node);
      REQUIRE(result == expected_xml);
    }

    THEN("Appropriate pretty object")
    {
      REQUIRE(to_pretty(single_node) == "Node name: nodeValue");
    }
  }
  GIVEN("Nested nodes")
  {
    const labelt child1_label = labelt{{"child", "1"}};
    const structured_data_entryt child1_value =
      structured_data_entryt::data_node(json_stringt{"c1v"});
    const labelt child2_label = labelt{{"child", "2"}};
    const structured_data_entryt child2_value =
      structured_data_entryt::data_node(json_stringt{"c2v"});
    structured_datat single_node{
      {{child1_label, child1_value}, {child2_label, child2_value}}};

    THEN("Appropriate json object")
    {
      json_objectt expected;
      expected["child1"] = json_stringt{"c1v"};
      expected["child2"] = json_stringt{"c2v"};
      REQUIRE(to_json(single_node) == expected);
    }

    THEN("Empty xml object")
    {
      xmlt expected_xml{"root"};
      xmlt expected_child1 = xmlt{"child-1"};
      expected_child1.data = "c1v";
      xmlt expected_child2 = xmlt{"child-2"};
      expected_child2.data = "c2v";
      expected_xml.elements = {{expected_child1, expected_child2}};
      auto result = to_xml(single_node);
      REQUIRE(result == expected_xml);
    }

    THEN("Empty pretty object")
    {
      std::string expected =
        "Child 1: c1v\n"
        "Child 2: c2v";
      REQUIRE(to_pretty(single_node) == expected);
    }
  }
  SECTION("Non-string data")
  {
    const labelt child1_label = labelt{{"child", "1"}};
    const structured_data_entryt child1_value =
      structured_data_entryt::data_node(json_numbert{"10"});

    structured_datat single_node{{{child1_label, child1_value}}};

    THEN("Appropriate json object")
    {
      json_objectt expected;
      expected["child1"] = json_numbert{"10"};
      REQUIRE(to_json(single_node) == expected);
    }

    THEN("Empty xml object")
    {
      xmlt expected_xml = xmlt{"child-1"};
      expected_xml.data = "10";
      auto result = to_xml(single_node);
      REQUIRE(result == expected_xml);
    }

    THEN("Empty pretty object")
    {
      std::string expected = "Child 1: 10";
      REQUIRE(to_pretty(single_node) == expected);
    }
  }
}