refactor: PR suggestions

pull/298/head
Allison Ryan 3 years ago
parent 575cd45249
commit 5a3654c5d0

@ -3,7 +3,7 @@ import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.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 class AndroidSpaceshipBonusBehavior extends Component
with HasGameRef<PinballGame>, ParentIsA<AndroidAcres> { with HasGameRef<PinballGame>, ParentIsA<AndroidAcres> {
@override @override
@ -14,13 +14,14 @@ class AndroidSpaceshipBonusBehavior extends Component
// TODO(alestiago): Refactor subscription management once the following is // TODO(alestiago): Refactor subscription management once the following is
// merged: // merged:
// https://github.com/flame-engine/flame/pull/1538 // https://github.com/flame-engine/flame/pull/1538
androidSpaceship.bloc.stream.listen((_) { androidSpaceship.bloc.stream.listen((state) {
if (androidSpaceship.bloc.state == AndroidSpaceshipState.activated) { final listenWhen = state == AndroidSpaceshipState.withBonus;
if (!listenWhen) return;
gameRef gameRef
.read<GameBloc>() .read<GameBloc>()
.add(const BonusActivated(GameBonus.androidSpaceship)); .add(const BonusActivated(GameBonus.androidSpaceship));
androidSpaceship.bloc.onBonusAwarded(); androidSpaceship.bloc.onBonusAwarded();
}
}); });
} }
} }

@ -11,6 +11,6 @@ class AndroidSpaceshipEntranceBallContactBehavior
super.beginContact(other, contact); super.beginContact(other, contact);
if (other is! Ball) return; if (other is! Ball) return;
parent.parent.bloc.onEntered(); parent.parent.bloc.onBallEntered();
} }
} }

@ -5,9 +5,9 @@ import 'package:bloc/bloc.dart';
part 'android_spaceship_state.dart'; part 'android_spaceship_state.dart';
class AndroidSpaceshipCubit extends Cubit<AndroidSpaceshipState> { class AndroidSpaceshipCubit extends Cubit<AndroidSpaceshipState> {
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);
} }

@ -3,6 +3,6 @@
part of 'android_spaceship_cubit.dart'; part of 'android_spaceship_cubit.dart';
enum AndroidSpaceshipState { enum AndroidSpaceshipState {
idle, withoutBonus,
activated, withBonus,
} }

@ -41,6 +41,7 @@ void main() {
await tester.pump(); await tester.pump();
}, },
verify: (game, tester) async { verify: (game, tester) async {
const goldenFilePath = '../golden/android_spaceship/';
final animationDuration = game final animationDuration = game
.descendants() .descendants()
.whereType<SpriteAnimationComponent>() .whereType<SpriteAnimationComponent>()
@ -50,21 +51,21 @@ void main() {
await expectLater( await expectLater(
find.byGame<TestGame>(), find.byGame<TestGame>(),
matchesGoldenFile('../golden/android_spaceship/start.png'), matchesGoldenFile('${goldenFilePath}start.png'),
); );
game.update(animationDuration * 0.5); game.update(animationDuration * 0.5);
await tester.pump(); await tester.pump();
await expectLater( await expectLater(
find.byGame<TestGame>(), find.byGame<TestGame>(),
matchesGoldenFile('../golden/android_spaceship/middle.png'), matchesGoldenFile('${goldenFilePath}middle.png'),
); );
game.update(animationDuration * 0.5); game.update(animationDuration * 0.5);
await tester.pump(); await tester.pump();
await expectLater( await expectLater(
find.byGame<TestGame>(), find.byGame<TestGame>(),
matchesGoldenFile('../golden/android_spaceship/end.png'), matchesGoldenFile('${goldenFilePath}end.png'),
); );
}, },
); );
@ -77,7 +78,7 @@ void main() {
whenListen( whenListen(
bloc, bloc,
const Stream<AndroidSpaceshipState>.empty(), const Stream<AndroidSpaceshipState>.empty(),
initialState: AndroidSpaceshipState.idle, initialState: AndroidSpaceshipState.withoutBonus,
); );
when(bloc.close).thenAnswer((_) async {}); when(bloc.close).thenAnswer((_) async {});
final androidSpaceship = AndroidSpaceship.test(bloc: bloc); final androidSpaceship = AndroidSpaceship.test(bloc: bloc);

@ -27,14 +27,14 @@ void main() {
}); });
flameTester.test( flameTester.test(
'beginContact emits onEntered when entrance contacts with a ball', 'beginContact calls onBallEntered when entrance contacts with a ball',
(game) async { (game) async {
final behavior = AndroidSpaceshipEntranceBallContactBehavior(); final behavior = AndroidSpaceshipEntranceBallContactBehavior();
final bloc = _MockAndroidSpaceshipCubit(); final bloc = _MockAndroidSpaceshipCubit();
whenListen( whenListen(
bloc, bloc,
const Stream<AndroidSpaceshipState>.empty(), const Stream<AndroidSpaceshipState>.empty(),
initialState: AndroidSpaceshipState.idle, initialState: AndroidSpaceshipState.withoutBonus,
); );
final entrance = AndroidSpaceshipEntrance(); final entrance = AndroidSpaceshipEntrance();
@ -47,7 +47,7 @@ void main() {
behavior.beginContact(MockBall(), MockContact()); behavior.beginContact(MockBall(), MockContact());
verify(androidSpaceship.bloc.onEntered).called(1); verify(androidSpaceship.bloc.onBallEntered).called(1);
}, },
); );
}, },

@ -7,17 +7,17 @@ void main() {
'AndroidSpaceshipCubit', 'AndroidSpaceshipCubit',
() { () {
blocTest<AndroidSpaceshipCubit, AndroidSpaceshipState>( blocTest<AndroidSpaceshipCubit, AndroidSpaceshipState>(
'onEntered emits activated', 'onBallEntered emits withBonus',
build: AndroidSpaceshipCubit.new, build: AndroidSpaceshipCubit.new,
act: (bloc) => bloc.onEntered(), act: (bloc) => bloc.onBallEntered(),
expect: () => [AndroidSpaceshipState.activated], expect: () => [AndroidSpaceshipState.withBonus],
); );
blocTest<AndroidSpaceshipCubit, AndroidSpaceshipState>( blocTest<AndroidSpaceshipCubit, AndroidSpaceshipState>(
'onBonusAwarded emits idle', 'onBonusAwarded emits withoutBonus',
build: AndroidSpaceshipCubit.new, build: AndroidSpaceshipCubit.new,
act: (bloc) => bloc.onBonusAwarded(), act: (bloc) => bloc.onBonusAwarded(),
expect: () => [AndroidSpaceshipState.idle], expect: () => [AndroidSpaceshipState.withoutBonus],
); );
}, },
); );

@ -57,7 +57,7 @@ void main() {
flameBlocTester.testGameWidget( flameBlocTester.testGameWidget(
'adds GameBonus.androidSpaceship to the game ' 'adds GameBonus.androidSpaceship to the game '
'when android spacehship is activated', 'when android spacehship has a bonus',
setUp: (game, tester) async { setUp: (game, tester) async {
final behavior = AndroidSpaceshipBonusBehavior(); final behavior = AndroidSpaceshipBonusBehavior();
final parent = AndroidAcres.test(); final parent = AndroidAcres.test();
@ -67,7 +67,7 @@ void main() {
await game.ensureAdd(parent); await game.ensureAdd(parent);
await parent.ensureAdd(behavior); await parent.ensureAdd(behavior);
androidSpaceship.bloc.onEntered(); androidSpaceship.bloc.onBallEntered();
await tester.pump(); await tester.pump();
verify( verify(

Loading…
Cancel
Save