From 2e36c805bbb42101185fe21a27a54425a753154d Mon Sep 17 00:00:00 2001 From: alestiago Date: Tue, 12 Apr 2022 08:36:48 +0100 Subject: [PATCH] feat: tested and made classes private --- lib/game/components/google_word.dart | 9 ++-- lib/game/pinball_game.dart | 1 - test/game/components/google_word_test.dart | 62 ++++++++++++++++++---- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/lib/game/components/google_word.dart b/lib/game/components/google_word.dart index 73e78361..655b7d5c 100644 --- a/lib/game/components/google_word.dart +++ b/lib/game/components/google_word.dart @@ -4,7 +4,6 @@ import 'dart:async'; import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flutter/material.dart'; import 'package:pinball/flame/flame.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -26,6 +25,7 @@ class GoogleWord extends Component @override Future onLoad() async { await super.onLoad(); + gameRef.addContactCallback(_GoogleLetterBallContactCallback()); final offsets = [ Vector2(-12.92, -1.82), @@ -73,11 +73,10 @@ class _GoogleWordController extends ComponentController } /// Activates a [GoogleLetter] when it contacts with a [Ball]. -@visibleForTesting -class BonusLetterBallContactCallback - extends ContactCallback { +class _GoogleLetterBallContactCallback + extends ContactCallback { @override - void begin(Ball ball, GoogleLetter googleLetter, Contact contact) { + void begin(GoogleLetter googleLetter, _, __) { (googleLetter.parent! as GoogleWord) .controller .activate(googleLetter.index); diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 63165c66..71dbf1f1 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -72,7 +72,6 @@ class PinballGame extends Forge2DGame void _addContactCallbacks() { addContactCallback(BallScorePointsCallback(this)); addContactCallback(BottomWallBallContactCallback()); - addContactCallback(BonusLetterBallContactCallback()); } Future _addGameBoundaries() async { diff --git a/test/game/components/google_word_test.dart b/test/game/components/google_word_test.dart index ec687982..d6d5f8ff 100644 --- a/test/game/components/google_word_test.dart +++ b/test/game/components/google_word_test.dart @@ -1,8 +1,13 @@ // 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/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:mockingjay/mockingjay.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -10,26 +15,65 @@ import '../../helpers/helpers.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - final flameTester = FlameTester(EmptyPinballGameTest.new); group('GoogleWord', () { - const googleWord = 'Google'; + late GameBloc gameBloc; + + setUp(() { + gameBloc = MockGameBloc(); + whenListen( + gameBloc, + const Stream.empty(), + initialState: const GameState.initial(), + ); + }); + + final flameTester = FlameTester(EmptyPinballGameTest.new); + final flameBlocTester = FlameBlocTester( + gameBuilder: EmptyPinballGameTest.new, + blocBuilder: () => gameBloc, + ); flameTester.test( 'loads the letters correctly', (game) async { - final bonusWord = GoogleWord( - position: Vector2.zero(), - ); - await game.ensureAdd(bonusWord); + const word = 'Google'; + final googleWord = GoogleWord(position: Vector2.zero()); + await game.ensureAdd(googleWord); - final letters = bonusWord.children.whereType(); - expect(letters.length, equals(googleWord.length)); + final letters = googleWord.children.whereType(); + expect(letters.length, equals(word.length)); - for (var index = 0; index < googleWord.length; index++) { + for (var index = 0; index < word.length; index++) { expect(letters.elementAt(index).index, equals(index)); } }, ); + + flameBlocTester.testGameWidget( + 'adds GameBonus.word to the game when all letters are activated', + setUp: (game, _) async { + final ball = Ball(baseColor: const Color(0xFFFF0000)); + final googleWord = GoogleWord(position: Vector2.zero()); + await game.ensureAddAll([googleWord, ball]); + + final letters = googleWord.children.whereType(); + expect(letters, isNotEmpty); + for (final letter in letters) { + beginContact(game, letter, ball); + await game.ready(); + + if (letter == letters.last) { + verify( + () => gameBloc.add(const BonusActivated(GameBonus.word)), + ).called(1); + } else { + verifyNever( + () => gameBloc.add(const BonusActivated(GameBonus.word)), + ); + } + } + }, + ); }); }