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

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save