Skip to content

Commit 4d7741c

Browse files
committed
Improved / fixed the delegate tests.
1 parent 5873ae4 commit 4d7741c

File tree

5 files changed

+68
-50
lines changed

5 files changed

+68
-50
lines changed

FZAccordionTableView/FZAccordionTableView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@
125125
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView willCloseSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header;
126126
- (void)tableView:(FZAccordionTableView * _Nonnull)tableView didCloseSection:(NSInteger)section withHeader:(UITableViewHeaderFooterView * _Nonnull)header;
127127

128-
@end
128+
@end

FZAccordionTableView/FZAccordionTableView.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ - (void)autoCollapseAllSectionsExceptSection:(NSInteger)section {
403403
}
404404
}
405405

406-
407406
#pragma mark - <UITableViewDataSource> -
408407

409408
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
@@ -432,7 +431,6 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
432431
[self.sectionInfos addObject:section];
433432
}
434433

435-
436434
return numOfSections;
437435
}
438436

@@ -451,8 +449,6 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
451449
numOfRows = [self.subclassDataSource tableView:tableView numberOfRowsInSection:section];;
452450
}
453451

454-
return 5;
455-
456452
[self.sectionInfos[section] setNumberOfRows:numOfRows];
457453

458454
return ([self isSectionOpen:section]) ? numOfRows : 0;

Tests/Testing_Example/FZAccordionTableViewTests.xcodeproj/project.pbxproj

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
0137E0ED1CCDAED200C2CFAC /* FZAccordionTableViewInitializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0137E0EC1CCDAED200C2CFAC /* FZAccordionTableViewInitializationTests.m */; };
1111
0137E0F01CCDAF0400C2CFAC /* FZAccordionTableViewTestHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 0137E0EF1CCDAF0400C2CFAC /* FZAccordionTableViewTestHelpers.m */; };
12-
0156F1441CCD219800E0530C /* FZAccordionTableViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0156F1431CCD219800E0530C /* FZAccordionTableViewUITests.m */; };
13-
0156F14B1CCD26F000E0530C /* MainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 01D1F33A1B24D94A007B97BD /* MainViewController.m */; };
14-
0156F14C1CCD26F500E0530C /* FZAccordionTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 01D1F35D1B24D9B2007B97BD /* FZAccordionTableView.m */; };
1512
0156F14D1CCD2A6E00E0530C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 01D1F33F1B24D94A007B97BD /* Main.storyboard */; };
1613
0156F14E1CCD2A7600E0530C /* AccordionHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 01D1F3671B24DD86007B97BD /* AccordionHeaderView.xib */; };
17-
0156F14F1CCD2A7B00E0530C /* AccordionHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 01D1F3651B24DD61007B97BD /* AccordionHeaderView.m */; };
18-
0156F1501CCD2A7E00E0530C /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 01D1F3371B24D94A007B97BD /* AppDelegate.m */; };
1914
0156F1581CCD2BA400E0530C /* FZAccordionTableViewGeneralTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 0156F1571CCD2BA400E0530C /* FZAccordionTableViewGeneralTests.m */; };
2015
01B0D1B81CD037BC0012BBCA /* FZAccordionTableViewDelegateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 01B0D1B71CD037BC0012BBCA /* FZAccordionTableViewDelegateTests.m */; };
2116
01B0D1BA1CD038000012BBCA /* FZAccordionTableViewTestBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 01B0D1B91CD038000012BBCA /* FZAccordionTableViewTestBase.m */; };
@@ -325,11 +320,6 @@
325320
isa = PBXSourcesBuildPhase;
326321
buildActionMask = 2147483647;
327322
files = (
328-
0156F1441CCD219800E0530C /* FZAccordionTableViewUITests.m in Sources */,
329-
0156F14C1CCD26F500E0530C /* FZAccordionTableView.m in Sources */,
330-
0156F14F1CCD2A7B00E0530C /* AccordionHeaderView.m in Sources */,
331-
0156F14B1CCD26F000E0530C /* MainViewController.m in Sources */,
332-
0156F1501CCD2A7E00E0530C /* AppDelegate.m in Sources */,
333323
);
334324
runOnlyForDeploymentPostprocessing = 0;
335325
};

Tests/Testing_Example/FZAccordionTableViewUnitTests/FZAccordionTableViewDelegateTests.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,28 @@ - (void)tearDown {
3939
#pragma mark - Tests -
4040

4141
- (void)testOpening {
42+
XCTAssert(!self.willOpenSectionCalled);
43+
XCTAssert(!self.didOpenSectionCalled);
44+
4245
[self waitForHeaderViewInSection:0];
4346
[self.tableView toggleSection:0];
4447

4548
XCTAssert(self.willOpenSectionCalled, @"On opening of a section, 'willOpenSection:' must be called");
4649

47-
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
50+
XCTestExpectation *expectation = [self expectationWithDescription:@"On opening of a section, 'didOpenSection:' must be called"];
51+
52+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
53+
if (self.didOpenSectionCalled) {
54+
[expectation fulfill];
55+
}
56+
});
4857

49-
XCTAssert(self.didOpenSectionCalled, @"On opening of a section, 'didOpenSection:' must be called");
58+
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError * _Nullable error) { }];
5059
}
5160

5261
- (void)testClosing {
62+
XCTAssert(!self.willCloseSectionCalled);
63+
XCTAssert(!self.didCloseSectionCalled);
5364

5465
[self waitForHeaderViewInSection:0];
5566
// First open
@@ -59,9 +70,13 @@ - (void)testClosing {
5970

6071
XCTAssert(self.willCloseSectionCalled, @"On closing of a section, 'willCloseSection:' must be called");
6172

62-
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
63-
64-
XCTAssert(self.didCloseSectionCalled, @"On closing of a section, 'didCloseSection:' must be called");
73+
XCTestExpectation *expectation = [self expectationWithDescription:@"On closing of a section, 'didCloseSection:' must be called"];
74+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
75+
if (self.didCloseSectionCalled) {
76+
[expectation fulfill];
77+
}
78+
});
79+
[self waitForExpectationsWithTimeout:5.0 handler:^(NSError * _Nullable error) { }];
6580
}
6681

6782

Tests/Testing_Example/FZAccordionTableViewUnitTests/FZAccordionTableViewGeneralTests.m

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
#import "FZAccordionTableViewTestBase.h"
1111
#include <stdlib.h>
1212

13+
@interface FZAccordionTableViewSectionInfo : NSObject
14+
@property (nonatomic, getter=isOpen) BOOL open;
15+
@property (nonatomic) NSInteger numberOfRows;
16+
@end
17+
18+
@interface FZAccordionTableView(Private)
19+
20+
@property (strong, nonatomic) NSMutableArray <FZAccordionTableViewSectionInfo *> *sectionInfos;
21+
22+
@end
23+
1324
@interface FZAccordionTableViewGeneralTests : FZAccordionTableViewTestBase
1425

1526
@end
@@ -86,51 +97,57 @@ - (void)testAllowMultipleSectionsOpenAsFalse {
8697
XCTAssert(![self.tableView isSectionOpen:0] && [self.tableView isSectionOpen:1], @"Section 0 should be closed when Section 1 was being forced to be open.");
8798
}
8899

89-
//#pragma mark - Deletion Tests -
90-
//
91-
//- (void)testDeletingRowWithOpenSection {
92-
//
93-
// NSInteger section = 0;
94-
//
95-
// [self.mainViewController.tableView toggleSection:section];
96-
// XCTAssert([self.mainViewController.tableView isSectionOpen:section], @"Section %d should be open.", (int)section);
97-
//
98-
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection'", (int)section);
99-
//
100-
// NSInteger numberOfRows = [self.mainViewController.sections[section] integerValue];
101-
// self.mainViewController.sections[section] = @(numberOfRows-1);
102-
//
103-
// XCTAssert([self.mainViewController.sections[section] integerValue] != [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
104-
//
105-
// // Calling 'deleteRowsAtIndexPaths' propogates the necessary methods/changes, but throws an exception that we can just ignore.
106-
// @try {
107-
// [self.mainViewController.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
108-
// }
109-
// @catch(NSException *exception) { }
110-
//
111-
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection' after deletion.", (int)section);
112-
//}
113-
//
100+
#pragma mark - Deletion Tests -
101+
102+
- (void)testDeletingRowWithOpenSection {
103+
104+
NSInteger section = 0;
105+
106+
// Open the section
107+
[self waitForHeaderViewInSection:section];
108+
[self.tableView toggleSection:section];
109+
XCTAssert([self.tableView isSectionOpen:section], @"Section %d should be open.", (int)section);
110+
111+
// Make sure initial conditions hold
112+
XCTAssert([self.mainViewController.sections[section] integerValue] == [self.tableView.sectionInfos[section] numberOfRows], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView.", (int)section);
113+
114+
// "Delete" in the user data source
115+
NSInteger numberOfRows = [self.mainViewController.sections[section] integerValue];
116+
self.mainViewController.sections[section] = @(numberOfRows-1);
117+
118+
XCTAssert([self.mainViewController.sections[section] integerValue] != [self.tableView.sectionInfos[section] numberOfRows], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
119+
120+
// Delete in the tableView
121+
@try {
122+
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
123+
}
124+
@catch(NSException *exception) {
125+
// Calling 'deleteRowsAtIndexPaths' propogates the necessary methods/changes, but throws an exception that we can just ignore.
126+
}
127+
128+
XCTAssert([self.mainViewController.sections[section] integerValue] == [self.tableView.sectionInfos[section] numberOfRows], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView.", (int)section);
129+
}
130+
114131
//- (void)testDeletingRowWithClosedSection {
115132
//
116133
// NSInteger section = 0;
117134
//
118-
// XCTAssert(![self.mainViewController.tableView isSectionOpen:section], @"Section %d should be closed.", (int)section);
135+
// XCTAssert(![self.tableView isSectionOpen:section], @"Section %d should be closed.", (int)section);
119136
//
120-
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection'", (int)section);
137+
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection'", (int)section);
121138
//
122139
// NSInteger numberOfRows = [self.mainViewController.sections[section] integerValue];
123140
// self.mainViewController.sections[section] = @(numberOfRows-1);
124141
//
125-
// XCTAssert([self.mainViewController.sections[section] integerValue] != [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
142+
// XCTAssert([self.mainViewController.sections[section] integerValue] != [self.tableView.numOfRowsForSection[@(section)] integerValue], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
126143
//
127144
// // Calling 'deleteRowsAtIndexPaths' propogates the necessary methods/changes, but throws an exception that we can just ignore.
128145
// @try {
129-
// [self.mainViewController.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
146+
// [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
130147
// }
131148
// @catch(NSException *exception) { }
132149
//
133-
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.mainViewController.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection' after deletion.", (int)section);
150+
// XCTAssert([self.mainViewController.sections[section] integerValue] == [self.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection' after deletion.", (int)section);
134151
//}
135152

136153
@end

0 commit comments

Comments
 (0)