Refs #1599. Format code.

This commit is contained in:
Alexander Ljungberg
2012-07-06 15:53:03 +01:00
parent b1774cdccf
commit cff29904d7
2 changed files with 165 additions and 166 deletions
+4 -4
View File
@@ -1093,7 +1093,7 @@ var kvoNewAndOld = CPKeyValueObservingOptionNew | CPKeyValueObservingOpti
var dotIndex = aKeyPath.indexOf('.');
if (dotIndex === CPNotFound)
[CPException raise:CPInvalidArgumentException reason:"Created _CPKVOForwardingObserver without compound key path: "+aKeyPath];
[CPException raise:CPInvalidArgumentException reason:"Created _CPKVOForwardingObserver without compound key path: " + aKeyPath];
_firstPart = aKeyPath.substring(0, dotIndex);
_secondPart = aKeyPath.substring(dotIndex + 1);
@@ -1115,13 +1115,13 @@ var kvoNewAndOld = CPKeyValueObservingOptionNew | CPKeyValueObservingOpti
if (aKeyPath === _firstPart)
{
var oldValue = [_value valueForKeyPath:_secondPart],
newValue = [_object valueForKeyPath:_firstPart+"."+_secondPart],
newValue = [_object valueForKeyPath:_firstPart + "." + _secondPart],
pathChanges = [CPDictionary dictionaryWithObjectsAndKeys:
newValue ? newValue : [CPNull null], CPKeyValueChangeNewKey,
oldValue ? oldValue : [CPNull null], CPKeyValueChangeOldKey,
CPKeyValueChangeSetting, CPKeyValueChangeKindKey];
[_observer observeValueForKeyPath:_firstPart+"."+_secondPart ofObject:_object change:pathChanges context:_context];
[_observer observeValueForKeyPath:_firstPart + "." + _secondPart ofObject:_object change:pathChanges context:_context];
//since a has changed, we should remove ourselves as an observer of the old a, and observe the new one
if (_value)
@@ -1135,7 +1135,7 @@ var kvoNewAndOld = CPKeyValueObservingOptionNew | CPKeyValueObservingOpti
else
{
//a is the same, but a.b has changed -- nothing to do but forward this message along
[_observer observeValueForKeyPath:_firstPart+"."+aKeyPath ofObject:_object change:changes context:_context];
[_observer observeValueForKeyPath:_firstPart + "." + aKeyPath ofObject:_object change:changes context:_context];
}
}
+161 -162
View File
@@ -25,66 +25,66 @@
{
bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testAddObserver"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testAddObserver"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be 'set_bob', was: "+[bob valueForKey:@"name"]];
[self assertTrue: bob.name == @"set_bob" message: "bob.name should be 'set_bob', was: "+bob.name];
[self assertTrue: _sawObservation message:"Never received an observation"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message:@"valueForKey:'name' should be 'set_bob', was: " + [bob valueForKey:@"name"]];
[self assertTrue: bob.name == @"set_bob" message:@"bob.name should be 'set_bob', was: " + bob.name];
[self assertTrue: _sawObservation message:@"Never received an observation"];
}
- (void)testUnobservedKey
{
bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testUnobservedKey"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testUnobservedKey"];
[bob setValue:@"555" forKey:@"phoneNumber"];
[self assertTrue: [bob valueForKey:@"phoneNumber"] == @"555" message: "'phoneNumber' should be '555', was: "+[bob valueForKey:@"phoneNumber"]];
[self assertFalse: _sawObservation message:"Should not have recieved an observation"];
[self assertTrue: [bob valueForKey:@"phoneNumber"] == @"555" message:@"'phoneNumber' should be '555', was: " + [bob valueForKey:@"phoneNumber"]];
[self assertFalse: _sawObservation message:@"Should not have recieved an observation"];
}
- (void)testAddTwoObservers
{
bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testAddTwoObservers"];
[bob addObserver:[CPObject new] forKeyPath:@"name" options:nil context:"testAddTwoObservers"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testAddTwoObservers"];
[bob addObserver:[CPObject new] forKeyPath:@"name" options:nil context:@"testAddTwoObservers"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be bob, was: "+[bob valueForKey:@"name"]];
[self assertTrue: bob.name == @"set_bob" message: "bob.name should be 'bob', was: "+bob.name];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message:@"valueForKey:'name' should be bob, was: " + [bob valueForKey:@"name"]];
[self assertTrue: bob.name == @"set_bob" message:@"bob.name should be 'bob', was: " + bob.name];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testDirectIVarObservation
{
var bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"phoneNumber" options:nil context:"testDirectIVarObservation"];
[bob addObserver:self forKeyPath:@"phoneNumber" options:nil context:@"testDirectIVarObservation"];
[bob setValue:@"555" forKey:@"phoneNumber"];
[self assertTrue: [bob valueForKey:@"phoneNumber"] == @"555" message: "valueForKey:'phoneNumber' should be '555', was: "+[bob valueForKey:@"phoneNumber"]];
[self assertTrue: bob.phoneNumber == @"555" message: "bob.phoneNumber should be '555', was: "+bob.phoneNumber];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: [bob valueForKey:@"phoneNumber"] == @"555" message:@"valueForKey:'phoneNumber' should be '555', was: " + [bob valueForKey:@"phoneNumber"]];
[self assertTrue: bob.phoneNumber == @"555" message:@"bob.phoneNumber should be '555', was: " + bob.phoneNumber];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testRemoveObserver
{
bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testRemoveObserver"];
[bob addObserver:[CPString new] forKeyPath:@"name" options:nil context:"testRemoveObserver"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testRemoveObserver"];
[bob addObserver:[CPString new] forKeyPath:@"name" options:nil context:@"testRemoveObserver"];
[bob removeObserver:self forKeyPath:@"name"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be bob, was: "+[bob valueForKey:@"name"]];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message:@"valueForKey:'name' should be bob, was: " + [bob valueForKey:@"name"]];
}
- (void)testRemoveOtherObserver
@@ -92,15 +92,15 @@
bob = [[PersonTester alloc] init];
obj = [CPString new];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testRemoveOtherObserver"];
[bob addObserver:obj forKeyPath:@"name" options:nil context:"testRemoveOtherObserver"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testRemoveOtherObserver"];
[bob addObserver:obj forKeyPath:@"name" options:nil context:@"testRemoveOtherObserver"];
[bob removeObserver:obj forKeyPath:@"name"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be bob, was: "+[bob valueForKey:@"name"]];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message:@"valueForKey:'name' should be bob, was: " + [bob valueForKey:@"name"]];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testRemoveAllObservers
@@ -109,8 +109,8 @@
obj = [CPArray new];
obj2 = [CPString new];
[bob addObserver:obj forKeyPath:@"name" options:nil context:"testRemoveAllObservers"];
[bob addObserver:obj2 forKeyPath:@"name" options:nil context:"testRemoveAllObservers"];
[bob addObserver:obj forKeyPath:@"name" options:nil context:@"testRemoveAllObservers"];
[bob addObserver:obj2 forKeyPath:@"name" options:nil context:@"testRemoveAllObservers"];
[bob removeObserver:obj forKeyPath:@"name"];
[bob removeObserver:obj2 forKeyPath:@"name"];
@@ -118,8 +118,7 @@
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be bob, was: "+[bob valueForKey:@"name"]];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message:@"valueForKey:'name' should be bob, was: " + [bob valueForKey:@"name"]];
}
- (void)testDictionary
@@ -131,11 +130,11 @@
[dict addObserver:self
forKeyPath:@"dictionaryKey"
options:nil
context:"testDictionary"];
context:@"testDictionary"];
[dict setObject:@"Jo Bob Ray" forKey:@"dictionaryKey"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testPriorObservationOption
@@ -144,11 +143,11 @@
bob = [[PersonTester alloc] init];
[bob addObserver:self forKeyPath:@"name" options:CPKeyValueObservingOptionPrior context:"testPriorObservationOption"];
[bob addObserver:self forKeyPath:@"name" options:CPKeyValueObservingOptionPrior context:@"testPriorObservationOption"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: _sawPriorObservation message: "asked for CPKeyValueObservingOptionPrior but did not recieve corresponding notification"];
[self assertTrue: _sawPriorObservation message:@"asked for CPKeyValueObservingOptionPrior but did not recieve corresponding notification"];
}
- (void)testInitialObservationOption
@@ -159,12 +158,12 @@
bob.name = "paul";
[bob addObserver:self forKeyPath:@"name" options:CPKeyValueObservingOptionInitial context:"testInitialObservationOption"];
[bob addObserver:self forKeyPath:@"name" options:CPKeyValueObservingOptionInitial context:@"testInitialObservationOption"];
[bob removeObserver:self forKeyPath:@"name"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: _sawInitialObservation message: "asked for CPKeyValueObservingOptionInitial but did not recieve corresponding notification"];
[self assertTrue: _sawInitialObservation message:@"asked for CPKeyValueObservingOptionInitial but did not recieve corresponding notification"];
}
- (void)testDependentKeyObservation
@@ -175,13 +174,13 @@
bob.name = "paul";
[bob addObserver:self forKeyPath:@"bobName" options:0 context:"testDependentKeyObservation"];
[bob addObserver:self forKeyPath:@"twiceRemoved" options:0 context:"testDependentKeyObservation2"];
[bob addObserver:self forKeyPath:@"bobName" options:0 context:@"testDependentKeyObservation"];
[bob addObserver:self forKeyPath:@"twiceRemoved" options:0 context:@"testDependentKeyObservation2"];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue:_sawDependentObservation message:"asked for bobName but did not recieve corresponding notification"];
[self assertTrue: [bob valueForKey:@"bobName"] === @"BOB! set_bob" message: "should have been BOB! set_bob, was "+[bob valueForKey:@"bobName"]];
[self assertTrue:_sawDependentObservation message:@"asked for bobName but did not recieve corresponding notification"];
[self assertTrue: [bob valueForKey:@"bobName"] === @"BOB! set_bob" message:@"should have been BOB! set_bob, was " + [bob valueForKey:@"bobName"]];
}
- (void)testMultipartKey
@@ -191,12 +190,12 @@
[cs101 setTeacher:bob];
[cs101 addObserver:self forKeyPath:@"teacher.name" options:0 context:"testMultipartKey"];
[cs101 addObserver:self forKeyPath:@"teacher.name" options:0 context:@"testMultipartKey"];
[bob setName:@"bob"];
[self assertTrue:[cs101 valueForKeyPath:@"teacher.name"] == "set_bob" message:"teacher.name should be: set_bob, was: "+[cs101 valueForKeyPath:@"teacher.name"]];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue:[cs101 valueForKeyPath:@"teacher.name"] == "set_bob" message:@"teacher.name should be: set_bob, was: " + [cs101 valueForKeyPath:@"teacher.name"]];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testMultiPartKeysWhereValuesEvaluateToSelf
@@ -204,13 +203,13 @@
focus = [CarTester new];
bob = [PersonTester new];
[bob setValue:focus forKey:"car"];
[bob setValue:focus forKey:@"car"];
[bob addObserver:self forKeyPath:@"self.car.thisCar.model" options:0 context:"testMultiPartKeysWhereValuesEvaluateToSelf"];
[bob addObserver:self forKeyPath:@"self.car.thisCar.model" options:0 context:@"testMultiPartKeysWhereValuesEvaluateToSelf"];
[focus setValue:"ford focus" forKey:"model"];
[focus setValue:@"ford focus" forKey:@"model"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testThreePartKey
@@ -220,12 +219,12 @@
bob = [PersonTester new];
[cs101 setTeacher:bob];
[bob setValue:focus forKey:"car"];
[bob setValue:focus forKey:@"car"];
[cs101 addObserver:self forKeyPath:@"teacher.car.model" options:0 context:"testThreePartKey"];
[cs101 addObserver:self forKeyPath:@"teacher.car.model" options:0 context:@"testThreePartKey"];
[focus setValue:"ford focus" forKey:"model"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[focus setValue:@"ford focus" forKey:@"model"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testThreePartKeyPart2
@@ -235,13 +234,13 @@
bob = [PersonTester new];
[cs101 setTeacher:bob];
[focus setValue:"2000" forKey:"year"];
[focus setValue:@"2000" forKey:@"year"];
[cs101 addObserver:self forKeyPath:@"teacher.car.year" options:0 context:"testThreePartKeyPart2"];
[cs101 addObserver:self forKeyPath:@"teacher.car.year" options:0 context:@"testThreePartKeyPart2"];
[bob setValue:focus forKey:"car"];
[bob setValue:focus forKey:@"car"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testRemoveMultipartKey
@@ -251,13 +250,13 @@
[cs101 setTeacher:bob];
[cs101 addObserver:self forKeyPath:@"teacher.name" options:0 context:"testRemoveMultipartKey"];
[cs101 addObserver:self forKeyPath:@"teacher.name" options:0 context:@"testRemoveMultipartKey"];
[cs101 removeObserver:self forKeyPath:@"teacher.name"];
[bob setName:@"bob"];
[self assertFalse: _sawObservation message:"Should not have recieved an observation"];
[self assertFalse: _sawObservation message:@"Should not have recieved an observation"];
}
- (void)testRemoveThreePartKey
@@ -267,69 +266,69 @@
bob = [PersonTester new];
[cs101 setTeacher:bob];
[bob setValue:focus forKey:"car"];
[bob setValue:focus forKey:@"car"];
[cs101 addObserver:self forKeyPath:@"teacher.car.model" options:0 context:"testRemoveThreePartKey"];
[cs101 addObserver:self forKeyPath:@"teacher.car.model" options:0 context:@"testRemoveThreePartKey"];
[cs101 removeObserver:self forKeyPath:@"teacher.car.model"];
[focus setValue:"ford focus" forKey:"model"];
[focus setValue:@"ford focus" forKey:@"model"];
[self assertFalse: _sawObservation message:"Should not have recieved an observation"];
[self assertFalse: _sawObservation message:@"Should not have recieved an observation"];
}
- (void)testCrazyKeyPathChanges
{
var a = [A new];
[a setValue:[B new] forKeyPath:"b"];
[a setValue:[C new] forKeyPath:"b.c"];
[a setValue:[D new] forKeyPath:"b.c.d"];
[a setValue:[E new] forKeyPath:"b.c.d.e"];
[a setValue:[F new] forKeyPath:"b.c.d.e.f"];
[a setValue:[B new] forKeyPath:@"b"];
[a setValue:[C new] forKeyPath:@"b.c"];
[a setValue:[D new] forKeyPath:@"b.c.d"];
[a setValue:[E new] forKeyPath:@"b.c.d.e"];
[a setValue:[F new] forKeyPath:@"b.c.d.e.f"];
[a addObserver:self forKeyPath:"b.c.d.e.f" options:0 context:"testCrazyKeyPathChanges"];
[a addObserver:self forKeyPath:@"b.c.d.e.f" options:0 context:@"testCrazyKeyPathChanges"];
var newD = [D new];
[newD setValue:[E new] forKeyPath:@"e"];
[newD setValue:[F new] forKeyPath:@"e.f"];
[a setValue:newD forKeyPath:"b.c.d"];
[a setValue:newD forKeyPath:@"b.c.d"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testCrazyKeyPathChanges2
{
var a = [A new];
[a setValue:[B new] forKeyPath:"b"];
[a setValue:[C new] forKeyPath:"b.c"];
[a setValue:[D new] forKeyPath:"b.c.d"];
[a setValue:[E new] forKeyPath:"b.c.d.e"];
[a setValue:[F new] forKeyPath:"b.c.d.e.f"];
[a setValue:[B new] forKeyPath:@"b"];
[a setValue:[C new] forKeyPath:@"b.c"];
[a setValue:[D new] forKeyPath:@"b.c.d"];
[a setValue:[E new] forKeyPath:@"b.c.d.e"];
[a setValue:[F new] forKeyPath:@"b.c.d.e.f"];
[a addObserver:self forKeyPath:"b.c.d.e.f" options:0 context:"testCrazyKeyPathChanges2"];
[a addObserver:self forKeyPath:@"b.c.d.e.f" options:0 context:@"testCrazyKeyPathChanges2"];
[a setValue:nil forKeyPath:"b.c"];
[a setValue:nil forKeyPath:@"b.c"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testCrazyKeyPathChanges3
{
var a = [A new];
[a setValue:[B new] forKeyPath:"b"];
[a setValue:[C new] forKeyPath:"b.c"];
[a setValue:[D new] forKeyPath:"b.c.d"];
[a setValue:[E new] forKeyPath:"b.c.d.e"];
[a setValue:[F new] forKeyPath:"b.c.d.e.f"];
[a setValue:[B new] forKeyPath:@"b"];
[a setValue:[C new] forKeyPath:@"b.c"];
[a setValue:[D new] forKeyPath:@"b.c.d"];
[a setValue:[E new] forKeyPath:@"b.c.d.e"];
[a setValue:[F new] forKeyPath:@"b.c.d.e.f"];
[a addObserver:self forKeyPath:"b.c.d.e.f" options:0 context:"testCrazyKeyPathChanges3"];
[a addObserver:self forKeyPath:@"b.c.d.e.f" options:0 context:@"testCrazyKeyPathChanges3"];
[a setValue:7 forKeyPath:"b.c.d.e.f"];
[a setValue:7 forKeyPath:@"b.c.d.e.f"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testInsertIntoToManyProperty
@@ -338,11 +337,11 @@
[tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:"testInsertIntoToManyProperty"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:@"testInsertIntoToManyProperty"];
[tester insertObject:5 inManagedObjectsAtIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testRemoveFromToManyProperty
@@ -351,11 +350,11 @@
[tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:"testRemoveFromToManyProperty"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:@"testRemoveFromToManyProperty"];
[tester removeObjectFromManagedObjectsAtIndex:0];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testInsertIntoToManyPropertyIndirectly
@@ -364,11 +363,11 @@
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"tester.managedObjects" options:0 context:"testInsertIntoToManyPropertyIndirectly"];
[tester addObserver:self forKeyPath:@"tester.managedObjects" options:0 context:@"testInsertIntoToManyPropertyIndirectly"];
[tester.tester insertObject:5 inManagedObjectsAtIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testInsertIntoArrayPropertyIndirectly
@@ -378,11 +377,11 @@
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"subviews"];
[tester addObserver:self forKeyPath:@"tester.subviews" options:0 context:"testInsertIntoArrayPropertyIndirectly"];
[tester addObserver:self forKeyPath:@"tester.subviews" options:0 context:@"testInsertIntoArrayPropertyIndirectly"];
[tester.tester insertSubview:5 atIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testChangeTopLevelObject
@@ -392,46 +391,46 @@
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"subviews"];
[tester addObserver:self forKeyPath:@"tester.subviews" options:0 context:"testChangeTopLevelObject"];
[tester addObserver:self forKeyPath:@"tester.subviews" options:0 context:@"testChangeTopLevelObject"];
var newTesterTester = [ToManyTester new];
[newTesterTester setValue:[5, 6, 7, 8] forKey:@"subviews"];
[tester setValue:newTesterTester forKey:@"tester"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
[self assertTrue: _sawObservation message:@"Never recieved an observation"];
}
- (void)testPerformance
{
bob = [PersonTester new];
[bob setValue:"initial bob" forKey:"name"];
[bob setValue:@"initial bob" forKey:@"name"];
var startTime = new Date();
for (var i = 0; i < 1000; i++)
[bob setValue:i + "bob" forKey:"name"];
[bob setValue:i + @"bob" forKey:@"name"];
var total = new Date() - startTime;
[bob addObserver:[CPObject new] forKeyPath:"name" options:nil context:nil];
[bob addObserver:[CPObject new] forKeyPath:@"name" options:nil context:nil];
startTime = new Date();
for (var i = 0; i < 1000; i++)
[bob setValue: i + "bob" forKey:"name"];
[bob setValue: i + @"bob" forKey:@"name"];
var secondTotal = new Date() - startTime;
CPLog.info("runtime overhead was: "+(secondTotal/total)+" but test has been disabled for the moment");
//[self assertTrue: (secondTotal < total*4) message: "Overhead of one observer exceeded 400%. first: "+total+" second: "+secondTotal+" %"+FLOOR(secondTotal/total*100)];
//[self assertTrue: (secondTotal < total * 4) message:@"Overhead of one observer exceeded 400%. first: "+total+" second: "+secondTotal+" %"+FLOOR(secondTotal / total * 100)];
}
- (void)testDependentKeysPaths
{
var object = [[TestObject alloc] init];
[object addObserver:self forKeyPath:@"key" options:0 context:"testDependentKeysPaths"];
[object addObserver:self forKeyPath:@"key" options:0 context:@"testDependentKeysPaths"];
observationCount = 0;
@@ -447,19 +446,19 @@
var bob = [[PersonTester alloc] init],
betty = [CPObject new];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testSettersReplacedOnce"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testSettersReplacedOnce"];
var oldImp = class_getMethodImplementation(bob.isa, @selector(setName:));
[bob removeObserver:self forKeyPath:@"name"];
[bob addObserver:self forKeyPath:@"name" options:nil context:"testSettersReplacedOnce"];
[bob addObserver:self forKeyPath:@"name" options:nil context:@"testSettersReplacedOnce"];
var newImp = class_getMethodImplementation(bob.isa, @selector(setName:));
[self assertTrue:newImp === oldImp];
[bob addObserver:betty forKeyPath:@"name" options:nil context:"testSettersReplacedOnce"];
[bob addObserver:betty forKeyPath:@"name" options:nil context:@"testSettersReplacedOnce"];
var newImp = class_getMethodImplementation(bob.isa, @selector(setName:));
@@ -476,34 +475,34 @@
case "testDependentKeysPaths": ++observationCount;
break;
case "testAddObserver":
[self assertTrue: newValue == "set_bob" message: "newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message: "oldValue should be CPNull was: "+oldValue];
[self assertTrue: anObject == bob message: "anObject should be: "+[bob description]+", was: "+[anObject description]];
[self assertTrue: newValue == "set_bob" message:@"newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message:@"oldValue should be CPNull was: "+oldValue];
[self assertTrue: anObject == bob message:@"anObject should be: " + [bob description]+", was: " + [anObject description]];
break;
case "testUnobservedKey":
[self assertFalse: YES message: "not observing this key, should never get here"];
[self assertFalse: YES message:@"not observing this key, should never get here"];
break;
case "testAddTwoObservers":
[self assertTrue: newValue == "set_bob" message: "newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message: "oldValue should be CPNull was: "+oldValue];
[self assertTrue: newValue == "set_bob" message:@"newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message:@"oldValue should be CPNull was: "+oldValue];
break;
case "testDirectIVarObservation":
[self assertTrue: newValue == "555" message: "newValue should be: 555 was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message: "oldValue should be CPNull was: "+oldValue];
[self assertTrue: newValue == "555" message:@"newValue should be: 555 was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message:@"oldValue should be CPNull was: "+oldValue];
break;
case "testRemoveObserver":
[self assertTrue: NO message: "observer was removed, but notification was still received"];
[self assertTrue: NO message:@"observer was removed, but notification was still received"];
break;
case "testRemoveOtherObserver":
[self assertTrue: newValue == "set_bob" message: "newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message: "oldValue should be CPNull was: "+oldValue];
[self assertTrue: newValue == "set_bob" message:@"newValue should be: set_bob was: "+newValue];
[self assertTrue: oldValue == [CPNull null] message:@"oldValue should be CPNull was: "+oldValue];
break;
case "testRemoveAllObservers":
[self assertTrue: NO message: "all observers were removed, but notification was still received"];
[self assertTrue: NO message:@"all observers were removed, but notification was still received"];
break;
case "testPriorObservationOption":
@@ -511,72 +510,72 @@
if (!_sawPriorObservation)
{
[self assertTrue:prior message:"Have not been sent the prior notification, but it should have been sent"];
[self assertTrue:oldValue == [CPNull null] message: "Shoudl be no initial value"];
[self assertFalse:newValue message: "Should be no object for the new value key on the prior notification"];
[self assertTrue:prior message:@"Have not been sent the prior notification, but it should have been sent"];
[self assertTrue:oldValue == [CPNull null] message:@"Shoudl be no initial value"];
[self assertFalse:newValue message:@"Should be no object for the new value key on the prior notification"];
_sawPriorObservation = YES;
}
else
{
[self assertFalse: prior message: "there should be no value for the notification is prior key, that notification was already sent"];
[self assertTrue: newValue == "set_bob" message: "newValue should be: set_bob was: "+newValue];
[self assertFalse: prior message:@"there should be no value for the notification is prior key, that notification was already sent"];
[self assertTrue: newValue == "set_bob" message:@"newValue should be: set_bob was: "+newValue];
}
break;
case "testInitialObservationOption":
if (!_sawInitialObservation)
{
[self assertTrue: newValue == "paul" message:"Expected old value to be: paul was: "+oldValue];
[self assertFalse: oldValue message:"Should be no value for new change key on initial observation"];
[self assertTrue: newValue == "paul" message:@"Expected old value to be: paul was: "+oldValue];
[self assertFalse: oldValue message:@"Should be no value for new change key on initial observation"];
_sawInitialObservation = YES;
}
else
[self assertFalse:YES message:"Should never have received this notification"];
[self assertFalse:YES message:@"Should never have received this notification"];
break;
case "testMultipartKey":
[self assertTrue: aKeyPath == "teacher.name" message:"Keypath should be: teacher.name, was: "+aKeyPath];
[self assertTrue: newValue == "set_bob" message:"New value should be: set_bob, was: "+newValue];
[self assertTrue: anObject == cs101 message: "anObject should be: "+[cs101 description]+", was: "+[anObject description]];
[self assertTrue: aKeyPath == "teacher.name" message:@"Keypath should be: teacher.name, was: "+aKeyPath];
[self assertTrue: newValue == "set_bob" message:@"New value should be: set_bob, was: "+newValue];
[self assertTrue: anObject == cs101 message:@"anObject should be: " + [cs101 description]+", was: " + [anObject description]];
break;
case "testRemoveMultipartKey":
[self assertFalse:YES message:"Should never have received this notification"];
[self assertFalse:YES message:@"Should never have received this notification"];
break;
case "testRemoveThreePartKey":
[self assertFalse:YES message:"Should never have received this notification"];
[self assertFalse:YES message:@"Should never have received this notification"];
break;
case "testThreePartKey":
[self assertTrue: aKeyPath == "teacher.car.model" message:"Keypath should be: teacher.car.model, was: "+aKeyPath];
[self assertTrue: newValue == "ford focus" message:"New value should be: ford focus, was: "+newValue];
[self assertTrue: anObject == cs101 message: "anObject should be: "+[cs101 description]+", was: "+[anObject description]];
[self assertTrue: aKeyPath == "teacher.car.model" message:@"Keypath should be: teacher.car.model, was: "+aKeyPath];
[self assertTrue: newValue == "ford focus" message:@"New value should be: ford focus, was: "+newValue];
[self assertTrue: anObject == cs101 message:@"anObject should be: " + [cs101 description]+", was: " + [anObject description]];
break;
case "testThreePartKeyPart2":
[self assertTrue: aKeyPath == "teacher.car.year" message:"Keypath should be: teacher.car, was: "+aKeyPath];
[self assertTrue: newValue == "2000" message:"New value should be a car with year: 2000, was: "+[newValue description]];
[self assertTrue: anObject == cs101 message: "anObject should be: "+[cs101 description]+", was: "+[anObject description]];
[self assertTrue: aKeyPath == "teacher.car.year" message:@"Keypath should be: teacher.car, was: "+aKeyPath];
[self assertTrue: newValue == "2000" message:@"New value should be a car with year: 2000, was: " + [newValue description]];
[self assertTrue: anObject == cs101 message:@"anObject should be: " + [cs101 description]+", was: " + [anObject description]];
break;
case "testCrazyKeyPathChanges":
[self assertTrue: [anObject class] == A message:"Should be observing an A class, was: "+[anObject class]];
[self assertTrue: [newValue class] == F message:"Changed class was a F class, got: "+[newValue class]];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
[self assertTrue: [anObject class] == A message:@"Should be observing an A class, was: " + [anObject class]];
[self assertTrue: [newValue class] == F message:@"Changed class was a F class, got: " + [newValue class]];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:@"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
break;
case "testCrazyKeyPathChanges2":
[self assertTrue: [anObject class] == A message:"Should be observing an A class, was: "+[anObject class]];
[self assertTrue: newValue == [CPNull null] message:"Expected null, got: "+newValue];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
[self assertTrue: [anObject class] == A message:@"Should be observing an A class, was: " + [anObject class]];
[self assertTrue: newValue == [CPNull null] message:@"Expected null, got: "+newValue];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:@"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
break;
case "testCrazyKeyPathChanges3":
[self assertTrue: [anObject class] == A message:"Should be observing an A class, was: "+[anObject class]];
[self assertTrue: newValue == 7 message:"Expected 7, got: "+newValue];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
[self assertTrue: [anObject class] == A message:@"Should be observing an A class, was: " + [anObject class]];
[self assertTrue: newValue == 7 message:@"Expected 7, got: "+newValue];
[self assertTrue: aKeyPath == "b.c.d.e.f" message:@"Expected keyPath b.c.d.e.f, got: "+aKeyPath];
break;
case "testDependentKeyObservation":
@@ -603,41 +602,41 @@
case "testInsertIntoToManyProperty":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[self assertTrue: type == CPKeyValueChangeInsertion message: "Should have been an insertion, was: "+type];
[self assertTrue: type == CPKeyValueChangeInsertion message:@"Should have been an insertion, was: " + type];
var values = [changes objectForKey:CPKeyValueChangeNewKey];
[self assertTrue: [values isEqual:[5]] message: "array should have contained 5, was: "+values+" type: "+[values.isa description]+" length: "+values.length];
[self assertTrue: [values isEqual:[5]] message:@"array should have contained 5, was: " + values + " type: " + [values.isa description] + " length: " + values.length];
break;
case "testInsertIntoToManyPropertyIndirectly":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[self assertTrue: type == CPKeyValueChangeInsertion message: "Should have been an insertion, was: "+type];
[self assertTrue: type == CPKeyValueChangeInsertion message:@"Should have been an insertion, was: " + type];
var values = [changes objectForKey:CPKeyValueChangeNewKey];
[self assertTrue: [values isEqual:[5]] message: "array should have contained 5, was: "+values+" type: "+[values.isa description]+" length: "+values.length];
[self assertTrue:[values isEqual:[5]] message:@"array should have contained 5, was: " + values + " type: " + [values.isa description] + " length: " + values.length];
[self assert:aKeyPath equals:"tester.managedObjects"];
[self assert:aKeyPath equals:@"tester.managedObjects"];
[self assert:[anObject valueForKeyPath:@"tester.managedObjects"] equals:[1, 2, 3, 4, 5]];
break;
case "testInsertIntoArrayPropertyIndirectly":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[self assertTrue: type == CPKeyValueChangeInsertion message: "Should have been an insertion, was: "+type];
[self assertTrue: type == CPKeyValueChangeInsertion message:@"Should have been an insertion, was: " + type];
var values = [changes objectForKey:CPKeyValueChangeNewKey];
[self assertTrue: [values isEqual:[5]] message: "array should have contained 5, was: "+values+" type: "+[values.isa description]+" length: "+values.length];
[self assertTrue:[values isEqual:[5]] message:@"array should have contained 5, was: " + values + " type: " + [values.isa description] + " length: " + values.length];
[self assert:aKeyPath equals:"tester.subviews"];
[self assert:aKeyPath equals:@"tester.subviews"];
[self assert:[anObject valueForKeyPath:aKeyPath] equals:[1, 2, 3, 4, 5]];
break;
case "testChangeTopLevelObject":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[self assertTrue: type == CPKeyValueChangeSetting message: "Should have been a set, was: "+type];
[self assertTrue: type == CPKeyValueChangeSetting message:@"Should have been a set, was: " + type];
var oldValue = [changes objectForKey:CPKeyValueChangeOldKey],
newValue = [changes objectForKey:CPKeyValueChangeNewKey];
@@ -648,30 +647,30 @@
case "testRemoveFromToManyProperty":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[self assertTrue: type == CPKeyValueChangeRemoval message: "Should have been a removal, was: "+type];
[self assertTrue: type == CPKeyValueChangeRemoval message:@"Should have been a removal, was: " + type];
var values = [changes objectForKey:CPKeyValueChangeOldKey];
[self assertTrue: [values isEqual:[1]] message: "array should have contained 1, was: "+values+" type: "+[values.isa description]+" length: "+values.length];
[self assertTrue: [values isEqual:[1]] message:@"array should have contained 1, was: " + values + " type: " + [values.isa description] + " length: " + values.length];
[self assert:[anObject valueForKey:@"managedObjects"] equals:[2, 3, 4]];
[self assert:aKeyPath equals:"managedObjects"];
[self assert:aKeyPath equals:@"managedObjects"];
break;
case "testMultiPartKeysWhereValuesEvaluateToSelf":
[self assert:aKeyPath equals:"self.car.thisCar.model"];
[self assert:newValue equals:"ford focus"];
[self assert:aKeyPath equals:@"self.car.thisCar.model"];
[self assert:newValue equals:@"ford focus"];
[self assert:anObject equals:bob];
break;
case "testDictionary":
[self assert:aKeyPath equals:"dictionaryKey"];
[self assert:oldValue equals:"Bob Jones"];
[self assert:newValue equals:"Jo Bob Ray"];
[self assert:aKeyPath equals:@"dictionaryKey"];
[self assert:oldValue equals:@"Bob Jones"];
[self assert:newValue equals:@"Jo Bob Ray"];
break;
default:
[self assertFalse:YES message:"unhandled observation, must be an error"];
[self assertFalse:YES message:@"unhandled observation, must be an error"];
return;
}
@@ -698,12 +697,12 @@
+ (CPSet)keyPathsForValuesAffectingTwiceRemoved
{
return [CPSet setWithObject:"bobName"];
return [CPSet setWithObject:@"bobName"];
}
+ (CPSet)keyPathsForValuesAffectingBobName
{
return [CPSet setWithObject:"name"];
return [CPSet setWithObject:@"name"];
}
- (void)setName:(CPString)aName