Adds name to Monster detail view.
Adds json initializers for Monster.
This commit is contained in:
@@ -64,7 +64,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
+(int)abilityModifierForScore: (int)score;
|
||||
+(int)hitDieForSize: (NSString*)size;
|
||||
|
||||
-(id)initWithJSON:(NSString*)jsonData;
|
||||
-(id)initWithJSONString:(NSString*)jsonString;
|
||||
-(id)initWithJSONData:(NSData*)jsonData;
|
||||
-(NSString*)meta;
|
||||
-(int)abilityScoreForAbilityScoreName: (NSString*)abilityScoreName;
|
||||
-(int)abilityModifierForAbilityScoreName: (NSString*)abilityScoreName;
|
||||
|
||||
@@ -19,16 +19,31 @@
|
||||
}
|
||||
|
||||
-(id)init {
|
||||
NSLog(@"TOM_init");
|
||||
self = [super init];
|
||||
|
||||
self.name = @"";
|
||||
// TODO: Actually initialize the class.
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
-(id)initWithJSON: (NSString*)jsonData {
|
||||
-(id)initWithJSONString: (NSString*)jsonString {
|
||||
NSLog(@"TOM_initWithJSONString");
|
||||
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
|
||||
|
||||
return [self initWithJSONData:jsonData];
|
||||
}
|
||||
|
||||
-(id)initWithJSONData: (NSData*)jsonData {
|
||||
NSLog(@"TOM_initWithJSONData");
|
||||
self = [super init];
|
||||
|
||||
NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
|
||||
NSString *name = [jsonRoot objectForKey:@"name"];
|
||||
NSLog(@"decoded name is |%@|", name);
|
||||
self.name = name;//[jsonRoot objectForKey:@"name"];
|
||||
// TODO: Actually initialize the class.
|
||||
|
||||
return self;
|
||||
|
||||
@@ -26,8 +26,7 @@
|
||||
monster = [[Monster alloc] init];
|
||||
monster.name = @"Pixie";
|
||||
[_monsters addObject:monster];
|
||||
monster = [[Monster alloc] init];
|
||||
monster.name = @"Acolyte";
|
||||
monster = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
|
||||
[_monsters addObject:monster];
|
||||
|
||||
// Do any additional setup after loading the view.
|
||||
|
||||
59
MonsterCardsTests/Models/MonsterTests.m
Normal file
59
MonsterCardsTests/Models/MonsterTests.m
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// 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;
|
||||
NSString *_jsonString;
|
||||
NSData *_jsonData;
|
||||
}
|
||||
|
||||
- (void)setUp {
|
||||
_monster = [[Monster alloc] init];
|
||||
_jsonString = @"{\"name\":\"Acolyte\"}";
|
||||
_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);
|
||||
}
|
||||
|
||||
- (void)testInitWithJSONString {
|
||||
_monster = [[Monster alloc] initWithJSONString:_jsonString];
|
||||
|
||||
XCTAssertNotNil(_monster);
|
||||
XCTAssertEqualObjects(@"Acolyte", _monster.name);
|
||||
}
|
||||
|
||||
- (void)testInitWithJSONData {
|
||||
_monster = [[Monster alloc] initWithJSONData:_jsonData];
|
||||
|
||||
XCTAssertNotNil(_monster);
|
||||
XCTAssertEqualObjects(@"Acolyte", _monster.name);
|
||||
}
|
||||
|
||||
- (void)testNameGetterAndSetter {
|
||||
NSString *name = @"Pixie";
|
||||
_monster.name = name;
|
||||
XCTAssertEqualObjects(name, _monster.name);
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user