@@ -18,18 +18,15 @@ @implementation TennisTests {
18
18
NSString *expectedScore;
19
19
}
20
20
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 )];
27
23
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;
33
30
}
34
31
35
32
+ (NSArray *)allScores {
@@ -75,6 +72,48 @@ + (NSArray*)allScores {
75
72
];
76
73
}
77
74
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
+
78
117
- (void )checkAllScoresForGame : (TennisGame *)game {
79
118
int highestScore = MAX (player1Score, player2Score);
80
119
for (int i = 0 ; i < highestScore; i++) {
@@ -87,39 +126,18 @@ - (void)checkAllScoresForGame:(TennisGame *)game {
87
126
}
88
127
89
128
- (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];
99
131
}
100
132
101
133
- (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];
111
136
}
112
137
113
138
- (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];
123
141
}
124
142
125
143
@end
0 commit comments