Skip to content

Commit aeb8c87

Browse files
author
Stefan van den Oord
committed
Converted to parametrized test; not as nice as it ought to be, but not exactly easy using OCUnit...
1 parent 99a5861 commit aeb8c87

File tree

3 files changed

+86
-70
lines changed

3 files changed

+86
-70
lines changed

Tennis/objc/.idea/workspace.xml

Lines changed: 29 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tennis/objc/TennisTests/TennisTests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
@interface TennisTests : SenTestCase
1212

13+
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores;
1314
@end

Tennis/objc/TennisTests/TennisTests.m

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ @implementation TennisTests {
1818
NSString *expectedScore;
1919
}
2020

21-
- (void)setUp
22-
{
23-
[super setUp];
24-
25-
// Set-up code here.
26-
}
21+
+ (id)defaultTestSuite {
22+
SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)];
2723

28-
- (void)tearDown
29-
{
30-
// Tear-down code here.
31-
32-
[super tearDown];
24+
NSArray *allScores = [self allScores];
25+
for (NSArray *scores in allScores) {
26+
[self addTestWithScores:scores toTestSuite:testSuite];
27+
}
28+
29+
return testSuite;
3330
}
3431

3532
+ (NSArray*)allScores {
@@ -75,6 +72,48 @@ + (NSArray*)allScores {
7572
];
7673
}
7774

75+
+ (void)addTestWithScores:(NSArray *)scores toTestSuite:(SenTestSuite *)testSuite {
76+
NSArray *testInvocations = [self testInvocations];
77+
for (NSInvocation *testInvocation in testInvocations) {
78+
79+
// Create a new instance of our test case for each method found using the given set of parameters.
80+
SenTestCase *test = [[TennisTests alloc] initWithInvocation:testInvocation
81+
scores:scores];
82+
83+
// Add the new test instance to the suite. The OCUnit framework eventually executes the entire test suite.
84+
[testSuite addTest:test];
85+
}
86+
}
87+
88+
- (NSString *)name {
89+
return [NSString stringWithFormat:@"%@ (%d,%d,%@)", [super name], player1Score, player2Score, expectedScore];
90+
}
91+
92+
93+
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores {
94+
self = [super initWithInvocation:invocation];
95+
if (self) {
96+
player1Score = [scores[0] intValue];
97+
player2Score = [scores[1] intValue];
98+
expectedScore = scores[2];
99+
}
100+
return self;
101+
}
102+
103+
- (void)setUp
104+
{
105+
[super setUp];
106+
107+
// Set-up code here.
108+
}
109+
110+
- (void)tearDown
111+
{
112+
// Tear-down code here.
113+
114+
[super tearDown];
115+
}
116+
78117
- (void)checkAllScoresForGame:(TennisGame *)game {
79118
int highestScore = MAX(player1Score, player2Score);
80119
for (int i = 0; i < highestScore; i++) {
@@ -87,39 +126,18 @@ - (void)checkAllScoresForGame:(TennisGame *)game {
87126
}
88127

89128
- (void)testAllScoresTennisGame1 {
90-
for (NSArray * score in [TennisTests allScores]) {
91-
92-
player1Score = [score[0] intValue];
93-
player2Score = [score[1] intValue];
94-
expectedScore = score[2];
95-
96-
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
97-
[self checkAllScoresForGame:game];
98-
}
129+
TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"];
130+
[self checkAllScoresForGame:game];
99131
}
100132

101133
- (void)testAllScoresTennisGame2 {
102-
for (NSArray * score in [TennisTests allScores]) {
103-
104-
player1Score = [score[0] intValue];
105-
player2Score = [score[1] intValue];
106-
expectedScore = score[2];
107-
108-
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
109-
[self checkAllScoresForGame:game];
110-
}
134+
TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"];
135+
[self checkAllScoresForGame:game];
111136
}
112137

113138
- (void)testAllScoresTennisGame3 {
114-
for (NSArray * score in [TennisTests allScores]) {
115-
116-
player1Score = [score[0] intValue];
117-
player2Score = [score[1] intValue];
118-
expectedScore = score[2];
119-
120-
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
121-
[self checkAllScoresForGame:game];
122-
}
139+
TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"];
140+
[self checkAllScoresForGame:game];
123141
}
124142

125143
@end

0 commit comments

Comments
 (0)