From 81726e9554cab9851ea170e8961f50ee42f39683 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Thu, 17 Sep 2020 01:32:52 -0700 Subject: [PATCH] Adds JSONHelper methods to read dictionaries. --- iOS/MonsterCards/Helpers/JSONHelper.h | 4 + iOS/MonsterCards/Helpers/JSONHelper.m | 26 +++++ .../Helpers/JSONHelperTests.m | 95 +++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/iOS/MonsterCards/Helpers/JSONHelper.h b/iOS/MonsterCards/Helpers/JSONHelper.h index aaf2107..50948e0 100644 --- a/iOS/MonsterCards/Helpers/JSONHelper.h +++ b/iOS/MonsterCards/Helpers/JSONHelper.h @@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN +(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; ++(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key; ++(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSDictionary* _Nullable)defaultValue; +(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index; +(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue; @@ -29,6 +31,8 @@ NS_ASSUME_NONNULL_BEGIN +(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; ++(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index; ++(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSDictionary* _Nullable)defaultValue; @end diff --git a/iOS/MonsterCards/Helpers/JSONHelper.m b/iOS/MonsterCards/Helpers/JSONHelper.m index 5b64643..1b81862 100644 --- a/iOS/MonsterCards/Helpers/JSONHelper.m +++ b/iOS/MonsterCards/Helpers/JSONHelper.m @@ -87,6 +87,19 @@ BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) { return coerceObjectToBool(object, defaultValue); } ++(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key { + return [JSONHelper readDictionaryFromDictionary:dictionary forKey:key withDefaultValue:nil]; +} + ++(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSDictionary* _Nullable)defaultValue { + NSObject *object = [dictionary objectForKey:key]; + if ([object isKindOfClass:[NSDictionary class]]) { + return (NSDictionary*)object; + } + + return defaultValue; +} + +(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{ return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil]; } @@ -123,4 +136,17 @@ BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) { return coerceObjectToBool(object, defaultValue); } ++(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index { + return [JSONHelper readDictionaryFromArray:array forIndex:index withDefaultValue:nil]; +} + ++(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSDictionary* _Nullable)defaultValue { + NSObject *object = [array objectAtIndex:index]; + if ([object isKindOfClass:[NSDictionary class]]) { + return (NSDictionary*)object; + } + + return defaultValue; +} + @end diff --git a/iOS/MonsterCardsTests/Helpers/JSONHelperTests.m b/iOS/MonsterCardsTests/Helpers/JSONHelperTests.m index 7fa1bff..ddf42d6 100644 --- a/iOS/MonsterCardsTests/Helpers/JSONHelperTests.m +++ b/iOS/MonsterCardsTests/Helpers/JSONHelperTests.m @@ -23,6 +23,10 @@ NSString *_jsonBooleanKey; BOOL _jsonBooleanValue; NSString *_jsonBooleanFragment; + NSString *_jsonDictionaryKey; + NSDictionary *_jsonDictionaryValue; + NSString *_jsonDictionaryFragment; + NSString *_jsonDictionaryStringValue; } NSString* escapeStringForJSON(NSString *unescaped) { @@ -59,6 +63,10 @@ NSArray* readJSONArrayFromString(NSString *jsonString) { _jsonBooleanKey = @"my_bool"; _jsonBooleanValue = YES; _jsonBooleanFragment = [NSString stringWithFormat:@"\"%@\":true", escapeStringForJSON(_jsonBooleanKey)]; + _jsonDictionaryKey = @"my_dictionary"; + _jsonDictionaryValue = [NSDictionary dictionaryWithObjectsAndKeys:_jsonStringValue, _jsonStringKey, nil]; + _jsonDictionaryStringValue = [NSString stringWithFormat:@"{%@}", _jsonStringFragment]; + _jsonDictionaryFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonDictionaryKey), _jsonDictionaryStringValue]; } - (void)tearDown { @@ -242,6 +250,54 @@ NSArray* readJSONArrayFromString(NSString *jsonString) { XCTAssertEqual(0, readNumber); } +#pragma mark - Dictionaries in Dictionaries + +- (void)testReadDictionaryFromDictionaryReturnsNilIfKeyNotPresent { + NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment]; + NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + + NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey]; + XCTAssertNil(readValue); +} + +- (void)testReadDictionaryFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent{ + NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment]; + NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey withDefaultValue:@{}]; + XCTAssertEqualObjects(@{}, readValue); +} + +- (void)testReadDictionaryFromDictionaryReturnsCorrectValue { + NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonDictionaryFragment]; + NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey]; + XCTAssertEqualObjects(_jsonDictionaryValue, readValue); +} + +- (void)testReadDictionaryFromDictionaryWithDefaultReturnsCorrectValue { + NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment]; + NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey withDefaultValue:@{}]; + XCTAssertEqualObjects(@{}, readValue); +} + +- (void)testReadDictionaryFromDictionaryReturnsNilIfWrongType { + NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonDictionaryKey, _jsonStringValue]; + NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey]; + XCTAssertNil(readValue); +} + #pragma mark - Integers in Arrays - (void)testReadIntegerFromArrayReturnsNilIfNotAnInteger { @@ -404,4 +460,43 @@ NSArray* readJSONArrayFromString(NSString *jsonString) { XCTAssertEqual(_jsonBooleanValue, readValue); } +#pragma mark - Dictionaries in Arrays + +- (void)testReadDictionaryFromArrayReturnsNilIfNotCoercable { + NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue]; + NSArray *jsonRoot = readJSONArrayFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0]; + XCTAssertNil(readValue); +} + +- (void)testReadDictionaryFromArrayWithDefaultReturnsDefaultValueIfNotCoercable { + NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue]; + NSArray *jsonRoot = readJSONArrayFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0 withDefaultValue:@{}]; + XCTAssertEqualObjects(@{}, readValue); +} + +- (void)testReadDictionaryFromArrayThrowsIfIndexOutOfRange { + // TODO: Decide if this should throw or return nil + NSString *jsonString = @"[]"; + NSArray *jsonRoot = readJSONArrayFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + XCTAssertThrows([JSONHelper readDictionaryFromArray:jsonRoot forIndex:0]); + XCTAssertThrows([JSONHelper readDictionaryFromArray:jsonRoot forIndex:-1]); +} + +- (void)testReadDictionaryFromArrayReturnsCorrectValue { + NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonDictionaryStringValue]; + NSArray *jsonRoot = readJSONArrayFromString(jsonString); + XCTAssertNotNil(jsonRoot); + + NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0]; + XCTAssertEqualObjects(_jsonDictionaryValue, readValue); +} + @end