Skip to content

Commit fdb04b3

Browse files
committed
Merge branch 'staging' into INTENG-7291-fix-keychain
2 parents 87f8897 + 0bc502f commit fdb04b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1281
-67
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
//
2+
// BNCJSONUtilityTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 9/17/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCJSONUtility.h"
11+
#import "BNCJsonLoader.h"
12+
13+
@interface BNCJSONUtilityTests : XCTestCase
14+
@property (nonatomic, strong, readwrite) NSDictionary *json;
15+
@end
16+
17+
@implementation BNCJSONUtilityTests
18+
19+
- (void)setUp {
20+
self.json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"example"];
21+
XCTAssertNotNil(self.json);
22+
}
23+
24+
- (void)tearDown {
25+
26+
}
27+
28+
- (void)testIsNumber {
29+
NSNumber *number = [NSNumber numberWithInt:314];
30+
XCTAssertTrue([BNCJSONUtility isNumber:number]);
31+
}
32+
33+
- (void)testIsNumber_Boxed {
34+
XCTAssertTrue([BNCJSONUtility isNumber:@(1.0)]);
35+
}
36+
37+
- (void)testIsNumber_Nil {
38+
XCTAssertFalse([BNCJSONUtility isNumber:nil]);
39+
}
40+
41+
- (void)testIsNumber_String {
42+
XCTAssertFalse([BNCJSONUtility isNumber:@"1.0"]);
43+
}
44+
45+
- (void)testIsString {
46+
XCTAssertTrue([BNCJSONUtility isString:@"1.0"]);
47+
}
48+
49+
- (void)testIsString_MutableString {
50+
NSMutableString *string = [NSMutableString new];
51+
XCTAssertTrue([BNCJSONUtility isString:string]);
52+
}
53+
54+
- (void)testIsString_EmptyString {
55+
XCTAssertTrue([BNCJSONUtility isString:@""]);
56+
}
57+
58+
- (void)testIsString_Nil {
59+
XCTAssertFalse([BNCJSONUtility isString:nil]);
60+
}
61+
62+
- (void)testIsString_Number {
63+
XCTAssertFalse([BNCJSONUtility isString:@(1.0)]);
64+
}
65+
66+
- (void)testIsArray {
67+
NSArray *tmp = @[@1, @2];
68+
XCTAssertTrue([BNCJSONUtility isArray:tmp]);
69+
}
70+
71+
- (void)testIsArray_MutableArray {
72+
NSMutableArray *tmp = [NSMutableArray new];
73+
XCTAssertTrue([BNCJSONUtility isArray:tmp]);
74+
}
75+
76+
- (void)testIsArray_EmptyArray {
77+
XCTAssertTrue([BNCJSONUtility isArray:@[]]);
78+
}
79+
80+
- (void)testIsArray_Nil {
81+
XCTAssertFalse([BNCJSONUtility isArray:nil]);
82+
}
83+
84+
- (void)testIsArray_Dictionary {
85+
XCTAssertFalse([BNCJSONUtility isArray:[NSDictionary new]]);
86+
}
87+
88+
// successful call on untyped dictionary
89+
- (void)testUntypedDictionary_CorrectType {
90+
NSString *string = self.json[@"user_string"];
91+
XCTAssertNotNil(string);
92+
XCTAssertTrue(([string isKindOfClass:[NSString class]] || [string isKindOfClass:[NSMutableString class]]));
93+
}
94+
95+
// demonstrates that an untyped dictionary can lead to type mismatches cause it always returns id
96+
- (void)testUntypedDictionary_IncorrectType {
97+
NSString *string = self.json[@"user_number"];
98+
XCTAssertNotNil(string);
99+
XCTAssertTrue(([string isKindOfClass:[NSNumber class]]));
100+
}
101+
102+
- (void)testStringForKey_InvalidKey {
103+
id key = @(1);
104+
NSString *string = [BNCJSONUtility stringForKey:key json:self.json];
105+
XCTAssertNil(string);
106+
}
107+
108+
- (void)testStringForKey {
109+
NSString *string = [BNCJSONUtility stringForKey:@"user_string" json:self.json];
110+
XCTAssertNotNil(string);
111+
XCTAssertTrue(([string isKindOfClass:[NSString class]] || [string isKindOfClass:[NSMutableString class]]));
112+
}
113+
114+
- (void)testStringForKey_IncorrectType {
115+
NSString *string = [BNCJSONUtility stringForKey:@"user_number" json:self.json];
116+
XCTAssertNil(string);
117+
}
118+
119+
- (void)testNumberForKey {
120+
NSNumber *number = [BNCJSONUtility numberForKey:@"user_number" json:self.json];
121+
XCTAssertNotNil(number);
122+
XCTAssertTrue([number isKindOfClass:[NSNumber class]]);
123+
}
124+
125+
- (void)testNumberForKey_IncorrectType {
126+
NSNumber *number = [BNCJSONUtility numberForKey:@"user_string" json:self.json];
127+
XCTAssertNil(number);
128+
}
129+
130+
- (void)testDictionaryForKey {
131+
NSDictionary *dict = [BNCJSONUtility dictionaryForKey:@"user_dict" json:self.json];
132+
XCTAssertNotNil(dict);
133+
XCTAssertTrue(([dict isKindOfClass:NSDictionary.class] || [dict isKindOfClass:NSMutableDictionary.class]));
134+
}
135+
136+
- (void)testDictionaryForKey_IncorrectType {
137+
NSDictionary *dict = [BNCJSONUtility dictionaryForKey:@"user_array" json:self.json];
138+
XCTAssertNil(dict);
139+
}
140+
141+
- (void)testArrayForKey {
142+
NSArray *array = [BNCJSONUtility arrayForKey:@"user_array" json:self.json];
143+
XCTAssertNotNil(array);
144+
XCTAssertTrue(([array isKindOfClass:[NSArray class]] || [array isKindOfClass:[NSMutableArray class]]));
145+
}
146+
147+
- (void)testArrayForKey_IncorrectType {
148+
NSArray *array = [BNCJSONUtility arrayForKey:@"user_dict" json:self.json];
149+
XCTAssertNil(array);
150+
}
151+
152+
- (void)testStringArrayForKey {
153+
NSArray<NSString *> *array = [BNCJSONUtility stringArrayForKey:@"user_array" json:self.json];
154+
XCTAssertNotNil(array);
155+
XCTAssertTrue(array.count > 0);
156+
}
157+
158+
- (void)testStringArrayForKey_MixedTypes {
159+
NSArray<NSString *> *array = [BNCJSONUtility stringArrayForKey:@"user_array_mixed" json:self.json];
160+
XCTAssertNotNil(array);
161+
XCTAssertTrue(array.count > 0);
162+
}
163+
164+
- (void)testStringArrayForKey_Numbers {
165+
NSArray<NSString *> *array = [BNCJSONUtility stringArrayForKey:@"user_array_numbers" json:self.json];
166+
XCTAssertNotNil(array);
167+
XCTAssertTrue(array.count == 0);
168+
}
169+
170+
- (void)testStringDictionaryForKey {
171+
NSDictionary<NSString *, NSString *> *dict = [BNCJSONUtility stringDictionaryForKey:@"user_dict" json:self.json];
172+
XCTAssertNotNil(dict);
173+
XCTAssertTrue(dict.count > 0);
174+
}
175+
176+
- (void)testStringDictionaryForKey_MixedTypes {
177+
NSDictionary<NSString *, NSString *> *dict = [BNCJSONUtility stringDictionaryForKey:@"user_dict_mixed" json:self.json];
178+
XCTAssertNotNil(dict);
179+
XCTAssertTrue(dict.count > 0);
180+
}
181+
182+
- (void)testStringDictionaryForKey_Numbers {
183+
NSDictionary<NSString *, NSString *> *dict = [BNCJSONUtility stringDictionaryForKey:@"user_dict_numbers" json:self.json];
184+
XCTAssertNotNil(dict);
185+
XCTAssertTrue(dict.count == 0);
186+
}
187+
188+
@end

Branch-SDK-Tests/BNCJsonLoader.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// BNCJsonLoader.h
3+
// Branch-TestBed
4+
//
5+
// Created by Ernest Cho on 9/16/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface BNCJsonLoader : NSObject
14+
15+
// test utility that loads json files from the Test Bundle. only works on hosted tests
16+
+ (NSDictionary *)dictionaryFromJSONFileNamed:(NSString *)fileName;
17+
18+
@end
19+
20+
NS_ASSUME_NONNULL_END

Branch-SDK-Tests/BNCJsonLoader.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// BNCJsonLoader.m
3+
// Branch-TestBed
4+
//
5+
// Created by Ernest Cho on 9/16/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import "BNCJsonLoader.h"
10+
11+
@implementation BNCJsonLoader
12+
13+
+ (NSDictionary *)dictionaryFromJSONFileNamed:(NSString *)fileName {
14+
15+
// Since this class is part of the Test target, [self class] returns the Test Bundle
16+
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:fileName ofType:@"json"];
17+
18+
NSString *jsonString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
19+
20+
id dict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
21+
if ([dict isKindOfClass:NSDictionary.class]) {
22+
return dict;
23+
}
24+
return nil;
25+
}
26+
27+
@end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//
2+
// BranchCrossPlatformIDTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 9/16/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BranchCrossPlatformID.h"
11+
#import "BNCJsonLoader.h"
12+
13+
@interface BranchCrossPlatformIDTests : XCTestCase
14+
15+
@end
16+
17+
@implementation BranchCrossPlatformIDTests
18+
19+
- (void)setUp {
20+
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
}
22+
23+
- (void)tearDown {
24+
// Put teardown code here. This method is called after the invocation of each test method in the class.
25+
}
26+
27+
- (void)testBuildFromJSON {
28+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid"];
29+
XCTAssertNotNil(json);
30+
31+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
32+
XCTAssertNotNil(cpid);
33+
XCTAssertNotNil(cpid.developerID);
34+
XCTAssertNotNil(cpid.crossPlatformID);
35+
XCTAssertNotNil(cpid.pastCrossPlatformIDs);
36+
XCTAssertNotNil(cpid.probabiliticCrossPlatformIDs);
37+
}
38+
39+
- (void)testBuildFromJSON_EmptyId {
40+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_id"];
41+
XCTAssertNotNil(json);
42+
43+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
44+
XCTAssertNotNil(cpid);
45+
XCTAssertTrue([@"" isEqualToString:cpid.crossPlatformID]);
46+
}
47+
48+
- (void)testBuildFromJSON_EmptyPast {
49+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_past"];
50+
XCTAssertNotNil(json);
51+
52+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
53+
XCTAssertNotNil(cpid);
54+
XCTAssertTrue(cpid.pastCrossPlatformIDs.count == 0);
55+
}
56+
57+
- (void)testBuildFromJSON_EmptyProb {
58+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_prob"];
59+
XCTAssertNotNil(json);
60+
61+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
62+
XCTAssertNotNil(cpid);
63+
XCTAssertTrue(cpid.probabiliticCrossPlatformIDs.count == 0);
64+
}
65+
66+
- (void)testBuildFromJSON_EmptyDevId {
67+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_dev_id"];
68+
XCTAssertNotNil(json);
69+
70+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
71+
XCTAssertNotNil(cpid);
72+
XCTAssertTrue([@"" isEqualToString:cpid.developerID]);
73+
}
74+
75+
- (void)testBuildFromJSON_MissingId {
76+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_id"];
77+
XCTAssertNotNil(json);
78+
79+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
80+
XCTAssertNil(cpid);
81+
}
82+
83+
- (void)testBuildFromJSON_MissingPast {
84+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_past"];
85+
XCTAssertNotNil(json);
86+
87+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
88+
XCTAssertNil(cpid);
89+
}
90+
91+
- (void)testBuildFromJSON_MissingProb {
92+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_prob"];
93+
XCTAssertNotNil(json);
94+
95+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
96+
XCTAssertNil(cpid);
97+
}
98+
99+
- (void)testBuildFromJSON_MissingDevId {
100+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_dev_id"];
101+
XCTAssertNotNil(json);
102+
103+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
104+
XCTAssertNotNil(cpid);
105+
XCTAssertNil(cpid.developerID);
106+
}
107+
108+
@end

0 commit comments

Comments
 (0)