Skip to content

Commit

Permalink
Reorganize and improve tests of the match class
Browse files Browse the repository at this point in the history
  • Loading branch information
matco committed Feb 11, 2024
1 parent b64a69c commit 5d96b74
Showing 1 changed file with 70 additions and 36 deletions.
106 changes: 70 additions & 36 deletions source/test/MatchTest.mc
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,14 @@ module MatchTest {
BetterTest.assertEqual(match.getType(), SINGLE, "Match is created with correct type");
BetterTest.assertEqual(match.getMaximumSets(), 1, "Match is created with corret maximum number of set");
BetterTest.assertEqual(match.getSets().size(), 1, "Match has only one set at the beginning");
BetterTest.assertEqual(match.getCurrentSet().getBeginner(), YOU, "Match is created with correct player");

BetterTest.assertEqual(match.getTotalRalliesNumber(), 0, "Newly created match has 0 rally");
BetterTest.assertEqual(match.getDuration().value(), 0, "Newly created match has a duration of 0");
BetterTest.assertFalse(match.hasEnded(), "Newly created match has not ended");
BetterTest.assertNull(match.getWinner(), "Newly created match has no winner");
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Newly created match has a total score of 0 for player 1");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Newly created match has a total score of 0 for player 2");

BetterTest.assertEqual(match.getCurrentSet().getScore(YOU), 0, "Newly created match has a set score of 0 for player 1");
BetterTest.assertEqual(match.getCurrentSet().getScore(OPPONENT), 0, "Newly created match has a set score of 0 for player 2");

BetterTest.assertEqual(match.getDuration().value(), 0, "Newly created match has a duration of 0");
return true;
}

(:test)
function testBeginMatch(logger as Logger) as Boolean {
var match = new Match(create_match_config(SINGLE, 1, YOU, true, 21, 30));
//BetterTest.assertEqual(match.beginner, YOU, "Beginner of match began with player 1 is player 1");

BetterTest.assertFalse(match.hasEnded(), "Began match has not ended");
var set = match.getCurrentSet();
BetterTest.assertEqual(set.getBeginner(), YOU, "Match is created with correct player");

BetterTest.assertEqual(match.getTotalRalliesNumber(), 0, "Just began match has 0 rally");
BetterTest.assertNull(match.getWinner(), "Just began match has now winner");
BetterTest.assertNotNull(match.getDuration(), "Began match has a non null duration");
return true;
}

Expand All @@ -53,27 +36,34 @@ module MatchTest {
var match = new Match(create_match_config(SINGLE, 1, YOU, true, 21, 30));
var set = match.getCurrentSet();

match.score(YOU);

BetterTest.assertEqual(match.getTotalScore(YOU), 1, "Score of player 1 is set to 1 after player 1 scored once");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Score of player 2 is still 0 after player 1 scored once");
BetterTest.assertEqual(set.getScore(YOU), 0, "Newly created match has a set score of 0 for player 1");
BetterTest.assertEqual(set.getScore(OPPONENT), 0, "Newly created match has a set score of 0 for player 2");
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Newly created match has a total score of 0 for player 1");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Newly created match has a total score of 0 for player 2");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 0, "Newly created match has 0 rally");

match.score(YOU);
BetterTest.assertEqual(set.getScore(YOU), 1, "Score of player 1 is set to 1 after player 1 scored");
BetterTest.assertEqual(set.getScore(OPPONENT), 0, "Score of player 2 is still 0 after player 1 scored");
BetterTest.assertEqual(match.getTotalScore(YOU), 1, "Total score of player 1 is set to 1 after player 1 scored once");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Total score of player 2 is still 0 after player 1 scored once");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 1, "Rallies are counted properly");

match.score(YOU);
BetterTest.assertEqual(set.getScore(YOU), 2, "Score of player 1 is set to 2 after player 1 scored twice");
BetterTest.assertEqual(set.getScore(OPPONENT), 0, "Score of player 2 is still 0 after player 1 scored twice");

BetterTest.assertFalse(match.hasEnded(), "Began match has not ended");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 2, "Match with 2 rallies has 2 rally number");
BetterTest.assertNull(match.getWinner(), "Just began match has no winner");
BetterTest.assertNotNull(match.getDuration(), "Began match has a non null duration");
BetterTest.assertEqual(match.getTotalScore(YOU), 2, "Total score of player 1 is set to 2 after player 1 scored twice");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Total score of player 2 is still 0 after player 1 scored twice");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 2, "Rallies are counted properly");

match.score(YOU);
match.score(OPPONENT);
BetterTest.assertEqual(set.getScore(YOU), 3, "Score of player 1 who scored twice is 2");
BetterTest.assertEqual(set.getScore(OPPONENT), 1, "Score of player 2 who scored once is 1");
BetterTest.assertEqual(match.getTotalScore(YOU), 3, "Total score of player 1 who scored twice is 2");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 1, "Total score of player 2 who scored once is 1");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 4, "Rallies are counted properly");

return true;
}

Expand All @@ -83,37 +73,45 @@ module MatchTest {
var set = match.getCurrentSet();

match.undo();
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(set.getScore(YOU), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(set.getScore(OPPONENT), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo when match has not begun does nothing");

match.score(YOU);
match.undo();

BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Undo removes a point from the last player who scored");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo does not touch the score of the other player");
BetterTest.assertEqual(set.getScore(YOU), 0, "Undo removes a point from the last player who scored");
BetterTest.assertEqual(set.getScore(OPPONENT), 0, "Undo does not touch the score of the other player");
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Undo removes a point from the last player who scored");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo does not touch the score of the other player");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 0, "Undo handles rallies property");

match.undo();
BetterTest.assertEqual(match.getTotalScore(YOU), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo when match has not begun does nothing");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 0, "Undo handles rallies property");

match.score(YOU);
match.score(YOU);
match.score(OPPONENT);
match.score(YOU);
BetterTest.assertEqual(match.getTotalScore(YOU), 3, "Score of player 1 is now 3");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 1, "Score of player 2 is now 1");
BetterTest.assertEqual(set.getScore(YOU), 3, "Score of player 1 is now 3");
BetterTest.assertEqual(set.getScore(OPPONENT), 1, "Score of player 2 is now 1");
BetterTest.assertEqual(match.getTotalScore(YOU), 3, "Total score of player 1 is now 3");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 1, "Total score of player 2 is now 1");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 4, "Total number of rallies is now 4");

match.undo();
BetterTest.assertEqual(match.getTotalScore(YOU), 2, "Undo removes a point from the last player who scored");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 1, "Undo does not touch the score of the other player");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 3, "Undo handles rallies property");

match.undo();
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 0, "Undo does not touch the score of the other player");
BetterTest.assertEqual(match.getTotalScore(YOU), 2, "Undo removes a point from the last player who scored");
BetterTest.assertEqual(match.getTotalRalliesNumber(), 2, "Undo handles rallies property");

return true;
}

Expand Down Expand Up @@ -154,6 +152,8 @@ module MatchTest {
BetterTest.assertFalse(match.hasEnded(), "Match has not ended if no rallies have been played");

match.score(YOU);
BetterTest.assertFalse(match.hasEnded(), "Match has not ended if its single set has not ended");

match.score(YOU);
match.score(YOU);
BetterTest.assertEqual(set.getScore(YOU), 3, "Score of player 1 is now 3");
Expand All @@ -163,7 +163,7 @@ module MatchTest {
match.undo();
BetterTest.assertEqual(set.getScore(YOU), 2, "Score of player 1 is now 2");
BetterTest.assertFalse(set.hasEnded(), "Set has not ended if no player has reached the maximum point");
BetterTest.assertFalse(match.hasEnded(), "Match has not ended if its single has not ended");
BetterTest.assertFalse(match.hasEnded(), "Match has not ended if its single set has not ended");

match.score(OPPONENT);
match.score(OPPONENT);
Expand Down Expand Up @@ -260,6 +260,15 @@ module MatchTest {
match.end(null);
BetterTest.assertEqual(match.getWinner(), YOU, "Match is won by the player with the most sets won");

try {
match.end(null);
BetterTest.fail("Match cannot be ended twice");
}
catch(exception) {
BetterTest.assertEqual(exception.getErrorMessage(), "Unable to end a match that has already been ended", "It is not possible to end a match that has already been ended");
BetterTest.assertTrue(exception instanceof Toybox.Lang.OperationNotAllowedException, "It is not possible to end a match that has already been ended");
}

//match draw while no set has been ended
match = new Match(create_match_config(SINGLE, null, YOU, true, 3, 5));
set = match.getCurrentSet();
Expand Down Expand Up @@ -336,6 +345,31 @@ module MatchTest {
match.end(null);
BetterTest.assertEqual(match.getWinner(), OPPONENT, "If both players have the same number of sets won, the match is won by the player with the highest score");

//match ended after the third set has ended
match = new Match(create_match_config(SINGLE, null, YOU, true, 3, 5));
set = match.getCurrentSet();

match.score(YOU);
match.score(YOU);
match.score(YOU);

match.nextSet();

match.score(OPPONENT);
match.score(OPPONENT);
match.score(OPPONENT);

match.nextSet();

match.score(YOU);
match.score(YOU);
match.score(YOU);

match.end(null);
BetterTest.assertEqual(match.getWinner(), YOU, "Match is won by the player with the most sets won");
BetterTest.assertEqual(match.getTotalScore(YOU), 6, "Total score of player 1 is 6");
BetterTest.assertEqual(match.getTotalScore(OPPONENT), 3, "Total score of player 2 is 3");

return true;
}

Expand Down

0 comments on commit 5d96b74

Please sign in to comment.