|
| 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 |
0 commit comments