From 5a3654c5d0438ce577f104688d7c1c1cc4278e99 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Mon, 2 May 2022 15:56:02 -0500 Subject: [PATCH] refactor: PR suggestions --- .../android_spaceship_bonus_behavior.dart | 17 +++++++++-------- ...hip_entrance_ball_contact_behavior.dart.dart | 2 +- .../cubit/android_spaceship_cubit.dart | 6 +++--- .../cubit/android_spaceship_state.dart | 4 ++-- .../android_spaceship_test.dart | 9 +++++---- ...hip_entrance_ball_contact_behavior_test.dart | 6 +++--- .../cubit/android_spaceship_cubit_test.dart | 10 +++++----- .../android_spaceship_bonus_behavior_test.dart | 4 ++-- 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lib/game/components/android_acres/behaviors/android_spaceship_bonus_behavior.dart b/lib/game/components/android_acres/behaviors/android_spaceship_bonus_behavior.dart index 743c8fa6..833ac8e4 100644 --- a/lib/game/components/android_acres/behaviors/android_spaceship_bonus_behavior.dart +++ b/lib/game/components/android_acres/behaviors/android_spaceship_bonus_behavior.dart @@ -3,7 +3,7 @@ import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; -/// Adds a [GameBonus.androidSpaceship] when [AndroidSpaceship] is activated. +/// Adds a [GameBonus.androidSpaceship] when [AndroidSpaceship] has a bonus. class AndroidSpaceshipBonusBehavior extends Component with HasGameRef, ParentIsA { @override @@ -14,13 +14,14 @@ class AndroidSpaceshipBonusBehavior extends Component // TODO(alestiago): Refactor subscription management once the following is // merged: // https://github.com/flame-engine/flame/pull/1538 - androidSpaceship.bloc.stream.listen((_) { - if (androidSpaceship.bloc.state == AndroidSpaceshipState.activated) { - gameRef - .read() - .add(const BonusActivated(GameBonus.androidSpaceship)); - androidSpaceship.bloc.onBonusAwarded(); - } + androidSpaceship.bloc.stream.listen((state) { + final listenWhen = state == AndroidSpaceshipState.withBonus; + if (!listenWhen) return; + + gameRef + .read() + .add(const BonusActivated(GameBonus.androidSpaceship)); + androidSpaceship.bloc.onBonusAwarded(); }); } } diff --git a/packages/pinball_components/lib/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior.dart.dart b/packages/pinball_components/lib/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior.dart.dart index 41d2935e..58a8b3c3 100644 --- a/packages/pinball_components/lib/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior.dart.dart +++ b/packages/pinball_components/lib/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior.dart.dart @@ -11,6 +11,6 @@ class AndroidSpaceshipEntranceBallContactBehavior super.beginContact(other, contact); if (other is! Ball) return; - parent.parent.bloc.onEntered(); + parent.parent.bloc.onBallEntered(); } } diff --git a/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_cubit.dart b/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_cubit.dart index a95ff5fe..ad9de251 100644 --- a/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_cubit.dart +++ b/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_cubit.dart @@ -5,9 +5,9 @@ import 'package:bloc/bloc.dart'; part 'android_spaceship_state.dart'; class AndroidSpaceshipCubit extends Cubit { - AndroidSpaceshipCubit() : super(AndroidSpaceshipState.idle); + AndroidSpaceshipCubit() : super(AndroidSpaceshipState.withoutBonus); - void onEntered() => emit(AndroidSpaceshipState.activated); + void onBallEntered() => emit(AndroidSpaceshipState.withBonus); - void onBonusAwarded() => emit(AndroidSpaceshipState.idle); + void onBonusAwarded() => emit(AndroidSpaceshipState.withoutBonus); } diff --git a/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_state.dart b/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_state.dart index 75082ab0..aae41c17 100644 --- a/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_state.dart +++ b/packages/pinball_components/lib/src/components/android_spaceship/cubit/android_spaceship_state.dart @@ -3,6 +3,6 @@ part of 'android_spaceship_cubit.dart'; enum AndroidSpaceshipState { - idle, - activated, + withoutBonus, + withBonus, } diff --git a/packages/pinball_components/test/src/components/android_spaceship/android_spaceship_test.dart b/packages/pinball_components/test/src/components/android_spaceship/android_spaceship_test.dart index 07a4514b..1b672be4 100644 --- a/packages/pinball_components/test/src/components/android_spaceship/android_spaceship_test.dart +++ b/packages/pinball_components/test/src/components/android_spaceship/android_spaceship_test.dart @@ -41,6 +41,7 @@ void main() { await tester.pump(); }, verify: (game, tester) async { + const goldenFilePath = '../golden/android_spaceship/'; final animationDuration = game .descendants() .whereType() @@ -50,21 +51,21 @@ void main() { await expectLater( find.byGame(), - matchesGoldenFile('../golden/android_spaceship/start.png'), + matchesGoldenFile('${goldenFilePath}start.png'), ); game.update(animationDuration * 0.5); await tester.pump(); await expectLater( find.byGame(), - matchesGoldenFile('../golden/android_spaceship/middle.png'), + matchesGoldenFile('${goldenFilePath}middle.png'), ); game.update(animationDuration * 0.5); await tester.pump(); await expectLater( find.byGame(), - matchesGoldenFile('../golden/android_spaceship/end.png'), + matchesGoldenFile('${goldenFilePath}end.png'), ); }, ); @@ -77,7 +78,7 @@ void main() { whenListen( bloc, const Stream.empty(), - initialState: AndroidSpaceshipState.idle, + initialState: AndroidSpaceshipState.withoutBonus, ); when(bloc.close).thenAnswer((_) async {}); final androidSpaceship = AndroidSpaceship.test(bloc: bloc); diff --git a/packages/pinball_components/test/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior_test.dart b/packages/pinball_components/test/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior_test.dart index a0be58e6..45a38e8d 100644 --- a/packages/pinball_components/test/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior_test.dart +++ b/packages/pinball_components/test/src/components/android_spaceship/behaviors/android_spaceship_entrance_ball_contact_behavior_test.dart @@ -27,14 +27,14 @@ void main() { }); flameTester.test( - 'beginContact emits onEntered when entrance contacts with a ball', + 'beginContact calls onBallEntered when entrance contacts with a ball', (game) async { final behavior = AndroidSpaceshipEntranceBallContactBehavior(); final bloc = _MockAndroidSpaceshipCubit(); whenListen( bloc, const Stream.empty(), - initialState: AndroidSpaceshipState.idle, + initialState: AndroidSpaceshipState.withoutBonus, ); final entrance = AndroidSpaceshipEntrance(); @@ -47,7 +47,7 @@ void main() { behavior.beginContact(MockBall(), MockContact()); - verify(androidSpaceship.bloc.onEntered).called(1); + verify(androidSpaceship.bloc.onBallEntered).called(1); }, ); }, diff --git a/packages/pinball_components/test/src/components/android_spaceship/cubit/android_spaceship_cubit_test.dart b/packages/pinball_components/test/src/components/android_spaceship/cubit/android_spaceship_cubit_test.dart index 364f5bf4..47b763af 100644 --- a/packages/pinball_components/test/src/components/android_spaceship/cubit/android_spaceship_cubit_test.dart +++ b/packages/pinball_components/test/src/components/android_spaceship/cubit/android_spaceship_cubit_test.dart @@ -7,17 +7,17 @@ void main() { 'AndroidSpaceshipCubit', () { blocTest( - 'onEntered emits activated', + 'onBallEntered emits withBonus', build: AndroidSpaceshipCubit.new, - act: (bloc) => bloc.onEntered(), - expect: () => [AndroidSpaceshipState.activated], + act: (bloc) => bloc.onBallEntered(), + expect: () => [AndroidSpaceshipState.withBonus], ); blocTest( - 'onBonusAwarded emits idle', + 'onBonusAwarded emits withoutBonus', build: AndroidSpaceshipCubit.new, act: (bloc) => bloc.onBonusAwarded(), - expect: () => [AndroidSpaceshipState.idle], + expect: () => [AndroidSpaceshipState.withoutBonus], ); }, ); diff --git a/test/game/components/android_acres/behaviors/android_spaceship_bonus_behavior_test.dart b/test/game/components/android_acres/behaviors/android_spaceship_bonus_behavior_test.dart index ea2d37f9..eb8ad211 100644 --- a/test/game/components/android_acres/behaviors/android_spaceship_bonus_behavior_test.dart +++ b/test/game/components/android_acres/behaviors/android_spaceship_bonus_behavior_test.dart @@ -57,7 +57,7 @@ void main() { flameBlocTester.testGameWidget( 'adds GameBonus.androidSpaceship to the game ' - 'when android spacehship is activated', + 'when android spacehship has a bonus', setUp: (game, tester) async { final behavior = AndroidSpaceshipBonusBehavior(); final parent = AndroidAcres.test(); @@ -67,7 +67,7 @@ void main() { await game.ensureAdd(parent); await parent.ensureAdd(behavior); - androidSpaceship.bloc.onEntered(); + androidSpaceship.bloc.onBallEntered(); await tester.pump(); verify(