OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "platform/json.h" | 6 #include "platform/json.h" |
| 7 #include "vm/json_stream.h" |
7 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
8 | 9 |
9 namespace dart { | 10 namespace dart { |
10 | 11 |
11 | 12 |
12 TEST_CASE(JSON_ScanJSON) { | 13 TEST_CASE(JSON_ScanJSON) { |
13 const char* msg = "{ \"id\": 5, \"command\" : \"Debugger.pause\" }"; | 14 const char* msg = "{ \"id\": 5, \"command\" : \"Debugger.pause\" }"; |
14 | 15 |
15 JSONScanner scanner(msg); | 16 JSONScanner scanner(msg); |
16 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenIllegal); | 17 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenIllegal); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 w.Printf(", \"%s\" : \"%s\" }", "command", "stopIt"); | 110 w.Printf(", \"%s\" : \"%s\" }", "command", "stopIt"); |
110 EXPECT_STREQ("{ \"length\" : 175, \"command\" : \"stopIt\" }", w.buf()); | 111 EXPECT_STREQ("{ \"length\" : 175, \"command\" : \"stopIt\" }", w.buf()); |
111 | 112 |
112 JSONReader r(w.buf()); | 113 JSONReader r(w.buf()); |
113 bool found = r.Seek("command"); | 114 bool found = r.Seek("command"); |
114 EXPECT(found); | 115 EXPECT(found); |
115 EXPECT_EQ(r.Type(), JSONReader::kString); | 116 EXPECT_EQ(r.Type(), JSONReader::kString); |
116 EXPECT(r.IsStringLiteral("stopIt")); | 117 EXPECT(r.IsStringLiteral("stopIt")); |
117 } | 118 } |
118 | 119 |
| 120 |
| 121 TEST_CASE(JSON_JSONStream_Primitives) { |
| 122 TextBuffer tb(256); |
| 123 JSONStream js(&tb); |
| 124 |
| 125 js.OpenObject(); |
| 126 js.CloseObject(); |
| 127 |
| 128 EXPECT_STREQ("{}", tb.buf()); |
| 129 |
| 130 js.Clear(); |
| 131 js.OpenArray(); |
| 132 js.CloseArray(); |
| 133 EXPECT_STREQ("[]", tb.buf()); |
| 134 |
| 135 js.Clear(); |
| 136 js.PrintValueBool(true); |
| 137 EXPECT_STREQ("true", tb.buf()); |
| 138 |
| 139 js.Clear(); |
| 140 js.PrintValueBool(false); |
| 141 EXPECT_STREQ("false", tb.buf()); |
| 142 |
| 143 js.Clear(); |
| 144 js.PrintValue((intptr_t)4); |
| 145 EXPECT_STREQ("4", tb.buf()); |
| 146 |
| 147 js.Clear(); |
| 148 js.PrintValue(1.0); |
| 149 EXPECT_STREQ("1.000000", tb.buf()); |
| 150 |
| 151 js.Clear(); |
| 152 js.PrintValue("hello"); |
| 153 EXPECT_STREQ("\"hello\"", tb.buf()); |
| 154 } |
| 155 |
| 156 |
| 157 TEST_CASE(JSON_JSONStream_Array) { |
| 158 TextBuffer tb(256); |
| 159 JSONStream js(&tb); |
| 160 js.Clear(); |
| 161 js.OpenArray(); |
| 162 js.PrintValueBool(true); |
| 163 js.PrintValueBool(false); |
| 164 js.CloseArray(); |
| 165 EXPECT_STREQ("[true,false]", tb.buf()); |
| 166 } |
| 167 |
| 168 |
| 169 TEST_CASE(JSON_JSONStream_Object) { |
| 170 TextBuffer tb(256); |
| 171 JSONStream js(&tb); |
| 172 js.Clear(); |
| 173 js.OpenObject(); |
| 174 js.PrintProperty("key1", "a"); |
| 175 js.PrintProperty("key2", "b"); |
| 176 js.CloseObject(); |
| 177 EXPECT_STREQ("{\"key1\":\"a\",\"key2\":\"b\"}", tb.buf()); |
| 178 } |
| 179 |
| 180 TEST_CASE(JSON_JSONStream_NestedObject) { |
| 181 TextBuffer tb(256); |
| 182 JSONStream js(&tb); |
| 183 js.OpenObject(); |
| 184 js.OpenObject("key"); |
| 185 js.PrintProperty("key1", "d"); |
| 186 js.CloseObject(); |
| 187 js.CloseObject(); |
| 188 EXPECT_STREQ("{\"key\":{\"key1\":\"d\"}}", tb.buf()); |
| 189 } |
| 190 |
| 191 |
| 192 TEST_CASE(JSON_JSONStream_ObjectArray) { |
| 193 TextBuffer tb(256); |
| 194 JSONStream js(&tb); |
| 195 js.OpenArray(); |
| 196 js.OpenObject(); |
| 197 js.PrintProperty("key", "e"); |
| 198 js.CloseObject(); |
| 199 js.OpenObject(); |
| 200 js.PrintProperty("yek", "f"); |
| 201 js.CloseObject(); |
| 202 js.CloseArray(); |
| 203 EXPECT_STREQ("[{\"key\":\"e\"},{\"yek\":\"f\"}]", tb.buf()); |
| 204 } |
| 205 |
| 206 |
| 207 TEST_CASE(JSON_JSONStream_ArrayArray) { |
| 208 TextBuffer tb(256); |
| 209 JSONStream js(&tb); |
| 210 js.OpenArray(); |
| 211 js.OpenArray(); |
| 212 js.PrintValue((intptr_t)4); |
| 213 js.CloseArray(); |
| 214 js.OpenArray(); |
| 215 js.PrintValueBool(false); |
| 216 js.CloseArray(); |
| 217 js.CloseArray(); |
| 218 EXPECT_STREQ("[[4],[false]]", tb.buf()); |
| 219 } |
| 220 |
| 221 |
119 } // namespace dart | 222 } // namespace dart |
OLD | NEW |