Permalink
Browse files

Tests for `setPropNameWithJSONObject:`

1 parent 533040a commit 242b400b169d88ecf18c8cba8c8d9e0f2ff483d6 @billinghamj billinghamj committed Oct 7, 2016
Showing with 36 additions and 0 deletions.
  1. +8 −0 Examples/Tests/Models/Implementations/EnumModel.m
  2. +28 −0 Examples/Tests/PrimitiveTypesReadTests.m
@@ -35,6 +35,14 @@ -(void)setNestedStatusWithNSNumber:(NSNumber*)statusNumber
_status = statusNumber.boolValue?StatusOpen:StatusClosed;
}
+- (void)setNestedStatusWithJSONObject:(id <NSObject>)object
+{
+ if ([object isKindOfClass:[NSArray class]])
+ _status = [((NSArray *)object).firstObject isEqualToString:@"open"] ? StatusOpen : StatusClosed;
+ else
+ _status = StatusClosed;
+}
+
-(id)JSONObjectForStatus
{
return (self.status==StatusOpen)?@"open":@"closed";
@@ -83,6 +83,8 @@ -(void)testCustomSetters
{
NSString* json1 = @"{\"nested\":{\"status\":\"open\"},\"nsStatus\":\"closed\",\"nsuStatus\":\"open\",\"statusString\":\"open\"}";
NSString* json2 = @"{\"nested\":{\"status\":true},\"nsStatus\":\"closed\",\"nsuStatus\":\"open\",\"statusString\":\"open\"}";
+ NSString* json3 = @"{\"nested\":{\"status\":[\"open\"]},\"nsStatus\":\"closed\",\"nsuStatus\":\"open\",\"statusString\":\"open\"}";
+ NSString* json4 = @"{\"nested\":{\"status\":{}},\"nsStatus\":\"closed\",\"nsuStatus\":\"open\",\"statusString\":\"open\"}";
NSError* err;
@@ -94,6 +96,14 @@ -(void)testCustomSetters
XCTAssertNil(err, "%@", [err localizedDescription]);
XCTAssertNotNil(p2, @"Could not read input json text");
+ EnumModel* p3 = [[EnumModel alloc] initWithString: json3 error:&err];
+ XCTAssertNil(err, "%@", [err localizedDescription]);
+ XCTAssertNotNil(p3, @"Could not read input json text");
+
+ EnumModel* p4 = [[EnumModel alloc] initWithString: json4 error:&err];
+ XCTAssertNil(err, "%@", [err localizedDescription]);
+ XCTAssertNotNil(p4, @"Could not read input json text");
+
XCTAssertTrue(p1.status==StatusOpen, @"Status is not StatusOpen");
XCTAssertTrue(p1.nsStatus==NSE_StatusClosed, @"nsStatus is not NSE_StatusClosed");
XCTAssertTrue(p1.nsuStatus==NSEU_StatusOpen, @"nsuStatus is not NSEU_StatusOpen");
@@ -102,6 +112,14 @@ -(void)testCustomSetters
XCTAssertTrue(p2.nsStatus==NSE_StatusClosed, @"nsStatus is not NSE_StatusClosed");
XCTAssertTrue(p2.nsuStatus==NSEU_StatusOpen, @"nsuStatus is not NSEU_StatusOpen");
+ XCTAssertTrue(p3.status==StatusOpen, @"Status is not StatusOpen");
+ XCTAssertTrue(p3.nsStatus==NSE_StatusClosed, @"nsStatus is not NSE_StatusClosed");
+ XCTAssertTrue(p3.nsuStatus==NSEU_StatusOpen, @"nsuStatus is not NSEU_StatusOpen");
+
+ XCTAssertTrue(p4.status==StatusClosed, @"Status is not StatusOpen");
+ XCTAssertTrue(p4.nsStatus==NSE_StatusClosed, @"nsStatus is not NSE_StatusClosed");
+ XCTAssertTrue(p4.nsuStatus==NSEU_StatusOpen, @"nsuStatus is not NSEU_StatusOpen");
+
NSString* out1 = [p1 toJSONString];
XCTAssertTrue([out1 rangeOfString:@"\"statusString\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
XCTAssertTrue([out1 rangeOfString:@"\"nsuStatus\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
@@ -111,6 +129,16 @@ -(void)testCustomSetters
XCTAssertTrue([out2 rangeOfString:@"\"statusString\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
XCTAssertTrue([out2 rangeOfString:@"\"nsuStatus\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
XCTAssertTrue([out2 rangeOfString:@"\"nsStatus\":\"closed\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+
+ NSString* out3 = [p3 toJSONString];
+ XCTAssertTrue([out3 rangeOfString:@"\"statusString\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+ XCTAssertTrue([out3 rangeOfString:@"\"nsuStatus\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+ XCTAssertTrue([out3 rangeOfString:@"\"nsStatus\":\"closed\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+
+ NSString* out4 = [p4 toJSONString];
+ XCTAssertTrue([out4 rangeOfString:@"\"statusString\":\"closed\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+ XCTAssertTrue([out4 rangeOfString:@"\"nsuStatus\":\"open\""].location!=NSNotFound, @"Exporting enum value didn't work out");
+ XCTAssertTrue([out4 rangeOfString:@"\"nsStatus\":\"closed\""].location!=NSNotFound, @"Exporting enum value didn't work out");
}
@end

0 comments on commit 242b400

Please sign in to comment.