fix: solved tests

pull/116/head
alestiago 4 years ago
parent 265513efeb
commit 02d21fb7fe

@ -81,14 +81,14 @@ class LaunchedBallController extends BallController
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
return (previousState?.balls ?? 0) < newState.balls; return (previousState?.balls ?? 0) > newState.balls;
} }
@override @override
void onNewState(GameState state) { void onNewState(GameState state) {
super.onNewState(state); super.onNewState(state);
component.shouldRemove = true; 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 /// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if

@ -1,6 +1,9 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'dart:math';
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -22,6 +25,13 @@ void main() {
ball = Ball(baseColor: const Color(0xFF00FFFF)); ball = Ball(baseColor: const Color(0xFF00FFFF));
}); });
test('can be instantiated', () {
expect(
BonusBallController(ball),
isA<LaunchedBallController>(),
);
});
flameTester.test( flameTester.test(
'lost removes ball', 'lost removes ball',
(game) async { (game) async {
@ -39,77 +49,20 @@ void main() {
}); });
group('LaunchedBallController', () { group('LaunchedBallController', () {
late Ball ball; test('can be instantiated', () {
late GameBloc gameBloc; expect(
LaunchedBallController(MockBall()),
setUp(() { isA<LaunchedBallController>(),
ball = Ball(baseColor: const Color(0xFF00FFFF));
gameBloc = MockGameBloc();
whenListen(
gameBloc,
const Stream<GameState>.empty(),
initialState: const GameState.initial(),
); );
}); });
final tester = flameBlocTester<PinballGame>( group('description', () {
game: PinballGameTest.create, late Ball ball;
gameBloc: () => gameBloc, late 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();
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(() { setUp(() {
ball = Ball(baseColor: const Color(0xFF00FFFF));
gameBloc = MockGameBloc();
whenListen( whenListen(
gameBloc, gameBloc,
const Stream<GameState>.empty(), const Stream<GameState>.empty(),
@ -117,55 +70,138 @@ void main() {
); );
}); });
tester.testGameWidget( final tester = flameBlocTester<PinballGame>(
'removes ball', game: PinballGameTest.create,
setUp: (game, _) => game.ready(), gameBloc: () => gameBloc,
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);
},
); );
tester.testGameWidget( tester.testGameWidget(
'spawns a new ball when the ball is not the last one', 'lost adds BallLost to GameBloc',
setUp: (game, tester) async { setUp: (game, tester) async {
final controller = LaunchedBallController(ball); final controller = LaunchedBallController(ball);
await ball.add(controller);
await game.add(ball); await game.add(ball);
await game.ready(); await game.ready();
final state = MockGameState(); controller.lost();
when(() => state.balls).thenReturn(2);
controller.onNewState(state);
}, },
verify: (game, tester) async { verify: (game, tester) async {
expect(game.contains(ball), isFalse); verify(() => gameBloc.add(const BallLost())).called(1);
}, },
); );
tester.testGameWidget( group('listenWhen', () {
'does not spawn a new ball is the last one', tester.testGameWidget(
setUp: (game, tester) async { 'listens when a ball has been lost',
final controller = LaunchedBallController(ball); setUp: (game, tester) async {
await game.add(ball); final controller = LaunchedBallController(ball);
await game.ready();
await ball.add(controller);
await game.add(ball);
await game.ready();
},
verify: (game, tester) async {
final controller =
game.descendants().whereType<LaunchedBallController>().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(); tester.testGameWidget(
when(() => state.balls).thenReturn(1); '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<LaunchedBallController>().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); group('onNewState', () {
}, tester.testGameWidget(
verify: (game, tester) async { 'removes ball',
expect(game.contains(ball), isFalse); 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<Ball>().toList();
controller.onNewState(state);
await game.ready();
final currentBalls = game.descendants().whereType<Ball>();
// 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<Ball>().toList();
controller.onNewState(state);
await game.ready();
final currentBalls = game.descendants().whereType<Ball>();
// TODO(erickzanardo): This expect should be in verify?
expect(
currentBalls.length,
equals((previousBalls..remove(ball)).length),
);
},
);
});
}); });
}); });
} }

@ -1,6 +1,7 @@
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
class TestGame extends Forge2DGame { class TestGame extends Forge2DGame with FlameBloc {
TestGame() { TestGame() {
images.prefix = ''; images.prefix = '';
} }

Loading…
Cancel
Save