From aeb8c878d03a9d500c368ed6c6056ba2898ec2f8 Mon Sep 17 00:00:00 2001 From: Stefan van den Oord Date: Fri, 31 May 2013 20:12:57 +0200 Subject: [PATCH] Converted to parametrized test; not as nice as it ought to be, but not exactly easy using OCUnit... --- Tennis/objc/.idea/workspace.xml | 63 +++++++++--------- Tennis/objc/TennisTests/TennisTests.h | 1 + Tennis/objc/TennisTests/TennisTests.m | 94 ++++++++++++++++----------- 3 files changed, 87 insertions(+), 71 deletions(-) diff --git a/Tennis/objc/.idea/workspace.xml b/Tennis/objc/.idea/workspace.xml index 2aef8bcb..6a966889 100644 --- a/Tennis/objc/.idea/workspace.xml +++ b/Tennis/objc/.idea/workspace.xml @@ -2,17 +2,13 @@ - - - - - - + + - + @@ -24,6 +20,7 @@ + diff --git a/Tennis/objc/TennisTests/TennisTests.h b/Tennis/objc/TennisTests/TennisTests.h index da3b0004..dda29b8a 100644 --- a/Tennis/objc/TennisTests/TennisTests.h +++ b/Tennis/objc/TennisTests/TennisTests.h @@ -10,4 +10,5 @@ @interface TennisTests : SenTestCase +- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores; @end diff --git a/Tennis/objc/TennisTests/TennisTests.m b/Tennis/objc/TennisTests/TennisTests.m index 0f616d5b..f1c0c112 100644 --- a/Tennis/objc/TennisTests/TennisTests.m +++ b/Tennis/objc/TennisTests/TennisTests.m @@ -18,18 +18,15 @@ NSString *expectedScore; } -- (void)setUp -{ - [super setUp]; - - // Set-up code here. -} ++ (id)defaultTestSuite { + SenTestSuite *testSuite = [[SenTestSuite alloc] initWithName:NSStringFromClass(self)]; -- (void)tearDown -{ - // Tear-down code here. - - [super tearDown]; + NSArray *allScores = [self allScores]; + for (NSArray *scores in allScores) { + [self addTestWithScores:scores toTestSuite:testSuite]; + } + + return testSuite; } + (NSArray*)allScores { @@ -75,6 +72,48 @@ ]; } ++ (void)addTestWithScores:(NSArray *)scores toTestSuite:(SenTestSuite *)testSuite { + NSArray *testInvocations = [self testInvocations]; + for (NSInvocation *testInvocation in testInvocations) { + + // Create a new instance of our test case for each method found using the given set of parameters. + SenTestCase *test = [[TennisTests alloc] initWithInvocation:testInvocation + scores:scores]; + + // Add the new test instance to the suite. The OCUnit framework eventually executes the entire test suite. + [testSuite addTest:test]; + } +} + +- (NSString *)name { + return [NSString stringWithFormat:@"%@ (%d,%d,%@)", [super name], player1Score, player2Score, expectedScore]; +} + + +- (id)initWithInvocation:(NSInvocation *)invocation scores:(NSArray *)scores { + self = [super initWithInvocation:invocation]; + if (self) { + player1Score = [scores[0] intValue]; + player2Score = [scores[1] intValue]; + expectedScore = scores[2]; + } + return self; +} + +- (void)setUp +{ + [super setUp]; + + // Set-up code here. +} + +- (void)tearDown +{ + // Tear-down code here. + + [super tearDown]; +} + - (void)checkAllScoresForGame:(TennisGame *)game { int highestScore = MAX(player1Score, player2Score); for (int i = 0; i < highestScore; i++) { @@ -87,39 +126,18 @@ } - (void)testAllScoresTennisGame1 { - for (NSArray * score in [TennisTests allScores]) { - - player1Score = [score[0] intValue]; - player2Score = [score[1] intValue]; - expectedScore = score[2]; - - TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"]; - [self checkAllScoresForGame:game]; - } + TennisGame1 * game = [[TennisGame1 alloc] initWithPlayer1:@"player1" player2:@"player2"]; + [self checkAllScoresForGame:game]; } - (void)testAllScoresTennisGame2 { - for (NSArray * score in [TennisTests allScores]) { - - player1Score = [score[0] intValue]; - player2Score = [score[1] intValue]; - expectedScore = score[2]; - - TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"]; - [self checkAllScoresForGame:game]; - } + TennisGame2 * game = [[TennisGame2 alloc] initWithPlayer1:@"player1" player2:@"player2"]; + [self checkAllScoresForGame:game]; } - (void)testAllScoresTennisGame3 { - for (NSArray * score in [TennisTests allScores]) { - - player1Score = [score[0] intValue]; - player2Score = [score[1] intValue]; - expectedScore = score[2]; - - TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"]; - [self checkAllScoresForGame:game]; - } + TennisGame3 * game = [[TennisGame3 alloc] initWithPlayer1:@"player1" player2:@"player2"]; + [self checkAllScoresForGame:game]; } @end