Skip to content

Commit 248c53b

Browse files
author
Stefan van den Oord
committed
Refactored test so that parametrized part is separated better and more explicit
1 parent aeb8c87 commit 248c53b

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

Tennis/objc/.idea/workspace.xml

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

Tennis/objc/TennisTests/TennisTests.m

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,41 @@
1212
#import "TennisGame2.h"
1313
#import "TennisGame3.h"
1414

15-
@implementation TennisTests {
16-
int player1Score;
17-
int player2Score;
18-
NSString *expectedScore;
19-
}
20-
15+
@interface TennisTests (Parametrized)
16+
+ (NSArray*)parameters;
17+
@end
18+
@implementation TennisTests (Parametrized)
2119
+ (id)defaultTestSuite {
2220
SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)];
2321

24-
NSArray *allScores = [self allScores];
22+
NSArray *allScores = [self parameters];
2523
for (NSArray *scores in allScores) {
2624
[self addTestWithScores:scores toTestSuite:testSuite];
2725
}
2826

2927
return testSuite;
3028
}
29+
+ (void)addTestWithScores:(NSArray *)scores toTestSuite:(SenTestSuite *)testSuite {
30+
NSArray *testInvocations = [self testInvocations];
31+
for (NSInvocation *testInvocation in testInvocations) {
32+
33+
// Create a new instance of our test case for each method found using the given set of parameters.
34+
SenTestCase *test = [[TennisTests alloc] initWithInvocation:testInvocation
35+
scores:scores];
36+
37+
// Add the new test instance to the suite. The OCUnit framework eventually executes the entire test suite.
38+
[testSuite addTest:test];
39+
}
40+
}
41+
@end
42+
43+
@implementation TennisTests {
44+
int player1Score;
45+
int player2Score;
46+
NSString *expectedScore;
47+
}
3148

32-
+ (NSArray*)allScores {
49+
+ (NSArray*)parameters {
3350
return @[
3451
@[ @0, @0, @"Love-All"],
3552
@[ @1, @1, @"Fifteen-All"],
@@ -72,24 +89,6 @@ + (NSArray*)allScores {
7289
];
7390
}
7491

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-
9392
- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores {
9493
self = [super initWithInvocation:invocation];
9594
if (self) {
@@ -100,18 +99,8 @@ - (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores {
10099
return self;
101100
}
102101

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];
102+
- (NSString *)name {
103+
return [NSString stringWithFormat:@"%@ (%d,%d,%@)", [super name], player1Score, player2Score, expectedScore];
115104
}
116105

117106
- (void)checkAllScoresForGame:(TennisGame *)game {

0 commit comments

Comments
 (0)