Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "gtest/gtest.h" |
michael@0 | 7 | |
michael@0 | 8 | #include <sstream> |
michael@0 | 9 | #include "JSStreamWriter.h" |
michael@0 | 10 | |
michael@0 | 11 | TEST(JSStreamWriter, NoOutput) { |
michael@0 | 12 | std::stringstream ss; |
michael@0 | 13 | JSStreamWriter b(ss); |
michael@0 | 14 | ASSERT_TRUE(ss.str().compare("") == 0); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | TEST(JSStreamWriter, EmptyObject) { |
michael@0 | 18 | std::stringstream ss; |
michael@0 | 19 | JSStreamWriter b(ss); |
michael@0 | 20 | b.BeginObject(); |
michael@0 | 21 | b.EndObject(); |
michael@0 | 22 | ASSERT_TRUE(ss.str().compare("{}") == 0); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | TEST(JSStreamWriter, OnePropertyObject) { |
michael@0 | 26 | std::stringstream ss; |
michael@0 | 27 | JSStreamWriter b(ss); |
michael@0 | 28 | b.BeginObject(); |
michael@0 | 29 | b.Name("a"); |
michael@0 | 30 | b.Value(1); |
michael@0 | 31 | b.EndObject(); |
michael@0 | 32 | ASSERT_TRUE(ss.str().compare("{\"a\":1}") == 0); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | TEST(JSStreamWriter, MultiPropertyObject) { |
michael@0 | 36 | std::stringstream ss; |
michael@0 | 37 | JSStreamWriter b(ss); |
michael@0 | 38 | b.BeginObject(); |
michael@0 | 39 | b.Name("a"); |
michael@0 | 40 | b.Value(1); |
michael@0 | 41 | b.Name("b"); |
michael@0 | 42 | b.Value(2); |
michael@0 | 43 | b.EndObject(); |
michael@0 | 44 | ASSERT_TRUE(ss.str().compare("{\"a\":1,\"b\":2}") == 0); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | TEST(JSStreamWriter, OnePropertyArray) { |
michael@0 | 48 | std::stringstream ss; |
michael@0 | 49 | JSStreamWriter b(ss); |
michael@0 | 50 | b.BeginArray(); |
michael@0 | 51 | b.Value(1); |
michael@0 | 52 | b.EndArray(); |
michael@0 | 53 | ASSERT_TRUE(ss.str().compare("[1]") == 0); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | TEST(JSStreamWriter, MultiPropertyArray) { |
michael@0 | 57 | std::stringstream ss; |
michael@0 | 58 | JSStreamWriter b(ss); |
michael@0 | 59 | b.BeginArray(); |
michael@0 | 60 | b.Value(1); |
michael@0 | 61 | b.Value(2); |
michael@0 | 62 | b.EndArray(); |
michael@0 | 63 | ASSERT_TRUE(ss.str().compare("[1,2]") == 0); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | TEST(JSStreamWriter, NestedObject) { |
michael@0 | 67 | std::stringstream ss; |
michael@0 | 68 | JSStreamWriter b(ss); |
michael@0 | 69 | b.BeginObject(); |
michael@0 | 70 | b.Name("a"); |
michael@0 | 71 | b.BeginObject(); |
michael@0 | 72 | b.Name("b"); |
michael@0 | 73 | b.Value(1); |
michael@0 | 74 | b.EndObject(); |
michael@0 | 75 | b.EndObject(); |
michael@0 | 76 | ASSERT_TRUE(ss.str().compare("{\"a\":{\"b\":1}}") == 0); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | TEST(JSStreamWriter, NestedObjectInArray) { |
michael@0 | 80 | std::stringstream ss; |
michael@0 | 81 | JSStreamWriter b(ss); |
michael@0 | 82 | b.BeginArray(); |
michael@0 | 83 | b.BeginObject(); |
michael@0 | 84 | b.Name("a"); |
michael@0 | 85 | b.Value(1); |
michael@0 | 86 | b.EndObject(); |
michael@0 | 87 | b.EndArray(); |
michael@0 | 88 | ASSERT_TRUE(ss.str().compare("[{\"a\":1}]") == 0); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | TEST(JSStreamWriter, NestedArrayInObject) { |
michael@0 | 92 | std::stringstream ss; |
michael@0 | 93 | JSStreamWriter b(ss); |
michael@0 | 94 | b.BeginObject(); |
michael@0 | 95 | b.Name("a"); |
michael@0 | 96 | b.BeginArray(); |
michael@0 | 97 | b.Value(1); |
michael@0 | 98 | b.EndArray(); |
michael@0 | 99 | b.EndObject(); |
michael@0 | 100 | ASSERT_TRUE(ss.str().compare("{\"a\":[1]}") == 0); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | TEST(JSStreamWriter, StingEscaping) { |
michael@0 | 104 | std::stringstream ss; |
michael@0 | 105 | JSStreamWriter b(ss); |
michael@0 | 106 | b.Value("a\"a"); |
michael@0 | 107 | ASSERT_TRUE(ss.str().compare("\"a\\\"a\"") == 0); |
michael@0 | 108 | |
michael@0 | 109 | std::stringstream ss2; |
michael@0 | 110 | JSStreamWriter b2(ss2); |
michael@0 | 111 | b2.Value("a\na"); |
michael@0 | 112 | ASSERT_TRUE(ss2.str().compare("\"a\\u000Aa\"") == 0); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | TEST(JSStreamWriter, ArrayOfOjects) { |
michael@0 | 116 | std::stringstream ss; |
michael@0 | 117 | JSStreamWriter b(ss); |
michael@0 | 118 | b.BeginArray(); |
michael@0 | 119 | b.BeginObject(); |
michael@0 | 120 | b.EndObject(); |
michael@0 | 121 | |
michael@0 | 122 | b.BeginObject(); |
michael@0 | 123 | b.EndObject(); |
michael@0 | 124 | b.EndArray(); |
michael@0 | 125 | ASSERT_TRUE(ss.str().compare("[{},{}]") == 0); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | TEST(JSStreamWriter, Complex) { |
michael@0 | 129 | std::stringstream ss; |
michael@0 | 130 | JSStreamWriter b(ss); |
michael@0 | 131 | b.BeginObject(); |
michael@0 | 132 | b.Name("a"); |
michael@0 | 133 | b.BeginArray(); |
michael@0 | 134 | b.Value(1); |
michael@0 | 135 | |
michael@0 | 136 | b.BeginObject(); |
michael@0 | 137 | b.EndObject(); |
michael@0 | 138 | |
michael@0 | 139 | b.BeginObject(); |
michael@0 | 140 | b.Name("b"); |
michael@0 | 141 | b.Value("c"); |
michael@0 | 142 | b.EndObject(); |
michael@0 | 143 | b.EndArray(); |
michael@0 | 144 | |
michael@0 | 145 | b.Name("b"); |
michael@0 | 146 | b.BeginArray(); |
michael@0 | 147 | b.BeginArray(); |
michael@0 | 148 | b.EndArray(); |
michael@0 | 149 | b.EndArray(); |
michael@0 | 150 | b.EndObject(); |
michael@0 | 151 | ASSERT_TRUE(ss.str().compare("{\"a\":[1,{},{\"b\":\"c\"}],\"b\":[[]]}") == 0); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | TEST(JSStreamWriter, Complex2) { |
michael@0 | 155 | std::stringstream ss; |
michael@0 | 156 | JSStreamWriter b(ss); |
michael@0 | 157 | b.BeginObject(); |
michael@0 | 158 | b.Name("a"); |
michael@0 | 159 | b.BeginArray(); |
michael@0 | 160 | b.BeginObject(); |
michael@0 | 161 | b.Name("b"); |
michael@0 | 162 | b.Value("c"); |
michael@0 | 163 | b.Name("d"); |
michael@0 | 164 | b.BeginArray(); |
michael@0 | 165 | b.BeginObject(); |
michael@0 | 166 | b.Name("e"); |
michael@0 | 167 | b.BeginArray(); |
michael@0 | 168 | b.BeginObject(); |
michael@0 | 169 | b.Name("f"); |
michael@0 | 170 | b.Value("g"); |
michael@0 | 171 | b.EndObject(); |
michael@0 | 172 | b.BeginObject(); |
michael@0 | 173 | b.Name("h"); |
michael@0 | 174 | b.Value("i"); |
michael@0 | 175 | b.EndObject(); |
michael@0 | 176 | b.EndArray(); |
michael@0 | 177 | b.EndObject(); |
michael@0 | 178 | b.EndArray(); |
michael@0 | 179 | b.EndObject(); |
michael@0 | 180 | b.EndArray(); |
michael@0 | 181 | b.EndObject(); |
michael@0 | 182 | ASSERT_TRUE(ss.str().compare("{\"a\":[{\"b\":\"c\",\"d\":[{\"e\":[{\"f\":\"g\"},{\"h\":\"i\"}]}]}]}") |
michael@0 | 183 | } |