Skip to content

Commit 5b52f7a

Browse files
committed
add tests for item change in sorted array
1 parent 7b52b9c commit 5b52f7a

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

FirebaseDatabaseUI/FUISortedArray.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ - (void)observeQuery {
8787
if (index == NSNotFound) { /* error */ return; }
8888

8989
// Since changes can change ordering, model changes as a deletion and an insertion.
90+
FIRDataSnapshot *removed = self.contents[index];
9091
[self.contents removeObjectAtIndex:index];
9192
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
92-
[self.delegate array:self didRemoveObject:snapshot atIndex:index];
93+
[self.delegate array:self didRemoveObject:removed atIndex:index];
9394
}
9495

9596
NSInteger newIndex = [self insertSnapshot:snapshot];
@@ -135,7 +136,7 @@ - (NSInteger)indexOfSnapshot:(FIRDataSnapshot *)snapshot {
135136
// Don't binary search here because we use snapshot keys
136137
// for equality; sort descriptor block provides the entire
137138
// snapshot so binary search isn't reliable. i.e. if the
138-
// whole array is NSOrderedSame binary search won't work
139+
// whole array is NSOrderedSame binary search won't work.
139140
for (NSInteger index = 0; index < self.contents.count; index++) {
140141
if ([self.contents[index].key isEqualToString:snapshot.key]) {
141142
return index;

FirebaseDatabaseUITests/FUISortedArrayTest.m

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ - (void)testArrayCanBeInitialized {
5454
XCTAssertNotNil(self.array, @"expected array to not be nil when initialized");
5555
}
5656

57+
- (void)testEmptyArrayUpdatesCountOnInsert {
58+
// Test delegate
59+
__block BOOL delegateWasCalled = NO;
60+
__block BOOL expectedParametersWereCorrect = NO;
61+
self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) {
62+
// Xcode complains about retain cycles if an XCTAssert is placed in here.
63+
delegateWasCalled = YES;
64+
expectedParametersWereCorrect = (array == self.array &&
65+
object == self.snap &&
66+
index == 0);
67+
};
68+
69+
// Test insert
70+
self.snap.key = @"snapshot";
71+
[self.observable sendEvent:FIRDataEventTypeChildAdded
72+
withObject:self.snap
73+
previousKey:nil
74+
error:nil];
75+
// Array expectations
76+
XCTAssert(self.array.count == 1, @"expected empty array to contain one item after insert");
77+
78+
// Delegate expectations
79+
XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion");
80+
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
81+
}
82+
5783
- (void)testItSortsItselfOnMiddleInsert {
5884
[self.observable populateWithCount:10];
5985

@@ -141,30 +167,52 @@ - (void)testItSortsItselfOnEndInsert {
141167
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
142168
}
143169

144-
- (void)testEmptyArrayUpdatesCountOnInsert {
145-
// Test delegate
146-
__block BOOL delegateWasCalled = NO;
147-
__block BOOL expectedParametersWereCorrect = NO;
170+
- (void)testItSortsItselfWhenChangingObjects {
171+
[self.observable removeAllObservers];
172+
self.array = [[FUISortedArray alloc] initWithQuery:self.observable
173+
delegate:self.arrayDelegate
174+
sortDescriptor:^NSComparisonResult(FIRDataSnapshot *left,
175+
FIRDataSnapshot *right) {
176+
// sort by value, so that changes can cause moves as well
177+
return [left.value compare:right.value];
178+
}];
179+
[self.array observeQuery];
180+
[self.observable populateWithCount:10];
181+
182+
// Test delegate. Changes in the sorted array are modelled as
183+
// a remove and an insert, since changes may cause reordering.
184+
__block BOOL removeWasCalled = NO;
185+
__block BOOL insertWasCalled = NO;
186+
__block BOOL removeParametersWereCorrect = NO;
187+
__block BOOL insertParametersWereCorrect = NO;
148188
self.arrayDelegate.didAddObject = ^(FUISortedArray *array, id object, NSUInteger index) {
149189
// Xcode complains about retain cycles if an XCTAssert is placed in here.
150-
delegateWasCalled = YES;
151-
expectedParametersWereCorrect = (array == self.array &&
152-
object == self.snap &&
153-
index == 0);
190+
insertWasCalled = YES;
191+
insertParametersWereCorrect = (array == self.array &&
192+
object == self.snap &&
193+
index == 9);
194+
NSLog(@"insert: %@", insertParametersWereCorrect ? @"YES" : @"NO");
195+
};
196+
self.arrayDelegate.didRemoveObject = ^(FUISortedArray *array, id object, NSUInteger index) {
197+
removeWasCalled = YES;
198+
removeParametersWereCorrect = (array == self.array &&
199+
index == 2);
200+
NSLog(@"remove: %@", removeParametersWereCorrect ? @"YES" : @"NO");
154201
};
155202

156-
// Test insert
157-
self.snap.key = @"snapshot";
158-
[self.observable sendEvent:FIRDataEventTypeChildAdded
203+
// Test change
204+
self.snap.key = @"2";
205+
self.snap.value = @"a";
206+
[self.observable sendEvent:FIRDataEventTypeChildChanged
159207
withObject:self.snap
160-
previousKey:nil
208+
previousKey:@"1"
161209
error:nil];
162-
// Array expectations
163-
XCTAssert(self.array.count == 1, @"expected empty array to contain one item after insert");
164210

165211
// Delegate expectations
166-
XCTAssert(delegateWasCalled, @"expected delegate to receive callback for insertion");
167-
XCTAssert(expectedParametersWereCorrect, @"unexpected parameter in delegate callback");
212+
XCTAssert(removeWasCalled, @"expected delegate to receive callback for removal");
213+
XCTAssert(insertWasCalled, @"expected delegate to receive callback for insertion");
214+
XCTAssert(insertParametersWereCorrect, @"unexpected parameter in delegate callback");
215+
XCTAssert(removeParametersWereCorrect, @"unexpected parameter in delegate callback");
168216
}
169217

170218
@end

0 commit comments

Comments
 (0)