Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better JSON String Test Code #1687

Merged
merged 1 commit into from Dec 21, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 8 additions & 19 deletions Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m
Expand Up @@ -242,12 +242,7 @@ - (void)testShouldSerializeNestedObjectsContainingDatesToJSON
NSData *data = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Encodes differently on iOS / OS X
#if TARGET_OS_IPHONE
expect(string).to.equal(@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01T00:00:00.000Z\"}}");
#else
expect(string).to.equal(@"{\"hasOne\":{\"date\":\"1970-01-01T00:00:00.000Z\"},\"stringTest\":\"The string\"}");
#endif
expect((@[@"{\"stringTest\":\"The string\",\"hasOne\":{\"date\":\"1970-01-01T00:00:00.000Z\"}}", @"{\"hasOne\":{\"date\":\"1970-01-01T00:00:00.000Z\"},\"stringTest\":\"The string\"}"])).to.contain(string);
}

- (void)testShouldEncloseTheSerializationInAContainerIfRequested
Expand Down Expand Up @@ -413,7 +408,7 @@ - (void)testParameterizationOfBooleanPropertiesToJSONBooleansFromObjectPropertie

expect(error).to.beNil();
// Unordered dictionary handling
NSArray *serializations = @[ @"{\"is_valid\":0,\"name\":\"Whatever\",\"is_boolean\":true}", @"{\"name\":\"Whatever\",\"is_valid\":0,\"is_boolean\":true}" ];
NSArray *serializations = @[ @"{\"is_valid\":0,\"name\":\"Whatever\",\"is_boolean\":true}", @"{\"name\":\"Whatever\",\"is_valid\":0,\"is_boolean\":true}", @"{\"name\":\"Whatever\",\"is_valid\":false,\"is_boolean\":true}"];
expect(serializations).to.contain(string);
}

Expand Down Expand Up @@ -661,13 +656,10 @@ - (void)testShouldSerializeHasOneRelatioshipsToJSON
RKObjectMapping *serializationMapping = [userMapping inverseMapping];
NSDictionary *params = [RKObjectParameterization parametersWithObject:user requestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:serializationMapping objectClass:[RKTestUser class] rootKeyPath:nil method:RKRequestMethodAny] error:nil];
NSError *error = nil;
NSString *JSON = [[NSString alloc] initWithData:[RKMIMETypeSerialization dataFromObject:params MIMEType:RKMIMETypeJSON error:nil] encoding:NSUTF8StringEncoding];
NSData *JSON = [RKMIMETypeSerialization dataFromObject:params MIMEType:RKMIMETypeJSON error:nil];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
assertThat(error, is(nilValue()));
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
assertThat(JSON, is(equalTo(@"{\"name\":\"Blake Watters\",\"address\":{\"state\":\"North Carolina\",\"city\":null}}")));
#else
assertThat(JSON, is(equalTo(@"{\"name\":\"Blake Watters\",\"address\":{\"city\":null,\"state\":\"North Carolina\"}}")));
#endif
assertThat(jsonDictionary, is(equalTo(params)));
}

- (void)testShouldSerializeHasManyRelationshipsToJSON
Expand All @@ -689,13 +681,10 @@ - (void)testShouldSerializeHasManyRelationshipsToJSON
RKObjectMapping *serializationMapping = [userMapping inverseMapping];
NSDictionary *params = [RKObjectParameterization parametersWithObject:user requestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:serializationMapping objectClass:[RKTestUser class] rootKeyPath:nil method:RKRequestMethodAny] error:nil];
NSError *error = nil;
NSString *JSON = [[NSString alloc] initWithData:[RKMIMETypeSerialization dataFromObject:params MIMEType:RKMIMETypeJSON error:nil] encoding:NSUTF8StringEncoding];
NSData *JSON = [RKMIMETypeSerialization dataFromObject:params MIMEType:RKMIMETypeJSON error:nil];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
assertThat(error, is(nilValue()));
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
assertThat(JSON, is(equalTo(@"{\"name\":\"Blake Watters\",\"friends\":[{\"state\":null,\"city\":\"Carrboro\"},{\"state\":null,\"city\":\"New York City\"}]}")));
#else
assertThat(JSON, is(equalTo(@"{\"name\":\"Blake Watters\",\"friends\":[{\"city\":\"Carrboro\",\"state\":null},{\"city\":\"New York City\",\"state\":null}]}")));
#endif
assertThat(jsonDictionary, is(equalTo(params)));
}

- (void)testShouldSerializeManagedHasManyRelationshipsToJSON
Expand Down