bubbletwist/test/unit_test.dart

139 lines
3.8 KiB
Dart
Raw Normal View History

2024-06-19 23:02:07 +02:00
import 'package:bubbletwist/enums/stone_color.dart';
import 'package:bubbletwist/game/board.dart';
import 'package:bubbletwist/game/game.dart';
import 'package:bubbletwist/game/i_game_consumer.dart';
import 'package:bubbletwist/game/stone/stone.dart';
import 'package:bubbletwist/game/stone/stone_location.dart';
import 'package:test/test.dart';
void main() {
group('Game Tests', () {
late Game game;
late MockGameConsumer mockGameConsumer;
setUp(() {
mockGameConsumer = MockGameConsumer();
game = Game(mockGameConsumer);
});
tearDown(() {
game.stop(); // Ensure game is stopped after each test
});
test('Initial points should be 0', () {
expect(game.getPoints(), 0);
});
test('Initial time should be equal to startTime', () {
expect(game.getTimeInSeconds(), game.startTime);
});
test('Start method should start the game', () {
game.start();
expect(game.isRunning(), isTrue);
});
test('Stop method should stop the game', () {
game.start();
game.stop();
expect(game.isRunning(), isFalse);
});
test('AddPoints should update points if game is running', () {
game.start();
game.addPoints(10);
expect(game.getPoints(), 10);
});
});
group('Board Tests', () {
late Game game;
late Board board;
setUp(() {
game = Game(MockGameConsumer());
board = Board(game);
});
tearDown(() {
game.stop(); // Ensure game is stopped after each test
});
test('Swap stones should correctly swap positions', () {
// Arrange
// Arrange: Create a board with a match
board.stones[0][0]!.setColor(StoneColors.red);
board.stones[0][1]!.setColor(StoneColors.red);
board.stones[0][2]!.setColor(StoneColors.green);
board.stones[1][2]!.setColor(StoneColors.red);
StoneLocation sl1 = StoneLocation(row: 0, column: 2);
StoneLocation sl2 = StoneLocation(row: 1, column: 2);
Stone? originalStone1 = board.getStone(sl1);
Stone? originalStone2 = board.getStone(sl2);
// Act
board.swap(sl1, sl2);
// Assert
expect(board.getStone(sl1), originalStone2);
expect(board.getStone(sl2), originalStone1);
});
test('CheckBoard should detect and remove matches vertically', () {
// Arrange: Create a board with a match
board.stones[0][0]!.setColor(StoneColors.red);
board.stones[0][1]!.setColor(StoneColors.red);
board.stones[0][2]!.setColor(StoneColors.red);
// Act
bool boardChanged = board.checkBoard();
// Assert
expect(boardChanged, isTrue);
});
test('CheckBoard should detect and remove matches horizontally', () {
// Arrange: Create a board with a match
board.stones[0][0]!.setColor(StoneColors.red);
board.stones[1][0]!.setColor(StoneColors.red);
board.stones[2][0]!.setColor(StoneColors.red);
// Act
bool boardChanged = board.checkBoard();
// Assert
expect(boardChanged, isTrue);
});
test('ApplyGravity should move stones down and refill', () {
// Arrange: Create a board with some empty spaces
board.stones[0][0] = null;
board.stones[1][0] = null;
// Act
board.applyGravity();
// Assert
// Verify that stones have fallen down and all empty spaces are filled
expect(board.getStone(StoneLocation(row: 0, column: 0)), isNotNull);
expect(board.getStone(StoneLocation(row: 1, column: 0)), isNotNull);
expect(board.getStone(StoneLocation(row: 2, column: 0)), isNotNull);
});
});
}
// Mock class for IGameConsumer
class MockGameConsumer implements IGameConsumer {
@override
void gameStopped() {}
@override
void updatePoints() {}
@override
void updateStones() {}
@override
void updateTime() {}
}