File: JsonParserTest.C

package info (click to toggle)
witty 3.2.1-2%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 70,436 kB
  • sloc: cpp: 117,095; ansic: 77,999; xml: 7,564; sh: 1,037; perl: 208; makefile: 144; java: 86; sql: 14
file content (118 lines) | stat: -rw-r--r-- 2,498 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
/*
 * Copyright (C) 2011 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#include <boost/test/unit_test.hpp>

#include <Wt/Json/Parser>
#include <Wt/Json/Object>
#include <Wt/Json/Array>

#if !defined(WT_NO_SPIRIT) && BOOST_VERSION >= 104100
#  define JSON_PARSER
#endif

#ifdef JSON_PARSER

#define JS(...) #__VA_ARGS__

using namespace Wt;

BOOST_AUTO_TEST_CASE( json_parse_empty_test )
{
  Json::Value result;
  Json::parse("{}", result);

  BOOST_REQUIRE(result.type() == Json::ObjectType);
  BOOST_REQUIRE(((const Json::Object&) result).empty());
}

BOOST_AUTO_TEST_CASE( json_parse_object1_test )
{
  Json::Object result;
  Json::parse("{ "
	      "  \"a\": \"That's great\", "
	      "  \"b\": true "
	      "}",
	      result);
  BOOST_REQUIRE(result.size() == 2);

  WString a = result.get("a");
  bool b = result.get("b");

  BOOST_REQUIRE(a == "That's great");
  BOOST_REQUIRE(b == true);
}

BOOST_AUTO_TEST_CASE( json_parse_object2_test )
{
  Json::Object result;
  Json::parse("{ \"a\": 5 }", result);

  BOOST_REQUIRE(result.size() == 1);

  int i = result.get("a");

  BOOST_REQUIRE(i == 5);
}

BOOST_AUTO_TEST_CASE( json_parse_strings_test )
{
  Json::Object result;
  Json::parse(JS({
	"s1": "simple",
	"s2": "escaped: \\ \t \n \b \r",
	"s3": "unicode: \u0194"
	  }), result);

  BOOST_REQUIRE(result.size() == 3);

  const WString& s1 = result.get("s1");
  BOOST_REQUIRE(s1 == "simple");
  const WString& s2 = result.get("s2");
  BOOST_REQUIRE(s2 == "escaped: \\ \t \n \b \r");
  const WString& s3 = result.get("s3");
  BOOST_REQUIRE(s3 == L"unicode: \x0194");
}

BOOST_AUTO_TEST_CASE( json_structure_test )
{
  Json::Object result;
  Json::parse(JS({
     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
     "address":
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber":
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
	  }), result);

  BOOST_REQUIRE(result.size() == 5);

  const Json::Array& phoneNumbers = result.get("phoneNumber");
  BOOST_REQUIRE(phoneNumbers.size() == 2);

  const Json::Object& p1 = phoneNumbers[0];
  WString t1 = p1.get("type");
  WString n1 = p1.get("number");
  BOOST_REQUIRE(t1 == "home");
  BOOST_REQUIRE(n1 == "212 555-1234");
}

#endif // JSON_PARSER