From 7beff5287ace9b6e78216422491895c39c73c651 Mon Sep 17 00:00:00 2001 From: alestiago Date: Mon, 28 Feb 2022 17:51:31 +0000 Subject: [PATCH] feat: tested and improved GameState --- lib/game/bloc/game_state.dart | 13 +++++- test/game/bloc/game_state_test.dart | 63 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 test/game/bloc/game_state_test.dart diff --git a/lib/game/bloc/game_state.dart b/lib/game/bloc/game_state.dart index e630d709..add81975 100644 --- a/lib/game/bloc/game_state.dart +++ b/lib/game/bloc/game_state.dart @@ -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({ diff --git a/test/game/bloc/game_state_test.dart b/test/game/bloc/game_state_test.dart new file mode 100644 index 00000000..066e091e --- /dev/null +++ b/test/game/bloc/game_state_test.dart @@ -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); + }); + }); +}