diff --git a/Source/Objects/GTLRObject.m b/Source/Objects/GTLRObject.m index 2bdafc93e..a976b413d 100644 --- a/Source/Objects/GTLRObject.m +++ b/Source/Objects/GTLRObject.m @@ -432,7 +432,7 @@ + (NSArray *)allKnownKeys { - (NSString *)description { NSString *jsonDesc = [self JSONDescription]; - NSString *str = [NSString stringWithFormat:@"%@ %p: {%@}", + NSString *str = [NSString stringWithFormat:@"%@ %p: %@", [self class], self, jsonDesc]; return str; } @@ -442,7 +442,7 @@ - (NSString *)JSONDescription { // Find the list of declared and otherwise known JSON keys for this class. NSArray *knownKeys = [[self class] allKnownKeys]; - NSMutableString *descStr = [NSMutableString string]; + NSMutableString *descStr = [NSMutableString stringWithString:@"{"]; NSString *spacer = @""; for (NSString *key in _json) { @@ -473,6 +473,7 @@ - (NSString *)JSONDescription { [descStr appendFormat:@"%@%@%@:%@", spacer, key, qmark, value]; spacer = @" "; } + [descStr appendString:@"}"]; return descStr; } @@ -601,7 +602,7 @@ @implementation GTLRDataObject - (NSString *)description { NSString *jsonDesc = @""; if (self.JSON.count > 0) { - jsonDesc = [NSString stringWithFormat:@"{%@}", [self JSONDescription]]; + jsonDesc = [self JSONDescription]; } return [NSString stringWithFormat:@"%@ %p: %tu bytes, contentType:%@ %@", [self class], self, self.data.length, self.contentType, jsonDesc]; @@ -651,6 +652,11 @@ - (NSArray *)itemsWithItemClass:(Class)itemClass { return result; } +- (NSString *)JSONDescription { + // Just like GTLRObject's handing of arrays, just return the count. + return [NSString stringWithFormat:@"[%tu]", self.JSON.count]; +} + @end Class GTLRObjectResolveClass( diff --git a/Source/Tests/GTLRObjectTest.m b/Source/Tests/GTLRObjectTest.m index d531e88a7..084eb716d 100644 --- a/Source/Tests/GTLRObjectTest.m +++ b/Source/Tests/GTLRObjectTest.m @@ -23,6 +23,10 @@ #import "GTLRDuration.h" #import "GTLRErrorObject.h" +@interface GTLRObject (ExposedForTesting) +- (NSString *)JSONDescription; +@end + // Custom subclass for testing the property handling. @class GTLRTestingObject; @interface GTLRTestingObject : GTLRObject @@ -1694,4 +1698,54 @@ - (void)testBatchResultCoding { XCTAssertEqualObjects(result.responseHeaders, result2.responseHeaders); } +#pragma mark NSObject description + +- (void)testObjectDescription { + // -description uses the internal -JSONDescription; we test that since + // it won't include instance pointer values. + + GTLRTestingObject *obj = [GTLRTestingObject object]; + obj.aStr = @"a string"; + obj.aNum = @123; + obj.aBool = @YES; + obj.arrayNumber = @[ @1, @2, @3 ]; + obj.JSON[@"unknown"] = @"something"; + + GTLRTestingObject *obj2 = [GTLRTestingObject object]; + obj2.aStr = @"kid"; + obj2.JSON[@"un"] = @"value"; + + obj.child = obj2; + + XCTAssertEqualObjects([obj JSONDescription], + @"{a.num:123 a_bool:1 a_str:\"a string\" arrayNumber:[3] unknown?:\"something\" child:{a_str,un}}"); + XCTAssertEqualObjects([obj2 JSONDescription], + @"{a_str:\"kid\" un?:\"value\"}"); + + // Test the special case wrapper for arrays: of Object + + NSString * const jsonStr = @"[ {\"a_str\":\"obj 1\"}, {\"a_str\":\"obj 2\"} ]"; + NSError *err = nil; + NSMutableDictionary *json = [self objectWithString:jsonStr error:&err]; + XCTAssertNil(err); + XCTAssertNotNil(json); + + GTLRTestingResultArray *arrayResult = [GTLRTestingResultArray objectWithJSON:json]; + XCTAssertNotNil(arrayResult); + + XCTAssertEqualObjects([arrayResult JSONDescription], @"[2]"); + + // Test the special case wrapper for arrays: of Object + + NSString * const jsonStr2 = @"[ \"str 1\", \"str 2\" ]"; + json = [self objectWithString:jsonStr2 error:&err]; + XCTAssertNil(err); + XCTAssertNotNil(json); + + GTLRTestingResultArray2 *arrayResult2 = [GTLRTestingResultArray2 objectWithJSON:json]; + XCTAssertNotNil(arrayResult2); + + XCTAssertEqualObjects([arrayResult2 JSONDescription], @"[2]"); +} + @end