Skip to content

Commit

Permalink
Test/Fixes for -description.
Browse files Browse the repository at this point in the history
- Hook the -description for the base array object it doesn't
  throw.
- Add tests so it doesn't regress.
  • Loading branch information
thomasvl committed Nov 8, 2018
1 parent 3a6bd5c commit 3bb8e4e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Source/Objects/GTLRObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down Expand Up @@ -473,6 +473,7 @@ - (NSString *)JSONDescription {
[descStr appendFormat:@"%@%@%@:%@", spacer, key, qmark, value];
spacer = @" ";
}
[descStr appendString:@"}"];
return descStr;
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(
Expand Down
54 changes: 54 additions & 0 deletions Source/Tests/GTLRObjectTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3bb8e4e

Please sign in to comment.