-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
RCTJSONTests.m
131 lines (111 loc) · 4.17 KB
/
RCTJSONTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <XCTest/XCTest.h>
#import "RCTUtils.h"
@interface RCTJSONTests : XCTestCase
@end
@implementation RCTJSONTests
- (void)testEncodingObject
{
NSDictionary<NSString *, id> *obj = @{@"foo": @"bar"};
NSString *json = @"{\"foo\":\"bar\"}";
XCTAssertEqualObjects(json, RCTJSONStringify(obj, NULL));
}
- (void)testEncodingArray
{
NSArray<id> *array = @[@"foo", @"bar"];
NSString *json = @"[\"foo\",\"bar\"]";
XCTAssertEqualObjects(json, RCTJSONStringify(array, NULL));
}
- (void)testEncodingString
{
NSString *text = @"Hello\nWorld";
NSString *json = @"\"Hello\\nWorld\"";
XCTAssertEqualObjects(json, RCTJSONStringify(text, NULL));
}
- (void)testEncodingNSError
{
NSError *underlyingError = [NSError errorWithDomain:@"underlyingDomain" code:421 userInfo:nil];
NSError *err = [NSError errorWithDomain:@"domain" code:68 userInfo:@{@"NSUnderlyingError": underlyingError}];
// An assertion on the full object would be too brittle since it contains an iOS stack trace
// so we are relying on the behavior of RCTJSONParse, which is tested below.
NSDictionary<NSString *, id> *jsonObject = RCTJSErrorFromNSError(err);
NSString *jsonString = RCTJSONStringify(jsonObject, NULL);
NSDictionary<NSString *, id> *json = RCTJSONParse(jsonString, NULL);
XCTAssertEqualObjects(json[@"code"], @"EDOMAIN68");
XCTAssertEqualObjects(json[@"message"], @"The operation couldn’t be completed. (domain error 68.)");
XCTAssertEqualObjects(json[@"domain"], @"domain");
XCTAssertEqualObjects(json[@"userInfo"][@"NSUnderlyingError"][@"code"], @"421");
XCTAssertEqualObjects(json[@"userInfo"][@"NSUnderlyingError"][@"message"], @"underlying error");
XCTAssertEqualObjects(json[@"userInfo"][@"NSUnderlyingError"][@"domain"], @"underlyingDomain");
}
- (void)testDecodingObject
{
NSDictionary<NSString *, id> *obj = @{@"foo": @"bar"};
NSString *json = @"{\"foo\":\"bar\"}";
XCTAssertEqualObjects(obj, RCTJSONParse(json, NULL));
}
- (void)testDecodingArray
{
NSArray<id> *array = @[@"foo", @"bar"];
NSString *json = @"[\"foo\",\"bar\"]";
XCTAssertEqualObjects(array, RCTJSONParse(json, NULL));
}
- (void)testDecodingString
{
NSString *text = @"Hello\nWorld";
NSString *json = @"\"Hello\\nWorld\"";
XCTAssertEqualObjects(text, RCTJSONParse(json, NULL));
}
- (void)testDecodingMutableArray
{
NSString *json = @"[1,2,3]";
NSMutableArray<id> *array = RCTJSONParseMutable(json, NULL);
XCTAssertNoThrow([array addObject:@4]);
XCTAssertEqualObjects(array, (@[@1, @2, @3, @4]));
}
- (void)testLeadingWhitespace
{
NSDictionary<NSString *, id> *obj = @{@"foo": @"bar"};
NSString *json = @" \r\n\t{\"foo\":\"bar\"}";
XCTAssertEqualObjects(obj, RCTJSONParse(json, NULL));
}
- (void)testNotJSONSerializable
{
NSDictionary<NSString *, id> *obj = @{@"foo": [NSDate date]};
NSString *json = @"{\"foo\":null}";
XCTAssertEqualObjects(json, RCTJSONStringify(obj, NULL));
}
- (void)testNaN
{
NSDictionary<NSString *, id> *obj = @{@"foo": @(NAN)};
NSString *json = @"{\"foo\":0}";
XCTAssertEqualObjects(json, RCTJSONStringify(obj, NULL));
}
- (void)testNotUTF8Convertible
{
//see https://gist.github.com/0xced/56035d2f57254cf518b5
NSString *string = [[NSString alloc] initWithBytes:"\xd8\x00" length:2 encoding:NSUTF16StringEncoding];
NSDictionary<NSString *, id> *obj = @{@"foo": string};
NSString *json = @"{\"foo\":null}";
XCTAssertEqualObjects(json, RCTJSONStringify(obj, NULL));
}
- (void)testErrorPointer
{
NSDictionary<NSString *, id> *obj = @{@"foo": [NSDate date]};
NSError *error;
XCTAssertNil(RCTJSONStringify(obj, &error));
XCTAssertNotNil(error);
}
@end