feat: tested and improved GameState

pull/3/head
alestiago 4 years ago
parent 4cb90730a8
commit 7beff5287a

@ -1,14 +1,25 @@
part of 'game_bloc.dart';
/// {@template game_state}
/// Represents the state of the pinball game.
/// {@endtemplate}
class GameState extends Equatable {
/// {@macro game_state}
const GameState({
required this.score,
required this.balls,
});
}) : assert(score >= 0, "Score can't be negative"),
assert(balls >= 0, "Number of balls can't be negative");
/// The current score of the game.
final int score;
/// The number of balls left in the game.
///
/// When the number of balls is 0, the game is over.
final int balls;
/// Determines when the game is over.
bool get isGameOver => balls == 0;
GameState copyWith({

@ -0,0 +1,63 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart';
void main() {
group('GameState', () {
test('supports value equality', () {
expect(
const GameState(score: 0, balls: 0),
equals(const GameState(score: 0, balls: 0)),
);
});
group('constructor', () {
test('can be instantiated', () {
expect(const GameState(score: 0, balls: 0), isNotNull);
});
});
test(
'throws AssertionError '
'when balls are negative',
() {
expect(
() => GameState(balls: -1, score: 0),
throwsAssertionError,
);
},
);
test(
'throws AssertionError '
'when score is negative',
() {
expect(
() => GameState(balls: 0, score: -1),
throwsAssertionError,
);
},
);
});
group('isGameOver', () {
test(
'is true '
'when no balls are left', () {
const gameState = GameState(
balls: 0,
score: 0,
);
expect(gameState.isGameOver, isTrue);
});
test(
'is false '
'when one 1 ball left', () {
const gameState = GameState(
balls: 1,
score: 0,
);
expect(gameState.isGameOver, isFalse);
});
});
}
Loading…
Cancel
Save