7 #include "gtest/gtest.h"
13 ASSERT_TRUE([codec encode:nil] == nil);
14 ASSERT_TRUE([codec decode:nil] == nil);
19 ASSERT_TRUE([[codec encode:
@""] isEqualTo:[NSData data]]);
20 ASSERT_TRUE([[codec decode:[NSData data]] isEqualTo:
@""]);
24 NSString* value =
@"hello world";
26 NSData* encoded = [codec encode:value];
27 NSString* decoded = [codec decode:encoded];
28 ASSERT_TRUE([value isEqualTo:decoded]);
32 NSString* value =
@"hello \u263A world";
34 NSData* encoded = [codec encode:value];
35 NSString* decoded = [codec decode:encoded];
36 ASSERT_TRUE([value isEqualTo:decoded]);
40 NSString* value =
@"hello \U0001F602 world";
42 NSData* encoded = [codec encode:value];
43 NSString* decoded = [codec decode:encoded];
44 ASSERT_TRUE([value isEqualTo:decoded]);
47 TEST(FlutterJSONCodec, ThrowsOnInvalidEncode) {
48 NSString* value = [[NSString alloc] initWithBytes:
"\xdf\xff"
50 encoding:NSUTF16StringEncoding];
52 EXPECT_EXIT([codec encode:value], testing::KilledBySignal(SIGABRT),
"failed to convert to UTF8");
55 TEST(FlutterJSONCodec, CanDecodeZeroLength) {
57 ASSERT_TRUE([codec decode:[NSData data]] == nil);
60 TEST(FlutterJSONCodec, ThrowsOnInvalidDecode) {
61 NSString* value =
@"{{{";
63 EXPECT_EXIT([codec decode:[value dataUsingEncoding:value.fastestEncoding]],
64 testing::KilledBySignal(SIGABRT),
"No string key for value in object around line 1");
67 TEST(FlutterJSONCodec, CanEncodeAndDecodeNil) {
69 ASSERT_TRUE([codec encode:nil] == nil);
70 ASSERT_TRUE([codec decode:nil] == nil);
73 TEST(FlutterJSONCodec, CanEncodeAndDecodeArray) {
74 NSArray* value = @[ [NSNull
null],
@"hello", @3.14, @47, @{
@"a" :
@"nested"} ];
76 NSData* encoded = [codec encode:value];
77 NSArray* decoded = [codec decode:encoded];
78 ASSERT_TRUE([value isEqualTo:decoded]);
81 TEST(FlutterJSONCodec, CanEncodeAndDecodeDictionary) {
82 NSDictionary* value = @{
@"a" : @3.14,
@"b" : @47,
@"c" : [NSNull
null],
@"d" : @[
@"nested" ]};
84 NSData* encoded = [codec encode:value];
85 NSDictionary* decoded = [codec decode:encoded];
86 ASSERT_TRUE([value isEqualTo:decoded]);