From 0e75c4a3b8f123f98a23f83816dee0507f72084c Mon Sep 17 00:00:00 2001 From: alestiago Date: Mon, 28 Feb 2022 18:09:56 +0000 Subject: [PATCH] feat: tested and improved GameState --- lib/game/bloc/game_state.dart | 5 ++ test/game/bloc/game_state_test.dart | 90 +++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/lib/game/bloc/game_state.dart b/lib/game/bloc/game_state.dart index add81975..7058c0b6 100644 --- a/lib/game/bloc/game_state.dart +++ b/lib/game/bloc/game_state.dart @@ -26,6 +26,11 @@ class GameState extends Equatable { int? score, int? balls, }) { + assert( + score == null || score >= this.score, + "Score can't be decreased", + ); + return GameState( score: score ?? this.score, balls: balls ?? this.balls, diff --git a/test/game/bloc/game_state_test.dart b/test/game/bloc/game_state_test.dart index 066e091e..ae648782 100644 --- a/test/game/bloc/game_state_test.dart +++ b/test/game/bloc/game_state_test.dart @@ -37,27 +37,83 @@ void main() { ); }, ); - }); - group('isGameOver', () { - test( - 'is true ' - 'when no balls are left', () { - const gameState = GameState( - balls: 0, - score: 0, - ); - expect(gameState.isGameOver, isTrue); + 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); + }); }); - test( - 'is false ' - 'when one 1 ball left', () { - const gameState = GameState( - balls: 1, - score: 0, + group('copyWith', () { + test( + 'throws AssertionError ' + 'when scored is decreased', + () { + const gameState = GameState( + balls: 0, + score: 2, + ); + expect( + () => gameState.copyWith(score: gameState.score - 1), + throwsAssertionError, + ); + }, + ); + + test( + 'copies correctly ' + 'when no arguement specified', + () { + const gameState = GameState( + balls: 0, + score: 2, + ); + expect( + gameState.copyWith(), + equals(gameState), + ); + }, + ); + + test( + 'copies correctly ' + 'when all arguements specified', + () { + const gameState = GameState( + score: 2, + balls: 0, + ); + final otherGameState = GameState( + score: gameState.score + 1, + balls: gameState.balls + 1, + ); + expect(gameState, isNot(otherGameState)); + + expect( + gameState.copyWith( + score: otherGameState.score, + balls: otherGameState.balls, + ), + equals(otherGameState), + ); + }, ); - expect(gameState.isGameOver, isFalse); }); }); }