Skip to content

Commit

Permalink
Merge pull request #1538 from mrcarlberg/cappuccino
Browse files Browse the repository at this point in the history
---

Dont 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]);
  • Loading branch information
aljungberg committed May 14, 2012
1 parent 3525bd0 commit bc370a2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
16 changes: 7 additions & 9 deletions Foundation/CPArray/CPArray.j
Expand Up @@ -799,22 +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],
objectDescription = object && object.isa ? [object description] : String(object);

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 + ')';
Expand Down
15 changes: 14 additions & 1 deletion Foundation/CPDictionary.j
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Foundation/CPObject.j
Expand Up @@ -529,3 +529,23 @@ CPLog(@"Got some class: %@", inst);
}

@end

function CPDescriptionOfObject(anObject)
{
if (anObject.isa)
return [anObject description];

if (typeof(anObject) !== "object")
return String(anObject);

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");
}
12 changes: 6 additions & 6 deletions Objective-J/CFDictionary.js
Expand Up @@ -136,17 +136,17 @@ DISPLAY_NAME(CFDictionary.prototype.valueForKey);
CFDictionary.prototype.toString = function()
{
var string = "{\n",
keys = this._keys,
index = 0,
count = this._count;

keys = this._keys,
index = 0,
count = this._count;
for (; index < count; ++index)
{
var key = keys[index];

string += "\t" + key + " = \"" + String(this.valueForKey(key)).split('\n').join("\n\t") + "\"\n";
}

return string + "}";
};

Expand Down
12 changes: 12 additions & 0 deletions Tests/Foundation/CPArrayTest.j
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions Tests/Foundation/CPDictionaryTest.j
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Tests/Foundation/CPMutableArrayTest.j
Expand Up @@ -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
Expand Down

0 comments on commit bc370a2

Please sign in to comment.