Adds JSONHelper methods to read numbers as ints.
This commit is contained in:
@@ -16,10 +16,15 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSString* _Nullable)defaultValue;
|
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSString* _Nullable)defaultValue;
|
||||||
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
||||||
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSNumber* _Nullable)defaultValue;
|
+(NSNumber*)readNumberFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSNumber* _Nullable)defaultValue;
|
||||||
|
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
||||||
|
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(int)defaultValue;
|
||||||
|
|
||||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
|
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
|
||||||
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||||
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSNumber* _Nullable)defaultValue;
|
+(NSNumber*)readNumberFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSNumber* _Nullable)defaultValue;
|
||||||
|
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||||
|
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(int)defaultValue;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,23 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
|
|||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int coerceObjectToInt(NSObject *object, int defaultValue) {
|
||||||
|
if ([object isKindOfClass:[NSNumber class]]) {
|
||||||
|
return [(NSNumber*)object intValue];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([object isKindOfClass:[NSString class]]) {
|
||||||
|
NSScanner *scanner;
|
||||||
|
int temp;
|
||||||
|
scanner = [NSScanner scannerWithString:(NSString*)object];
|
||||||
|
if ([scanner scanInt:&temp]) {
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
||||||
return [JSONHelper readStringFromDictionary:dictionary forKey:key withDefaultValue:nil];
|
return [JSONHelper readStringFromDictionary:dictionary forKey:key withDefaultValue:nil];
|
||||||
}
|
}
|
||||||
@@ -44,6 +61,15 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
|
|||||||
return coerceObjectToNumber(object, defaultValue);
|
return coerceObjectToNumber(object, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
||||||
|
return [JSONHelper readIntFromDictionary:dictionary forKey:key withDefaultValue:0];
|
||||||
|
}
|
||||||
|
|
||||||
|
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(int)defaultValue {
|
||||||
|
NSObject *object = [dictionary objectForKey:key];
|
||||||
|
return coerceObjectToInt(object, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
|
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
|
||||||
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
|
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
|
||||||
}
|
}
|
||||||
@@ -62,4 +88,13 @@ NSNumber* coerceObjectToNumber(NSObject *object, NSNumber *defaultValue) {
|
|||||||
return coerceObjectToNumber(object, defaultValue);
|
return coerceObjectToNumber(object, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index {
|
||||||
|
return [JSONHelper readIntFromArray:array forIndex:index withDefaultValue:0];
|
||||||
|
}
|
||||||
|
|
||||||
|
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(int)defaultValue {
|
||||||
|
NSObject *object = [array objectAtIndex:index];
|
||||||
|
return coerceObjectToInt(object, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -191,6 +191,51 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
|||||||
XCTAssertNil(readNumber);
|
XCTAssertNil(readNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromDictionaryReturnsZeroIfKeyNotPresent {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||||
|
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromDictionary:jsonRoot forKey:_jsonIntegerKey];
|
||||||
|
XCTAssertEqual(0, readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||||
|
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromDictionary:jsonRoot forKey:_jsonIntegerKey withDefaultValue:[_jsonIntegerValue intValue]];
|
||||||
|
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void) testReadIntFromDictionaryReturnsCorrectValue {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
|
||||||
|
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromDictionary:jsonRoot forKey:_jsonIntegerKey];
|
||||||
|
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromDictionaryWithDefaultReturnsCorrectValue {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
|
||||||
|
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromDictionary:jsonRoot forKey:_jsonIntegerKey withDefaultValue:67890];
|
||||||
|
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void) testReadIntFromDictionaryReturnsZeroIfWrongType {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonIntegerKey, _jsonStringValue];
|
||||||
|
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromDictionary:jsonRoot forKey:_jsonIntegerKey];
|
||||||
|
XCTAssertEqual(0, readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - Integers in Arrays
|
#pragma mark - Integers in Arrays
|
||||||
|
|
||||||
- (void)testReadIntegerFromArrayReturnsNilIfNotAnInteger {
|
- (void)testReadIntegerFromArrayReturnsNilIfNotAnInteger {
|
||||||
@@ -230,4 +275,41 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
|||||||
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
|
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromArrayReturnsNilIfNotAnInteger {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||||
|
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromArray:jsonRoot forIndex:0];
|
||||||
|
XCTAssertEqual(0, readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromArrayWithDefaultReturnsDefaultValueIfNotAnInteger {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||||
|
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromArray:jsonRoot forIndex:0 withDefaultValue:[_jsonIntegerValue intValue]];
|
||||||
|
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromArrayThrowsIfIndexOutOfRange {
|
||||||
|
// TODO: Decide if this should throw or return 0
|
||||||
|
NSString *jsonString = @"[]";
|
||||||
|
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
XCTAssertThrows([JSONHelper readIntFromArray:jsonRoot forIndex:0]);
|
||||||
|
XCTAssertThrows([JSONHelper readIntFromArray:jsonRoot forIndex:-1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)testReadIntFromArrayReturnsCorrectValue {
|
||||||
|
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonIntegerValue];
|
||||||
|
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||||
|
XCTAssertNotNil(jsonRoot);
|
||||||
|
|
||||||
|
int readNumber = [JSONHelper readIntFromArray:jsonRoot forIndex:0];
|
||||||
|
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user