Flutter Engine
The Flutter Engine
json_test.cc
Go to the documentation of this 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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/assert.h"
7#include "vm/dart_api_impl.h"
8#include "vm/json_stream.h"
9#include "vm/unit_test.h"
10
11namespace dart {
12
13#ifndef PRODUCT
14
15TEST_CASE(JSON_TextBuffer) {
16 TextBuffer w(5); // Small enough to make buffer grow at least once.
17 w.Printf("{ \"%s\" : %d", "length", 175);
18 EXPECT_STREQ("{ \"length\" : 175", w.buffer());
19 w.Printf(", \"%s\" : \"%s\" }", "command", "stopIt");
20 EXPECT_STREQ("{ \"length\" : 175, \"command\" : \"stopIt\" }", w.buffer());
21}
22
23TEST_CASE(JSON_JSONStream_Primitives) {
24 {
26 {
27 JSONObject jsobj(&js);
28 }
29 EXPECT_STREQ("{}", js.ToCString());
30 }
31 {
33 {
34 JSONArray jsarr(&js);
35 }
36 EXPECT_STREQ("[]", js.ToCString());
37 }
38 {
40 {
41 JSONArray jsarr(&js);
42 jsarr.AddValue(true);
43 }
44 EXPECT_STREQ("[true]", js.ToCString());
45 }
46 {
48 {
49 JSONArray jsarr(&js);
50 jsarr.AddValue(false);
51 }
52 EXPECT_STREQ("[false]", js.ToCString());
53 }
54 {
56 {
57 JSONArray jsarr(&js);
58 jsarr.AddValue(static_cast<intptr_t>(4));
59 }
60 EXPECT_STREQ("[4]", js.ToCString());
61 }
62 {
64 {
65 JSONArray jsarr(&js);
66 jsarr.AddValue(1.0);
67 }
68 EXPECT_STREQ("[1.0]", js.ToCString());
69 }
70 {
72 {
73 JSONArray jsarr(&js);
74 jsarr.AddValue("hello");
75 }
76 EXPECT_STREQ("[\"hello\"]", js.ToCString());
77 }
78 {
80 {
81 JSONArray jsarr(&js);
82 jsarr.AddValueF("h%s", "elo");
83 }
84 EXPECT_STREQ("[\"helo\"]", js.ToCString());
85 }
86}
87
88TEST_CASE(JSON_JSONStream_Array) {
90 {
91 JSONArray jsarr(&js);
92 jsarr.AddValue(true);
93 jsarr.AddValue(false);
94 }
95 EXPECT_STREQ("[true,false]", js.ToCString());
96}
97
98TEST_CASE(JSON_JSONStream_Base64String) {
100 {
101 JSONBase64String jsonBase64String(&js);
102 jsonBase64String.AppendBytes(reinterpret_cast<const uint8_t*>("Hello"), 5);
103 jsonBase64String.AppendBytes(reinterpret_cast<const uint8_t*>(", "), 2);
104 jsonBase64String.AppendBytes(reinterpret_cast<const uint8_t*>("world!"), 6);
105 }
106 EXPECT_STREQ("\"SGVsbG8sIHdvcmxkIQ==\"", js.ToCString());
107}
108
109TEST_CASE(JSON_JSONStream_Object) {
111 {
112 JSONObject jsobj(&js);
113 jsobj.AddProperty("key1", "a");
114 jsobj.AddProperty("key2", "b");
115 }
116 EXPECT_STREQ("{\"key1\":\"a\",\"key2\":\"b\"}", js.ToCString());
117}
118
119TEST_CASE(JSON_JSONStream_NestedObject) {
121 {
122 JSONObject jsobj(&js);
123 JSONObject jsobj1(&jsobj, "key");
124 jsobj1.AddProperty("key1", "d");
125 }
126 EXPECT_STREQ("{\"key\":{\"key1\":\"d\"}}", js.ToCString());
127}
128
129TEST_CASE(JSON_JSONStream_ObjectArray) {
131 {
132 JSONArray jsarr(&js);
133 {
134 JSONObject jsobj(&jsarr);
135 jsobj.AddProperty("key", "e");
136 }
137 {
138 JSONObject jsobj(&jsarr);
139 jsobj.AddProperty("yek", "f");
140 }
141 }
142 EXPECT_STREQ("[{\"key\":\"e\"},{\"yek\":\"f\"}]", js.ToCString());
143}
144
145TEST_CASE(JSON_JSONStream_ArrayArray) {
147 {
148 JSONArray jsarr(&js);
149 {
150 JSONArray jsarr1(&jsarr);
151 jsarr1.AddValue(static_cast<intptr_t>(4));
152 }
153 {
154 JSONArray jsarr1(&jsarr);
155 jsarr1.AddValue(false);
156 }
157 }
158 EXPECT_STREQ("[[4],[false]]", js.ToCString());
159}
160
161TEST_CASE(JSON_JSONStream_Printf) {
163 {
164 JSONArray jsarr(&js);
165 jsarr.AddValueF("%d %s", 2, "hello");
166 }
167 EXPECT_STREQ("[\"2 hello\"]", js.ToCString());
168}
169
170TEST_CASE(JSON_JSONStream_ObjectPrintf) {
172 {
173 JSONObject jsobj(&js);
174 jsobj.AddPropertyF("key", "%d %s", 2, "hello");
175 }
176 EXPECT_STREQ("{\"key\":\"2 hello\"}", js.ToCString());
177}
178
179ISOLATE_UNIT_TEST_CASE(JSON_JSONStream_DartObject) {
181 {
182 JSONArray jsarr(&js);
184 JSONObject jsobj(&jsarr);
185 jsobj.AddProperty("object_key", Object::Handle(Object::null()));
186 }
187 // WARNING: This MUST be big enough for the serialized JSON string.
188 const int kBufferSize = 2048;
189 char buffer[kBufferSize];
190 const char* json_str = js.ToCString();
191 ASSERT(strlen(json_str) < kBufferSize);
192 ElideJSONSubstring("classes", json_str, buffer);
193 ElideJSONSubstring("libraries", buffer, buffer);
194 ElideJSONSubstring("objects", buffer, buffer);
196 ElideJSONSubstring("column", buffer, buffer);
198
199 EXPECT_STREQ(
200 "[{\"type\":\"@Instance\",\"_vmType\":\"null\",\"class\":{\"type\":\"@"
201 "Class\",\"fixedId\":true,\"id\":\"\",\"name\":\"Null\",\"location\":{"
202 "\"type\":\"SourceLocation\",\"script\":{\"type\":\"@Script\","
203 "\"fixedId\":true,\"id\":\"\",\"uri\":\"dart:core\\/null.dart\","
204 "\"_kind\":\"kernel\"}},\"library\":{"
205 "\"type\":\"@Library\",\"fixedId\":true,\"id\":\"\",\"name\":\"dart."
206 "core\",\"uri\":\"dart:core\"}},\"kind\":\"Null\",\"fixedId\":true,"
207 "\"id\":\"\",\"valueAsString\":\"null\"},{\"object_key\":{\"type\":\"@"
208 "Instance\",\"_vmType\":\"null\",\"class\":{\"type\":\"@Class\","
209 "\"fixedId\":true,\"id\":\"\",\"name\":\"Null\",\"location\":{\"type\":"
210 "\"SourceLocation\",\"script\":{\"type\":\"@Script\",\"fixedId\":true,"
211 "\"id\":\"\",\"uri\":\"dart:core\\/null.dart\",\"_kind\":\"kernel\"}},"
212 "\"library\":{\"type\":\"@"
213 "Library\",\"fixedId\":true,\"id\":\"\",\"name\":\"dart.core\",\"uri\":"
214 "\"dart:core\"}},\"kind\":\"Null\",\"fixedId\":true,\"id\":\"\","
215 "\"valueAsString\":\"null\"}}]",
216 buffer);
217}
218
219TEST_CASE(JSON_JSONStream_EscapedString) {
221 {
222 JSONArray jsarr(&js);
223 jsarr.AddValue("Hel\"\"lo\r\n\t");
224 }
225 EXPECT_STREQ("[\"Hel\\\"\\\"lo\\r\\n\\t\"]", js.ToCString());
226}
227
228TEST_CASE(JSON_JSONStream_DartString) {
229 const char* kScriptChars =
230 "var ascii = 'Hello, World!';\n"
231 "var unicode = '\\u00CE\\u00F1\\u0163\\u00E9r\\u00F1\\u00E5\\u0163"
232 "\\u00EE\\u00F6\\u00F1\\u00E5\\u013C\\u00EE\\u017E\\u00E5\\u0163"
233 "\\u00EE\\u1EDD\\u00F1';\n"
234 "var surrogates = '\\u{1D11E}\\u{1D11E}\\u{1D11E}"
235 "\\u{1D11E}\\u{1D11E}';\n"
236 "var wrongEncoding = '\\u{1D11E}' + surrogates[0] + '\\u{1D11E}';"
237 "var nullInMiddle = 'This has\\u0000 four words.';";
238
239 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, nullptr);
240 EXPECT_VALID(lib);
241
243 TransitionNativeToVM transition1(thread);
244 String& obj = String::Handle();
245
246 auto do_test = [&](const char* field_name, const char* expected) {
247 {
248 TransitionVMToNative to_native(thread);
249 result = Dart_GetField(lib, NewString(field_name));
251 }
252
254
255 {
257 {
258 JSONObject jsobj(&js);
259 EXPECT(!jsobj.AddPropertyStr(field_name, obj));
260 }
261 EXPECT_STREQ(expected, js.ToCString());
262 }
263 };
264
265 {
266 TransitionVMToNative to_native(thread);
267 result = Dart_GetField(lib, NewString("ascii"));
269 }
270
272
273 {
275 {
276 JSONObject jsobj(&js);
277 EXPECT(jsobj.AddPropertyStr("subrange", obj, 1, 4));
278 }
279 EXPECT_STREQ("{\"subrange\":\"ello\"}", js.ToCString());
280 }
281
282 do_test("ascii", "{\"ascii\":\"Hello, World!\"}");
283 do_test("unicode", "{\"unicode\":\"Îñţérñåţîöñåļîžåţîờñ\"}");
284 do_test("surrogates", "{\"surrogates\":\"𝄞𝄞𝄞𝄞𝄞\"}");
285 do_test("wrongEncoding", "{\"wrongEncoding\":\"𝄞\\uD834𝄞\"}");
286 do_test("nullInMiddle", "{\"nullInMiddle\":\"This has\\u0000 four words.\"}");
287}
288
289TEST_CASE(JSON_JSONStream_Params) {
290 const char* param_keys[] = {"dog", "cat"};
291 const char* param_values[] = {"apple", "banana"};
292
294 EXPECT(js.num_params() == 0);
295 js.SetParams(&param_keys[0], &param_values[0], 2);
296 EXPECT(js.num_params() == 2);
297 EXPECT(!js.HasParam("lizard"));
298 EXPECT(js.HasParam("dog"));
299 EXPECT(js.HasParam("cat"));
300 EXPECT(js.ParamIs("cat", "banana"));
301 EXPECT(!js.ParamIs("dog", "banana"));
302}
303
304#endif // !PRODUCT
305
306} // namespace dart
static const size_t kBufferSize
Definition: SkString.cpp:27
#define EXPECT(type, expectedAlignment, expectedSize)
static ObjectPtr UnwrapHandle(Dart_Handle object)
void AddValue(bool b) const
Definition: json_stream.h:494
void AddValueF(const char *format,...) const PRINTF_ATTRIBUTE(2
Definition: json_stream.cc:596
void AppendBytes(const uint8_t *bytes, intptr_t length)
Definition: json_stream.cc:603
void AddProperty(const char *name, bool b) const
Definition: json_stream.h:395
bool AddPropertyStr(const char *name, const String &s, intptr_t offset=0, intptr_t count=-1) const
Definition: json_stream.h:421
void AddPropertyF(const char *name, const char *format,...) const PRINTF_ATTRIBUTE(3
Definition: json_stream.cc:589
static ObjectPtr null()
Definition: object.h:433
static Object & Handle()
Definition: object.h:407
static Dart_Handle LoadTestScript(const char *script, Dart_NativeEntryResolver resolver, const char *lib_uri=RESOLVED_USER_TEST_URI, bool finalize=true, bool allow_compile_errors=false)
Definition: unit_test.cc:436
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
#define ASSERT(E)
GAsyncResult * result
Definition: dart_vm.cc:33
void ElideJSONSubstring(const char *prefix, const char *in, char *out, const char *postfix)
Definition: unit_test.cc:790
DART_EXPORT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
TEST_CASE(DirectoryCurrent)
Dart_Handle NewString(const char *str)
void StripTokenPositions(char *buffer)
Definition: unit_test.cc:813
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
SkScalar w
#define EXPECT_VALID(handle)
Definition: unit_test.h:643