File: json_object.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 (106 lines) | stat: -rw-r--r-- 3,340 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
/*******************************************************************\

Module: Catch tests for json_objectt

Author: Diffblue Ltd.

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

#include <testing-utils/use_catch.h>

#include <util/json.h>
#include <util/optional_utils.h>
#include <util/range.h>

#include <algorithm>
#include <iterator>
#include <string>
#include <vector>

SCENARIO(
  "Test that json_objectt is compatible with STL algorithms",
  "[core][util][json]")
{
  GIVEN("An empty json_objectt")
  {
    json_objectt object;
    THEN("std::transform can be used to insert into the json_object.")
    {
      const std::vector<std::string> input_values = {"one", "two", "three"};
      const std::insert_iterator<json_objectt> insert_it =
        std::inserter(object, object.end());
      std::transform(
        input_values.begin(),
        input_values.end(),
        insert_it,
        [](const std::string &number) {
          return make_pair(number, json_stringt{number});
        });

      const std::optional<jsont> one = optional_lookup(object, "one");
      REQUIRE(one);
      REQUIRE(one->value == "one");
      const std::optional<jsont> two = optional_lookup(object, "two");
      REQUIRE(two);
      REQUIRE(two->value == "two");
      const std::optional<jsont> three = optional_lookup(object, "three");
      REQUIRE(three);
      REQUIRE(three->value == "three");
    }
  }
}

SCENARIO(
  "Test that json_objectt can be constructed from an initializer list.",
  "[core][util][json]")
{
  GIVEN("A json_objectt constructed from an initializer list.")
  {
    const json_objectt object{
      {"number", json_numbert{"6"}},
      {"string", json_stringt{"eggs"}},
      {"mice",
       json_objectt{{"number", json_numbert{"3"}},
                    {"string", json_stringt{"blind"}}}}};
    THEN("The fields of the json_objectt match the initialiser list.")
    {
      REQUIRE(object["number"].kind == jsont::kindt::J_NUMBER);
      REQUIRE(object["number"].value == "6");
      REQUIRE(object["string"].kind == jsont::kindt::J_STRING);
      REQUIRE(object["string"].value == "eggs");
      const json_objectt mice = to_json_object(object["mice"]);
      REQUIRE(mice["number"].kind == jsont::kindt::J_NUMBER);
      REQUIRE(mice["number"].value == "3");
      REQUIRE(mice["string"].kind == jsont::kindt::J_STRING);
      REQUIRE(mice["string"].value == "blind");
    }
  }
}

SCENARIO(
  "Test that json_objectt can be constructed using `ranget`",
  "[core][util][json]")
{
  GIVEN("A vector of numbers.")
  {
    const std::vector<int> input{1, 2, 3};
    THEN(
      "A json_objectt can be constructed from the vector of numbers using "
      "range and map.")
    {
      const json_objectt output =
        make_range(input)
          .map([](const int number) {
            const std::string number_as_string = std::to_string(number);
            return make_pair(number_as_string, json_stringt{number_as_string});
          })
          .collect<json_objectt>();
      REQUIRE(output["1"].kind == jsont::kindt::J_STRING);
      REQUIRE(output["1"].value == "1");
      REQUIRE(output["2"].kind == jsont::kindt::J_STRING);
      REQUIRE(output["2"].value == "2");
      REQUIRE(output["3"].kind == jsont::kindt::J_STRING);
      REQUIRE(output["3"].value == "3");
    };
  }
}