Created
September 19, 2023 13:17
My unit testing file for Matching Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:exam1_emoji_matching/emoji_matching_game.dart'; | |
import 'package:test/test.dart'; | |
void main() { | |
late MatchingGame game3; | |
late MatchingGame game5; | |
late MatchingGame game10; | |
setUp(() { | |
game3 = MatchingGame(3); | |
game10 = MatchingGame(10); | |
game5 = MatchingGame(5); | |
game5.cards = "π,π΄,π,π’,π±,π,π΄,π,π’,π±".split(","); | |
}); | |
test("Constructor", () { | |
// Cards random (hard to test values) | |
expect(game3.cards.length, 6); | |
expect(game10.cards.length, 20); | |
// Card States | |
expect(game3.cardStates.length, 6); | |
expect(game10.cardStates.length, 20); | |
for (int k = 0; k < 6; k++) { | |
expect(game3.cardStates[k], CardState.hidden); | |
} | |
for (int k = 0; k < 20; k++) { | |
expect(game10.cardStates[k], CardState.hidden); | |
} | |
// Card back selection (comes form this list) | |
expect(game10.allCardBacks.contains(game10.cardBack), true); | |
}); | |
test("Before Press 1", () { | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.hidden); | |
expect(game5.cardStates[2], CardState.hidden); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
test("Press 1", () { | |
game5.pressedCard(index: 1); | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.shown); | |
expect(game5.cardStates[2], CardState.hidden); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.waitingForSecondSelection); | |
expect(game5.firstSelection, 1); | |
}); | |
test("Press 2, no match", () { | |
game5.pressedCard(index: 1); | |
game5.pressedCard(index: 2); | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.shown); | |
expect(game5.cardStates[2], CardState.shown); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.turnComplete); | |
expect(game5.firstSelection, 1); | |
expect(game5.secondSelection, 2); | |
game5.startNewTurn(); | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.hidden); | |
expect(game5.cardStates[2], CardState.hidden); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
test("Press 2, match!", () { | |
game5.pressedCard(index: 1); | |
game5.pressedCard(index: 6); | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.shown); | |
expect(game5.cardStates[6], CardState.shown); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.turnComplete); | |
expect(game5.firstSelection, 1); | |
expect(game5.secondSelection, 6); | |
game5.startNewTurn(); | |
expect(game5.cardStates[0], CardState.hidden); | |
expect(game5.cardStates[1], CardState.removed); | |
expect(game5.cardStates[6], CardState.removed); | |
expect(game5.cardStates[9], CardState.hidden); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
test("Completed 2 matches", () { | |
game5.pressedCard(index: 1); | |
game5.pressedCard(index: 6); | |
game5.startNewTurn(); | |
game5.pressedCard(index: 0); | |
game5.pressedCard(index: 5); | |
game5.startNewTurn(); | |
const removedIndexes = [0, 1, 5, 6]; | |
for (int k = 0; k < game5.cardStates.length; k++) { | |
expect(game5.cardStates[k], | |
(removedIndexes.contains(k)) ? CardState.removed : CardState.hidden); | |
} | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
test("Completed 4 matches", () { | |
for (int k = 0; k < 4; k++) { | |
game5.pressedCard(index: k); | |
game5.pressedCard(index: k + 5); | |
game5.startNewTurn(); | |
} | |
const removedIndexes = [0, 1, 2, 3, 5, 6, 7, 8]; | |
for (int k = 0; k < game5.cardStates.length; k++) { | |
expect(game5.cardStates[k], | |
(removedIndexes.contains(k)) ? CardState.removed : CardState.hidden); | |
} | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
test("Completed all matches", () { | |
for (int k = 0; k < 5; k++) { | |
game5.pressedCard(index: k); | |
game5.pressedCard(index: k + 5); | |
game5.startNewTurn(); | |
} | |
for (int k = 0; k < game5.cardStates.length; k++) { | |
expect(game5.cardStates[k], CardState.removed); | |
} | |
expect(game5.state, GameState.gameOver); | |
}); | |
test("foolish second press - same card twice", () { | |
game3.pressedCard(index: 1); | |
game3.pressedCard(index: 1); | |
expect(game3.cardStates[0], CardState.hidden); | |
expect(game3.cardStates[1], CardState.shown); | |
expect(game3.cardStates[2], CardState.hidden); | |
expect(game3.cardStates[3], CardState.hidden); | |
expect(game3.cardStates[4], CardState.hidden); | |
expect(game3.cardStates[5], CardState.hidden); | |
expect(game3.state, GameState.waitingForSecondSelection); | |
expect(game3.firstSelection, 1); | |
game3.startNewTurn(); // At an invalid time. Ignored. | |
expect(game3.state, GameState.waitingForSecondSelection); | |
}); | |
test("foolish press - removed card", () { | |
game5.pressedCard(index: 3); | |
game5.pressedCard(index: 8); | |
game5.startNewTurn(); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
game5.startNewTurn(); // Invalid time ignored. | |
game5.pressedCard(index: 3); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
game5.startNewTurn(); // Invalid time ignored. | |
game5.pressedCard(index: 8); | |
expect(game5.state, GameState.waitingForFirstSelection); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment