blob: b284b501bbd9b4c55e24abb6499fbd778adf3fe4 [file] [log] [blame]
Austin Schuh24adb6b2015-09-06 17:37:40 -07001// Copyright (c) 2013, Matt Godbolt
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9//
10// Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24// POSSIBILITY OF SUCH DAMAGE.
25
26#include "seasocks/util/Json.h"
27
28#include <gmock/gmock.h>
29#include <ostream>
30#include <map>
31#include <unordered_map>
32
33using namespace testing;
34using namespace seasocks;
35
36namespace {
37
38TEST(JsonTests, shouldHandleMaps) {
39 EXPECT_EQ("{\"monkey\":12}", makeMap("monkey", 12));
40}
41
42TEST(JsonTests, shouldHandleQuotedStrings) {
43 EXPECT_EQ("{\"key\":\"I have \\\"quotes\\\"\"}",
44 makeMap("key", "I have \"quotes\""));
45}
46
47TEST(JsonTests, shouldHandleNewLinesInStrings) {
48 std::stringstream str;
49 jsonToStream(str, "I have\nnew\rlines");
50 EXPECT_EQ("\"I have\\nnew\\rlines\"", str.str());
51}
52
53TEST(JsonTests, shouldHandleCrazyChars) {
54 std::stringstream str;
55 jsonToStream(str, "\x01\x02\x1f");
56 EXPECT_EQ("\"\\u0001\\u0002\\u001f\"", str.str());
57}
58
59TEST(JsonTests, shouldHandleDate) {
60 std::stringstream str;
61 jsonToStream(str, EpochTimeAsLocal(209001600));
62 EXPECT_EQ("new Date(209001600000).toLocaleString()", str.str());
63}
64
65struct Object {
66 void jsonToStream(std::ostream &ostr) const {
67 ostr << makeMap("object", true);
68 }
69 // Clang is pernickity about this. We don't want use this function
70 // but it's here to catch errors where we accidentally use it instead of the
71 // jsonToStream.
72 friend std::ostream &operator << (std::ostream &o, const Object &ob) {
73 return o << "Not this one";
74 }
75};
76
77struct Object2 {
78 friend std::ostream &operator << (std::ostream &o, const Object2 &ob) {
79 return o << "This is object 2";
80 }
81};
82
83static_assert(is_streamable<Object2>::value, "Should be streamable");
84
85TEST(JsonTests, shouldHandleCustomObjects) {
86 EXPECT_EQ(R"({"obj":{"object":true}})", makeMap("obj", Object()));
87 Object o;
88 EXPECT_EQ(R"({"obj":{"object":true}})", makeMap("obj", o));
89 EXPECT_EQ(R"({"obj":"This is object 2"})", makeMap("obj", Object2()));
90 Object2 o2;
91 EXPECT_EQ(R"({"obj":"This is object 2"})", makeMap("obj", o2));
92 // Putting a clang-specific pragma to thwart the unused warning in Object
93 // upsets GCC...so we just put a test for this to ensure it's used.
94 std::ostringstream ost;
95 ost << Object();
96 EXPECT_EQ("Not this one", ost.str()); // See comment
97}
98
99TEST(JsonTests, to_json) {
100 EXPECT_EQ("1", to_json(1));
101 EXPECT_EQ("3.14", to_json(3.14));
102 EXPECT_EQ("\"hello\"", to_json("hello"));
103 EXPECT_EQ(R"({"object":true})", to_json(Object()));
104 EXPECT_EQ(R"("This is object 2")", to_json(Object2()));
105}
106TEST(JsonTests, handlesArrays) {
107 EXPECT_EQ(R"([])", makeArray());
108 EXPECT_EQ(R"([1])", makeArray(1));
109 EXPECT_EQ(R"([1,2,3])", makeArray(1, 2, 3));
110 EXPECT_EQ(R"([1,2,3])", makeArray({1, 2, 3}));
111 EXPECT_EQ(R"(["abc"])", makeArray("abc"));
112 EXPECT_EQ(R"(["a","b","c"])", makeArray("a", "b", "c"));
113 EXPECT_EQ(R"(["a","b","c"])", makeArray({"a", "b", "c"}));
114 std::vector<JsonnedString> strs = { to_json(false), to_json(true) };
115 EXPECT_EQ(R"([false,true])", makeArrayFromContainer(strs));
116}
117
118TEST(JsonTests, handlesMaps) {
119 std::map<std::string, JsonnedString> ordMap;
120 ordMap["hello"] = to_json(true);
121 ordMap["goodbye"] = to_json(false);
122 EXPECT_EQ(R"({"goodbye":false,"hello":true})", makeMapFromContainer(ordMap));
123 std::map<std::string, JsonnedString> unordMap;
124 unordMap["hello"] = to_json(true);
125 unordMap["goodbye"] = to_json(false);
126 EXPECT_THAT(makeMapFromContainer(unordMap), AnyOf(
127 Eq(R"({"goodbye":false,"hello":true})"),
128 Eq(R"({"hello":true,"goodbye":false})")));
129}
130
131}