refactor: test with an empty game (#147)

* refactor: used EmptyPinballGame

* refactor: removed extension

* refactor: used EmptyPinballGame

* fix: merge conflicts
pull/149/head
Alejandro Santiago 4 years ago committed by GitHub
parent 625e033709
commit 1a8f534ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,8 +22,6 @@ class PinballGame extends Forge2DGame
final PinballAudio audio; final PinballAudio audio;
late final Plunger plunger;
@override @override
void onAttach() { void onAttach() {
super.onAttach(); super.onAttach();
@ -73,7 +71,7 @@ class PinballGame extends Forge2DGame
} }
Future<void> _addPlunger() async { Future<void> _addPlunger() async {
plunger = Plunger(compressionDistance: 29) final plunger = Plunger(compressionDistance: 29)
..initialPosition = ..initialPosition =
BoardDimensions.bounds.center.toVector2() + Vector2(41.5, -49); BoardDimensions.bounds.center.toVector2() + Vector2(41.5, -49);
await add(plunger); await add(plunger);
@ -90,14 +88,20 @@ class PinballGame extends Forge2DGame
); );
} }
void spawnBall() { Future<void> spawnBall() async {
// TODO(alestiago): Remove once this logic is moved to controller.
var plunger = firstChild<Plunger>();
if (plunger == null) {
await add(plunger = Plunger(compressionDistance: 1));
}
final ball = ControlledBall.launch( final ball = ControlledBall.launch(
theme: theme, theme: theme,
)..initialPosition = Vector2( )..initialPosition = Vector2(
plunger.body.position.x, plunger.body.position.x,
plunger.body.position.y + Ball.size.y, plunger.body.position.y + Ball.size.y,
); );
add(ball); await add(ball);
} }
} }

@ -9,7 +9,7 @@ import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
group('Board', () { group('Board', () {
flameTester.test( flameTester.test(
@ -78,7 +78,6 @@ void main() {
flameTester.test( flameTester.test(
'one FlutterForest', 'one FlutterForest',
(game) async { (game) async {
// TODO(alestiago): change to [NestBumpers] once provided.
final board = Board(); final board = Board();
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);

@ -14,16 +14,18 @@ import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
group('BonusWord', () { group('BonusWord', () {
flameTester.test( flameTester.test(
'loads the letters correctly', 'loads the letters correctly',
(game) async { (game) async {
await game.ready(); final bonusWord = BonusWord(
position: Vector2.zero(),
);
await game.ensureAdd(bonusWord);
final bonusWord = game.children.whereType<BonusWord>().first; final letters = bonusWord.descendants().whereType<BonusLetter>();
final letters = bonusWord.children.whereType<BonusLetter>();
expect(letters.length, equals(GameBloc.bonusWord.length)); expect(letters.length, equals(GameBloc.bonusWord.length));
}, },
); );
@ -135,7 +137,7 @@ void main() {
}); });
group('BonusLetter', () { group('BonusLetter', () {
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
@ -215,8 +217,7 @@ void main() {
late PinballAudio pinballAudio; late PinballAudio pinballAudio;
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
// TODO(alestiago): Use TestGame once BonusLetter has controller. gameBuilder: EmptyPinballGameTest.new,
gameBuilder: PinballGameTest.create,
blocBuilder: () => gameBloc, blocBuilder: () => gameBloc,
repositories: () => [ repositories: () => [
RepositoryProvider<PinballAudio>.value(value: pinballAudio), RepositoryProvider<PinballAudio>.value(value: pinballAudio),
@ -238,14 +239,20 @@ void main() {
flameBlocTester.testGameWidget( flameBlocTester.testGameWidget(
'adds BonusLetterActivated to GameBloc when not activated', 'adds BonusLetterActivated to GameBloc when not activated',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); final bonusWord = BonusWord(
final bonusLetter = game.descendants().whereType<BonusLetter>().first; position: Vector2.zero(),
);
await game.ensureAdd(bonusWord);
final bonusLetters =
game.descendants().whereType<BonusLetter>().toList();
for (var index = 0; index < bonusLetters.length; index++) {
final bonusLetter = bonusLetters[index];
bonusLetter.activate(); bonusLetter.activate();
await game.ready(); await game.ready();
},
verify: (game, tester) async { verify(() => gameBloc.add(BonusLetterActivated(index))).called(1);
verify(() => gameBloc.add(const BonusLetterActivated(0))).called(1); }
}, },
); );
@ -309,25 +316,33 @@ void main() {
); );
flameBlocTester.testGameWidget( flameBlocTester.testGameWidget(
'only listens when there is a change on the letter status', 'listens when there is a change on the letter status',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); final bonusWord = BonusWord(
final bonusLetter = game.descendants().whereType<BonusLetter>().first; position: Vector2.zero(),
);
await game.ensureAdd(bonusWord);
final bonusLetters =
game.descendants().whereType<BonusLetter>().toList();
for (var index = 0; index < bonusLetters.length; index++) {
final bonusLetter = bonusLetters[index];
bonusLetter.activate(); bonusLetter.activate();
}, await game.ready();
verify: (game, tester) async {
const state = GameState( final state = GameState(
score: 0, score: 0,
balls: 2, balls: 2,
activatedBonusLetters: [0], activatedBonusLetters: [index],
activatedDashNests: {}, activatedDashNests: const {},
bonusHistory: [], bonusHistory: const [],
); );
final bonusLetter = game.descendants().whereType<BonusLetter>().first;
expect( expect(
bonusLetter.listenWhen(const GameState.initial(), state), bonusLetter.listenWhen(const GameState.initial(), state),
isTrue, isTrue,
); );
}
}, },
); );
}); });

@ -13,7 +13,7 @@ import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
group('BonusBallController', () { group('BonusBallController', () {
late Ball ball; late Ball ball;
@ -67,7 +67,7 @@ void main() {
}); });
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
gameBuilder: PinballGameTest.create, gameBuilder: EmptyPinballGameTest.new,
blocBuilder: () => gameBloc, blocBuilder: () => gameBloc,
); );
@ -155,13 +155,13 @@ void main() {
await game.ensureAdd(ball); await game.ensureAdd(ball);
final state = MockGameState(); final state = MockGameState();
when(() => state.balls).thenReturn(2); when(() => state.balls).thenReturn(1);
final previousBalls = game.descendants().whereType<Ball>().toList(); final previousBalls = game.descendants().whereType<Ball>().toList();
controller.onNewState(state); controller.onNewState(state);
await game.ready(); await game.ready();
final currentBalls = game.descendants().whereType<Ball>(); final currentBalls = game.descendants().whereType<Ball>().toList();
expect(currentBalls.contains(ball), isFalse); expect(currentBalls.contains(ball), isFalse);
expect(currentBalls.length, equals(previousBalls.length)); expect(currentBalls.length, equals(previousBalls.length));

@ -10,7 +10,7 @@ import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
group('FlipperController', () { group('FlipperController', () {
group('onKeyEvent', () { group('onKeyEvent', () {

@ -25,7 +25,7 @@ void beginContact(Forge2DGame game, BodyComponent bodyA, BodyComponent bodyB) {
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(EmptyPinballGameTest.new);
group('FlutterForest', () { group('FlutterForest', () {
flameTester.test( flameTester.test(
@ -146,16 +146,15 @@ void main() {
}); });
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>( final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
gameBuilder: PinballGameTest.create, gameBuilder: EmptyPinballGameTest.new,
blocBuilder: () => gameBloc, blocBuilder: () => gameBloc,
); );
flameBlocTester.testGameWidget( flameBlocTester.testGameWidget(
'add DashNestActivated event', 'add DashNestActivated event',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ready(); final flutterForest = FlutterForest();
final flutterForest = await game.ensureAdd(flutterForest);
game.descendants().whereType<FlutterForest>().first;
await game.ensureAdd(ball); await game.ensureAdd(ball);
final bumpers = final bumpers =
@ -177,15 +176,16 @@ void main() {
final flutterForest = FlutterForest(); final flutterForest = FlutterForest();
await game.ensureAdd(flutterForest); await game.ensureAdd(flutterForest);
await game.ensureAdd(ball); await game.ensureAdd(ball);
game.addContactCallback(BallScorePointsCallback(game));
final bumpers = final bumpers = flutterForest.descendants().whereType<ScorePoints>();
flutterForest.descendants().whereType<DashNestBumper>();
for (final bumper in bumpers) { for (final bumper in bumpers) {
beginContact(game, bumper, ball); beginContact(game, bumper, ball);
final points = (bumper as ScorePoints).points;
verify( verify(
() => gameBloc.add(Scored(points: points)), () => gameBloc.add(
Scored(points: bumper.points),
),
).called(1); ).called(1);
} }
}, },

@ -12,8 +12,8 @@ import '../helpers/helpers.dart';
void main() { void main() {
group('PinballGame', () { group('PinballGame', () {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.new);
final debugModeFlameTester = FlameTester(DebugPinballGameTest.create); final debugModeFlameTester = FlameTester(DebugPinballGameTest.new);
// TODO(alestiago): test if [PinballGame] registers // TODO(alestiago): test if [PinballGame] registers
// [BallScorePointsCallback] once the following issue is resolved: // [BallScorePointsCallback] once the following issue is resolved:

@ -3,24 +3,27 @@ import 'package:pinball_theme/pinball_theme.dart';
import 'helpers.dart'; import 'helpers.dart';
/// [PinballGame] extension to reduce boilerplate in tests. class PinballGameTest extends PinballGame {
extension PinballGameTest on PinballGame { PinballGameTest()
/// Create [PinballGame] with default [PinballTheme]. : super(
static PinballGame create() => PinballGame( audio: MockPinballAudio(),
theme: const PinballTheme( theme: const PinballTheme(
characterTheme: DashTheme(), characterTheme: DashTheme(),
), ),
audio: MockPinballAudio(), );
)..images.prefix = '';
} }
/// [DebugPinballGame] extension to reduce boilerplate in tests. class DebugPinballGameTest extends DebugPinballGame {
extension DebugPinballGameTest on DebugPinballGame { DebugPinballGameTest()
/// Create [PinballGame] with default [PinballTheme]. : super(
static DebugPinballGame create() => DebugPinballGame( audio: MockPinballAudio(),
theme: const PinballTheme( theme: const PinballTheme(
characterTheme: DashTheme(), characterTheme: DashTheme(),
), ),
audio: MockPinballAudio(),
); );
} }
class EmptyPinballGameTest extends PinballGameTest {
@override
Future<void> onLoad() async {}
}

Loading…
Cancel
Save