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/json_stream.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
| 9 #include "vm/dart_api_impl.h" |
9 | 10 |
10 namespace dart { | 11 namespace dart { |
11 | 12 |
12 | 13 |
13 TEST_CASE(JSON_ScanJSON) { | 14 TEST_CASE(JSON_ScanJSON) { |
14 const char* msg = "{ \"id\": 5, \"command\" : \"Debugger.pause\" }"; | 15 const char* msg = "{ \"id\": 5, \"command\" : \"Debugger.pause\" }"; |
15 | 16 |
16 JSONScanner scanner(msg); | 17 JSONScanner scanner(msg); |
17 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenIllegal); | 18 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenIllegal); |
18 scanner.Scan(); | 19 scanner.Scan(); |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 TEST_CASE(JSON_JSONStream_EscapedString) { | 309 TEST_CASE(JSON_JSONStream_EscapedString) { |
309 JSONStream js; | 310 JSONStream js; |
310 { | 311 { |
311 JSONArray jsarr(&js); | 312 JSONArray jsarr(&js); |
312 jsarr.AddValue("Hel\"\"lo\r\n\t"); | 313 jsarr.AddValue("Hel\"\"lo\r\n\t"); |
313 } | 314 } |
314 EXPECT_STREQ("[\"Hel\\\"\\\"lo\\r\\n\\t\"]", js.ToCString()); | 315 EXPECT_STREQ("[\"Hel\\\"\\\"lo\\r\\n\\t\"]", js.ToCString()); |
315 } | 316 } |
316 | 317 |
317 | 318 |
| 319 TEST_CASE(JSON_JSONStream_DartString) { |
| 320 const char* kScriptChars = |
| 321 "var ascii = 'Hello, World!';\n" |
| 322 "var unicode = '\\u00CE\\u00F1\\u0163\\u00E9r\\u00F1\\u00E5\\u0163" |
| 323 "\\u00EE\\u00F6\\u00F1\\u00E5\\u013C\\u00EE\\u017E\\u00E5\\u0163" |
| 324 "\\u00EE\\u1EDD\\u00F1';\n" |
| 325 "var surrogates = '\\u{1D11E}\\u{1D11E}\\u{1D11E}\\u{1D11E}" |
| 326 "\\u{1D11E}';\n" |
| 327 "var nullInMiddle = 'This has\\u0000 four words.';"; |
| 328 |
| 329 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 330 EXPECT_VALID(lib); |
| 331 |
| 332 Dart_Handle result; |
| 333 String& obj = String::Handle(); |
| 334 |
| 335 { |
| 336 result = Dart_GetField(lib, NewString("ascii")); |
| 337 EXPECT_VALID(result); |
| 338 obj ^= Api::UnwrapHandle(result); |
| 339 |
| 340 JSONStream js; |
| 341 { |
| 342 JSONObject jsobj(&js); |
| 343 jsobj.AddPropertyStr("ascci", obj);; |
| 344 } |
| 345 EXPECT_STREQ("{\"ascci\":\"Hello, World!\"}", js.ToCString()); |
| 346 } |
| 347 |
| 348 { |
| 349 result = Dart_GetField(lib, NewString("unicode")); |
| 350 EXPECT_VALID(result); |
| 351 obj ^= Api::UnwrapHandle(result); |
| 352 |
| 353 JSONStream js; |
| 354 { |
| 355 JSONObject jsobj(&js); |
| 356 jsobj.AddPropertyStr("unicode", obj); |
| 357 } |
| 358 EXPECT_STREQ("{\"unicode\":\"\\u00CE\\u00F1\\u0163\\u00E9r\\u00F1\\u00E5" |
| 359 "\\u0163\\u00EE\\u00F6\\u00F1\\u00E5\\u013C\\u00EE\\u017E" |
| 360 "\\u00E5\\u0163\\u00EE\\u1EDD\\u00F1\"}", js.ToCString()); |
| 361 } |
| 362 |
| 363 { |
| 364 result = Dart_GetField(lib, NewString("surrogates")); |
| 365 EXPECT_VALID(result); |
| 366 obj ^= Api::UnwrapHandle(result); |
| 367 |
| 368 JSONStream js; |
| 369 { |
| 370 JSONObject jsobj(&js); |
| 371 jsobj.AddPropertyStr("surrogates", obj); |
| 372 } |
| 373 EXPECT_STREQ("{\"surrogates\":\"\\uD834\\uDD1E\\uD834\\uDD1E\\uD834\\uDD1E" |
| 374 "\\uD834\\uDD1E\\uD834\\uDD1E\"}", js.ToCString()); |
| 375 } |
| 376 |
| 377 { |
| 378 result = Dart_GetField(lib, NewString("nullInMiddle")); |
| 379 EXPECT_VALID(result); |
| 380 obj ^= Api::UnwrapHandle(result); |
| 381 |
| 382 JSONStream js; |
| 383 { |
| 384 JSONObject jsobj(&js); |
| 385 jsobj.AddPropertyStr("nullInMiddle", obj); |
| 386 } |
| 387 EXPECT_STREQ("{\"nullInMiddle\":\"This has\\u0000 four words.\"}", |
| 388 js.ToCString()); |
| 389 } |
| 390 } |
| 391 |
| 392 |
318 TEST_CASE(JSON_JSONStream_Options) { | 393 TEST_CASE(JSON_JSONStream_Options) { |
319 const char* arguments[] = {"a", "b", "c"}; | 394 const char* arguments[] = {"a", "b", "c"}; |
320 const char* option_keys[] = {"dog", "cat"}; | 395 const char* option_keys[] = {"dog", "cat"}; |
321 const char* option_values[] = {"apple", "banana"}; | 396 const char* option_values[] = {"apple", "banana"}; |
322 | 397 |
323 JSONStream js; | 398 JSONStream js; |
324 EXPECT(js.num_arguments() == 0); | 399 EXPECT(js.num_arguments() == 0); |
325 js.SetArguments(&arguments[0], 3); | 400 js.SetArguments(&arguments[0], 3); |
326 EXPECT(js.num_arguments() == 3); | 401 EXPECT(js.num_arguments() == 3); |
327 EXPECT_STREQ("a", js.command()); | 402 EXPECT_STREQ("a", js.command()); |
328 | 403 |
329 EXPECT(js.num_options() == 0); | 404 EXPECT(js.num_options() == 0); |
330 js.SetOptions(&option_keys[0], &option_values[0], 3); | 405 js.SetOptions(&option_keys[0], &option_values[0], 3); |
331 EXPECT(js.num_options() == 3); | 406 EXPECT(js.num_options() == 3); |
332 EXPECT(!js.HasOption("lizard")); | 407 EXPECT(!js.HasOption("lizard")); |
333 EXPECT(js.HasOption("dog")); | 408 EXPECT(js.HasOption("dog")); |
334 EXPECT(js.HasOption("cat")); | 409 EXPECT(js.HasOption("cat")); |
335 EXPECT(js.OptionIs("cat", "banana")); | 410 EXPECT(js.OptionIs("cat", "banana")); |
336 EXPECT(!js.OptionIs("dog", "banana")); | 411 EXPECT(!js.OptionIs("dog", "banana")); |
337 } | 412 } |
338 | 413 |
339 } // namespace dart | 414 } // namespace dart |
OLD | NEW |