Skip to content

Commit

Permalink
Add tests for BEMCheckBoxGroup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris-Em committed Oct 19, 2016
1 parent f532db6 commit a1e344d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Binary file not shown.
111 changes: 111 additions & 0 deletions Sample Project/CheckBoxTests/GroupTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,126 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "BEMCheckBox.h"
#import "BEMCheckBoxGroup.h"

@interface GroupTests : XCTestCase

@property (strong, nonatomic) NSArray <BEMCheckBox *> *checkBoxes;

@end

@implementation GroupTests

- (void)setUp {
[super setUp];

NSMutableArray <BEMCheckBox *> *checkBoxes = [NSMutableArray new];

for (NSInteger i = 0; i < 5; i++) {
[checkBoxes addObject:[BEMCheckBox new]];
}
self.checkBoxes = [NSArray arrayWithArray:checkBoxes];
}

- (void)testSelectedCheckBox {
BEMCheckBoxGroup *group = [BEMCheckBoxGroup groupWithCheckBoxes:self.checkBoxes];
XCTAssertNotNil(group);
XCTAssertNil(group.selectedCheckBox);

group.selectedCheckBox = [BEMCheckBox new];
XCTAssertNil(group.selectedCheckBox);
XCTAssertEqual([self selectedCheckBoxForGroup:group], group.selectedCheckBox);
XCTAssertEqual([self numberOfSelectedCheckBoxesForGroup:group], 0);

group.selectedCheckBox = self.checkBoxes[1];
XCTAssertEqual(group.selectedCheckBox, self.checkBoxes[1]);
XCTAssertEqual([self selectedCheckBoxForGroup:group], group.selectedCheckBox);
XCTAssertEqual([self numberOfSelectedCheckBoxesForGroup:group], 1);

group.selectedCheckBox = self.checkBoxes[2];
XCTAssertEqual(group.selectedCheckBox, self.checkBoxes[2]);
XCTAssertEqual([self selectedCheckBoxForGroup:group], group.selectedCheckBox);
XCTAssertEqual([self numberOfSelectedCheckBoxesForGroup:group], 1);

group.selectedCheckBox = self.checkBoxes[0];
XCTAssertEqual(group.selectedCheckBox, self.checkBoxes[0]);
XCTAssertEqual([self selectedCheckBoxForGroup:group], group.selectedCheckBox);
XCTAssertEqual([self numberOfSelectedCheckBoxesForGroup:group], 1);

group.selectedCheckBox = nil;
XCTAssertNil(group.selectedCheckBox);
XCTAssertEqual([self selectedCheckBoxForGroup:group], nil);
XCTAssertEqual([self numberOfSelectedCheckBoxesForGroup:group], 0);
}

- (void)testAutoSelectFirstCheckBox {
BEMCheckBoxGroup *group = [BEMCheckBoxGroup groupWithCheckBoxes:self.checkBoxes];
XCTAssertNotNil(group);
XCTAssertNil(group.selectedCheckBox);

group.mustHaveSelection = YES;
XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]);

group.selectedCheckBox = nil;
XCTAssertEqual(group.selectedCheckBox, [self.checkBoxes firstObject]);
}

- (void)testAddCheckBox {
BEMCheckBoxGroup *group = [BEMCheckBoxGroup groupWithCheckBoxes:self.checkBoxes];
XCTAssertNotNil(group);
XCTAssertEqual(group.checkBoxes.count, self.checkBoxes.count);

BEMCheckBox *checkBox = [BEMCheckBox new];
[group addCheckBoxToGroup:checkBox];

XCTAssertEqual(group.checkBoxes.count, self.checkBoxes.count + 1);
}

- (void)testRemoveCheckBox {
BEMCheckBoxGroup *group = [BEMCheckBoxGroup groupWithCheckBoxes:self.checkBoxes];
XCTAssertNotNil(group);
XCTAssertEqual(group.checkBoxes.count, self.checkBoxes.count);

[group removeCheckBoxFromGroup:[self.checkBoxes firstObject]];
XCTAssertEqual(group.checkBoxes.count, self.checkBoxes.count - 1);
}

- (void)testEmptyGroup {
BEMCheckBoxGroup *emptyGroup = [BEMCheckBoxGroup groupWithCheckBoxes:nil];
XCTAssertNotNil(emptyGroup);
XCTAssertEqual(emptyGroup.checkBoxes.count, 0);

[emptyGroup removeCheckBoxFromGroup:[BEMCheckBox new]];
XCTAssertEqual(emptyGroup.checkBoxes.count, 0);

emptyGroup.mustHaveSelection = YES;
XCTAssertEqual(emptyGroup.checkBoxes.count, 0);

[emptyGroup addCheckBoxToGroup:[BEMCheckBox new]];
XCTAssertEqual(emptyGroup.checkBoxes.count, 1);
}

#pragma mark Helper Methods

- (NSInteger)numberOfSelectedCheckBoxesForGroup:(BEMCheckBoxGroup *)group {
NSInteger numberOfSelectedCheckBoxes = 0;
for (BEMCheckBox *checkBox in group.checkBoxes) {
if (checkBox.on == YES) {
numberOfSelectedCheckBoxes++;
}
}

return numberOfSelectedCheckBoxes;
}

- (BEMCheckBox *)selectedCheckBoxForGroup:(BEMCheckBoxGroup *)group {
for (BEMCheckBox *checkBox in group.checkBoxes) {
if (checkBox.on == YES) {
return checkBox;
}
}

return nil;
}

- (void)tearDown {
Expand Down

0 comments on commit a1e344d

Please sign in to comment.