From 70182a0ea2f38839c554bbb66daf2fbc4fb31632 Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Fri, 11 May 2012 16:48:56 +0200 Subject: [PATCH 1/4] Description methods in CPArray and CPDictionary will display JavaScript object attributes instead of displaying [Object object] Don't you hate when your logs of arrays and dictionaries display [Object object] when you have JavaScript objects in them. Now with this patch all JavaScript objects will be display with all attributes in a nice good looking way. Log CGRect, CGPoint and other JavaScript objects withour any hassle anymore. For example: console.log([myRect]); --- Foundation/CPArray/CPArray.j | 13 ++++++++++++- Objective-J/CFDictionary.js | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Foundation/CPArray/CPArray.j b/Foundation/CPArray/CPArray.j index 78c462aae..a4bf453fd 100755 --- a/Foundation/CPArray/CPArray.j +++ b/Foundation/CPArray/CPArray.j @@ -807,7 +807,18 @@ var concat = Array.prototype.concat, description += '\n'; var object = [self objectAtIndex:index], - objectDescription = object && object.isa ? [object description] : String(object); + desc = object; + if (object && typeof(object) === "object" && !object.isa) + { + desc = "JSObject\n{\n"; + for (var property in object) + { + if (object.hasOwnProperty(property)) + desc += " " + property + ":" + object[property] + "\n"; + } + desc += "}"; + } + var objectDescription = object && object.isa ? [object description] : String(desc); description += "\t" + objectDescription.split('\n').join("\n\t"); diff --git a/Objective-J/CFDictionary.js b/Objective-J/CFDictionary.js index 99fd02fb4..33a9d617d 100644 --- a/Objective-J/CFDictionary.js +++ b/Objective-J/CFDictionary.js @@ -142,9 +142,21 @@ CFDictionary.prototype.toString = function() for (; index < count; ++index) { - var key = keys[index]; + var key = keys[index], + value = this.valueForKey(key), + description = value; + if (value && typeof(value) === "object" && !value.isa) + { + description = "JSObject\n{\n"; + for (var property in value) + { + if (value.hasOwnProperty(property)) + description += " " + property + ":" + value[property] + "\n"; + } + description += "}"; + } - string += "\t" + key + " = \"" + String(this.valueForKey(key)).split('\n').join("\n\t") + "\"\n"; + string += "\t" + key + " = \"" + String(description).split('\n').join("\n\t") + "\"\n"; } return string + "}"; From 4af27541d0b641c0dd269957b7b99f4c4dfae010 Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Fri, 11 May 2012 23:33:37 +0200 Subject: [PATCH 2/4] Clean-up --- Foundation/CPArray/CPArray.j | 27 +++++++-------------------- Foundation/CPObject.j | 23 +++++++++++++++++++++++ Objective-J/CFDictionary.js | 17 +++-------------- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Foundation/CPArray/CPArray.j b/Foundation/CPArray/CPArray.j index a4bf453fd..b423ffeb7 100755 --- a/Foundation/CPArray/CPArray.j +++ b/Foundation/CPArray/CPArray.j @@ -799,33 +799,20 @@ var concat = Array.prototype.concat, { var index = 0, count = [self count], - description = '('; + description = "("; for (; index < count; ++index) { if (index === 0) - description += '\n'; + description += "\n\t"; - var object = [self objectAtIndex:index], - desc = object; - if (object && typeof(object) === "object" && !object.isa) - { - desc = "JSObject\n{\n"; - for (var property in object) - { - if (object.hasOwnProperty(property)) - desc += " " + property + ":" + object[property] + "\n"; - } - desc += "}"; - } - var objectDescription = object && object.isa ? [object description] : String(desc); - - description += "\t" + objectDescription.split('\n').join("\n\t"); + var object = [self objectAtIndex:index]; + description += CPDescriptionOfObject(object); if (index !== count - 1) - description += ", "; - - description += '\n'; + description += ",\n\t"; + else + description += "\n"; } return description + ')'; diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index c840c63ff..c9e4ad99d 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -529,3 +529,26 @@ CPLog(@"Got some class: %@", inst); } @end + +function CPDescriptionOfObject(anObject) +{ + var desc = ""; + + if (anObject.isa) + desc = [anObject description]; + else if (typeof(anObject) !== "object") + desc = String(anObject); + else + { + desc = "JSObject\n{\n"; + + for (var property in anObject) + { + if (anObject.hasOwnProperty(property)) + desc += " " + property + ":" + CPDescriptionOfObject(anObject[property]) + "\n"; + } + desc += "}"; + } + + return desc.split('\n').join("\n\t"); +} diff --git a/Objective-J/CFDictionary.js b/Objective-J/CFDictionary.js index 33a9d617d..3b5586f5d 100644 --- a/Objective-J/CFDictionary.js +++ b/Objective-J/CFDictionary.js @@ -135,7 +135,7 @@ DISPLAY_NAME(CFDictionary.prototype.valueForKey); CFDictionary.prototype.toString = function() { - var string = "{\n", + var string = "{\n\t", keys = this._keys, index = 0, count = this._count; @@ -143,20 +143,9 @@ CFDictionary.prototype.toString = function() for (; index < count; ++index) { var key = keys[index], - value = this.valueForKey(key), - description = value; - if (value && typeof(value) === "object" && !value.isa) - { - description = "JSObject\n{\n"; - for (var property in value) - { - if (value.hasOwnProperty(property)) - description += " " + property + ":" + value[property] + "\n"; - } - description += "}"; - } + value = this.valueForKey(key); - string += "\t" + key + " = \"" + String(description).split('\n').join("\n\t") + "\"\n"; + string += key + " = \"" + CPDescriptionOfObject(value).split('\n').join("\n\t") + "\"\n\t"; } return string + "}"; From 327debe3a84564fdad98f423b1b547fc1151b5be Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Fri, 11 May 2012 23:39:41 +0200 Subject: [PATCH 3/4] Some more clean-up --- Foundation/CPObject.j | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index c9e4ad99d..6e7960090 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -532,23 +532,20 @@ CPLog(@"Got some class: %@", inst); function CPDescriptionOfObject(anObject) { - var desc = ""; - if (anObject.isa) - desc = [anObject description]; - else if (typeof(anObject) !== "object") - desc = String(anObject); - else + return [anObject description]; + + if (typeof(anObject) !== "object") + return String(anObject); + + desc = "JSObject\n{\n"; + + for (var property in anObject) { - desc = "JSObject\n{\n"; - - for (var property in anObject) - { - if (anObject.hasOwnProperty(property)) - desc += " " + property + ":" + CPDescriptionOfObject(anObject[property]) + "\n"; - } - desc += "}"; + if (anObject.hasOwnProperty(property)) + desc += " " + property + ":" + CPDescriptionOfObject(anObject[property]) + "\n"; } + desc += "}"; return desc.split('\n').join("\n\t"); } From 0abd9855c2ed9dfe2c2148b058f3229555e49922 Mon Sep 17 00:00:00 2001 From: Martin Carlberg Date: Mon, 14 May 2012 10:14:05 +0200 Subject: [PATCH 4/4] Moved new description functionallity from CFDictionary to CPDictionary. Added testcase for both CPDictionary and CPArray. Fixed failing testcase. Checked all new code with capp_lint for correct formatting. --- Foundation/CPDictionary.j | 15 ++++++++++++++- Foundation/CPObject.j | 4 ++-- Objective-J/CFDictionary.js | 19 +++++++++---------- Tests/Foundation/CPArrayTest.j | 12 ++++++++++++ Tests/Foundation/CPDictionaryTest.j | 13 +++++++++++++ Tests/Foundation/CPMutableArrayTest.j | 4 ++-- 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Foundation/CPDictionary.j b/Foundation/CPDictionary.j index caea40a50..17e3d14bb 100755 --- a/Foundation/CPDictionary.j +++ b/Foundation/CPDictionary.j @@ -576,7 +576,20 @@ */ - (CPString)description { - return self.toString(); + var string = "{\n\t", + keys = _keys, + index = 0, + count = _count; + + for (; index < count; ++index) + { + var key = keys[index], + value = valueForKey(key); + + string += key + " = \"" + CPDescriptionOfObject(value).split('\n').join("\n\t") + "\"\n\t"; + } + + return string + "}"; } - (BOOL)containsKey:(id)aKey diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index 6e7960090..6703ace87 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -543,9 +543,9 @@ function CPDescriptionOfObject(anObject) for (var property in anObject) { if (anObject.hasOwnProperty(property)) - desc += " " + property + ":" + CPDescriptionOfObject(anObject[property]) + "\n"; + desc += " " + property + ": " + CPDescriptionOfObject(anObject[property]) + "\n"; } desc += "}"; - + return desc.split('\n').join("\n\t"); } diff --git a/Objective-J/CFDictionary.js b/Objective-J/CFDictionary.js index 3b5586f5d..8336920ec 100644 --- a/Objective-J/CFDictionary.js +++ b/Objective-J/CFDictionary.js @@ -135,19 +135,18 @@ DISPLAY_NAME(CFDictionary.prototype.valueForKey); CFDictionary.prototype.toString = function() { - var string = "{\n\t", - keys = this._keys, - index = 0, - count = this._count; - + var string = "{\n", + keys = this._keys, + index = 0, + count = this._count; + for (; index < count; ++index) { - var key = keys[index], - value = this.valueForKey(key); - - string += key + " = \"" + CPDescriptionOfObject(value).split('\n').join("\n\t") + "\"\n\t"; + var key = keys[index]; + + string += "\t" + key + " = \"" + String(this.valueForKey(key)).split('\n').join("\n\t") + "\"\n"; } - + return string + "}"; }; diff --git a/Tests/Foundation/CPArrayTest.j b/Tests/Foundation/CPArrayTest.j index e47d73e49..75951c471 100644 --- a/Tests/Foundation/CPArrayTest.j +++ b/Tests/Foundation/CPArrayTest.j @@ -526,6 +526,18 @@ [self assert:input1[1] equals:[output valueForKey:"1"] message:@"output[0]"]; } +- (void)testJSObjectDescription +{ + var array = [CGRectMake(1, 2, 3, 4), CGPointMake(5, 6)], + d = [array description]; + + [self assertTrue:d.indexOf("x: 1") !== -1 message:"Can't find 'x: 1' in description of array " + d]; + [self assertTrue:d.indexOf("y: 2") !== -1 message:"Can't find 'y: 2' in description of array " + d]; + [self assertTrue:d.indexOf("width: 3") !== -1 message:"Can't find 'width: 3' in description of array " + d]; + [self assertTrue:d.indexOf("height: 4") !== -1 message:"Can't find 'height: 4' in description of array " + d]; + [self assertTrue:d.indexOf("x: 5") !== -1 message:"Can't find 'x: 5' in description of array " + d]; + [self assertTrue:d.indexOf("y: 6") !== -1 message:"Can't find 'y: 6' in description of array " + d]; +} @end diff --git a/Tests/Foundation/CPDictionaryTest.j b/Tests/Foundation/CPDictionaryTest.j index d2e0245b5..82a06fe73 100644 --- a/Tests/Foundation/CPDictionaryTest.j +++ b/Tests/Foundation/CPDictionaryTest.j @@ -237,4 +237,17 @@ [self assert:expected equals:result]; } +- (void)testJSObjectDescription +{ + var dict = [[CPDictionary alloc] initWithObjects:[CGRectMake(1, 2, 3, 4), CGPointMake(5, 6)] forKeys:[@"key1", @"key2"]], + d = [dict description]; + + [self assertTrue:d.indexOf("x: 1") !== -1 message:"Can't find 'x: 1' in description of dictionary " + d]; + [self assertTrue:d.indexOf("y: 2") !== -1 message:"Can't find 'y: 2' in description of dictionary " + d]; + [self assertTrue:d.indexOf("width: 3") !== -1 message:"Can't find 'width: 3' in description of dictionary " + d]; + [self assertTrue:d.indexOf("height: 4") !== -1 message:"Can't find 'height: 4' in description of dictionary " + d]; + [self assertTrue:d.indexOf("x: 5") !== -1 message:"Can't find 'x: 5' in description of dictionary " + d]; + [self assertTrue:d.indexOf("y: 6") !== -1 message:"Can't find 'y: 6' in description of dictionary " + d]; +} + @end diff --git a/Tests/Foundation/CPMutableArrayTest.j b/Tests/Foundation/CPMutableArrayTest.j index 8cb356633..cd88314fc 100644 --- a/Tests/Foundation/CPMutableArrayTest.j +++ b/Tests/Foundation/CPMutableArrayTest.j @@ -380,11 +380,11 @@ [pretty sortUsingDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"value" ascending:NO]]]; - [self assert:"(\n\t3:d, \n\t2:c, \n\t1:b, \n\t0:a\n)" equals:[pretty description]]; + [self assert:"(\n\t3:d,\n\t2:c,\n\t1:b,\n\t0:a\n)" equals:[pretty description]]; [pretty sortUsingDescriptors:[[[CPSortDescriptor alloc] initWithKey:@"value" ascending:YES]]]; - [self assert:"(\n\t0:a, \n\t1:b, \n\t2:c, \n\t3:d\n)" equals:[pretty description]] + [self assert:"(\n\t0:a,\n\t1:b,\n\t2:c,\n\t3:d\n)" equals:[pretty description]] } - (void)testThatCPArrayDoesSortUsingTwoDescriptors