-
Notifications
You must be signed in to change notification settings - Fork 333
/
CPKVOTest.j
850 lines (613 loc) · 27.5 KB
/
CPKVOTest.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
@import <Foundation/CPKeyValueCoding.j>
@import <Foundation/CPKeyValueObserving.j>
@implementation CPKVOTest : OJTestCase
{
BOOL _sawInitialObservation;
BOOL _sawPriorObservation;
BOOL _sawObservation;
BOOL _sawDependentObservation;
id bob;
id obj;
id cs101;
id focus;
CPInteger observationCount;
}
- (void)setUp
{
_sawObservation = NO;
}
- (void)testAddObserver
{
bob = [[PersonTester alloc] init];
[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"];
}
- (void)testUnobservedKey
{
bob = [[PersonTester alloc] init];
[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"];
}
- (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 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"];
}
- (void)testDirectIVarObservation
{
var bob = [[PersonTester alloc] init];
[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"];
}
- (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 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"]];
}
- (void)testRemoveOtherObserver
{
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 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"];
}
- (void)testRemoveAllObservers
{
bob = [[PersonTester alloc] init];
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 removeObserver:obj forKeyPath:@"name"];
[bob removeObserver:obj2 forKeyPath:@"name"];
[bob removeObserver:nil forKeyPath:nil];
[bob setValue:@"bob" forKey:@"name"];
[self assertTrue: [bob valueForKey:@"name"] == @"set_bob" message: "valueForKey:'name' should be bob, was: "+[bob valueForKey:@"name"]];
}
- (void)testDictionary
{
var dict = [[CPDictionary alloc] init];
[dict setObject:@"Bob Jones" forKey:@"dictionaryKey"];
[dict addObserver:self
forKeyPath:@"dictionaryKey"
options:nil
context:"testDictionary"];
[dict setObject:@"Jo Bob Ray" forKey:@"dictionaryKey"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testPriorObservationOption
{
_sawPriorObservation = NO;
bob = [[PersonTester alloc] init];
[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"];
}
- (void)testInitialObservationOption
{
_sawInitialObservation = NO;
bob = [[PersonTester alloc] init];
bob.name = "paul";
[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"];
}
- (void)testDependentKeyObservation
{
_sawDependentObservation = NO;
bob = [[PersonTester alloc] init];
bob.name = "paul";
[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"]];
}
- (void)testMultipartKey
{
cs101 = [ClassTester new];
bob = [PersonTester new];
[cs101 setTeacher:bob];
[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"];
}
- (void)testMultiPartKeysWhereValuesEvaluateToSelf
{
focus = [CarTester new];
bob = [PersonTester new];
[bob setValue:focus forKey:"car"];
[bob addObserver:self forKeyPath:@"self.car.thisCar.model" options:0 context:"testMultiPartKeysWhereValuesEvaluateToSelf"];
[focus setValue:"ford focus" forKey:"model"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testThreePartKey
{
focus = [CarTester new];
cs101 = [ClassTester new];
bob = [PersonTester new];
[cs101 setTeacher:bob];
[bob setValue:focus forKey:"car"];
[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"];
}
- (void)testThreePartKeyPart2
{
focus = [CarTester new];
cs101 = [ClassTester new];
bob = [PersonTester new];
[cs101 setTeacher:bob];
[focus setValue:"2000" forKey:"year"];
[cs101 addObserver:self forKeyPath:@"teacher.car.year" options:0 context:"testThreePartKeyPart2"];
[bob setValue:focus forKey:"car"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testRemoveMultipartKey
{
cs101 = [ClassTester new];
bob = [PersonTester new];
[cs101 setTeacher:bob];
[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"];
}
- (void)testRemoveThreePartKey
{
focus = [CarTester new];
cs101 = [ClassTester new];
bob = [PersonTester new];
[cs101 setTeacher:bob];
[bob setValue:focus forKey:"car"];
[cs101 addObserver:self forKeyPath:@"teacher.car.model" options:0 context:"testRemoveThreePartKey"];
[cs101 removeObserver:self forKeyPath:@"teacher.car.model"];
[focus setValue:"ford focus" forKey:"model"];
[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 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"];
[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 addObserver:self forKeyPath:"b.c.d.e.f" options:0 context:"testCrazyKeyPathChanges2"];
[a setValue:nil forKeyPath:"b.c"];
[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 addObserver:self forKeyPath:"b.c.d.e.f" options:0 context:"testCrazyKeyPathChanges3"];
[a setValue:7 forKeyPath:"b.c.d.e.f"];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testInsertIntoToManyProperty
{
var tester = [ToManyTester new];
[tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:"testInsertIntoToManyProperty"];
[tester insertObject:5 inManagedObjectsAtIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testRemoveFromToManyProperty
{
var tester = [ToManyTester new];
[tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"managedObjects" options:0 context:"testRemoveFromToManyProperty"];
[tester removeObjectFromManagedObjectsAtIndex:0];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testInsertIntoToManyPropertyIndirectly
{
var tester = [IndirectToManyTester new];
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"managedObjects"];
[tester addObserver:self forKeyPath:@"tester.managedObjects" options:0 context:"testInsertIntoToManyPropertyIndirectly"];
[tester.tester insertObject:5 inManagedObjectsAtIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testInsertIntoArrayPropertyIndirectly
{
var tester = [IndirectToManyTester new];
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"subviews"];
[tester addObserver:self forKeyPath:@"tester.subviews" options:0 context:"testInsertIntoArrayPropertyIndirectly"];
[tester.tester insertSubview:5 atIndex:4];
[self assertTrue: _sawObservation message:"Never recieved an observation"];
}
- (void)testChangeTopLevelObject
{
var tester = [IndirectToManyTester new];
tester.tester = [ToManyTester new];
[tester.tester setValue:[1, 2, 3, 4] forKey:@"subviews"];
[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"];
}
- (void)testPerformance
{
bob = [PersonTester new];
[bob setValue:"initial bob" forKey:"name"];
var startTime = new Date();
for (var i = 0; i < 1000; i++)
[bob setValue:i + "bob" forKey:"name"];
var total = new Date() - startTime;
[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"];
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)];
}
- (void)testDependentKeysPaths
{
var object = [[TestObject alloc] init];
[object addObserver:self forKeyPath:@"key" options:0 context:"testDependentKeysPaths"];
observationCount = 0;
[object setKey:@"value"];
[object setAffectingKey:@"value"];
[object setOtherAffectingKey:@"value"];
[self assert:observationCount equals:3];
}
- (void)testSettersReplacedOnce
{
var bob = [[PersonTester alloc] init],
betty = [CPObject new];
[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"];
var newImp = class_getMethodImplementation(bob.isa, @selector(setName:));
[self assertTrue:newImp === oldImp];
[bob addObserver:betty forKeyPath:@"name" options:nil context:"testSettersReplacedOnce"];
var newImp = class_getMethodImplementation(bob.isa, @selector(setName:));
[self assertTrue:newImp === oldImp];
}
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changes context:(id)aContext
{
var oldValue = [changes objectForKey:CPKeyValueChangeOldKey],
newValue = [changes objectForKey:CPKeyValueChangeNewKey];
switch (aContext)
{
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]];
break;
case "testUnobservedKey":
[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];
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];
break;
case "testRemoveObserver":
[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];
break;
case "testRemoveAllObservers":
[self assertTrue: NO message: "all observers were removed, but notification was still received"];
break;
case "testPriorObservationOption":
var prior = [changes objectForKey:CPKeyValueChangeNotificationIsPriorKey];
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"];
_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];
}
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"];
_sawInitialObservation = YES;
}
else
[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]];
break;
case "testRemoveMultipartKey":
[self assertFalse:YES message:"Should never have received this notification"];
break;
case "testRemoveThreePartKey":
[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]];
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]];
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];
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];
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];
break;
case "testDependentKeyObservation":
[self assertTrue:aKeyPath === "bobName"
message:@"expected key value change for bobName, got: " + aKeyPath];
[self assertTrue:oldValue === "BOB! paul"
message:@"expected old value of " + aKeyPath + " to be \"BOB! paul\", got: " + oldValue];
_sawDependentObservation = YES;
break;
case "testDependentKeyObservation2":
[self assertTrue:aKeyPath === "twiceRemoved"
message:@"expected key value change for twiceRemoved, got: " + aKeyPath];
[self assertTrue:oldValue === "BOB! paul twice"
message:@"expected old value to be \"BOB! paul twice\", got: " + oldValue];
_sawDependentObservation = YES;
break;
case "testInsertIntoToManyProperty":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[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];
break;
case "testInsertIntoToManyPropertyIndirectly":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[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 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];
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 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];
var oldValue = [changes objectForKey:CPKeyValueChangeOldKey],
newValue = [changes objectForKey:CPKeyValueChangeNewKey];
[self assert:oldValue equals:[1, 2, 3, 4]];
[self assert:newValue equals:[5, 6, 7, 8]];
break;
case "testRemoveFromToManyProperty":
var type = [changes objectForKey:CPKeyValueChangeKindKey];
[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 assert:[anObject valueForKey:@"managedObjects"] equals:[2, 3, 4]];
[self assert:aKeyPath equals:"managedObjects"];
break;
case "testMultiPartKeysWhereValuesEvaluateToSelf":
[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"];
break;
default:
[self assertFalse:YES message:"unhandled observation, must be an error"];
return;
}
_sawObservation = YES;
}
@end
@implementation CPObject (KVO)
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changes context:(id)aContext
{
}
@end
@implementation PersonTester : CPObject
{
CPString name;
CPString phoneNumber;
CarTester car;
}
+ (CPSet)keyPathsForValuesAffectingTwiceRemoved
{
return [CPSet setWithObject:"bobName"];
}
+ (CPSet)keyPathsForValuesAffectingBobName
{
return [CPSet setWithObject:"name"];
}
- (void)setName:(CPString)aName
{
name = "set_"+aName;
}
- (CPString)bobName
{
return "BOB! " + name;
}
- (CPString)twiceRemoved
{
return [self bobName] + " twice";
}
@end
@implementation ClassTester : CPObject
{
PersonTester teacher;
CPArray students;
}
- (void)setTeacher:(PersonTester)aPerson
{
teacher = aPerson;
}
@end
@implementation CarTester : CPObject
{
CPString model;
CPString year;
}
- (void)setModel:(CPString)aModel
{
model = aModel;
}
- (void)thisCar
{
return self;
}
@end
@implementation IndirectToManyTester : CPObject
{
ToManyTester tester @accessors;
}
@end
@implementation ToManyTester : CPObject
{
CPArray managedObjects;
CPArray subviews;
}
- (void)insertSubview:(CPView)aSubview atIndex:(int)anIndex
{
var mutatedIndexes = [CPIndexSet indexSetWithIndex:anIndex];
[self willChange:CPKeyValueChangeInsertion
valuesAtIndexes:mutatedIndexes
forKey:@"subviews"];
[subviews insertObject:aSubview atIndex:anIndex];
[self didChange:CPKeyValueChangeInsertion
valuesAtIndexes:mutatedIndexes
forKey:@"subviews"];
}
- (unsigned int)countOfManagedObjects
{
return [managedObjects count];
}
- (id)objectInManagedObjectsAtIndex:(unsigned)anIndex
{
return [managedObjects objectAtIndex:anIndex];
}
- (void)removeObjectFromManagedObjectsAtIndex:(unsigned)anIndex
{
[managedObjects removeObjectAtIndex:anIndex];
}
- (void)insertObject:(id)anObject inManagedObjectsAtIndex:(unsigned)anIndex
{
[managedObjects insertObject:anObject atIndex:anIndex];
}
@end
@implementation A : CPObject
{
id b;
}
@end
@implementation B : CPObject
{
id c;
}
@end
@implementation C : CPObject
{
id d;
}
@end
@implementation D : CPObject
{
id e;
}
@end
@implementation E : CPObject
{
id f;
}
@end
@implementation F : CPObject
{
id g;
}
@end
@implementation TestObject : CPObject
{
CPString key @accessors;
CPString affectingKey @accessors;
CPString otherAffectingKey @accessors;
}
+ (CPSet)keyPathsForValuesAffectingKey
{
return [CPSet setWithObjects:@"affectingKey", @"otherAffectingKey"];
}
@end