Convertes to Swift and SwiftUI

This commit is contained in:
2021-01-16 01:12:49 -08:00
parent 3d54342687
commit f6ef6a7f3d
459 changed files with 2301 additions and 23910 deletions

View File

@@ -1,697 +0,0 @@
//
// JSONHelperTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/15/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "JSONHelper.h"
@interface JSONHelperTests : XCTestCase
@end
@implementation JSONHelperTests {
NSString *_jsonStringKey;
NSString *_jsonStringValue;
NSString *_jsonStringFragment;
NSString *_jsonIntegerKey;
NSNumber *_jsonIntegerValue;
NSString *_jsonIntegerFragment;
NSString *_jsonBooleanKey;
BOOL _jsonBooleanValue;
NSString *_jsonBooleanFragment;
NSString *_jsonDictionaryKey;
NSDictionary *_jsonDictionaryValue;
NSString *_jsonDictionaryFragment;
NSString *_jsonDictionaryStringValue;
NSString *_jsonArrayKey;
NSArray *_jsonArrayValue;
NSString *_jsonArrayFragment;
NSString *_jsonArrayStringValue;
}
NSString* escapeStringForJSON(NSString *unescaped) {
return [[unescaped stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
}
NSDictionary* readJSONDictionaryFromString(NSString *jsonString) {
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
if (![jsonRoot isKindOfClass:[NSDictionary class]]) {
return nil;
} else {
return jsonRoot;
}
}
NSArray* readJSONArrayFromString(NSString *jsonString) {
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
if (![jsonRoot isKindOfClass:[NSArray class]]) {
return nil;
} else {
return jsonRoot;
}
}
- (void)setUp {
_jsonStringKey = @"my_string";
_jsonStringValue = @"Hello, World!";
_jsonStringFragment = [NSString stringWithFormat:@"\"%@\":\"%@\"", escapeStringForJSON(_jsonStringKey), escapeStringForJSON(_jsonStringValue)];
_jsonIntegerKey = @"my_int";
_jsonIntegerValue = @12345;
_jsonIntegerFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonIntegerKey), [_jsonIntegerValue stringValue]];
_jsonBooleanKey = @"my_bool";
_jsonBooleanValue = YES;
_jsonBooleanFragment = [NSString stringWithFormat:@"\"%@\":true", escapeStringForJSON(_jsonBooleanKey)];
_jsonDictionaryKey = @"my_dictionary";
_jsonDictionaryValue = @{_jsonStringKey: _jsonStringValue};
_jsonDictionaryStringValue = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
_jsonDictionaryFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonDictionaryKey), _jsonDictionaryStringValue];
_jsonArrayKey = @"my_array";
_jsonArrayValue = @[_jsonStringValue];
_jsonArrayStringValue = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
_jsonArrayFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonArrayKey), _jsonArrayStringValue];
}
- (void)tearDown {
}
#pragma mark - Strings in Dictionaries
- (void)testReadStringFromDictionaryReturnsNilIfKeyNotPresent {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromDictionary:jsonRoot forKey:_jsonStringKey];
XCTAssertNil(readString);
}
- (void)testReadStringFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromDictionary:jsonRoot forKey:_jsonStringKey withDefaultValue:_jsonStringValue];
XCTAssertEqualObjects(_jsonStringValue, readString);
}
- (void) testReadStringFromDictionaryReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromDictionary:jsonRoot forKey:_jsonStringKey];
XCTAssertEqualObjects(_jsonStringValue, readString);
}
- (void)testReadStringFromDictionaryWithDefaultReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromDictionary:jsonRoot forKey:_jsonStringKey withDefaultValue:@"Some other string"];
XCTAssertEqualObjects(_jsonStringValue, readString);
}
- (void) testReadStringFromDictionaryReturnsNilIfWrongType {
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":%@}", _jsonStringKey, _jsonIntegerValue];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromDictionary:jsonRoot forKey:_jsonStringKey];
XCTAssertNil(readString);
}
#pragma mark - Strings in Arrays
- (void)testReadStringFromArrayReturnsNilIfNotAString {
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonIntegerValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromArray:jsonRoot forIndex:0];
XCTAssertNil(readString);
}
- (void)testReadStringFromArrayWithDefaultReturnsDefaultValueIfNotAString {
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonIntegerValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromArray:jsonRoot forIndex:0 withDefaultValue:_jsonStringValue];
XCTAssertEqualObjects(_jsonStringValue, readString);
}
- (void)testReadStringFromArrayThrowsIfIndexOutOfRange {
// TODO: Decide if this should throw or return nil
NSString *jsonString = @"[]";
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
XCTAssertThrows([JSONHelper readStringFromArray:jsonRoot forIndex:0]);
XCTAssertThrows([JSONHelper readStringFromArray:jsonRoot forIndex:-1]);
}
- (void)testReadStringFromArrayReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSString *readString = [JSONHelper readStringFromArray:jsonRoot forIndex:0];
XCTAssertEqualObjects(_jsonStringValue, readString);
}
#pragma mark - Integers in Dictionaries
- (void)testReadIntegerFromDictionaryReturnsNilIfKeyNotPresent {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromDictionary:jsonRoot forKey:_jsonIntegerKey];
XCTAssertNil(readNumber);
}
- (void)testReadIntegerFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromDictionary:jsonRoot forKey:_jsonIntegerKey withDefaultValue:_jsonIntegerValue];
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
}
- (void) testReadIntegerFromDictionaryReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromDictionary:jsonRoot forKey:_jsonIntegerKey];
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
}
- (void)testReadIntegerFromDictionaryWithDefaultReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromDictionary:jsonRoot forKey:_jsonIntegerKey withDefaultValue:@67890];
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
}
- (void) testReadIntegerFromDictionaryReturnsNilIfWrongType {
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonIntegerKey, _jsonStringValue];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromDictionary:jsonRoot forKey:_jsonIntegerKey];
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 - 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 - Arrays in Dictionaries
- (void)testReadArrayFromDictionaryReturnsNilIfKeyNotPresent {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromDictionary:jsonRoot forKey:_jsonArrayKey];
XCTAssertNil(readValue);
}
- (void)testReadArrayFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent{
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromDictionary:jsonRoot forKey:_jsonArrayKey withDefaultValue:@[]];
XCTAssertEqualObjects(@[], readValue);
}
- (void)testReadArrayFromDictionaryReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonArrayFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromDictionary:jsonRoot forKey:_jsonArrayKey];
XCTAssertEqualObjects(_jsonArrayValue, readValue);
}
- (void)testReadArrayFromDictionaryWithDefaultReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromDictionary:jsonRoot forKey:_jsonArrayKey withDefaultValue:@[]];
XCTAssertEqualObjects(@[], readValue);
}
- (void)testReadArrayFromDictionaryReturnsNilIfWrongType {
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonArrayKey, _jsonStringValue];
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromDictionary:jsonRoot forKey:_jsonArrayKey];
XCTAssertNil(readValue);
}
#pragma mark - Integers in Arrays
- (void)testReadIntegerFromArrayReturnsNilIfNotAnInteger {
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromArray:jsonRoot forIndex:0];
XCTAssertNil(readNumber);
}
- (void)testReadIntegerFromArrayWithDefaultReturnsDefaultValueIfNotAnInteger {
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromArray:jsonRoot forIndex:0 withDefaultValue:_jsonIntegerValue];
XCTAssertEqualObjects(_jsonIntegerValue, readNumber);
}
- (void)testReadIntegerFromArrayThrowsIfIndexOutOfRange {
// TODO: Decide if this should throw or return nil
NSString *jsonString = @"[]";
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
XCTAssertThrows([JSONHelper readNumberFromArray:jsonRoot forIndex:0]);
XCTAssertThrows([JSONHelper readNumberFromArray:jsonRoot forIndex:-1]);
}
- (void)testReadIntegerFromArrayReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonIntegerValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSNumber *readNumber = [JSONHelper readNumberFromArray:jsonRoot forIndex:0];
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);
}
#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);
}
#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);
}
#pragma mark - Arrays in Arrays
- (void)testReadArrayFromArrayReturnsNilIfNotCoercable {
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromArray:jsonRoot forIndex:0];
XCTAssertNil(readValue);
}
- (void)testReadArrayFromArrayWithDefaultReturnsDefaultValueIfNotCoercable {
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromArray:jsonRoot forIndex:0 withDefaultValue:@[]];
XCTAssertEqualObjects(@[], readValue);
}
- (void)testReadArrayFromArrayThrowsIfIndexOutOfRange {
// TODO: Decide if this should throw or return nil
NSString *jsonString = @"[]";
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
XCTAssertThrows([JSONHelper readArrayFromArray:jsonRoot forIndex:0]);
XCTAssertThrows([JSONHelper readArrayFromArray:jsonRoot forIndex:-1]);
}
- (void)testReadArrayFromArrayReturnsCorrectValue {
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonArrayStringValue];
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
XCTAssertNotNil(jsonRoot);
NSArray *readValue = [JSONHelper readArrayFromArray:jsonRoot forIndex:0];
XCTAssertEqualObjects(_jsonArrayValue, readValue);
}
#pragma mark - JSON parsing
- (void)testParseJSONDataReturnsDictionary {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedResult = [JSONHelper parseJSONData:jsonData];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSDictionary class]]);
}
- (void)testParseJSONDataReturnsArray {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedResult = [JSONHelper parseJSONData:jsonData];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSArray class]]);
}
- (void)testParseJSONDataAsDictionaryReturnsDictionary {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *parsedResult = [JSONHelper parseJSONDataAsDictionary:jsonData];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSDictionary class]]);
}
- (void)testParseJSONDataAsDictionaryReturnsNilIfNotDictionary {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedResult = [JSONHelper parseJSONDataAsDictionary:jsonData];
XCTAssertNil(parsedResult);
}
- (void)testParseJSONDataAsArrayReturnsArray {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedResult = [JSONHelper parseJSONDataAsArray:jsonData];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSArray class]]);
}
- (void)testParseJSONDataAsArrayReturnsNilIfNotArray {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id parsedResult = [JSONHelper parseJSONDataAsArray:jsonData];
XCTAssertNil(parsedResult);
}
- (void)testParseJSONStringReturnsDictionary {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
id parsedResult = [JSONHelper parseJSONString:jsonString];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSDictionary class]]);
}
- (void)testParseJSONStringReturnsArray {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
id parsedResult = [JSONHelper parseJSONString:jsonString];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSArray class]]);
}
- (void)testParseJSONStringAsDictionaryReturnsDictionary {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
NSDictionary *parsedResult = [JSONHelper parseJSONStringAsDictionary:jsonString];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSDictionary class]]);
}
- (void)testParseJSONStringAsDictionaryReturnsNilIfNotDictionary {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
id parsedResult = [JSONHelper parseJSONStringAsDictionary:jsonString];
XCTAssertNil(parsedResult);
}
- (void)testParseJSONStringAsArrayReturnsArray {
NSString *jsonString = @"[\"a\",1,\"b\",2]";
id parsedResult = [JSONHelper parseJSONStringAsArray:jsonString];
XCTAssertNotNil(parsedResult);
XCTAssertTrue([parsedResult isKindOfClass:[NSArray class]]);
}
- (void)testParseJSONStringAsArrayReturnsNilIfNotArray {
NSString *jsonString = @"{\"a\":1,\"b\":2}";
id parsedResult = [JSONHelper parseJSONStringAsArray:jsonString];
XCTAssertNil(parsedResult);
}
@end

View File

@@ -1,54 +0,0 @@
//
// AbilityTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "Ability.h"
@interface AbilityTests : XCTestCase
@end
@implementation AbilityTests {
Ability *_ability;
NSString *_name;
NSString *_description;
}
- (void)setUp {
_ability = [[Ability alloc] init];
_name = @"My Ability Name";
_description = @"This is my ability description.";
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertEqualObjects(@"", _ability.name);
XCTAssertEqualObjects(@"", _ability.abilityDescription);
}
- (void)testNameGetterAndSetter {
_ability.name = _name;
XCTAssertEqualObjects(_name, _ability.name);
}
- (void)testDescriptionGetterAndSetter {
_ability.abilityDescription = _description;
XCTAssertEqualObjects(_description, _ability.abilityDescription);
}
- (void)testInitWithNameAndDescription {
_ability = [[Ability alloc] initWithName:_name andDescription:_description];
XCTAssertEqualObjects(_name, _ability.name);
XCTAssertEqualObjects(_description, _ability.abilityDescription);
}
@end

View File

@@ -1,54 +0,0 @@
//
// ActionTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "Action.h"
@interface ActionTests : XCTestCase
@end
@implementation ActionTests {
Action *_action;
NSString *_name;
NSString *_description;
}
- (void)setUp {
_action = [[Action alloc] init];
_name = @"My Action Name";
_description = @"This is my action description.";
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertEqualObjects(@"", _action.name);
XCTAssertEqualObjects(@"", _action.actionDescription);
}
- (void)testNameGetterAndSetter {
_action.name = _name;
XCTAssertEqualObjects(_name, _action.name);
}
- (void)testDescriptionGetterAndSetter {
_action.actionDescription = _description;
XCTAssertEqualObjects(_description, _action.actionDescription);
}
- (void)testInitWithNameAndDescription {
_action = [[Action alloc] initWithName:_name andDescription:_description];
XCTAssertEqualObjects(_name, _action.name);
XCTAssertEqualObjects(_description, _action.actionDescription);
}
@end

View File

@@ -1,61 +0,0 @@
//
// DamageTypeTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "DamageType.h"
@interface DamageTypeTests : XCTestCase {
DamageType *_damageType;
NSString *_name;
NSString *_note;
NSString *_type;
}
@end
@implementation DamageTypeTests
- (void)setUp {
_damageType = [[DamageType alloc] init];
_name = @"My Damage Type";
_note = @"A note";
_type = @"A type";
}
- (void)tearDown {}
- (void)testDefaultInitializer {
XCTAssertEqualObjects(@"", _damageType.name);
XCTAssertEqualObjects(@"", _damageType.note);
XCTAssertEqualObjects(@"", _damageType.type);
}
- (void)testInitWithNameNoteAndType {
_damageType = [[DamageType alloc] initWithName:_name note:_note andType:_type];
XCTAssertEqualObjects(_name, _damageType.name);
XCTAssertEqualObjects(_note, _damageType.note);
XCTAssertEqualObjects(_type, _damageType.type);
}
- (void)testNameGetterAndSetter {
_damageType.name = _name;
XCTAssertEqualObjects(_name, _damageType.name);
}
- (void)testNoteGetterAndSetter {
_damageType.note = _note;
XCTAssertEqualObjects(_note, _damageType.note);
}
- (void)testTypeGetterAndSetter {
_damageType.type = _type;
XCTAssertEqualObjects(_type, _damageType.type);
}
@end

View File

@@ -1,56 +0,0 @@
//
// Language.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "Language.h"
@interface LanguageTests : XCTestCase
@end
@implementation LanguageTests {
Language *_language;
NSString *_name;
BOOL _canSpeak;
}
- (void)setUp {
_language = [[Language alloc] init];
_name = @"English";
_canSpeak = YES;
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertNotNil(_language);
XCTAssertEqualObjects(@"", _language.name);
XCTAssertEqual(YES, _language.speaks);
}
- (void)testInitWithNameAndSpeaks {
_language = [[Language alloc] initWithName:_name andSpeaks:_canSpeak];
XCTAssertNotNil(_language);
XCTAssertEqualObjects(_name, _language.name);
XCTAssertEqual(_canSpeak, _language.speaks);
}
- (void)testNameGetterAndSetter {
_language.name = _name;
XCTAssertEqualObjects(_name, _language.name);
}
- (void)testSpeaksGetterAndSetter {
_language.speaks = NO;
XCTAssertEqual(NO, _language.speaks);
}
@end

View File

@@ -1,438 +0,0 @@
//
// MonsterTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
@import OCHamcrest;
@import OCMockito;
#import "Monster.h"
@interface MonsterTests : XCTestCase
@end
@implementation MonsterTests {
Monster *_monster;
NSManagedObjectContext *_context;
NSString *_jsonString;
NSData *_jsonData;
}
- (void)setUp {
_context = nil;
_monster = [[Monster alloc] initWithContext:_context];
_jsonString = @"{\"name\":\"Acolyte\",\"size\":\"medium\",\"type\":\"humanoid\",\"tag\":\"any race\",\"alignment\":\"any alignment\",\"strPoints\":8,\"dexPoints\":10,\"conPoints\":12,\"intPoints\":14,\"wisPoints\":16,\"chaPoints\":18,\"armorName\":\"none\",\"otherArmorDesc\":\"10\",\"shieldBonus\":2,\"hitDice\":3,\"customHP\":true,\"hpText\":\"1234 (1d1+magic)\"}";
_jsonData = [_jsonString dataUsingEncoding:NSUTF8StringEncoding];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"", _monster.name);
XCTAssertEqualObjects(@"", _monster.size);
XCTAssertEqualObjects(@"", _monster.type);
XCTAssertEqualObjects(@"", _monster.subtype);
XCTAssertEqualObjects(@"", _monster.alignment);
XCTAssertEqual(10, _monster.strengthScore);
XCTAssertEqual(10, _monster.dexterityScore);
XCTAssertEqual(10, _monster.constitutionScore);
XCTAssertEqual(10, _monster.intelligenceScore);
XCTAssertEqual(10, _monster.wisdomScore);
XCTAssertEqual(10, _monster.charismaScore);
XCTAssertEqualObjects(@"", _monster.armorType);
XCTAssertEqualObjects(@"", _monster.otherArmorDescription);
XCTAssertEqual(0, _monster.shieldBonus);
XCTAssertEqual(NO, _monster.customHP);
XCTAssertEqual(0, _monster.hitDice);
XCTAssertEqualObjects(@"", _monster.hpText);}
- (void)testInitWithJSONString {
_monster = [[Monster alloc] initWithJSONString:_jsonString andContext:_context];
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"Acolyte", _monster.name);
XCTAssertEqualObjects(@"medium", _monster.size);
XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
XCTAssertEqual(10, _monster.dexterityScore);
XCTAssertEqual(12, _monster.constitutionScore);
XCTAssertEqual(14, _monster.intelligenceScore);
XCTAssertEqual(16, _monster.wisdomScore);
XCTAssertEqual(18, _monster.charismaScore);
XCTAssertEqualObjects(@"none", _monster.armorType);
XCTAssertEqualObjects(@"10", _monster.otherArmorDescription);
XCTAssertEqual(2, _monster.shieldBonus);
XCTAssertEqual(YES, _monster.customHP);
XCTAssertEqual(3, _monster.hitDice);
XCTAssertEqualObjects(@"1234 (1d1+magic)", _monster.hpText);
}
- (void)testInitWithEmptyJSONString {
_monster = [[Monster alloc] initWithJSONString:@"{}" andContext:_context];
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"", _monster.name);
XCTAssertEqualObjects(@"", _monster.size);
XCTAssertEqualObjects(@"", _monster.type);
XCTAssertEqualObjects(@"", _monster.subtype);
XCTAssertEqualObjects(@"", _monster.alignment);
XCTAssertEqual(10, _monster.strengthScore);
XCTAssertEqual(10, _monster.dexterityScore);
XCTAssertEqual(10, _monster.constitutionScore);
XCTAssertEqual(10, _monster.intelligenceScore);
XCTAssertEqual(10, _monster.wisdomScore);
XCTAssertEqual(10, _monster.charismaScore);
XCTAssertEqualObjects(@"", _monster.armorType);
XCTAssertEqualObjects(@"", _monster.otherArmorDescription);
XCTAssertEqual(0, _monster.shieldBonus);
XCTAssertEqual(NO, _monster.customHP);
XCTAssertEqual(0, _monster.hitDice);
XCTAssertEqualObjects(@"", _monster.hpText);
}
- (void)testInitWithJSONData {
_monster = [[Monster alloc] initWithJSONData:_jsonData andContext:_context];
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"Acolyte", _monster.name);
XCTAssertEqualObjects(@"medium", _monster.size);
XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
XCTAssertEqual(10, _monster.dexterityScore);
XCTAssertEqual(12, _monster.constitutionScore);
XCTAssertEqual(14, _monster.intelligenceScore);
XCTAssertEqual(16, _monster.wisdomScore);
XCTAssertEqual(18, _monster.charismaScore);
XCTAssertEqualObjects(@"none", _monster.armorType);
XCTAssertEqualObjects(@"10", _monster.otherArmorDescription);
XCTAssertEqual(2, _monster.shieldBonus);
XCTAssertEqual(YES, _monster.customHP);
XCTAssertEqual(3, _monster.hitDice);
XCTAssertEqualObjects(@"1234 (1d1+magic)", _monster.hpText);
}
- (void)testNameGetterAndSetter {
NSString *name = @"Pixie";
_monster.name = name;
XCTAssertEqualObjects(name, _monster.name);
}
- (void)testSizeGetterAndSetter {
NSString *size = @"huge";
_monster.size = size;
XCTAssertEqualObjects(size, _monster.size);
}
- (void)testTypeGetterAndSetter {
NSString *type = @"fey";
_monster.type = type;
XCTAssertEqualObjects(type, _monster.type);
}
- (void)testSubtypeGetterAndSetter {
NSString *subtype = @"elf";
_monster.subtype = subtype;
XCTAssertEqualObjects(subtype, _monster.subtype);
}
- (void)testAlignmentGetterAndSetter {
NSString *alignment = @"chaotic good";
_monster.alignment = alignment;
XCTAssertEqualObjects(alignment, _monster.alignment);
}
- (void)testCopyFromMonster {
Monster *otherMonster = [[Monster alloc] initWithJSONString:_jsonString andContext:_context];
[_monster copyFromMonster:otherMonster];
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"Acolyte", _monster.name);
XCTAssertEqualObjects(@"medium", _monster.size);
XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
XCTAssertEqual(10, _monster.dexterityScore);
XCTAssertEqual(12, _monster.constitutionScore);
XCTAssertEqual(14, _monster.intelligenceScore);
XCTAssertEqual(16, _monster.wisdomScore);
XCTAssertEqual(18, _monster.charismaScore);
XCTAssertEqualObjects(@"none", _monster.armorType);
XCTAssertEqualObjects(@"10", _monster.otherArmorDescription);
XCTAssertEqual(2, _monster.shieldBonus);
}
- (void)testMetaWithNoFieldsSet {
XCTAssertEqualObjects(@"", _monster.meta);
}
- (void)testMetaWithSize {
_monster.size = @"large";
XCTAssertEqualObjects(@"large", _monster.meta);
}
- (void)testMetaWithType {
_monster.type = @"humanoid";
XCTAssertEqualObjects(@"humanoid", _monster.meta);
}
- (void)testMetaWithSizeAndType {
_monster.size = @"large";
_monster.type = @"humanoid";
XCTAssertEqualObjects(@"large humanoid", _monster.meta);
}
- (void)testMetaWithSubtype {
_monster.subtype = @"elf";
XCTAssertEqualObjects(@"(elf)", _monster.meta);
}
- (void)testMetaWithSizeAndSubtype {
_monster.size = @"large";
_monster.subtype = @"elf";
XCTAssertEqualObjects(@"large (elf)", _monster.meta);
}
- (void)testMetaWithTypeAndSubtype {
_monster.type = @"humanoid";
_monster.subtype = @"elf";
XCTAssertEqualObjects(@"humanoid (elf)", _monster.meta);
}
- (void)testMetaWithSizeTypeAndSubtype {
_monster.size = @"large";
_monster.type = @"humanoid";
_monster.subtype = @"elf";
XCTAssertEqualObjects(@"large humanoid (elf)", _monster.meta);
}
- (void)testMetaWithAlignment {
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"chaotic good", _monster.meta);
}
- (void)testMetaWithSizeAndAlignment {
_monster.size = @"large";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"large chaotic good", _monster.meta);
}
- (void)testMetaWithTypeAndAlignment {
_monster.type = @"humanoid";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"humanoid chaotic good", _monster.meta);
}
- (void)testMetaWithSizeTypeAndAlignment {
_monster.size = @"large";
_monster.type = @"humanoid";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"large humanoid chaotic good", _monster.meta);
}
- (void)testMetaWithSizeSubtypeAndAlignment {
_monster.size = @"large";
_monster.subtype = @"elf";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"large (elf) chaotic good", _monster.meta);
}
- (void)testMetaWithTypeSubtypeAndAlignment {
_monster.type = @"humanoid";
_monster.subtype = @"elf";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"humanoid (elf) chaotic good", _monster.meta);
}
- (void)testMetaWithSizeTypeSubtypeAndAlignment {
_monster.size = @"large";
_monster.type = @"humanoid";
_monster.subtype = @"elf";
_monster.alignment = @"chaotic good";
XCTAssertEqualObjects(@"large humanoid (elf) chaotic good", _monster.meta);
}
- (void)testAbilityModifierForScore {
XCTAssertEqual(-6, [Monster abilityModifierForScore:-1]);
XCTAssertEqual(-5, [Monster abilityModifierForScore:0]);
XCTAssertEqual(-5, [Monster abilityModifierForScore:1]);
XCTAssertEqual(-4, [Monster abilityModifierForScore:2]);
XCTAssertEqual(-4, [Monster abilityModifierForScore:3]);
XCTAssertEqual(-3, [Monster abilityModifierForScore:4]);
XCTAssertEqual(-3, [Monster abilityModifierForScore:5]);
XCTAssertEqual(-2, [Monster abilityModifierForScore:6]);
XCTAssertEqual(-2, [Monster abilityModifierForScore:7]);
XCTAssertEqual(-1, [Monster abilityModifierForScore:8]);
XCTAssertEqual(-1, [Monster abilityModifierForScore:9]);
XCTAssertEqual(0, [Monster abilityModifierForScore:10]);
XCTAssertEqual(0, [Monster abilityModifierForScore:11]);
XCTAssertEqual(1, [Monster abilityModifierForScore:12]);
XCTAssertEqual(1, [Monster abilityModifierForScore:13]);
XCTAssertEqual(2, [Monster abilityModifierForScore:14]);
XCTAssertEqual(2, [Monster abilityModifierForScore:15]);
XCTAssertEqual(3, [Monster abilityModifierForScore:16]);
XCTAssertEqual(3, [Monster abilityModifierForScore:17]);
XCTAssertEqual(4, [Monster abilityModifierForScore:18]);
XCTAssertEqual(4, [Monster abilityModifierForScore:19]);
XCTAssertEqual(5, [Monster abilityModifierForScore:20]);
}
- (void)testStrengthScoreGetterAndSetter {
_monster.strengthScore = 11;
XCTAssertEqual(11, _monster.strengthScore);
}
- (void)testStrengthModifier {
_monster.strengthScore = 9;
XCTAssertEqual(-1, _monster.strengthModifier);
_monster.strengthScore = 10;
XCTAssertEqual(0, _monster.strengthModifier);
_monster.strengthScore = 12;
XCTAssertEqual(1, _monster.strengthModifier);
}
- (void)testDexterityScoreGetterAndSetter {
_monster.dexterityScore = 11;
XCTAssertEqual(11, _monster.dexterityScore);
}
- (void)testDexterityModifier {
_monster.dexterityScore = 9;
XCTAssertEqual(-1, _monster.dexterityModifier);
_monster.dexterityScore = 10;
XCTAssertEqual(0, _monster.dexterityModifier);
_monster.dexterityScore = 12;
XCTAssertEqual(1, _monster.dexterityModifier);
}
- (void)testConstitutionScoreGetterAndSetter {
_monster.constitutionScore = 11;
XCTAssertEqual(11, _monster.constitutionScore);
}
- (void)testConstitutionModifier {
_monster.constitutionScore = 9;
XCTAssertEqual(-1, _monster.constitutionModifier);
_monster.constitutionScore = 10;
XCTAssertEqual(0, _monster.constitutionModifier);
_monster.constitutionScore = 12;
XCTAssertEqual(1, _monster.constitutionModifier);
}
- (void)testIntelligenceScoreGetterAndSetter {
_monster.intelligenceScore = 11;
XCTAssertEqual(11, _monster.intelligenceScore);
}
- (void)testIntelligenceModifier {
_monster.intelligenceScore = 9;
XCTAssertEqual(-1, _monster.intelligenceModifier);
_monster.intelligenceScore = 10;
XCTAssertEqual(0, _monster.intelligenceModifier);
_monster.intelligenceScore = 12;
XCTAssertEqual(1, _monster.intelligenceModifier);
}
- (void)testWisdomScoreGetterAndSetter {
_monster.wisdomScore = 11;
XCTAssertEqual(11, _monster.wisdomScore);
}
- (void)testWisdomModifier {
_monster.wisdomScore = 9;
XCTAssertEqual(-1, _monster.wisdomModifier);
_monster.wisdomScore = 10;
XCTAssertEqual(0, _monster.wisdomModifier);
_monster.wisdomScore = 12;
XCTAssertEqual(1, _monster.wisdomModifier);
}
- (void)testCharismaScoreGetterAndSetter {
_monster.charismaScore = 11;
XCTAssertEqual(11, _monster.charismaScore);
}
- (void)testCharismaModifier {
_monster.charismaScore = 9;
XCTAssertEqual(-1, _monster.charismaModifier);
_monster.charismaScore = 10;
XCTAssertEqual(0, _monster.charismaModifier);
_monster.charismaScore = 12;
XCTAssertEqual(1, _monster.charismaModifier);
}
- (void)testGetterAndSetterForArmorName {
_monster.armorType = @"dandelion";
XCTAssertEqualObjects(@"dandelion", _monster.armorType);
}
- (void)testGetterAndSetterForOtherArmorDescription {
_monster.otherArmorDescription = @"green";
XCTAssertEqualObjects(@"green", _monster.otherArmorDescription);
}
- (void)testHitDieForSizeReturnsExpectedValuesForKnownSizes {
}
- (void)testHitDieForSizeReutnrsEightForUnknownSizes {
XCTAssertEqual(4, [Monster hitDieForSize:kMonsterSizeTiny]);
XCTAssertEqual(6, [Monster hitDieForSize:kMonsterSizeSmall]);
XCTAssertEqual(8, [Monster hitDieForSize:kMonsterSizeMedium]);
XCTAssertEqual(10, [Monster hitDieForSize:kMonsterSizeLarge]);
XCTAssertEqual(12, [Monster hitDieForSize:kMonsterSizeHuge]);
XCTAssertEqual(20, [Monster hitDieForSize:kMonsterSizeGargantuan]);
}
- (void)testCustomHPGetterAndSetter {
XCTAssertEqual(8, [Monster hitDieForSize:nil]);
XCTAssertEqual(8, [Monster hitDieForSize:@""]);
XCTAssertEqual(8, [Monster hitDieForSize:@"unknown size"]);
}
- (void)testHitDiceGetterAndSetter {
_monster.hitDice = 9;
XCTAssertEqual(9, _monster.hitDice);
}
- (void)testHPTextGetterAndSetter {
_monster.hpText = @"This is my HP.";
XCTAssertEqualObjects(@"This is my HP.", _monster.hpText);
}
- (void)testHitPointsDescriptionWithCustomHP {
_monster = [[Monster alloc] initWithJSONString:_jsonString andContext:_context];
_monster.customHP = YES;
XCTAssertEqualObjects(@"1234 (1d1+magic)", _monster.hitPointsDescription);
}
- (void)testHitPointsDescriptionWithCalculatedHP {
_monster = [[Monster alloc] initWithJSONString:_jsonString andContext:_context];
_monster.customHP = NO;
XCTAssertEqualObjects(@"20 (3d8+3)", _monster.hitPointsDescription);
}
@end

View File

@@ -1,55 +0,0 @@
//
// SavingThrowTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "SavingThrow.h"
@interface SavingThrowTests : XCTestCase
@end
@implementation SavingThrowTests {
SavingThrow *_savingThrow;
NSString *_name;
int _order;
}
- (void)setUp {
_savingThrow = [[SavingThrow alloc] init];
_name = @"str";
_order = 9;
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertNotNil(_savingThrow);
XCTAssertEqualObjects(@"", _savingThrow.name);
XCTAssertEqual(-1, _savingThrow.order);
}
- (void)testInitWithNameAndOrder {
_savingThrow = [[SavingThrow alloc] initWithName:_name andOrder:_order];
XCTAssertNotNil(_savingThrow);
XCTAssertEqualObjects(_name, _savingThrow.name);
XCTAssertEqual(_order, _savingThrow.order);
}
- (void)testNameGetterAndSetter {
_savingThrow.name = _name;
XCTAssertEqualObjects(_name, _savingThrow.name);
}
- (void)testOrderGetterAndSetter {
_savingThrow.order = _order;
XCTAssertEqual(_order, _savingThrow.order);
}
@end

View File

@@ -1,106 +0,0 @@
//
// SkillTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/5/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
@import OCMockito;
@import OCHamcrest;
#import "Skill.h"
@interface SkillTests : XCTestCase
@end
@implementation SkillTests {
Skill *_skill;
NSString *_name;
NSString *_abilityScoreName;
NSString *_notes;
}
- (void)setUp {
_skill = [[Skill alloc] init];
_name = @"pranking";
_abilityScoreName = @"str";
_notes = @"some notes";
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testDefaultInitializer {
XCTAssertNotNil(_skill);
XCTAssertEqualObjects(@"", _skill.name);
XCTAssertEqualObjects(@"", _skill.abilityScoreName);
XCTAssertEqualObjects(@"", _skill.notes);
}
- (void)testInitWithNameAbilityScoreNameAndNotes {
_skill = [[Skill alloc] initWithName:_name abilityScoreName:_abilityScoreName andNotes:_notes];
XCTAssertNotNil(_skill);
XCTAssertEqualObjects(_name, _skill.name);
XCTAssertEqualObjects(_abilityScoreName, _skill.abilityScoreName);
XCTAssertEqualObjects(_notes, _skill.notes);
}
- (void)testNameGetterAndSetter {
_skill.name = _name;
XCTAssertEqualObjects(_name, _skill.name);
}
- (void)testAbilityScoreNameGetterAndSetter {
_skill.abilityScoreName = _abilityScoreName;
XCTAssertEqualObjects(_abilityScoreName, _skill.abilityScoreName);
}
- (void)testNotesGetterAndSetter {
_skill.notes = _notes;
XCTAssertEqualObjects(_notes, _skill.notes);
}
- (void)testSkillBonusForMonster {
Monster *monster = mock([Monster class]);
[given([monster abilityModifierForAbilityScoreName:_abilityScoreName]) willReturnInt:1];
stubProperty(monster, proficiencyBonus, @2);
_skill = [[Skill alloc] initWithName:_name abilityScoreName:_abilityScoreName andNotes:_notes];
XCTAssertEqual(3, [_skill skillBonusForMonster:monster]);
}
- (void)testSkillBonusForMonsterWithExpertise {
Monster *monster = mock([Monster class]);
[given([monster abilityModifierForAbilityScoreName:_abilityScoreName]) willReturnInt:1];
stubProperty(monster, proficiencyBonus, @2);
_skill = [[Skill alloc] initWithName:_name abilityScoreName:_abilityScoreName andNotes:@" (ex)"];
XCTAssertEqual(5, [_skill skillBonusForMonster:monster]);
}
- (void)testTextForMonster {
Monster *monster = mock([Monster class]);
[given([monster abilityModifierForAbilityScoreName:_abilityScoreName]) willReturnInt:1];
stubProperty(monster, proficiencyBonus, @2);
_skill = [[Skill alloc] initWithName:_name abilityScoreName:_abilityScoreName andNotes:_notes];
XCTAssertEqualObjects(@"Pranking 3", [_skill textForMonster:monster]);
}
- (void)testTextForMonsterWithExpertise {
Monster *monster = mock([Monster class]);
[given([monster abilityModifierForAbilityScoreName:_abilityScoreName]) willReturnInt:1];
stubProperty(monster, proficiencyBonus, @2);
_skill = [[Skill alloc] initWithName:_name abilityScoreName:_abilityScoreName andNotes:@" (ex)"];
XCTAssertEqualObjects(@"Pranking 5", [_skill textForMonster:monster]);
}
@end

View File

@@ -1,37 +0,0 @@
//
// MonsterCardsTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/2/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface MonsterCardsTests : XCTestCase
@end
@implementation MonsterCardsTests
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
@end

View File

@@ -0,0 +1,33 @@
//
// MonsterCardsTests.swift
// MonsterCardsTests
//
// Created by Tom Hicks on 1/15/21.
//
import XCTest
@testable import MonsterCards
class MonsterCardsTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}
}

View File

@@ -1,130 +0,0 @@
//
// EditMonsterViewControllerTests.m
// MonsterCardsTests
//
// Created by Tom Hicks on 9/12/20.
// Copyright © 2020 Tom Hicks. All rights reserved.
//
#import <XCTest/XCTest.h>
@import OCMockito;
@import OCHamcrest;
#import "MCShortStringFieldTableViewCell.h"
#import "EditMonsterViewController.h"
#import "AppDelegate.h"
@interface EditMonsterViewControllerTests : XCTestCase
@end
@implementation EditMonsterViewControllerTests {
EditMonsterViewController *_viewController;
NSManagedObjectContext *_context;
AppDelegate *_appDelegate;
NSPersistentCloudKitContainer *_persistentContainer;
Monster *_monster;
}
- (void)setUp {
_monster = mock([Monster class]);
_viewController = [[EditMonsterViewController alloc] init];
_context = mock([NSManagedObjectContext class]);
_appDelegate = mock([AppDelegate class]);
_persistentContainer = mock([NSPersistentCloudKitContainer class]);
UIApplication.sharedApplication.delegate = _appDelegate;
}
- (void)tearDown {
}
- (void)testRendersSubtypeCell {
UITableView *monstersTableView = mock([UITableView class]);
NSIndexPath *path = [NSIndexPath indexPathForRow:3 inSection:0];
MCShortStringFieldTableViewCell *shortStringCell = [[MCShortStringFieldTableViewCell alloc] init];
UITextField *textField = [[UITextField alloc] init];
shortStringCell.textField = textField;
[given([monstersTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]) willReturn:shortStringCell];
_monster.subtype = @"elf";
_viewController.originalMonster = _monster;
_viewController.monsterTableView = monstersTableView;
[_viewController viewDidLoad];
[_viewController viewWillAppear:NO];
UITableViewCell *cell = [_viewController tableView:monstersTableView cellForRowAtIndexPath:path];
XCTAssertNotNil(cell);
XCTAssertTrue([cell isKindOfClass:[MCShortStringFieldTableViewCell class]]);
shortStringCell = (MCShortStringFieldTableViewCell*)cell;
XCTAssertEqualObjects(@"monster.subtype", shortStringCell.identifier);
XCTAssertEqualObjects(@"Subtype", shortStringCell.textField.placeholder);
XCTAssertEqualObjects(@"", shortStringCell.textField.text);
XCTAssertEqual(_viewController, shortStringCell.delegate);
}
- (void)testEditingSubtype {
UIViewController *destinationVC = mock([UIViewController class]);
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"SaveChanges" source:_viewController destination:destinationVC performHandler:^{}];
_monster = [[Monster alloc] initWithContext:_context];
_monster.subtype = @"elf";
_viewController.originalMonster = _monster;
[_viewController viewDidLoad];
[_viewController viewWillAppear:NO];
[_viewController editableValueDidChange:@"newValue" forIdentifier:@"monster.subtype" andType:@"String"];
[_viewController prepareForSegue:segue sender:nil];
XCTAssertEqualObjects(@"newValue", _viewController.originalMonster.subtype);
}
- (void)testRendersAlignmentCell {
UITableView *monstersTableView = mock([UITableView class]);
NSIndexPath *path = [NSIndexPath indexPathForRow:4 inSection:0];
MCShortStringFieldTableViewCell *shortStringCell = [[MCShortStringFieldTableViewCell alloc] init];
UITextField *textField = [[UITextField alloc] init];
shortStringCell.textField = textField;
[given([monstersTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]) willReturn:shortStringCell];
_monster.alignment = @"chaotic good";
_viewController.originalMonster = _monster;
_viewController.monsterTableView = monstersTableView;
[_viewController viewDidLoad];
[_viewController viewWillAppear:NO];
UITableViewCell *cell = [_viewController tableView:monstersTableView cellForRowAtIndexPath:path];
XCTAssertNotNil(cell);
XCTAssertTrue([cell isKindOfClass:[MCShortStringFieldTableViewCell class]]);
shortStringCell = (MCShortStringFieldTableViewCell*)cell;
XCTAssertEqualObjects(@"monster.alignment", shortStringCell.identifier);
XCTAssertEqualObjects(@"Alignment", shortStringCell.textField.placeholder);
XCTAssertEqualObjects(@"", shortStringCell.textField.text);
XCTAssertEqual(_viewController, shortStringCell.delegate);
}
- (void)testEditingAlignment {
UIViewController *destinationVC = mock([UIViewController class]);
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"SaveChanges" source:_viewController destination:destinationVC performHandler:^{}];
_monster = [[Monster alloc] initWithContext:_context];
_monster.alignment = @"chaotic good";
_viewController.originalMonster = _monster;
[_viewController viewDidLoad];
[_viewController viewWillAppear:NO];
[_viewController editableValueDidChange:@"newValue" forIdentifier:@"monster.alignment" andType:@"String"];
[_viewController prepareForSegue:segue sender:nil];
XCTAssertEqualObjects(@"newValue", _viewController.originalMonster.alignment);
}
@end