Flutter iOS Embedder
flutter_codecs_unittest.mm
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 
6 
7 #include "gtest/gtest.h"
8 
10 
11 TEST(FlutterStringCodec, CanEncodeAndDecodeNil) {
12  FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
13  ASSERT_TRUE([codec encode:nil] == nil);
14  ASSERT_TRUE([codec decode:nil] == nil);
15 }
16 
17 TEST(FlutterStringCodec, CanEncodeAndDecodeEmptyString) {
18  FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
19  ASSERT_TRUE([[codec encode:@""] isEqualTo:[NSData data]]);
20  ASSERT_TRUE([[codec decode:[NSData data]] isEqualTo:@""]);
21 }
22 
23 TEST(FlutterStringCodec, CanEncodeAndDecodeAsciiString) {
24  NSString* value = @"hello world";
25  FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
26  NSData* encoded = [codec encode:value];
27  NSString* decoded = [codec decode:encoded];
28  ASSERT_TRUE([value isEqualTo:decoded]);
29 }
30 
31 TEST(FlutterStringCodec, CanEncodeAndDecodeNonAsciiString) {
32  NSString* value = @"hello \u263A world";
33  FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
34  NSData* encoded = [codec encode:value];
35  NSString* decoded = [codec decode:encoded];
36  ASSERT_TRUE([value isEqualTo:decoded]);
37 }
38 
39 TEST(FlutterStringCodec, CanEncodeAndDecodeNonBMPString) {
40  NSString* value = @"hello \U0001F602 world";
41  FlutterStringCodec* codec = [FlutterStringCodec sharedInstance];
42  NSData* encoded = [codec encode:value];
43  NSString* decoded = [codec decode:encoded];
44  ASSERT_TRUE([value isEqualTo:decoded]);
45 }
46 
47 TEST(FlutterJSONCodec, ThrowsOnInvalidEncode) {
48  NSString* value = [[NSString alloc] initWithBytes:"\xdf\xff"
49  length:2
50  encoding:NSUTF16StringEncoding];
51  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
52  EXPECT_EXIT([codec encode:value], testing::KilledBySignal(SIGABRT), "failed to convert to UTF8");
53 }
54 
55 TEST(FlutterJSONCodec, CanDecodeZeroLength) {
56  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
57  ASSERT_TRUE([codec decode:[NSData data]] == nil);
58 }
59 
60 TEST(FlutterJSONCodec, ThrowsOnInvalidDecode) {
61  NSString* value = @"{{{";
62  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
63  EXPECT_EXIT([codec decode:[value dataUsingEncoding:value.fastestEncoding]],
64  testing::KilledBySignal(SIGABRT), "No string key for value in object around line 1");
65 }
66 
67 TEST(FlutterJSONCodec, CanEncodeAndDecodeNil) {
68  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
69  ASSERT_TRUE([codec encode:nil] == nil);
70  ASSERT_TRUE([codec decode:nil] == nil);
71 }
72 
73 TEST(FlutterJSONCodec, CanEncodeAndDecodeArray) {
74  NSArray* value = @[ [NSNull null], @"hello", @3.14, @47, @{@"a" : @"nested"} ];
75  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
76  NSData* encoded = [codec encode:value];
77  NSArray* decoded = [codec decode:encoded];
78  ASSERT_TRUE([value isEqualTo:decoded]);
79 }
80 
81 TEST(FlutterJSONCodec, CanEncodeAndDecodeDictionary) {
82  NSDictionary* value = @{@"a" : @3.14, @"b" : @47, @"c" : [NSNull null], @"d" : @[ @"nested" ]};
83  FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance];
84  NSData* encoded = [codec encode:value];
85  NSDictionary* decoded = [codec decode:encoded];
86  ASSERT_TRUE([value isEqualTo:decoded]);
87 }
FlutterStringCodec
Definition: FlutterCodecs.h:63
FlutterCodecs.h
TEST
FLUTTER_ASSERT_ARC TEST(FlutterStringCodec, CanEncodeAndDecodeNil)
Definition: flutter_codecs_unittest.mm:11
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81