From 02d21fb7feacb619c978380f2670195afb5d6e5f Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 31 Mar 2022 14:22:30 +0100 Subject: [PATCH] fix: solved tests --- lib/game/components/controlled_ball.dart | 4 +- .../game/components/controlled_ball_test.dart | 236 ++++++++++-------- test/helpers/test_game.dart | 3 +- 3 files changed, 140 insertions(+), 103 deletions(-) diff --git a/lib/game/components/controlled_ball.dart b/lib/game/components/controlled_ball.dart index 11a1660d..257d4f1d 100644 --- a/lib/game/components/controlled_ball.dart +++ b/lib/game/components/controlled_ball.dart @@ -81,14 +81,14 @@ class LaunchedBallController extends BallController @override bool listenWhen(GameState? previousState, GameState newState) { - return (previousState?.balls ?? 0) < newState.balls; + return (previousState?.balls ?? 0) > newState.balls; } @override void onNewState(GameState state) { super.onNewState(state); component.shouldRemove = true; - if (state.balls > 0) gameRef.spawnBall(); + if (state.balls > 1) gameRef.spawnBall(); } /// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if diff --git a/test/game/components/controlled_ball_test.dart b/test/game/components/controlled_ball_test.dart index 737500b4..55f129df 100644 --- a/test/game/components/controlled_ball_test.dart +++ b/test/game/components/controlled_ball_test.dart @@ -1,6 +1,9 @@ // ignore_for_file: cascade_invocations +import 'dart:math'; + import 'package:bloc_test/bloc_test.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter/painting.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -22,6 +25,13 @@ void main() { ball = Ball(baseColor: const Color(0xFF00FFFF)); }); + test('can be instantiated', () { + expect( + BonusBallController(ball), + isA(), + ); + }); + flameTester.test( 'lost removes ball', (game) async { @@ -39,77 +49,20 @@ void main() { }); group('LaunchedBallController', () { - late Ball ball; - late GameBloc gameBloc; - - setUp(() { - ball = Ball(baseColor: const Color(0xFF00FFFF)); - gameBloc = MockGameBloc(); - whenListen( - gameBloc, - const Stream.empty(), - initialState: const GameState.initial(), + test('can be instantiated', () { + expect( + LaunchedBallController(MockBall()), + isA(), ); }); - final tester = flameBlocTester( - game: PinballGameTest.create, - gameBloc: () => gameBloc, - ); - - flameTester.testGameWidget( - 'lost adds BallLost to GameBloc', - setUp: (game, tester) async { - final controller = LaunchedBallController(ball); - await ball.add(controller); - await game.add(ball); - await game.ready(); - - controller.lost(); - }, - verify: (game, tester) async { - verify(() => gameBloc.add(const BallLost())).called(1); - }, - ); - - group('listenWhen', () { - flameTester.testGameWidget( - 'listens when a ball has been lost', - verify: (game, tester) async { - final controller = LaunchedBallController(ball); - await ball.add(controller); - await game.add(ball); - await game.ready(); - - final previousState = MockGameState(); - final newState = MockGameState(); - when(() => previousState.balls).thenReturn(3); - when(() => newState.balls).thenReturn(2); - - expect(controller.listenWhen(previousState, newState), isTrue); - }, - ); - - flameTester.testGameWidget( - 'does not listen when a ball has not been lost', - verify: (game, tester) async { - final controller = LaunchedBallController(ball); - await ball.add(controller); - await game.add(ball); - await game.ready(); + group('description', () { + late Ball ball; + late GameBloc gameBloc; - final previousState = MockGameState(); - final newState = MockGameState(); - when(() => previousState.balls).thenReturn(3); - when(() => newState.balls).thenReturn(3); - - expect(controller.listenWhen(previousState, newState), isTrue); - }, - ); - }); - - group('onNewState', () { setUp(() { + ball = Ball(baseColor: const Color(0xFF00FFFF)); + gameBloc = MockGameBloc(); whenListen( gameBloc, const Stream.empty(), @@ -117,55 +70,138 @@ void main() { ); }); - tester.testGameWidget( - 'removes ball', - setUp: (game, _) => game.ready(), - verify: (game, tester) async { - final controller = LaunchedBallController(ball); - await game.add(ball); - await game.ready(); - - final state = MockGameState(); - when(() => state.balls).thenReturn(any()); - controller.onNewState(MockGameState()); - - expect(game.contains(ball), isFalse); - }, + final tester = flameBlocTester( + game: PinballGameTest.create, + gameBloc: () => gameBloc, ); tester.testGameWidget( - 'spawns a new ball when the ball is not the last one', + 'lost adds BallLost to GameBloc', setUp: (game, tester) async { final controller = LaunchedBallController(ball); + await ball.add(controller); await game.add(ball); await game.ready(); - final state = MockGameState(); - when(() => state.balls).thenReturn(2); - - controller.onNewState(state); + controller.lost(); }, verify: (game, tester) async { - expect(game.contains(ball), isFalse); + verify(() => gameBloc.add(const BallLost())).called(1); }, ); - tester.testGameWidget( - 'does not spawn a new ball is the last one', - setUp: (game, tester) async { - final controller = LaunchedBallController(ball); - await game.add(ball); - await game.ready(); + group('listenWhen', () { + tester.testGameWidget( + 'listens when a ball has been lost', + setUp: (game, tester) async { + final controller = LaunchedBallController(ball); + + await ball.add(controller); + await game.add(ball); + await game.ready(); + }, + verify: (game, tester) async { + final controller = + game.descendants().whereType().first; + + final previousState = MockGameState(); + final newState = MockGameState(); + when(() => previousState.balls).thenReturn(3); + when(() => newState.balls).thenReturn(2); + + expect(controller.listenWhen(previousState, newState), isTrue); + }, + ); - final state = MockGameState(); - when(() => state.balls).thenReturn(1); + tester.testGameWidget( + 'does not listen when a ball has not been lost', + setUp: (game, tester) async { + final controller = LaunchedBallController(ball); + + await ball.add(controller); + await game.add(ball); + await game.ready(); + }, + verify: (game, tester) async { + final controller = + game.descendants().whereType().first; + + final previousState = MockGameState(); + final newState = MockGameState(); + when(() => previousState.balls).thenReturn(3); + when(() => newState.balls).thenReturn(3); + + expect(controller.listenWhen(previousState, newState), isFalse); + }, + ); + }); - controller.onNewState(state); - }, - verify: (game, tester) async { - expect(game.contains(ball), isFalse); - }, - ); + group('onNewState', () { + tester.testGameWidget( + 'removes ball', + setUp: (game, tester) async { + final controller = LaunchedBallController(ball); + await ball.add(controller); + await game.add(ball); + await game.ready(); + + final state = MockGameState(); + when(() => state.balls).thenReturn(1); + controller.onNewState(state); + await game.ready(); + }, + verify: (game, tester) async { + expect(game.contains(ball), isFalse); + }, + ); + + tester.testGameWidget( + 'spawns a new ball when the ball is not the last one', + setUp: (game, tester) async { + final controller = LaunchedBallController(ball); + await ball.add(controller); + await game.add(ball); + await game.ready(); + + final state = MockGameState(); + when(() => state.balls).thenReturn(2); + + final previousBalls = game.descendants().whereType().toList(); + controller.onNewState(state); + await game.ready(); + + final currentBalls = game.descendants().whereType(); + + // TODO(erickzanardo): This expect should be in verify? + expect(currentBalls.length, equals(previousBalls.length)); + }, + ); + + tester.testGameWidget( + 'does not spawn a new ball is the last one', + setUp: (game, tester) async { + final controller = LaunchedBallController(ball); + await ball.add(controller); + await game.add(ball); + await game.ready(); + + final state = MockGameState(); + when(() => state.balls).thenReturn(1); + + final previousBalls = game.descendants().whereType().toList(); + controller.onNewState(state); + await game.ready(); + + final currentBalls = game.descendants().whereType(); + + // TODO(erickzanardo): This expect should be in verify? + expect( + currentBalls.length, + equals((previousBalls..remove(ball)).length), + ); + }, + ); + }); }); }); } diff --git a/test/helpers/test_game.dart b/test/helpers/test_game.dart index a1219868..3c6ff42f 100644 --- a/test/helpers/test_game.dart +++ b/test/helpers/test_game.dart @@ -1,6 +1,7 @@ +import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -class TestGame extends Forge2DGame { +class TestGame extends Forge2DGame with FlameBloc { TestGame() { images.prefix = ''; }