Adds strengthScore and strengthModifier to Monster.

Adds tests for strengthScore and strengthModifier.
This commit is contained in:
2020-09-13 03:16:32 -07:00
parent 52a7ba871a
commit c14b10a032
2 changed files with 29 additions and 2 deletions

View File

@@ -70,12 +70,17 @@
self = [self initWithContext:context]; self = [self initWithContext:context];
NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSNumber *tempNumber;
self.name = [jsonRoot objectForKey:@"name"] ?: @""; self.name = [jsonRoot objectForKey:@"name"] ?: @"";
self.size = [jsonRoot objectForKey:@"size"] ?: @""; self.size = [jsonRoot objectForKey:@"size"] ?: @"";
self.type = [jsonRoot objectForKey:@"type"] ?: @""; self.type = [jsonRoot objectForKey:@"type"] ?: @"";
self.subtype = [jsonRoot objectForKey:@"tag"] ?: @""; self.subtype = [jsonRoot objectForKey:@"tag"] ?: @"";
self.alignment = [jsonRoot objectForKey:@"alignment"] ?: @""; self.alignment = [jsonRoot objectForKey:@"alignment"] ?: @"";
tempNumber = [jsonRoot objectForKey:@"strPoints"];
if (tempNumber != nil && [tempNumber isKindOfClass:[NSNumber class]]) {
self.strengthScore = tempNumber.intValue;
}
return self; return self;
} }
@@ -120,7 +125,7 @@
} }
-(int)strengthModifier { -(int)strengthModifier {
@throw [[NSException alloc] initWithName:@"unimplemented" reason:@"Method not implemented." userInfo:nil]; return [Monster abilityModifierForScore:self.strengthScore];
} }
-(int)dexterityModifier { -(int)dexterityModifier {
@@ -340,6 +345,7 @@
self.type = monster.type; self.type = monster.type;
self.subtype = monster.subtype; self.subtype = monster.subtype;
self.alignment = monster.alignment; self.alignment = monster.alignment;
self.strengthScore = monster.strengthScore;
} }
@end @end

View File

@@ -25,7 +25,7 @@
- (void)setUp { - (void)setUp {
_context = nil; _context = nil;
_monster = [[Monster alloc] initWithContext:_context]; _monster = [[Monster alloc] initWithContext:_context];
_jsonString = @"{\"name\":\"Acolyte\",\"size\":\"medium\",\"type\":\"humanoid\",\"tag\":\"any race\",\"alignment\":\"any alignment\"}"; _jsonString = @"{\"name\":\"Acolyte\",\"size\":\"medium\",\"type\":\"humanoid\",\"tag\":\"any race\",\"alignment\":\"any alignment\",\"strPoints\":8}";
_jsonData = [_jsonString dataUsingEncoding:NSUTF8StringEncoding]; _jsonData = [_jsonString dataUsingEncoding:NSUTF8StringEncoding];
} }
@@ -40,6 +40,7 @@
XCTAssertEqualObjects(@"", _monster.type); XCTAssertEqualObjects(@"", _monster.type);
XCTAssertEqualObjects(@"", _monster.subtype); XCTAssertEqualObjects(@"", _monster.subtype);
XCTAssertEqualObjects(@"", _monster.alignment); XCTAssertEqualObjects(@"", _monster.alignment);
XCTAssertEqual(0, _monster.strengthScore);
} }
- (void)testInitWithJSONString { - (void)testInitWithJSONString {
@@ -51,6 +52,7 @@
XCTAssertEqualObjects(@"humanoid", _monster.type); XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype); XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment); XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
} }
- (void)testInitWithEmptyJSONString { - (void)testInitWithEmptyJSONString {
@@ -62,6 +64,7 @@
XCTAssertEqualObjects(@"", _monster.type); XCTAssertEqualObjects(@"", _monster.type);
XCTAssertEqualObjects(@"", _monster.subtype); XCTAssertEqualObjects(@"", _monster.subtype);
XCTAssertEqualObjects(@"", _monster.alignment); XCTAssertEqualObjects(@"", _monster.alignment);
XCTAssertEqual(0, _monster.strengthScore);
} }
- (void)testInitWithJSONData { - (void)testInitWithJSONData {
_monster = [[Monster alloc] initWithJSONData:_jsonData andContext:_context]; _monster = [[Monster alloc] initWithJSONData:_jsonData andContext:_context];
@@ -72,6 +75,7 @@
XCTAssertEqualObjects(@"humanoid", _monster.type); XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype); XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment); XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
} }
- (void)testNameGetterAndSetter { - (void)testNameGetterAndSetter {
@@ -113,6 +117,7 @@
XCTAssertEqualObjects(@"humanoid", _monster.type); XCTAssertEqualObjects(@"humanoid", _monster.type);
XCTAssertEqualObjects(@"any race", _monster.subtype); XCTAssertEqualObjects(@"any race", _monster.subtype);
XCTAssertEqualObjects(@"any alignment", _monster.alignment); XCTAssertEqualObjects(@"any alignment", _monster.alignment);
XCTAssertEqual(8, _monster.strengthScore);
} }
- (void)testMetaWithNoFieldsSet { - (void)testMetaWithNoFieldsSet {
@@ -231,4 +236,20 @@
XCTAssertEqual(5, [Monster abilityModifierForScore:20]); 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);
}
@end @end