test: refactored tests

pull/416/head
RuiAlonso 3 years ago
parent e0fab6a25a
commit 11b998b97d

@ -2,15 +2,12 @@ import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:pinball/game/behaviors/behaviors.dart'; import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_bonus_behavior} /// {@template ramp_bonus_behavior}
/// Increases the score when a [Ball] is shot 10 times into the [SpaceshipRamp]. /// Increases the score when a [Ball] is shot 10 times into the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampBonusBehavior extends Component class RampBonusBehavior extends Component
with with FlameBlocListenable<SpaceshipRampCubit, SpaceshipRampState> {
ParentIsA<Component>,
FlameBlocListenable<SpaceshipRampCubit, SpaceshipRampState> {
/// {@macro ramp_bonus_behavior} /// {@macro ramp_bonus_behavior}
RampBonusBehavior({ RampBonusBehavior({
required Points points, required Points points,
@ -32,7 +29,7 @@ class RampBonusBehavior extends Component
@override @override
void onNewState(SpaceshipRampState state) { void onNewState(SpaceshipRampState state) {
parent.add( parent!.add(
ScoringBehavior( ScoringBehavior(
points: _points, points: _points,
position: Vector2(0, -60), position: Vector2(0, -60),

@ -21,13 +21,13 @@ class RampProgressBehavior extends Component
final spaceshipCubit = readBloc<SpaceshipRampCubit, SpaceshipRampState>(); final spaceshipCubit = readBloc<SpaceshipRampCubit, SpaceshipRampState>();
final canProgress = !gameBloc.state.isMaxMultiplier || final canProgress = !gameBloc.state.isMaxMultiplier ||
(gameBloc.state.isMaxMultiplier && !state.fullArrowLit); (gameBloc.state.isMaxMultiplier && !state.arrowFullyLit);
if (canProgress) { if (canProgress) {
spaceshipCubit.onProgressed(); spaceshipCubit.onProgressed();
} }
if (spaceshipCubit.state.fullArrowLit && !gameBloc.state.isMaxMultiplier) { if (spaceshipCubit.state.arrowFullyLit && !gameBloc.state.isMaxMultiplier) {
spaceshipCubit.onProgressed(); spaceshipCubit.onProgressed();
} }
} }

@ -2,15 +2,12 @@ import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:pinball/game/behaviors/behaviors.dart'; import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_shot_behavior} /// {@template ramp_shot_behavior}
/// Increases the score when a [Ball] is shot into the [SpaceshipRamp]. /// Increases the score when a [Ball] is shot into the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampShotBehavior extends Component class RampShotBehavior extends Component
with with FlameBlocListenable<SpaceshipRampCubit, SpaceshipRampState> {
ParentIsA<Component>,
FlameBlocListenable<SpaceshipRampCubit, SpaceshipRampState> {
/// {@macro ramp_shot_behavior} /// {@macro ramp_shot_behavior}
RampShotBehavior({ RampShotBehavior({
required Points points, required Points points,
@ -29,7 +26,7 @@ class RampShotBehavior extends Component
@override @override
void onNewState(SpaceshipRampState state) { void onNewState(SpaceshipRampState state) {
parent.add( parent!.add(
ScoringBehavior( ScoringBehavior(
points: _points, points: _points,
position: Vector2(0, -45), position: Vector2(0, -45),

@ -17,7 +17,7 @@ class SpaceshipRampState extends Equatable {
final int hits; final int hits;
final ArrowLightState lightState; final ArrowLightState lightState;
bool get fullArrowLit => lightState == ArrowLightState.active5; bool get arrowFullyLit => lightState == ArrowLightState.active5;
SpaceshipRampState copyWith({ SpaceshipRampState copyWith({
int? hits, int? hits,

@ -54,16 +54,8 @@ class SpaceshipRamp extends Component {
/// This can be used for testing [SpaceshipRamp]'s behaviors in isolation. /// This can be used for testing [SpaceshipRamp]'s behaviors in isolation.
@visibleForTesting @visibleForTesting
SpaceshipRamp.test({ SpaceshipRamp.test({
required SpaceshipRampCubit bloc,
Iterable<Component>? children, Iterable<Component>? children,
}) : super( }) : super(children: children);
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
create: () => bloc,
children: [...?children],
),
],
);
} }
class _SpaceshipRampBackground extends BodyComponent class _SpaceshipRampBackground extends BodyComponent

@ -1,12 +1,14 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:bloc_test/bloc_test.dart'; import 'package:bloc_test/bloc_test.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.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_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_components/src/components/spaceship_ramp/behavior/behavior.dart'; import 'package:pinball_components/src/components/spaceship_ramp/behavior/behavior.dart';
import 'package:pinball_flame/pinball_flame.dart';
class _TestGame extends Forge2DGame { class _TestGame extends Forge2DGame {
@override @override
@ -25,6 +27,20 @@ class _TestGame extends Forge2DGame {
Assets.images.android.ramp.arrow.active5.keyName, Assets.images.android.ramp.arrow.active5.keyName,
]); ]);
} }
Future<void> pump(
SpaceshipRamp children, {
required SpaceshipRampCubit bloc,
}) async {
await ensureAdd(
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: bloc,
children: [
ZCanvasComponent(children: [children]),
],
),
);
}
} }
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
@ -74,13 +90,15 @@ void main() {
final opening = SpaceshipRampBoardOpening.test(); final opening = SpaceshipRampBoardOpening.test();
final spaceshipRamp = SpaceshipRamp.test( final spaceshipRamp = SpaceshipRamp.test(
bloc: bloc,
children: [opening], children: [opening],
); );
when(() => body.linearVelocity).thenReturn(Vector2(0, -1)); when(() => body.linearVelocity).thenReturn(Vector2(0, -1));
await game.ensureAdd(spaceshipRamp); await game.pump(
spaceshipRamp,
bloc: bloc,
);
await opening.add(behavior); await opening.add(behavior);
behavior.beginContact(ball, _MockContact()); behavior.beginContact(ball, _MockContact());
@ -104,13 +122,15 @@ void main() {
final opening = SpaceshipRampBoardOpening.test(); final opening = SpaceshipRampBoardOpening.test();
final spaceshipRamp = SpaceshipRamp.test( final spaceshipRamp = SpaceshipRamp.test(
bloc: bloc,
children: [opening], children: [opening],
); );
when(() => body.linearVelocity).thenReturn(Vector2(0, 1)); when(() => body.linearVelocity).thenReturn(Vector2(0, 1));
await game.ensureAdd(spaceshipRamp); await game.pump(
spaceshipRamp,
bloc: bloc,
);
await opening.add(behavior); await opening.add(behavior);
behavior.beginContact(ball, _MockContact()); behavior.beginContact(ball, _MockContact());

@ -47,16 +47,16 @@ void main() {
}); });
test( test(
'fullArrowLit returns true when lightState is last one', 'arrowFullyLit returns true when lightState is last one',
() { () {
expect( expect(
SpaceshipRampState.initial().fullArrowLit, SpaceshipRampState.initial().arrowFullyLit,
isFalse, isFalse,
); );
expect( expect(
SpaceshipRampState.initial() SpaceshipRampState.initial()
.copyWith(lightState: ArrowLightState.active5) .copyWith(lightState: ArrowLightState.active5)
.fullArrowLit, .arrowFullyLit,
isTrue, isTrue,
); );
}, },

@ -30,6 +30,20 @@ class _TestGame extends Forge2DGame {
Assets.images.android.ramp.arrow.active5.keyName, Assets.images.android.ramp.arrow.active5.keyName,
]); ]);
} }
Future<void> pump(
SpaceshipRamp children, {
required SpaceshipRampCubit bloc,
}) async {
await ensureAdd(
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: bloc,
children: [
ZCanvasComponent(children: [children]),
],
),
);
}
} }
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
@ -56,8 +70,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: SpaceshipRampState.initial(), initialState: SpaceshipRampState.initial(),
); );
final ramp = SpaceshipRamp.test(bloc: bloc); final ramp = SpaceshipRamp.test();
await game.ensureAdd(ramp); await game.pump(ramp, bloc: bloc);
expect(game.descendants(), contains(ramp)); expect(game.descendants(), contains(ramp));
}, },
); );
@ -163,8 +177,8 @@ void main() {
flameTester.test('can be loaded', (game) async { flameTester.test('can be loaded', (game) async {
final component = SpaceshipRampBoardOpening(); final component = SpaceshipRampBoardOpening();
final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit()); final parent = SpaceshipRamp.test();
await game.ensureAdd(parent); await game.pump(parent, bloc: _MockSpaceshipRampCubit());
await parent.ensureAdd(component); await parent.ensureAdd(component);
expect(parent.children, contains(component)); expect(parent.children, contains(component));
@ -172,8 +186,8 @@ void main() {
flameTester.test('adds a RampBallAscendingContactBehavior', (game) async { flameTester.test('adds a RampBallAscendingContactBehavior', (game) async {
final component = SpaceshipRampBoardOpening(); final component = SpaceshipRampBoardOpening();
final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit()); final parent = SpaceshipRamp.test();
await game.ensureAdd(parent); await game.pump(parent, bloc: _MockSpaceshipRampCubit());
await parent.ensureAdd(component); await parent.ensureAdd(component);
expect( expect(
@ -197,8 +211,11 @@ void main() {
initialState: state, initialState: state,
); );
final arrow = SpaceshipRampArrowSpriteComponent(); final arrow = SpaceshipRampArrowSpriteComponent();
final ramp = SpaceshipRamp.test(bloc: bloc, children: [arrow]); final ramp = SpaceshipRamp.test(children: [arrow]);
await game.ensureAdd(ramp); await game.pump(
ramp,
bloc: bloc,
);
expect(arrow.current, ArrowLightState.inactive); expect(arrow.current, ArrowLightState.inactive);

@ -90,7 +90,7 @@ void main() {
); );
final behavior = RampBonusBehavior(points: shotPoints); final behavior = RampBonusBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(bloc: bloc, children: [behavior]); final parent = SpaceshipRamp.test(children: [behavior]);
await game.pump( await game.pump(
[parent], [parent],
bloc: bloc, bloc: bloc,
@ -119,7 +119,7 @@ void main() {
); );
final behavior = RampBonusBehavior(points: shotPoints); final behavior = RampBonusBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(bloc: bloc, children: [behavior]); final parent = SpaceshipRamp.test(children: [behavior]);
await game.pump( await game.pump(
[parent], [parent],
bloc: bloc, bloc: bloc,

@ -28,16 +28,27 @@ class _TestGame extends Forge2DGame {
Assets.images.android.ramp.arrow.active3.keyName, Assets.images.android.ramp.arrow.active3.keyName,
Assets.images.android.ramp.arrow.active4.keyName, Assets.images.android.ramp.arrow.active4.keyName,
Assets.images.android.ramp.arrow.active5.keyName, Assets.images.android.ramp.arrow.active5.keyName,
Assets.images.android.rail.main.keyName,
Assets.images.android.rail.exit.keyName,
Assets.images.score.fiveThousand.keyName,
]); ]);
} }
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required GameBloc gameBloc, required GameBloc gameBloc,
required SpaceshipRampCubit bloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameMultiBlocProvider(
providers: [
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
),
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: bloc,
),
],
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
@ -85,14 +96,12 @@ void main() {
when(() => gameBloc.add(any())).thenAnswer((_) async {}); when(() => gameBloc.add(any())).thenAnswer((_) async {});
final behavior = RampMultiplierBehavior(); final behavior = RampMultiplierBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(children: [behavior]);
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -122,14 +131,12 @@ void main() {
); );
final behavior = RampMultiplierBehavior(); final behavior = RampMultiplierBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(children: [behavior]);
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -159,14 +166,12 @@ void main() {
); );
final behavior = RampMultiplierBehavior(); final behavior = RampMultiplierBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(children: [behavior]);
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(hits: 1)); streamController.add(state.copyWith(hits: 1));

@ -34,10 +34,18 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required GameBloc gameBloc, required GameBloc gameBloc,
required SpaceshipRampCubit bloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameMultiBlocProvider(
providers: [
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
),
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: bloc,
),
],
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
@ -85,13 +93,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -122,13 +130,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -159,13 +167,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add( streamController.add(
@ -201,13 +209,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add( streamController.add(
@ -243,13 +251,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add( streamController.add(
@ -285,13 +293,13 @@ void main() {
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add( streamController.add(

@ -37,10 +37,18 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required GameBloc gameBloc, required GameBloc gameBloc,
required SpaceshipRampCubit bloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameMultiBlocProvider(
providers: [
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
),
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: bloc,
),
],
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
@ -78,13 +86,13 @@ void main() {
); );
final behavior = RampResetBehavior(); final behavior = RampResetBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController.add(state.copyWith(rounds: state.rounds - 1)); streamController.add(state.copyWith(rounds: state.rounds - 1));
@ -107,13 +115,13 @@ void main() {
); );
final behavior = RampResetBehavior(); final behavior = RampResetBehavior();
final parent = SpaceshipRamp.test( final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior], children: [behavior],
); );
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
bloc: bloc,
); );
streamController streamController

@ -90,7 +90,7 @@ void main() {
); );
final behavior = RampShotBehavior(points: shotPoints); final behavior = RampShotBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(bloc: bloc, children: [behavior]); final parent = SpaceshipRamp.test(children: [behavior]);
await game.pump( await game.pump(
[parent], [parent],
bloc: bloc, bloc: bloc,

Loading…
Cancel
Save