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/text_buffer.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 #include "vm/dart_api_impl.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 | 13 |
14 TEST_CASE(JSON_ScanJSON) { | |
15 const char* msg = "{ \"id\": 5, \"command\" : \"Debugger.pause\" }"; | |
16 | |
17 JSONScanner scanner(msg); | |
18 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenIllegal); | |
19 scanner.Scan(); | |
20 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenLBrace); | |
21 scanner.Scan(); | |
22 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenString); | |
23 EXPECT(scanner.IsStringLiteral("id")); | |
24 scanner.Scan(); | |
25 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenColon); | |
26 scanner.Scan(); | |
27 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenInteger); | |
28 scanner.Scan(); | |
29 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenComma); | |
30 scanner.Scan(); | |
31 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenString); | |
32 EXPECT(scanner.IsStringLiteral("command")); | |
33 scanner.Scan(); | |
34 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenColon); | |
35 scanner.Scan(); | |
36 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenString); | |
37 EXPECT(scanner.IsStringLiteral("Debugger.pause")); | |
38 scanner.Scan(); | |
39 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenRBrace); | |
40 scanner.Scan(); | |
41 EXPECT_EQ(scanner.CurrentToken(), JSONScanner::TokenEOM); | |
42 } | |
43 | |
44 | |
45 TEST_CASE(JSON_SyntaxError) { | |
46 const char* jobj = "{ \"id\": 5, " | |
47 " \"command\" : \"Debugger.stop\"" // Missing comma. | |
48 " \"params\" : { " | |
49 " \"url\" : \"blah.dart\", " // Missing comma. | |
50 " \"line\": 111, " | |
51 " }," | |
52 " \"foo\": \"outer foo\", " | |
53 "}"; | |
54 JSONReader reader(jobj); | |
55 bool found; | |
56 | |
57 found = reader.Seek("id"); | |
58 EXPECT(found); | |
59 EXPECT_EQ(reader.Type(), JSONReader::kInteger); | |
60 found = reader.Seek("params"); | |
61 EXPECT(!found); | |
62 EXPECT(reader.Error()); | |
63 EXPECT_EQ(reader.Type(), JSONReader::kNone); | |
64 EXPECT_EQ(0, reader.ValueLen()); | |
65 EXPECT(reader.ValueChars() == NULL); | |
66 } | |
67 | |
68 | |
69 TEST_CASE(JSON_JSONReader) { | |
70 const char* jobj = "{ \"id\": 5, " | |
71 " \"command\" : \"Debugger.setBreakpoint\"," | |
72 " \"params\" : { " | |
73 " \"url\" : \"blah.dart\", " | |
74 " \"foo\" : [null, 1, { }, \"bar\", true, false]," | |
75 " \"line\": 111, " | |
76 " }," | |
77 " \"foo\": \"outer foo\", " | |
78 " \"quote\": \"\\\"\", " | |
79 " \"white\": \"\\t \\n\", " | |
80 "}"; | |
81 | |
82 JSONReader reader(jobj); | |
83 bool found; | |
84 char s[128]; | |
85 | |
86 found = reader.Seek("id"); | |
87 EXPECT(found); | |
88 EXPECT_EQ(reader.Type(), JSONReader::kInteger); | |
89 found = reader.Seek("foo"); | |
90 EXPECT(found); | |
91 EXPECT_EQ(reader.Type(), JSONReader::kString); | |
92 EXPECT(reader.IsStringLiteral("outer foo")); | |
93 | |
94 found = reader.Seek("quote"); | |
95 EXPECT(found); | |
96 EXPECT_EQ(reader.Type(), JSONReader::kString); | |
97 reader.GetRawValueChars(s, sizeof s); | |
98 EXPECT_STREQ("\\\"", s); | |
99 reader.GetDecodedValueChars(s, sizeof s); | |
100 EXPECT_STREQ("\"", s); | |
101 | |
102 found = reader.Seek("white"); | |
103 EXPECT(found); | |
104 EXPECT_EQ(reader.Type(), JSONReader::kString); | |
105 reader.GetRawValueChars(s, sizeof s); | |
106 EXPECT_STREQ("\\t \\n", s); | |
107 reader.GetDecodedValueChars(s, sizeof s); | |
108 EXPECT_STREQ("\t \n", s); | |
109 | |
110 found = reader.Seek("line"); | |
111 EXPECT(!found); | |
112 found = reader.Seek("params"); | |
113 EXPECT(found); | |
114 EXPECT_EQ(reader.Type(), JSONReader::kObject); | |
115 reader.Set(reader.ValueChars()); | |
116 found = reader.Seek("foo"); | |
117 EXPECT(found); | |
118 EXPECT_EQ(reader.Type(), JSONReader::kArray); | |
119 found = reader.Seek("non-existing"); | |
120 EXPECT(!found); | |
121 found = reader.Seek("line"); | |
122 EXPECT(found); | |
123 EXPECT_EQ(reader.Type(), JSONReader::kInteger); | |
124 } | |
125 | |
126 | |
127 TEST_CASE(JSON_TextBuffer) { | 14 TEST_CASE(JSON_TextBuffer) { |
128 TextBuffer w(5); // Small enough to make buffer grow at least once. | 15 TextBuffer w(5); // Small enough to make buffer grow at least once. |
129 w.Printf("{ \"%s\" : %d", "length", 175); | 16 w.Printf("{ \"%s\" : %d", "length", 175); |
130 EXPECT_STREQ("{ \"length\" : 175", w.buf()); | 17 EXPECT_STREQ("{ \"length\" : 175", w.buf()); |
131 w.Printf(", \"%s\" : \"%s\" }", "command", "stopIt"); | 18 w.Printf(", \"%s\" : \"%s\" }", "command", "stopIt"); |
132 EXPECT_STREQ("{ \"length\" : 175, \"command\" : \"stopIt\" }", w.buf()); | 19 EXPECT_STREQ("{ \"length\" : 175, \"command\" : \"stopIt\" }", w.buf()); |
133 | |
134 JSONReader r(w.buf()); | |
135 bool found = r.Seek("command"); | |
136 EXPECT(found); | |
137 EXPECT_EQ(r.Type(), JSONReader::kString); | |
138 EXPECT(r.IsStringLiteral("stopIt")); | |
139 } | 20 } |
140 | 21 |
141 | 22 |
142 TEST_CASE(JSON_JSONStream_Primitives) { | 23 TEST_CASE(JSON_JSONStream_Primitives) { |
143 { | 24 { |
144 JSONStream js; | 25 JSONStream js; |
145 { | 26 { |
146 JSONObject jsobj(&js); | 27 JSONObject jsobj(&js); |
147 } | 28 } |
148 EXPECT_STREQ("{}", js.ToCString()); | 29 EXPECT_STREQ("{}", js.ToCString()); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 js.SetParams(¶m_keys[0], ¶m_values[0], 2); | 295 js.SetParams(¶m_keys[0], ¶m_values[0], 2); |
415 EXPECT(js.num_params() == 2); | 296 EXPECT(js.num_params() == 2); |
416 EXPECT(!js.HasParam("lizard")); | 297 EXPECT(!js.HasParam("lizard")); |
417 EXPECT(js.HasParam("dog")); | 298 EXPECT(js.HasParam("dog")); |
418 EXPECT(js.HasParam("cat")); | 299 EXPECT(js.HasParam("cat")); |
419 EXPECT(js.ParamIs("cat", "banana")); | 300 EXPECT(js.ParamIs("cat", "banana")); |
420 EXPECT(!js.ParamIs("dog", "banana")); | 301 EXPECT(!js.ParamIs("dog", "banana")); |
421 } | 302 } |
422 | 303 |
423 } // namespace dart | 304 } // namespace dart |
OLD | NEW |