Merge branch 'refs/heads/slevenbits-nsvalue-nib2cib'

This commit is contained in:
Alexander Ljungberg committed 2013-06-17 00:54:05 +01:00
commit 093ac8cf54
6 files changed
+146 -2

No files matched your search

+6
View File
@@ -187,6 +187,12 @@ var CPObjectAccessorsForClassKey = @"$CPObjectAccessorsForClassKey",
- (void)setValue:(id)aValue forKey:(CPString)aKey
{
// setValue:forKey: should unwrap CPValue by default. In Objective-C we would need to care about which type
// the setter takes (or target ivar) and send [aValue rectValue], [aValue pointValue] etc, but in
// Objective-C we can use them interchangably.
if (aValue && aValue.isa && [aValue isKindOfClass:CPValue])
aValue = [aValue JSObject];
var theClass = [self class],
modifier = nil,
modifiers = theClass[CPObjectModifiersForClassKey];
+1 -2
View File
@@ -112,7 +112,6 @@ function CGStringFromRect(aRect)
return "{" + CGStringFromPoint(aRect.origin) + ", " + CGStringFromSize(aRect.size) + "}";
}
function CGRectOffset(aRect, dX, dY)
{
return { origin:{ x:aRect.origin.x + dX, y:aRect.origin.y + dY }, size:{ width:aRect.size.width, height:aRect.size.height } };
@@ -424,7 +423,7 @@ function CGRectFromString(aString)
{
var comma = aString.indexOf(',', aString.indexOf(',') + 1);
return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) };
return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length - 1)) };
}
function CGPointFromEvent(anEvent)
+34
View File
@@ -0,0 +1,34 @@
@import <AppKit/CGGeometry.j>
@implementation CGGeometryTest : OJTestCase
- (void)testCGStringFromPoint
{
[self assert:@"{0, 0}" equals:CGStringFromPoint(CGPointMakeZero())];
[self assert:@"{123, 234}" equals:CGStringFromPoint(CGPointMake(123, 234))];
[self assert:@"{-123.45, -234}" equals:CGStringFromPoint(CGPointMake(-123.45, -234.))];
}
- (void)testCGStringFromSize
{
[self assert:@"{0, 0}" equals:CGStringFromSize(CGSizeMakeZero())];
[self assert:@"{123, 234}" equals:CGStringFromSize(CGSizeMake(123, 234))];
[self assert:@"{-123.45, -234}" equals:CGStringFromSize(CGSizeMake(-123.45, -234.))];
}
- (void)testCGStringFromRect
{
[self assert:@"{{0, 0}, {0, 0}}" equals:CGStringFromRect(CGRectMakeZero())];
[self assert:@"{{123, 234}, {345, 456}}" equals:CGStringFromRect(CGRectMake(123, 234, 345, 456))];
[self assert:@"{{-123.45, -234}, {345.5, 456}}" equals:CGStringFromRect(CGRectMake(-123.45, -234, 345.5, 456.))];
}
- (void)testCGRectFromString
{
[self assertTrue:CGRectEqualToRect(CGRectMakeZero(), CGRectFromString(@"{{0, 0}, {0, 0}}"))];
[self assertTrue:CGRectEqualToRect(CGRectMake(123, 234, 345, 456), CGRectFromString(@"{{123, 234}, {345, 456}}"))];
[self assertTrue:CGRectEqualToRect(CGRectMake(-123.45, -234, 345.5, 456), CGRectFromString(@"{{-123.45, -234}, {345.5, 456}}"))];
}
@end
+45
View File
@@ -489,6 +489,51 @@ var accessIVARS = YES;
@end
// CPValue unwrapping (AKA Wrapping and Unwrapping Structs)
@implementation CPKeyValueCodingTest (CPValueUnwrapping)
- (void)testIfCPValueIsUnwrapped
{
[KVCTestClass setAccessInstanceVariablesDirectly: YES];
var values = @[
[CPValue valueWithJSObject:CGPointMake(200, -100)],
[CPValue valueWithJSObject:CGSizeMake(100, 100)],
[CPValue valueWithJSObject:CGRectMake(100, 100, 50, 150)],
];
for (var i = 0; i < values.length; i++)
{
var value = values[i];
[kvcTestObject setValue:value forKey:"privatePropertyWithoutAccessors"];
[kvcTestObject setValue:value forKey:"publicPropertyWithoutAccessors"];
[kvcTestObject setValue:value forKey:"privateBoolPropertyWithoutAccessors"];
[kvcTestObject setValue:value forKey:"publicBoolPropertyWithoutAccessors"];
[kvcTestObject setValue:value forKey:"propertyWithPublicAccessor"];
[kvcTestObject setValue:value forKey:"propertyWithPrivateAccessor"];
var allKeys = ["privatePropertyWithoutAccessors","publicPropertyWithoutAccessors",
"privateBoolPropertyWithoutAccessors","publicBoolPropertyWithoutAccessors",
"propertyWithPublicAccessor", "propertyWithPrivateAccessor"
],
dictForKeys = [kvcTestObject dictionaryWithValuesForKeys:allKeys],
key,
readBackValue,
keyEnumerator = [dictForKeys keyEnumerator];
while ((key = [keyEnumerator nextObject]) !== nil)
{
readBackValue = [dictForKeys objectForKey:key];
[self assertFalse:readBackValue.isa message:"Expected to read back an unwrapped value, not " + readBackValue + "."];
[self assert:JSON.stringify([value JSObject]) equals:JSON.stringify(readBackValue)];
}
}
}
@end
@implementation Employee2 : CPObject
{
CPString _name @accessors(property=name);
+1
View File
@@ -32,3 +32,4 @@
@import "NSNull.j"
@import "NSNumberFormatter.j"
@import "NSSet.j"
@import "NSValue.j"
+59
View File
@@ -0,0 +1,59 @@
/*
* NSValue.j
* nib2cib
*
* Created by Alexander Ljungberg.
* Copyright 2013, SlevenBits Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/CPException.j>
@import <Foundation/CPKeyedUnarchiver.j>
@import <Foundation/CPObject.j>
@import <Foundation/CPRange.j>
@import <Foundation/CPValue.j>
@import <AppKit/CGGeometry.j>
var NSPointNSValueType = 1,
NSSizeNSValueType = 2,
NSRectNSValueType = 3,
NSRangeNSValueType = 4;
@implementation NSValue : CPObject
{
}
- (id)initWithCoder:(CPCoder)aCoder
{
var type = [aCoder decodeIntForKey:@"NS.special"];
switch (type)
{
case NSPointNSValueType:
return [[CPValue alloc] initWithJSObject:CGPointFromString([aCoder decodeObjectForKey:@"NS.pointval"])];
case NSSizeNSValueType:
return [[CPValue alloc] initWithJSObject:CGSizeFromString([aCoder decodeObjectForKey:@"NS.sizeval"])];
case NSRectNSValueType:
return [[CPValue alloc] initWithJSObject:CGRectFromString([aCoder decodeObjectForKey:@"NS.rectval"])];
case NSRangeNSValueType:
return [[CPValue alloc] initWithJSObject:CPMakeRange([aCoder decodeIntForKey:@"NS.rangeval.location"], [aCoder decodeIntForKey:@"NS.rangeval.length"])];
default:
[CPException raise:CPInvalidUnarchiveOperationException format:@"NSValue type %d is not supported by nib2cib.", type];
}
}
@end