Skip to content

Commit 53c95bb

Browse files
committed
SDK-479 save progress on cpid
1 parent b9f6a98 commit 53c95bb

30 files changed

+960
-19
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
34+
35+
- (void)testBuildFromJSON_EmptyId {
36+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_id"];
37+
XCTAssertNotNil(json);
38+
39+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
40+
XCTAssertNotNil(cpid);
41+
XCTAssertTrue([@"" isEqualToString:cpid.crossPlatformID]);
42+
}
43+
44+
- (void)testBuildFromJSON_EmptyPast {
45+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_past"];
46+
XCTAssertNotNil(json);
47+
48+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
49+
XCTAssertNotNil(cpid);
50+
XCTAssertTrue(cpid.pastCrossPlatformIDs.count == 0);
51+
}
52+
53+
- (void)testBuildFromJSON_EmptyProb {
54+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_empty_prob"];
55+
XCTAssertNotNil(json);
56+
57+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
58+
XCTAssertNotNil(cpid);
59+
XCTAssertTrue(cpid.probabiliticCrossPlatformIDs.count == 0);
60+
}
61+
62+
- (void)testBuildFromJSON_MissingId {
63+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_id"];
64+
XCTAssertNotNil(json);
65+
66+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
67+
XCTAssertNil(cpid);
68+
}
69+
70+
- (void)testBuildFromJSON_MissingPast {
71+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_past"];
72+
XCTAssertNotNil(json);
73+
74+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
75+
XCTAssertNil(cpid);
76+
}
77+
78+
- (void)testBuildFromJSON_MissingProb {
79+
NSDictionary *json = [BNCJsonLoader dictionaryFromJSONFileNamed:@"cpid_missing_prob"];
80+
XCTAssertNotNil(json);
81+
82+
BranchCrossPlatformID *cpid = [BranchCrossPlatformID buildFromJSON:json];
83+
XCTAssertNil(cpid);
84+
}
85+
86+
87+
@end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// BNCJSONUtility.h
3+
// Branch
4+
//
5+
// Created by Ernest Cho on 9/17/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
// Utility methods to convert untyped data to typed data
14+
@interface BNCJSONUtility : NSObject
15+
16+
+ (BOOL)isNumber:(nullable id)number;
17+
18+
+ (BOOL)isString:(nullable id)string;
19+
20+
+ (BOOL)isDictionary:(nullable id)dictionary;
21+
22+
+ (BOOL)isArray:(nullable id)array;
23+
24+
+ (nullable NSDictionary *)dictionaryForKey:(NSString *)key json:(NSDictionary *)json;
25+
26+
+ (nullable NSDictionary<NSString *, NSString *> *)stringDictionaryForKey:(NSString *)key json:(NSDictionary *)json;
27+
28+
+ (nullable NSArray *)arrayForKey:(NSString *)key json:(NSDictionary *)json;
29+
30+
+ (nullable NSArray<NSString *> *)stringArrayForKey:(NSString *)key json:(NSDictionary *)json;
31+
32+
+ (nullable NSString *)stringForKey:(NSString *)key json:(NSDictionary *)json;
33+
34+
+ (nullable NSNumber *)numberForKey:(NSString *)key json:(NSDictionary *)json;
35+
36+
@end
37+
38+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)