From 4c58c602ff9eadeaf85fabb00eeb7577e5f32f9c Mon Sep 17 00:00:00 2001 From: Daniel Stolzenberg Date: Tue, 26 Oct 2010 17:09:12 +0200 Subject: [PATCH] implemented Tests for CPKeyValueCoding "valueForKey:" --- Tests/Foundation/CPKeyValueCodingTest.j | 193 +++++++++++++++++++++++- 1 file changed, 189 insertions(+), 4 deletions(-) diff --git a/Tests/Foundation/CPKeyValueCodingTest.j b/Tests/Foundation/CPKeyValueCodingTest.j index f1600cf74..c098b1bda 100644 --- a/Tests/Foundation/CPKeyValueCodingTest.j +++ b/Tests/Foundation/CPKeyValueCodingTest.j @@ -2,8 +2,8 @@ * CPKeyValueCodingTest.j * Foundation * - * Created by Alexander Ljungberg. - * Copyright 2010, WireLoad, LLC. + * Created by Daniel Stolzenberg + * Copyright 2010, University of Rostock * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,14 +20,199 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +@import + +var accessIVARS = YES; + +@implementation KVCTestClass : CPObject +{ + id _privatePropertyWithoutAccessors; + id publicPropertyWithoutAccessors; + id _isPrivateBoolPropertyWithoutAccessors; + id isPublicBoolPropertyWithoutAccessors; + + id ___propertyWithPublicGetAccessor @accessors(getter=getPropertyWithPublicGetAccessor); + id ___propertyWithPublicAccessor @accessors(getter=propertyWithPublicAccessor); + id ___propertyWithPublicBoolAccessor @accessors(getter=isPropertyWithPublicBoolAccessor); + id ___propertyWithPrivateGetAccessor @accessors(getter=_getPropertyWithPrivateGetAccessor); + id ___propertyWithPrivateAccessor @accessors(getter=_propertyWithPrivateAccessor); + id ___propertyWithPrivateBoolAccessor @accessors(getter=_isPropertyWithPrivateBoolAccessor); +} + ++ (BOOL)accessInstanceVariablesDirectly +{ + return accessIVARS; +} + ++ (void)setAccessInstanceVariablesDirectly:(BOOL)accessDirectly +{ + accessIVARS = accessDirectly; +} + +@end + + @implementation CPKeyValueCodingTest : OJTestCase +{ + id kvcTestObject; +} + +- (void)setUp +{ + //do not allow direct access to assure that accessor are used by default + [KVCTestClass setAccessInstanceVariablesDirectly: NO]; + kvcTestObject = [[KVCTestClass alloc] init]; +} + +- (void)tearDown { } -- (void)testCPNull +@end + +// "valueForKey:" + +@implementation CPKeyValueCodingTest (CPNullTest) + +- (void)testIfCPNullReturnsCPNullForAllKeys { var nullObject = [CPNull null]; [self assert:[CPNull null] equals:[nullObject valueForKey:@"a"] message:@"CPNull valueForKey:X returns nil"]; } -@end \ No newline at end of file +@end + +@implementation CPKeyValueCodingTest (AccessValueForUndefinedKey) + +- (void)testIfExceptionIsThrownForUndefinedKey +{ + [self assertThrows:function(){[kvcTestObject valueForKey:@"anUndefinedKey"];}]; +} + +@end + +@implementation CPKeyValueCodingTest (AccessInstanceVariablesDirectly) + +- (void)testIfPrivateInstanceVariableCanDirectlyBeAccessedWhenAllowedByClassMethod +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"privatePropertyWithoutAccessors"];}]; +} + +- (void)testIfPublicInstanceVariableCanDirectlyBeAccessedWhenAllowedByClassMethod +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"publicPropertyWithoutAccessors"];}]; +} + +- (void)testIfBooleanPrivateInstanceVariableCanDirectlyBeAccessedWhenAllowedByClassMethod +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"privateBoolPropertyWithoutAccessors"];}]; +} + +- (void)testIfBooleanPublicInstanceVariableCanDirectlyBeAccessedWhenAllowedByClassMethod +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"publicBoolPropertyWithoutAccessors"];}]; +} + +@end + +@implementation CPKeyValueCodingTest (DoNotAccessInstanceVariables) + +- (void)testIfPrivateInstanceVariableCanNotDirectlyBeAccessedWhenProhibitedByClassMethod +{ + [self assertThrows:function(){[kvcTestObject valueForKey:@"privatePropertyWithoutAccessors"];}]; +} + +- (void)testIfPublicInstanceVariableCanNotDirectlyBeAccessedWhenProhibitedByClassMethod +{ + [self assertThrows:function(){[kvcTestObject valueForKey:@"publicPropertyWithoutAccessors"];}]; +} + +- (void)testIfBooleanPrivateInstanceVariableCanNotDirectlyBeAccessedWhenProhibitedByClassMethod +{ + [self assertThrows:function(){[kvcTestObject valueForKey:@"privateBoolPropertyWithoutAccessors"];}]; +} + +- (void)testIfBooleanPublicInstanceVariableCanNotDirectlyBeAccessedWhenProhibitedByClassMethod +{ + [self assertThrows:function(){[kvcTestObject valueForKey:@"publicBoolPropertyWithoutAccessors"];}]; +} + +@end + +@implementation CPKeyValueCodingTest (AccessorMethodPatterns) + +- (void)testIfPublicGetAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPublicGetAccessor"];}]; +} + +- (void)testIfPublicAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPublicAccessor"];}]; +} + +- (void)testIfPublicBoolAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPublicBoolAccessor"];}]; +} + +- (void)testIfPrivateGetAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPrivateGetAccessor"];}]; +} + +- (void)testIfPrivateAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPrivateAccessor"];}]; +} + +- (void)testIfPrivateBoolAccessorIsFound +{ + [self assertNoThrow:function(){[kvcTestObject valueForKey:@"propertyWithPrivateBoolAccessor"];}]; +} + +@end + +@implementation CPKeyValueCodingTest (DictionaryWithValuesForKeys) + +- (void)testIfDictionaryWithValuesForKeysDoesNotThrowsUndefinedKeyException +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + var allKeys = [ "privatePropertyWithoutAccessors","publicPropertyWithoutAccessors", + "privateBoolPropertyWithoutAccessors","publicBoolPropertyWithoutAccessors", + "propertyWithPublicGetAccessor", "propertyWithPublicAccessor","propertyWithPublicBoolAccessor", + "propertyWithPrivateGetAccessor", "propertyWithPrivateAccessor", "propertyWithPrivateBoolAccessor" + ]; + [self assertNoThrow:function(){[kvcTestObject dictionaryWithValuesForKeys: allKeys];}]; +} + +- (void)testIfDictionaryWithValuesForKeysDoesThrowUndefinedKeyExceptionBecauseOfProhibitedDirectInstanceVariableAccess +{ + var allKeys = [ "privatePropertyWithoutAccessors","publicPropertyWithoutAccessors", + "privateBoolPropertyWithoutAccessors","publicBoolPropertyWithoutAccessors", + "propertyWithPublicGetAccessor", "propertyWithPublicAccessor","propertyWithPublicBoolAccessor", + "propertyWithPrivateGetAccessor", "propertyWithPrivateAccessor", "propertyWithPrivateBoolAccessor" + ]; + [self assertThrows:function(){[kvcTestObject dictionaryWithValuesForKeys: allKeys];}]; +} + +- (void)testIfDictionaryWithValuesForKeysContainsValuesForEveryProperty +{ + [KVCTestClass setAccessInstanceVariablesDirectly: YES]; + var allKeys = [ "privatePropertyWithoutAccessors","publicPropertyWithoutAccessors", + "privateBoolPropertyWithoutAccessors","publicBoolPropertyWithoutAccessors", + "propertyWithPublicGetAccessor", "propertyWithPublicAccessor","propertyWithPublicBoolAccessor", + "propertyWithPrivateGetAccessor", "propertyWithPrivateAccessor", "propertyWithPrivateBoolAccessor" + ]; + var dictForKeys = [kvcTestObject dictionaryWithValuesForKeys: allKeys]; + + [self assert: [allKeys count] equals: [dictForKeys count]]; +} + +@end + +// "setValue: forKey:" \ No newline at end of file