From c7a766d0cad5f201b6a921334ddc1f741211c805 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Thu, 21 Apr 2022 19:48:26 +0200 Subject: [PATCH] test: test for multiplier at game bloc --- test/game/bloc/game_bloc_test.dart | 113 ++++++++++++++++++++++++++++ test/game/bloc/game_event_test.dart | 49 ++++++++++++ test/game/bloc/game_state_test.dart | 28 +++++++ 3 files changed, 190 insertions(+) diff --git a/test/game/bloc/game_bloc_test.dart b/test/game/bloc/game_bloc_test.dart index 37e14f73..0d94b03f 100644 --- a/test/game/bloc/game_bloc_test.dart +++ b/test/game/bloc/game_bloc_test.dart @@ -20,6 +20,7 @@ void main() { expect: () => [ const GameState( score: 0, + multiplier: 1, balls: 2, bonusHistory: [], ), @@ -38,11 +39,13 @@ void main() { expect: () => [ const GameState( score: 2, + multiplier: 1, balls: 3, bonusHistory: [], ), const GameState( score: 5, + multiplier: 1, balls: 3, bonusHistory: [], ), @@ -62,16 +65,19 @@ void main() { expect: () => [ const GameState( score: 0, + multiplier: 1, balls: 2, bonusHistory: [], ), const GameState( score: 0, + multiplier: 1, balls: 1, bonusHistory: [], ), const GameState( score: 0, + multiplier: 1, balls: 0, bonusHistory: [], ), @@ -79,6 +85,110 @@ void main() { ); }); + group('IncreasedMultiplier', () { + blocTest( + 'increases multiplier ' + 'when game is not over', + build: GameBloc.new, + act: (bloc) => bloc + ..add(const IncreasedMultiplier(increase: 1)) + ..add(const IncreasedMultiplier(increase: 1)), + expect: () => [ + const GameState( + score: 0, + multiplier: 2, + balls: 3, + bonusHistory: [], + ), + const GameState( + score: 0, + multiplier: 3, + balls: 3, + bonusHistory: [], + ), + ], + ); + + blocTest( + "doesn't increase multiplier " + 'when game is over', + build: GameBloc.new, + act: (bloc) { + for (var i = 0; i < bloc.state.balls; i++) { + bloc.add(const BallLost()); + } + bloc.add(const IncreasedMultiplier(increase: 1)); + }, + expect: () => [ + const GameState( + score: 0, + multiplier: 1, + balls: 2, + bonusHistory: [], + ), + const GameState( + score: 0, + multiplier: 1, + balls: 1, + bonusHistory: [], + ), + const GameState( + score: 0, + multiplier: 1, + balls: 0, + bonusHistory: [], + ), + ], + ); + }); + + group('AppliedMultiplier', () { + blocTest( + 'apply multiplier to score', + build: GameBloc.new, + seed: () => const GameState( + score: 5, + multiplier: 3, + balls: 2, + bonusHistory: [], + ), + act: (bloc) { + bloc.add(const AppliedMultiplier()); + }, + expect: () => [ + const GameState( + score: 15, + multiplier: 3, + balls: 2, + bonusHistory: [], + ), + ], + ); + }); + + group('ResetMultiplier', () { + blocTest( + 'resets multiplier', + build: GameBloc.new, + seed: () => const GameState( + score: 0, + multiplier: 3, + balls: 2, + bonusHistory: [], + ), + act: (bloc) { + bloc.add(const ResetMultiplier()); + }, + expect: () => [ + const GameState( + score: 0, + multiplier: 1, + balls: 2, + bonusHistory: [], + ), + ], + ); + }); group( 'BonusActivated', () { @@ -91,11 +201,13 @@ void main() { expect: () => const [ GameState( score: 0, + multiplier: 1, balls: 3, bonusHistory: [GameBonus.googleWord], ), GameState( score: 0, + multiplier: 1, balls: 3, bonusHistory: [GameBonus.googleWord, GameBonus.dashNest], ), @@ -112,6 +224,7 @@ void main() { expect: () => const [ GameState( score: 0, + multiplier: 1, balls: 3, bonusHistory: [GameBonus.sparkyTurboCharge], ), diff --git a/test/game/bloc/game_event_test.dart b/test/game/bloc/game_event_test.dart index d7d587bd..94d1c9fb 100644 --- a/test/game/bloc/game_event_test.dart +++ b/test/game/bloc/game_event_test.dart @@ -41,6 +41,55 @@ void main() { }); }); + group('IncreasedMultiplier', () { + test('can be instantiated', () { + expect(const IncreasedMultiplier(increase: 1), isNotNull); + }); + + test('supports value equality', () { + expect( + IncreasedMultiplier(increase: 1), + equals(const IncreasedMultiplier(increase: 1)), + ); + expect( + const IncreasedMultiplier(increase: 1), + isNot(equals(const IncreasedMultiplier(increase: 2))), + ); + }); + + test( + 'throws AssertionError ' + 'when increase is smaller than 1', () { + expect(() => IncreasedMultiplier(increase: 0), throwsAssertionError); + }); + }); + + group('AppliedMultiplier', () { + test('can be instantiated', () { + expect(const AppliedMultiplier(), isNotNull); + }); + + test('supports value equality', () { + expect( + AppliedMultiplier(), + equals(const AppliedMultiplier()), + ); + }); + }); + + group('ResetMultiplier', () { + test('can be instantiated', () { + expect(const ResetMultiplier(), isNotNull); + }); + + test('supports value equality', () { + expect( + ResetMultiplier(), + equals(const ResetMultiplier()), + ); + }); + }); + group('BonusActivated', () { test('can be instantiated', () { expect(const BonusActivated(GameBonus.dashNest), isNotNull); diff --git a/test/game/bloc/game_state_test.dart b/test/game/bloc/game_state_test.dart index 8170346f..7ba58f2f 100644 --- a/test/game/bloc/game_state_test.dart +++ b/test/game/bloc/game_state_test.dart @@ -9,12 +9,14 @@ void main() { expect( GameState( score: 0, + multiplier: 1, balls: 0, bonusHistory: const [], ), equals( const GameState( score: 0, + multiplier: 1, balls: 0, bonusHistory: [], ), @@ -27,6 +29,7 @@ void main() { expect( const GameState( score: 0, + multiplier: 1, balls: 0, bonusHistory: [], ), @@ -43,6 +46,7 @@ void main() { () => GameState( balls: -1, score: 0, + multiplier: 1, bonusHistory: const [], ), throwsAssertionError, @@ -58,6 +62,23 @@ void main() { () => GameState( balls: 0, score: -1, + multiplier: 1, + bonusHistory: const [], + ), + throwsAssertionError, + ); + }, + ); + + test( + 'throws AssertionError ' + 'when multiplier is less than 1', + () { + expect( + () => GameState( + balls: 0, + score: 1, + multiplier: 0, bonusHistory: const [], ), throwsAssertionError, @@ -72,6 +93,7 @@ void main() { const gameState = GameState( balls: 0, score: 0, + multiplier: 1, bonusHistory: [], ); expect(gameState.isGameOver, isTrue); @@ -83,6 +105,7 @@ void main() { const gameState = GameState( balls: 1, score: 0, + multiplier: 1, bonusHistory: [], ); expect(gameState.isGameOver, isFalse); @@ -97,6 +120,7 @@ void main() { const gameState = GameState( balls: 0, score: 2, + multiplier: 1, bonusHistory: [], ); expect( @@ -113,6 +137,7 @@ void main() { const gameState = GameState( balls: 0, score: 2, + multiplier: 1, bonusHistory: [], ); expect( @@ -128,11 +153,13 @@ void main() { () { const gameState = GameState( score: 2, + multiplier: 1, balls: 0, bonusHistory: [], ); final otherGameState = GameState( score: gameState.score + 1, + multiplier: gameState.multiplier + 1, balls: gameState.balls + 1, bonusHistory: const [GameBonus.googleWord], ); @@ -141,6 +168,7 @@ void main() { expect( gameState.copyWith( score: otherGameState.score, + multiplier: otherGameState.multiplier, balls: otherGameState.balls, bonusHistory: otherGameState.bonusHistory, ),