Skip to content

Commit e1bc1ae

Browse files
committed
Added tests for deleting rows.
1 parent 271ff74 commit e1bc1ae

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

Tests/FZAccordionTableViewTests.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
010C610E1CB0244500D57F77 /* FZAccordionTableView+Internals.m in Sources */ = {isa = PBXBuildFile; fileRef = 010C610D1CB0244500D57F77 /* FZAccordionTableView+Internals.m */; };
1011
015B94341B2E321F00C1E772 /* FZAccordionTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 015B94331B2E321F00C1E772 /* FZAccordionTableViewTests.m */; };
1112
015B943E1B2E333800C1E772 /* FZAccordionTableViewSimulator.m in Sources */ = {isa = PBXBuildFile; fileRef = 015B943D1B2E333800C1E772 /* FZAccordionTableViewSimulator.m */; };
1213
01AAAAC91B2E4EE8009D3AE3 /* FZAccordionTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = 01AAAAC81B2E4EE8009D3AE3 /* FZAccordionTableView.m */; };
1314
01AAAACE1B2E4F55009D3AE3 /* FZAccordionTableViewDelegateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 01AAAACD1B2E4F55009D3AE3 /* FZAccordionTableViewDelegateTests.m */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXFileReference section */
18+
010C610C1CB0244500D57F77 /* FZAccordionTableView+Internals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FZAccordionTableView+Internals.h"; sourceTree = "<group>"; };
19+
010C610D1CB0244500D57F77 /* FZAccordionTableView+Internals.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FZAccordionTableView+Internals.m"; sourceTree = "<group>"; };
1720
015B942E1B2E321F00C1E772 /* FZAccordionTableViewTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FZAccordionTableViewTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1821
015B94321B2E321F00C1E772 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1922
015B94331B2E321F00C1E772 /* FZAccordionTableViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FZAccordionTableViewTests.m; sourceTree = "<group>"; };
@@ -54,6 +57,8 @@
5457
015B94301B2E321F00C1E772 /* FZAccordionTableViewTests */ = {
5558
isa = PBXGroup;
5659
children = (
60+
010C610C1CB0244500D57F77 /* FZAccordionTableView+Internals.h */,
61+
010C610D1CB0244500D57F77 /* FZAccordionTableView+Internals.m */,
5762
015B943C1B2E333800C1E772 /* FZAccordionTableViewSimulator.h */,
5863
015B943D1B2E333800C1E772 /* FZAccordionTableViewSimulator.m */,
5964
015B94331B2E321F00C1E772 /* FZAccordionTableViewTests.m */,
@@ -147,6 +152,7 @@
147152
isa = PBXSourcesBuildPhase;
148153
buildActionMask = 2147483647;
149154
files = (
155+
010C610E1CB0244500D57F77 /* FZAccordionTableView+Internals.m in Sources */,
150156
015B943E1B2E333800C1E772 /* FZAccordionTableViewSimulator.m in Sources */,
151157
01AAAACE1B2E4F55009D3AE3 /* FZAccordionTableViewDelegateTests.m in Sources */,
152158
01AAAAC91B2E4EE8009D3AE3 /* FZAccordionTableView.m in Sources */,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// FZAccordionTableView+Internals.h
3+
// FZAccordionTableViewTests
4+
//
5+
// Created by Krisjanis Gaidis on 4/2/16.
6+
//
7+
//
8+
9+
#import "FZAccordionTableView.h"
10+
11+
@interface FZAccordionTableView (Internals)
12+
13+
@property (strong, nonatomic) NSMutableSet *openedSections;
14+
@property (strong, nonatomic) NSMutableDictionary *numOfRowsForSection;
15+
16+
@end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// FZAccordionTableView+Internals.m
3+
// FZAccordionTableViewTests
4+
//
5+
// Created by Krisjanis Gaidis on 4/2/16.
6+
//
7+
//
8+
9+
#import "FZAccordionTableView+Internals.h"
10+
11+
@implementation FZAccordionTableView (Internals)
12+
13+
@dynamic openedSections;
14+
@dynamic numOfRowsForSection;
15+
16+
@end

Tests/FZAccordionTableViewTests/FZAccordionTableViewSimulator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static const NSInteger kAccordionSimulatorNumberOfRows = 5;
1414

1515
@interface FZAccordionTableViewSimulator : NSObject
1616

17+
@property (strong, nonatomic, readonly) NSMutableArray <NSNumber *> *sections;
1718
@property (strong, nonatomic, readonly) FZAccordionTableView *tableView;
1819
@property (weak, nonatomic) id<FZAccordionTableViewDelegate> delegate;
1920

Tests/FZAccordionTableViewTests/FZAccordionTableViewSimulator.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ @implementation FZAccordionTableViewSimulator
2424
- (instancetype)init {
2525
if (self = [super init]) {
2626
_tableView = [FZAccordionTableView new];
27+
_sections = [NSMutableArray new];
28+
[self setupData];
2729
[self setupTableView];
2830
}
2931
return self;
3032
}
3133

34+
- (void)setupData {
35+
for (NSInteger i = 0; i < kAccordionSimulatorNumberOfSections; i++) {
36+
[self.sections addObject:@(kAccordionSimulatorNumberOfRows)];
37+
}
38+
}
39+
3240
- (void)setupTableView {
3341
self.tableView.delegate = self;
3442
self.tableView.dataSource = self;
@@ -40,11 +48,11 @@ - (void)setupTableView {
4048
#pragma mark - <UITableViewDataSource> / <UITableViewDelegate> -
4149

4250
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
43-
return kAccordionSimulatorNumberOfSections;
51+
return self.sections.count;
4452
}
4553

4654
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
47-
return kAccordionSimulatorNumberOfRows;
55+
return [self.sections[section] integerValue];
4856
}
4957

5058
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

Tests/FZAccordionTableViewTests/FZAccordionTableViewTests.m

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import <UIKit/UIKit.h>
1010
#import <XCTest/XCTest.h>
1111
#import "FZAccordionTableViewSimulator.h"
12+
#import "FZAccordionTableView+Internals.h"
1213

1314
@interface FZAccordionTableViewTests : XCTestCase
1415

@@ -149,4 +150,51 @@ - (void)testSectionsAlwaysOpen {
149150
}
150151
}
151152

153+
#pragma mark - Deletion Tests -
154+
155+
- (void)testDeletingRowWithOpenSection {
156+
157+
NSInteger section = 0;
158+
159+
[self.tableViewSimulator.tableView toggleSection:section];
160+
XCTAssert([self.tableViewSimulator.tableView isSectionOpen:section], @"Section %d should be open.", (int)section);
161+
162+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] == [self.tableViewSimulator.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection'", (int)section);
163+
164+
NSInteger numberOfRows = [self.tableViewSimulator.sections[section] integerValue];
165+
self.tableViewSimulator.sections[section] = @(numberOfRows-1);
166+
167+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] != [self.tableViewSimulator.tableView.numOfRowsForSection[@(section)] integerValue], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
168+
169+
// Calling 'deleteRowsAtIndexPaths' propogates the necessary methods/changes, but throws an exception that we can just ignore.
170+
@try {
171+
[self.tableViewSimulator.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
172+
}
173+
@catch(NSException *exception) { }
174+
175+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] == [self.tableViewSimulator.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);
176+
}
177+
178+
- (void)testDeletingRowWithClosedSection {
179+
180+
NSInteger section = 0;
181+
182+
XCTAssert(![self.tableViewSimulator.tableView isSectionOpen:section], @"Section %d should be closed.", (int)section);
183+
184+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] == [self.tableViewSimulator.tableView.numOfRowsForSection[@(section)] integerValue], @"The number of rows in section %d of our data source should match those of the FZAccordionTableView 'numOfRowsForSection'", (int)section);
185+
186+
NSInteger numberOfRows = [self.tableViewSimulator.sections[section] integerValue];
187+
self.tableViewSimulator.sections[section] = @(numberOfRows-1);
188+
189+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] != [self.tableViewSimulator.tableView.numOfRowsForSection[@(section)] integerValue], @"Without calling 'deleteForRows' there should be a difference between data source and what the tableView says.");
190+
191+
// Calling 'deleteRowsAtIndexPaths' propogates the necessary methods/changes, but throws an exception that we can just ignore.
192+
@try {
193+
[self.tableViewSimulator.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:numberOfRows-1 inSection:section]] withRowAnimation:UITableViewRowAnimationNone];
194+
}
195+
@catch(NSException *exception) { }
196+
197+
XCTAssert([self.tableViewSimulator.sections[section] integerValue] == [self.tableViewSimulator.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);
198+
}
199+
152200
@end

0 commit comments

Comments
 (0)