Adds monster type to editor.

Sets all entity attributes for monster to default to empty string instead of null.
Adds test for copyFromMonster.
Makes initWithMonster:andContext call copyFromMonster to ensure they use the same logic to clone the other monster.
This commit is contained in:
2020-09-12 03:30:37 -07:00
parent ee9994c2c8
commit 42ddfbd52f
4 changed files with 44 additions and 9 deletions

View File

@@ -73,6 +73,7 @@
self.name = [jsonRoot objectForKey:@"name"] ?: @"";
self.size = [jsonRoot objectForKey:@"size"] ?: @"";
self.type = [jsonRoot objectForKey:@"type"] ?: @"";
return self;
}
@@ -80,7 +81,7 @@
-(id)initWithMonster:(Monster* _Nonnull)monster {
self = [self initWithContext:monster.managedObjectContext];
self.name = monster.name;
[self copyFromMonster:monster];
return self;
}
@@ -334,6 +335,7 @@
-(void)copyFromMonster:(Monster*)monster {
self.name = monster.name;
self.size = monster.size;
self.type = monster.type;
}
@end

View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="16119" systemVersion="19G2021" minimumToolsVersion="Automatic" sourceLanguage="Objective-C" usedWithCloudKit="YES" userDefinedModelVersionIdentifier="">
<entity name="Monster" representedClassName="Monster" syncable="YES" codeGenerationType="category">
<attribute name="alignment" optional="YES" attributeType="String"/>
<attribute name="alignment" attributeType="String" defaultValueString=""/>
<attribute name="name" attributeType="String" defaultValueString=""/>
<attribute name="size" optional="YES" attributeType="String" defaultValueString=""/>
<attribute name="subtype" optional="YES" attributeType="String"/>
<attribute name="type" optional="YES" attributeType="String"/>
<attribute name="size" attributeType="String" defaultValueString=""/>
<attribute name="subtype" attributeType="String" defaultValueString=""/>
<attribute name="type" attributeType="String" defaultValueString=""/>
</entity>
<elements>
<element name="Monster" positionX="-63" positionY="-18" width="128" height="118"/>

View File

@@ -59,7 +59,7 @@
// * Subtype
// * Alignment
return 2;
return 3;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
@@ -91,6 +91,17 @@
shortStringCell.textField.text = self.editingMonster.size;
shortStringCell.textField.placeholder = NSLocalizedString(@"Size", @"Placehodler text for the size of a monster or NPC.");
return shortStringCell;
case 2:
shortStringCell = [self.monsterTableView dequeueReusableCellWithIdentifier:@"EditableShortString"];
if (shortStringCell == nil) {
shortStringCell = [[EditableShortStringTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditableShortString"];
}
shortStringCell.delegate = self;
shortStringCell.identifier = @"monster.type";
// TODO: make these use setters on EditableShortStringTableViewCell
shortStringCell.textField.text = self.editingMonster.type;
shortStringCell.textField.placeholder = NSLocalizedString(@"Type", @"Placehodler text for the type of a monster or NPC.");
return shortStringCell;
}
break;
}
@@ -106,6 +117,8 @@
self.editingMonster.name = (NSString*)value;
} else if ([@"monster.size" isEqualToString:identifier]) {
self.editingMonster.size = (NSString*)value;
} else if ([@"monster.type" isEqualToString:identifier]) {
self.editingMonster.type = (NSString*)value;
}
}
}

View File

@@ -25,7 +25,7 @@
- (void)setUp {
_context = nil;
_monster = [[Monster alloc] initWithContext:_context];
_jsonString = @"{\"name\":\"Acolyte\",\"size\":\"large\"}";
_jsonString = @"{\"name\":\"Acolyte\",\"size\":\"medium\",\"type\":\"humanoid\"}";
_jsonData = [_jsonString dataUsingEncoding:NSUTF8StringEncoding];
}
@@ -37,6 +37,7 @@
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"", _monster.name);
XCTAssertEqualObjects(@"", _monster.size);
XCTAssertEqualObjects(@"", _monster.type);
}
- (void)testInitWithJSONString {
@@ -44,7 +45,8 @@
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"Acolyte", _monster.name);
XCTAssertEqualObjects(@"large", _monster.size);
XCTAssertEqualObjects(@"medium", _monster.size);
XCTAssertEqualObjects(@"humanoid", _monster.type);
}
- (void)testInitWithEmptyJSONString {
@@ -53,13 +55,15 @@
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"", _monster.name);
XCTAssertEqualObjects(@"", _monster.size);
XCTAssertEqualObjects(@"", _monster.type);
}
- (void)testInitWithJSONData {
_monster = [[Monster alloc] initWithJSONData:_jsonData andContext:_context];
XCTAssertNotNil(_monster);
XCTAssertEqualObjects(@"Acolyte", _monster.name);
XCTAssertEqualObjects(@"large", _monster.size);
XCTAssertEqualObjects(@"medium", _monster.size);
XCTAssertEqualObjects(@"humanoid", _monster.type);
}
- (void)testNameGetterAndSetter {
@@ -74,4 +78,20 @@
XCTAssertEqualObjects(size, _monster.size);
}
- (void)testTypeGetterAndSetter {
NSString *type = @"fey";
_monster.type = type;
XCTAssertEqualObjects(type, _monster.type);
}
- (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);
}
@end