Adds methods to JSONHelper to read boolean values.
This commit is contained in:
@@ -18,6 +18,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
+(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;
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(BOOL)defaultValue;
|
||||
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
|
||||
@@ -25,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
+(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;
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(BOOL)defaultValue;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -43,6 +43,14 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) {
|
||||
if ([object isKindOfClass:[NSNumber class]]) {
|
||||
return [(NSNumber*)object boolValue];
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
+(NSString*)readStringFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
||||
return [JSONHelper readStringFromDictionary:dictionary forKey:key withDefaultValue:nil];
|
||||
}
|
||||
@@ -70,6 +78,15 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
|
||||
return coerceObjectToInt(object, defaultValue);
|
||||
}
|
||||
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
||||
return [JSONHelper readBoolFromDictionary:dictionary forKey:key withDefaultValue:NO];
|
||||
}
|
||||
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(BOOL)defaultValue {
|
||||
NSObject *object = [dictionary objectForKey:key];
|
||||
return coerceObjectToBool(object, defaultValue);
|
||||
}
|
||||
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
|
||||
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
|
||||
}
|
||||
@@ -97,4 +114,13 @@ int coerceObjectToInt(NSObject *object, int defaultValue) {
|
||||
return coerceObjectToInt(object, defaultValue);
|
||||
}
|
||||
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index {
|
||||
return [JSONHelper readBoolFromArray:array forIndex:index withDefaultValue:nil];
|
||||
}
|
||||
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(BOOL)defaultValue {
|
||||
NSObject *object = [array objectAtIndex:index];
|
||||
return coerceObjectToBool(object, defaultValue);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
NSString *_jsonIntegerKey;
|
||||
NSNumber *_jsonIntegerValue;
|
||||
NSString *_jsonIntegerFragment;
|
||||
NSString *_jsonBooleanKey;
|
||||
BOOL _jsonBooleanValue;
|
||||
NSString *_jsonBooleanFragment;
|
||||
}
|
||||
|
||||
NSString* escapeStringForJSON(NSString *unescaped) {
|
||||
@@ -53,6 +56,9 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
||||
_jsonIntegerKey = @"my_int";
|
||||
_jsonIntegerValue = @12345;
|
||||
_jsonIntegerFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonIntegerKey), [_jsonIntegerValue stringValue]];
|
||||
_jsonBooleanKey = @"my_bool";
|
||||
_jsonBooleanValue = YES;
|
||||
_jsonBooleanFragment = [NSString stringWithFormat:@"\"%@\":true", escapeStringForJSON(_jsonBooleanKey)];
|
||||
}
|
||||
|
||||
- (void)tearDown {
|
||||
@@ -312,4 +318,90 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
||||
XCTAssertEqual([_jsonIntegerValue intValue], readNumber);
|
||||
}
|
||||
|
||||
#pragma mark - BOOLs in Dictionaries
|
||||
|
||||
- (void)testReadBoolFromDictionaryReturnsFalseIfKeyNotPresent {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromDictionary:jsonRoot forKey:_jsonBooleanKey];
|
||||
XCTAssertEqual(0, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadBoolFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromDictionary:jsonRoot forKey:_jsonIntegerKey withDefaultValue:_jsonBooleanValue];
|
||||
XCTAssertEqual(_jsonBooleanValue, readValue);
|
||||
}
|
||||
|
||||
- (void) testReadBoolFromDictionaryReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonBooleanFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromDictionary:jsonRoot forKey:_jsonBooleanKey];
|
||||
XCTAssertEqual(_jsonBooleanValue, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadBoolFromDictionaryWithDefaultReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonBooleanFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromDictionary:jsonRoot forKey:_jsonBooleanKey withDefaultValue:NO];
|
||||
XCTAssertEqual(_jsonBooleanValue, readValue);
|
||||
}
|
||||
|
||||
- (void) testReadBoolFromDictionaryReturnsFalseIfWrongType {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonIntegerKey, _jsonStringValue];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromDictionary:jsonRoot forKey:_jsonIntegerKey];
|
||||
XCTAssertEqual(NO, readValue);
|
||||
}
|
||||
|
||||
#pragma mark - BOOLs in Arrays
|
||||
|
||||
- (void)testReadBoolFromArrayReturnsFalseIfNotCoercable {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromArray:jsonRoot forIndex:0];
|
||||
XCTAssertEqual(NO, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadBoolFromArrayWithDefaultReturnsDefaultValueIfNotCoercable {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readIntFromArray:jsonRoot forIndex:0 withDefaultValue:YES];
|
||||
XCTAssertEqual(YES, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadBoolFromArrayThrowsIfIndexOutOfRange {
|
||||
// TODO: Decide if this should throw or return 0
|
||||
NSString *jsonString = @"[]";
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
XCTAssertThrows([JSONHelper readBoolFromArray:jsonRoot forIndex:0]);
|
||||
XCTAssertThrows([JSONHelper readBoolFromArray:jsonRoot forIndex:-1]);
|
||||
}
|
||||
|
||||
- (void)testReadBoolFromArrayReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[%s]", _jsonBooleanValue ? "true" : "false"];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
BOOL readValue = [JSONHelper readBoolFromArray:jsonRoot forIndex:0];
|
||||
XCTAssertEqual(_jsonBooleanValue, readValue);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user