Flutter Engine
The Flutter Engine
fl_json_message_codec_test.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
6#include "gtest/gtest.h"
7
8#include <cmath>
9
10// Encodes a message using FlJsonMessageCodec to a UTF-8 string.
11static gchar* encode_message(FlValue* value) {
12 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
13 g_autoptr(GError) error = nullptr;
14 g_autofree gchar* result = fl_json_message_codec_encode(codec, value, &error);
15 EXPECT_EQ(error, nullptr);
16 return static_cast<gchar*>(g_steal_pointer(&result));
17}
18
19// Encodes a message using FlJsonMessageCodec to a UTF-8 string. Expect the
20// given error.
21static void encode_error_message(FlValue* value, GQuark domain, gint code) {
22 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
23 g_autoptr(GError) error = nullptr;
24 g_autofree gchar* result = fl_json_message_codec_encode(codec, value, &error);
25 EXPECT_TRUE(g_error_matches(error, domain, code));
26 EXPECT_EQ(result, nullptr);
27}
28
29// Decodes a message using FlJsonMessageCodec from UTF-8 string.
30static FlValue* decode_message(const char* text) {
31 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
32 g_autoptr(GError) error = nullptr;
34 EXPECT_EQ(error, nullptr);
35 EXPECT_NE(value, nullptr);
36 return fl_value_ref(value);
37}
38
39// Decodes a message using FlJsonMessageCodec from UTF-8 string. Expect the
40// given error.
41static void decode_error_message(const char* text, GQuark domain, gint code) {
42 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
43 g_autoptr(GError) error = nullptr;
45 EXPECT_TRUE(g_error_matches(error, domain, code));
46 EXPECT_EQ(value, nullptr);
47}
48
49TEST(FlJsonMessageCodecTest, EncodeNullptr) {
50 g_autofree gchar* text = encode_message(nullptr);
51 EXPECT_STREQ(text, "null");
52}
53
54TEST(FlJsonMessageCodecTest, EncodeNull) {
55 g_autoptr(FlValue) value = fl_value_new_null();
56 g_autofree gchar* text = encode_message(value);
57 EXPECT_STREQ(text, "null");
58}
59
60TEST(FlJsonMessageCodecTest, DecodeNull) {
61 g_autoptr(FlValue) value = decode_message("null");
63}
64
65static gchar* encode_bool(gboolean value) {
66 g_autoptr(FlValue) v = fl_value_new_bool(value);
67 return encode_message(v);
68}
69
70TEST(FlJsonMessageCodecTest, EncodeBoolFalse) {
71 g_autofree gchar* text = encode_bool(FALSE);
72 EXPECT_STREQ(text, "false");
73}
74
75TEST(FlJsonMessageCodecTest, EncodeBoolTrue) {
76 g_autofree gchar* text = encode_bool(TRUE);
77 EXPECT_STREQ(text, "true");
78}
79
80TEST(FlJsonMessageCodecTest, DecodeBoolFalse) {
81 g_autoptr(FlValue) value = decode_message("false");
83 EXPECT_FALSE(fl_value_get_bool(value));
84}
85
86TEST(FlJsonMessageCodecTest, DecodeBoolTrue) {
87 g_autoptr(FlValue) value = decode_message("true");
90}
91
92static gchar* encode_int(int64_t value) {
93 g_autoptr(FlValue) v = fl_value_new_int(value);
94 return encode_message(v);
95}
96
97TEST(FlJsonMessageCodecTest, EncodeIntZero) {
98 g_autofree gchar* text = encode_int(0);
99 EXPECT_STREQ(text, "0");
100}
101
102TEST(FlJsonMessageCodecTest, EncodeIntOne) {
103 g_autofree gchar* text = encode_int(1);
104 EXPECT_STREQ(text, "1");
105}
106
107TEST(FlJsonMessageCodecTest, EncodeInt12345) {
108 g_autofree gchar* text = encode_int(12345);
109 EXPECT_STREQ(text, "12345");
110}
111
112TEST(FlJsonMessageCodecTest, EncodeIntMin) {
113 g_autofree gchar* text = encode_int(G_MININT64);
114 EXPECT_STREQ(text, "-9223372036854775808");
115}
116
117TEST(FlJsonMessageCodecTest, EncodeIntMax) {
118 g_autofree gchar* text = encode_int(G_MAXINT64);
119 EXPECT_STREQ(text, "9223372036854775807");
120}
121
122TEST(FlJsonMessageCodecTest, DecodeIntZero) {
123 g_autoptr(FlValue) value = decode_message("0");
125 EXPECT_EQ(fl_value_get_int(value), 0);
126}
127
128TEST(FlJsonMessageCodecTest, DecodeIntOne) {
129 g_autoptr(FlValue) value = decode_message("1");
131 EXPECT_EQ(fl_value_get_int(value), 1);
132}
133
134TEST(FlJsonMessageCodecTest, DecodeInt12345) {
135 g_autoptr(FlValue) value = decode_message("12345");
137 EXPECT_EQ(fl_value_get_int(value), 12345);
138}
139
140TEST(FlJsonMessageCodecTest, DecodeIntMin) {
141 g_autoptr(FlValue) value = decode_message("-9223372036854775808");
143 EXPECT_EQ(fl_value_get_int(value), G_MININT64);
144}
145
146TEST(FlJsonMessageCodecTest, DecodeIntMax) {
147 g_autoptr(FlValue) value = decode_message("9223372036854775807");
149 EXPECT_EQ(fl_value_get_int(value), G_MAXINT64);
150}
151
152TEST(FlJsonMessageCodecTest, DecodeUintMax) {
153 // This is bigger than an signed 64 bit integer, so we expect it to be
154 // represented as a double.
155 g_autoptr(FlValue) value = decode_message("18446744073709551615");
157 EXPECT_EQ(fl_value_get_float(value), 1.8446744073709551615e+19);
158}
159
160TEST(FlJsonMessageCodecTest, DecodeHugeNumber) {
161 // This is bigger than an unsigned 64 bit integer, so we expect it to be
162 // represented as a double.
163 g_autoptr(FlValue) value = decode_message("184467440737095516150");
165 EXPECT_EQ(fl_value_get_float(value), 1.84467440737095516150e+20);
166}
167
168TEST(FlJsonMessageCodecTest, DecodeIntLeadingZero1) {
171}
172
173TEST(FlJsonMessageCodecTest, DecodeIntLeadingZero2) {
176}
177
178TEST(FlJsonMessageCodecTest, DecodeIntDoubleNegative) {
181}
182
183TEST(FlJsonMessageCodecTest, DecodeIntPositiveSign) {
186}
187
188TEST(FlJsonMessageCodecTest, DecodeIntHexChar) {
191}
192
193static gchar* encode_float(double value) {
194 g_autoptr(FlValue) v = fl_value_new_float(value);
195 return encode_message(v);
196}
197
198TEST(FlJsonMessageCodecTest, EncodeFloatZero) {
199 g_autofree gchar* text = encode_float(0);
200 EXPECT_STREQ(text, "0.0");
201}
202
203TEST(FlJsonMessageCodecTest, EncodeFloatOne) {
204 g_autofree gchar* text = encode_float(1);
205 EXPECT_STREQ(text, "1.0");
206}
207
208TEST(FlJsonMessageCodecTest, EncodeFloatMinusOne) {
209 g_autofree gchar* text = encode_float(-1);
210 EXPECT_STREQ(text, "-1.0");
211}
212
213TEST(FlJsonMessageCodecTest, EncodeFloatHalf) {
214 g_autofree gchar* text = encode_float(0.5);
215 EXPECT_STREQ(text, "0.5");
216}
217
218TEST(FlJsonMessageCodecTest, EncodeFloatPi) {
219 g_autofree gchar* text = encode_float(M_PI);
220 EXPECT_STREQ(text, "3.141592653589793");
221}
222
223TEST(FlJsonMessageCodecTest, EncodeFloatMinusZero) {
224 g_autofree gchar* text = encode_float(-0.0);
225 EXPECT_STREQ(text, "-0.0");
226}
227
228// NOTE(robert-ancell): JSON doesn't support encoding of NAN and INFINITY, but
229// rapidjson doesn't seem to either encode them or treat them as an error.
230
231TEST(FlJsonMessageCodecTest, DecodeFloatZero) {
232 g_autoptr(FlValue) value = decode_message("0.0");
234 EXPECT_EQ(fl_value_get_float(value), 0.0);
235}
236
237TEST(FlJsonMessageCodecTest, DecodeFloatOne) {
238 g_autoptr(FlValue) value = decode_message("1.0");
240 EXPECT_EQ(fl_value_get_float(value), 1.0);
241}
242
243TEST(FlJsonMessageCodecTest, DecodeFloatMinusOne) {
244 g_autoptr(FlValue) value = decode_message("-1.0");
246 EXPECT_EQ(fl_value_get_float(value), -1.0);
247}
248
249TEST(FlJsonMessageCodecTest, DecodeFloatHalf) {
250 g_autoptr(FlValue) value = decode_message("0.5");
252 EXPECT_EQ(fl_value_get_float(value), 0.5);
253}
254
255TEST(FlJsonMessageCodecTest, DecodeFloatPi) {
256 g_autoptr(FlValue) value = decode_message("3.1415926535897931");
258 EXPECT_EQ(fl_value_get_float(value), M_PI);
259}
260
261TEST(FlJsonMessageCodecTest, DecodeFloatMinusZero) {
262 g_autoptr(FlValue) value = decode_message("-0.0");
264 EXPECT_EQ(fl_value_get_float(value), -0.0);
265}
266
267TEST(FlJsonMessageCodecTest, DecodeFloatMissingFraction) {
270}
271
272TEST(FlJsonMessageCodecTest, DecodeFloatInvalidFraction) {
275}
276
277static gchar* encode_string(const gchar* value) {
278 g_autoptr(FlValue) v = fl_value_new_string(value);
279 return encode_message(v);
280}
281
282TEST(FlJsonMessageCodecTest, EncodeStringEmpty) {
283 g_autofree gchar* text = encode_string("");
284 EXPECT_STREQ(text, "\"\"");
285}
286
287TEST(FlJsonMessageCodecTest, EncodeStringHello) {
288 g_autofree gchar* text = encode_string("hello");
289 EXPECT_STREQ(text, "\"hello\"");
290}
291
292TEST(FlJsonMessageCodecTest, EncodeStringEmptySized) {
293 g_autoptr(FlValue) value = fl_value_new_string_sized(nullptr, 0);
294 g_autofree gchar* text = encode_message(value);
295 EXPECT_STREQ(text, "\"\"");
296}
297
298TEST(FlJsonMessageCodecTest, EncodeStringHelloSized) {
299 g_autoptr(FlValue) value = fl_value_new_string_sized("Hello World", 5);
300 g_autofree gchar* text = encode_message(value);
301 EXPECT_STREQ(text, "\"Hello\"");
302}
303
304TEST(FlJsonMessageCodecTest, EncodeStringEscapeQuote) {
305 g_autofree gchar* text = encode_string("\"");
306 EXPECT_STREQ(text, "\"\\\"\"");
307}
308
309TEST(FlJsonMessageCodecTest, EncodeStringEscapeBackslash) {
310 g_autofree gchar* text = encode_string("\\");
311 EXPECT_STREQ(text, "\"\\\\\"");
312}
313
314TEST(FlJsonMessageCodecTest, EncodeStringEscapeBackspace) {
315 g_autofree gchar* text = encode_string("\b");
316 EXPECT_STREQ(text, "\"\\b\"");
317}
318
319TEST(FlJsonMessageCodecTest, EncodeStringEscapeFormFeed) {
320 g_autofree gchar* text = encode_string("\f");
321 EXPECT_STREQ(text, "\"\\f\"");
322}
323
324TEST(FlJsonMessageCodecTest, EncodeStringEscapeNewline) {
325 g_autofree gchar* text = encode_string("\n");
326 EXPECT_STREQ(text, "\"\\n\"");
327}
328
329TEST(FlJsonMessageCodecTest, EncodeStringEscapeCarriageReturn) {
330 g_autofree gchar* text = encode_string("\r");
331 EXPECT_STREQ(text, "\"\\r\"");
332}
333
334TEST(FlJsonMessageCodecTest, EncodeStringEscapeTab) {
335 g_autofree gchar* text = encode_string("\t");
336 EXPECT_STREQ(text, "\"\\t\"");
337}
338
339TEST(FlJsonMessageCodecTest, EncodeStringEscapeUnicode) {
340 g_autofree gchar* text = encode_string("\u0001");
341 EXPECT_STREQ(text, "\"\\u0001\"");
342}
343
344TEST(FlJsonMessageCodecTest, EncodeStringEmoji) {
345 g_autofree gchar* text = encode_string("😀");
346 EXPECT_STREQ(text, "\"😀\"");
347}
348
349TEST(FlJsonMessageCodecTest, DecodeStringEmpty) {
350 g_autoptr(FlValue) value = decode_message("\"\"");
352 EXPECT_STREQ(fl_value_get_string(value), "");
353}
354
355TEST(FlJsonMessageCodecTest, DecodeStringHello) {
356 g_autoptr(FlValue) value = decode_message("\"hello\"");
358 EXPECT_STREQ(fl_value_get_string(value), "hello");
359}
360
361TEST(FlJsonMessageCodecTest, DecodeStringEscapeQuote) {
362 g_autoptr(FlValue) value = decode_message("\"\\\"\"");
364 EXPECT_STREQ(fl_value_get_string(value), "\"");
365}
366
367TEST(FlJsonMessageCodecTest, DecodeStringEscapeBackslash) {
368 g_autoptr(FlValue) value = decode_message("\"\\\\\"");
370 EXPECT_STREQ(fl_value_get_string(value), "\\");
371}
372
373TEST(FlJsonMessageCodecTest, DecodeStringEscapeSlash) {
374 g_autoptr(FlValue) value = decode_message("\"\\/\"");
376 EXPECT_STREQ(fl_value_get_string(value), "/");
377}
378
379TEST(FlJsonMessageCodecTest, DecodeStringEscapeBackspace) {
380 g_autoptr(FlValue) value = decode_message("\"\\b\"");
382 EXPECT_STREQ(fl_value_get_string(value), "\b");
383}
384
385TEST(FlJsonMessageCodecTest, DecodeStringEscapeFormFeed) {
386 g_autoptr(FlValue) value = decode_message("\"\\f\"");
388 EXPECT_STREQ(fl_value_get_string(value), "\f");
389}
390
391TEST(FlJsonMessageCodecTest, DecodeStringEscapeNewline) {
392 g_autoptr(FlValue) value = decode_message("\"\\n\"");
394 EXPECT_STREQ(fl_value_get_string(value), "\n");
395}
396
397TEST(FlJsonMessageCodecTest, DecodeStringEscapeCarriageReturn) {
398 g_autoptr(FlValue) value = decode_message("\"\\r\"");
400 EXPECT_STREQ(fl_value_get_string(value), "\r");
401}
402
403TEST(FlJsonMessageCodecTest, DecodeStringEscapeTab) {
404 g_autoptr(FlValue) value = decode_message("\"\\t\"");
406 EXPECT_STREQ(fl_value_get_string(value), "\t");
407}
408
409TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicode) {
410 g_autoptr(FlValue) value = decode_message("\"\\u0001\"");
412 EXPECT_STREQ(fl_value_get_string(value), "\u0001");
413}
414
415TEST(FlJsonMessageCodecTest, DecodeStringEmoji) {
416 g_autoptr(FlValue) value = decode_message("\"😀\"");
418 EXPECT_STREQ(fl_value_get_string(value), "😀");
419}
420
421TEST(FlJsonMessageCodecTest, DecodeInvalidUTF8) {
424}
425
426TEST(FlJsonMessageCodecTest, DecodeStringInvalidUTF8) {
429}
430
431TEST(FlJsonMessageCodecTest, DecodeStringBinary) {
434}
435
436TEST(FlJsonMessageCodecTest, DecodeStringNewline) {
439}
440
441TEST(FlJsonMessageCodecTest, DecodeStringCarriageReturn) {
444}
445
446TEST(FlJsonMessageCodecTest, DecodeStringTab) {
449}
450
451TEST(FlJsonMessageCodecTest, DecodeStringUnterminatedEmpty) {
454}
455
456TEST(FlJsonMessageCodecTest, DecodeStringExtraQuote) {
459}
460
461TEST(FlJsonMessageCodecTest, DecodeStringEscapedClosingQuote) {
464}
465
466TEST(FlJsonMessageCodecTest, DecodeStringUnknownEscape) {
469}
470
471TEST(FlJsonMessageCodecTest, DecodeStringInvalidEscapeUnicode) {
474}
475
476TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicodeNoData) {
479}
480
481TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicodeShortData) {
484}
485
486TEST(FlJsonMessageCodecTest, EncodeUint8ListEmpty) {
487 g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
488 g_autofree gchar* text = encode_message(value);
489 EXPECT_STREQ(text, "[]");
490}
491
492TEST(FlJsonMessageCodecTest, EncodeUint8List) {
493 uint8_t data[] = {0, 1, 2, 3, 4};
494 g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 5);
495 g_autofree gchar* text = encode_message(value);
496 EXPECT_STREQ(text, "[0,1,2,3,4]");
497}
498
499TEST(FlJsonMessageCodecTest, EncodeInt32ListEmpty) {
500 g_autoptr(FlValue) value = fl_value_new_int32_list(nullptr, 0);
501 g_autofree gchar* text = encode_message(value);
502 EXPECT_STREQ(text, "[]");
503}
504
505TEST(FlJsonMessageCodecTest, EncodeInt32List) {
506 int32_t data[] = {0, -1, 2, -3, 4};
507 g_autoptr(FlValue) value = fl_value_new_int32_list(data, 5);
508 g_autofree gchar* text = encode_message(value);
509 EXPECT_STREQ(text, "[0,-1,2,-3,4]");
510}
511
512TEST(FlJsonMessageCodecTest, EncodeInt64ListEmpty) {
513 g_autoptr(FlValue) value = fl_value_new_int64_list(nullptr, 0);
514 g_autofree gchar* text = encode_message(value);
515 EXPECT_STREQ(text, "[]");
516}
517
518TEST(FlJsonMessageCodecTest, EncodeInt64List) {
519 int64_t data[] = {0, -1, 2, -3, 4};
520 g_autoptr(FlValue) value = fl_value_new_int64_list(data, 5);
521 g_autofree gchar* text = encode_message(value);
522 EXPECT_STREQ(text, "[0,-1,2,-3,4]");
523}
524
525TEST(FlJsonMessageCodecTest, EncodeFloatListEmpty) {
526 g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
527 g_autofree gchar* text = encode_message(value);
528 EXPECT_STREQ(text, "[]");
529}
530
531TEST(FlJsonMessageCodecTest, EncodeFloatList) {
532 double data[] = {0, -0.5, 0.25, -0.125, 0.0625};
533 g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
534 g_autofree gchar* text = encode_message(value);
535 EXPECT_STREQ(text, "[0.0,-0.5,0.25,-0.125,0.0625]");
536}
537
538TEST(FlJsonMessageCodecTest, EncodeListEmpty) {
539 g_autoptr(FlValue) value = fl_value_new_list();
540 g_autofree gchar* text = encode_message(value);
541 EXPECT_STREQ(text, "[]");
542}
543
544TEST(FlJsonMessageCodecTest, EncodeListTypes) {
545 g_autoptr(FlValue) value = fl_value_new_list();
553 g_autofree gchar* text = encode_message(value);
554 EXPECT_STREQ(text, "[null,true,42,-1.5,\"hello\",[],{}]");
555}
556
557TEST(FlJsonMessageCodecTest, EncodeListNested) {
558 g_autoptr(FlValue) even_numbers = fl_value_new_list();
559 g_autoptr(FlValue) odd_numbers = fl_value_new_list();
560 for (int i = 0; i < 10; i++) {
561 if (i % 2 == 0) {
563 } else {
565 }
566 }
567 g_autoptr(FlValue) value = fl_value_new_list();
568 fl_value_append(value, even_numbers);
569 fl_value_append(value, odd_numbers);
570 g_autofree gchar* text = encode_message(value);
571 EXPECT_STREQ(text, "[[0,2,4,6,8],[1,3,5,7,9]]");
572}
573
574TEST(FlJsonMessageCodecTest, DecodeListEmpty) {
575 g_autoptr(FlValue) value = decode_message("[]");
577 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
578}
579
580TEST(FlJsonMessageCodecTest, DecodeListNoComma) {
583}
584
585TEST(FlJsonMessageCodecTest, DecodeListUnterminatedEmpty) {
588}
589
590TEST(FlJsonMessageCodecTest, DecodeListStartUnterminate) {
593}
594
595TEST(FlJsonMessageCodecTest, DecodeListUnterminated) {
598}
599
600TEST(FlJsonMessageCodecTest, DecodeListDoubleTerminated) {
603}
604
605TEST(FlJsonMessageCodecTest, EncodeMapEmpty) {
606 g_autoptr(FlValue) value = fl_value_new_map();
607 g_autofree gchar* text = encode_message(value);
608 EXPECT_STREQ(text, "{}");
609}
610
611TEST(FlJsonMessageCodecTest, EncodeMapNullKey) {
612 g_autoptr(FlValue) value = fl_value_new_map();
616}
617
618TEST(FlJsonMessageCodecTest, EncodeMapBoolKey) {
619 g_autoptr(FlValue) value = fl_value_new_map();
621 fl_value_new_string("bool"));
624}
625
626TEST(FlJsonMessageCodecTest, EncodeMapIntKey) {
627 g_autoptr(FlValue) value = fl_value_new_map();
631}
632
633TEST(FlJsonMessageCodecTest, EncodeMapFloatKey) {
634 g_autoptr(FlValue) value = fl_value_new_map();
636 fl_value_new_string("float"));
639}
640
641TEST(FlJsonMessageCodecTest, EncodeMapUint8ListKey) {
642 g_autoptr(FlValue) value = fl_value_new_map();
644 fl_value_new_string("uint8_list"));
647}
648
649TEST(FlJsonMessageCodecTest, EncodeMapInt32ListKey) {
650 g_autoptr(FlValue) value = fl_value_new_map();
652 fl_value_new_string("int32_list"));
655}
656
657TEST(FlJsonMessageCodecTest, EncodeMapInt64ListKey) {
658 g_autoptr(FlValue) value = fl_value_new_map();
660 fl_value_new_string("int64_list"));
663}
664
665TEST(FlJsonMessageCodecTest, EncodeMapFloatListKey) {
666 g_autoptr(FlValue) value = fl_value_new_map();
668 fl_value_new_string("float_list"));
671}
672
673TEST(FlJsonMessageCodecTest, EncodeMapListKey) {
674 g_autoptr(FlValue) value = fl_value_new_map();
678}
679
680TEST(FlJsonMessageCodecTest, EncodeMapMapKey) {
681 g_autoptr(FlValue) value = fl_value_new_map();
685}
686
687TEST(FlJsonMessageCodecTest, EncodeMapValueTypes) {
688 g_autoptr(FlValue) value = fl_value_new_map();
694 fl_value_new_float(-1.5));
696 fl_value_new_string("hello"));
699 g_autofree gchar* text = encode_message(value);
700 EXPECT_STREQ(text,
701 "{\"null\":null,\"bool\":true,\"int\":42,\"float\":-"
702 "1.5,\"string\":\"hello\",\"list\":[],\"map\":{}}");
703}
704
705TEST(FlJsonMessageCodecTest, EncodeMapNested) {
706 g_autoptr(FlValue) str_to_int = fl_value_new_map();
707 const char* numbers[] = {"zero", "one", "two", "three", nullptr};
708 for (int i = 0; numbers[i] != nullptr; i++) {
709 fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
711 }
712 g_autoptr(FlValue) value = fl_value_new_map();
713 fl_value_set_string(value, "str-to-int", str_to_int);
714 g_autofree gchar* text = encode_message(value);
715 EXPECT_STREQ(text,
716 "{\"str-to-int\":{\"zero\":0,\"one\":1,\"two\":2,\"three\":3}}");
717}
718
719TEST(FlJsonMessageCodecTest, DecodeMapEmpty) {
720 g_autoptr(FlValue) value = decode_message("{}");
722 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
723}
724
725TEST(FlJsonMessageCodecTest, DecodeMapUnterminatedEmpty) {
728}
729
730TEST(FlJsonMessageCodecTest, DecodeMapStartUnterminate) {
733}
734
735TEST(FlJsonMessageCodecTest, DecodeMapNoComma) {
736 decode_error_message("{\"zero\":0 \"one\":1}", FL_JSON_MESSAGE_CODEC_ERROR,
738}
739
740TEST(FlJsonMessageCodecTest, DecodeMapNoColon) {
741 decode_error_message("{\"zero\" 0,\"one\":1}", FL_JSON_MESSAGE_CODEC_ERROR,
743}
744
745TEST(FlJsonMessageCodecTest, DecodeMapUnterminated) {
746 decode_error_message("{\"zero\":0,\"one\":1", FL_JSON_MESSAGE_CODEC_ERROR,
748}
749
750TEST(FlJsonMessageCodecTest, DecodeMapDoubleTerminated) {
751 decode_error_message("{\"zero\":0,\"one\":1}}", FL_JSON_MESSAGE_CODEC_ERROR,
753}
754
755TEST(FlJsonMessageCodecTest, DecodeUnknownWord) {
758}
759
760TEST(FlJsonMessageCodecTest, EncodeDecode) {
761 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
762
763 g_autoptr(FlValue) input = fl_value_new_list();
771
772 g_autoptr(GError) error = nullptr;
773 g_autofree gchar* message =
774 fl_json_message_codec_encode(codec, input, &error);
775 ASSERT_NE(message, nullptr);
776 EXPECT_EQ(error, nullptr);
777
778 g_autoptr(FlValue) output =
780 EXPECT_EQ(error, nullptr);
781 EXPECT_NE(output, nullptr);
782
784}
#define M_PI
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
G_MODULE_EXPORT gchar * fl_json_message_codec_encode(FlJsonMessageCodec *codec, FlValue *value, GError **error)
G_MODULE_EXPORT FlValue * fl_json_message_codec_decode(FlJsonMessageCodec *codec, const gchar *text, GError **error)
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
#define FL_JSON_MESSAGE_CODEC_ERROR
TEST(FlJsonMessageCodecTest, EncodeNullptr)
static gchar * encode_string(const gchar *value)
static gchar * encode_float(double value)
static gchar * encode_int(int64_t value)
static FlValue * decode_message(const char *text)
static void encode_error_message(FlValue *value, GQuark domain, gint code)
static void decode_error_message(const char *text, GQuark domain, gint code)
static gchar * encode_message(FlValue *value)
static gchar * encode_bool(gboolean value)
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
GAsyncResult * result
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:639
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:309
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:592
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:618
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:600
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
G_MODULE_EXPORT FlValue * fl_value_new_uint8_list(const uint8_t *data, size_t data_length)
Definition: fl_value.cc:292
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
G_MODULE_EXPORT FlValue * fl_value_new_float_list(const double *data, size_t data_length)
Definition: fl_value.cc:339
G_MODULE_EXPORT FlValue * fl_value_new_string_sized(const gchar *value, size_t value_length)
Definition: fl_value.cc:283
G_MODULE_EXPORT FlValue * fl_value_new_int64_list(const int64_t *data, size_t data_length)
Definition: fl_value.cc:319
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:66
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:74
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:75
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:68
std::u16string text
Win32Message message
return FALSE
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678