refactor: moved FlameBlocProvider inside SpaceshipRamp and refactor tests

pull/416/head
RuiAlonso 3 years ago
parent 584fec5338
commit 5a78afb7ec

@ -53,7 +53,17 @@ 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() : super(); SpaceshipRamp.test({
required SpaceshipRampCubit bloc,
Iterable<Component>? children,
}) : super(
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
create: () => bloc,
children: [...?children],
),
],
);
} }
class _SpaceshipRampBackground extends BodyComponent class _SpaceshipRampBackground extends BodyComponent

@ -30,20 +30,6 @@ class _TestGame extends Forge2DGame {
Assets.images.android.ramp.arrow.active5.keyName, Assets.images.android.ramp.arrow.active5.keyName,
]); ]);
} }
Future<void> pump(
SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
}) async {
await ensureAdd(
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [
ZCanvasComponent(children: [child]),
],
),
);
}
} }
class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {}
@ -82,31 +68,31 @@ void main() {
streamController.stream, streamController.stream,
initialState: SpaceshipRampState.initial(), initialState: SpaceshipRampState.initial(),
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
expect(game.descendants(), contains(ramp)); expect(game.descendants(), contains(ramp));
}, },
); );
group('loads', () { group('loads', () {
flameTester.test('adds a FlameBlocProvider', (game) async {
final ramp = SpaceshipRamp();
await game.ensureAdd(ramp);
expect(
ramp.children
.whereType<
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>>()
.single,
isNotNull,
);
});
flameTester.test( flameTester.test(
'a SpaceshipRampBoardOpening', 'a SpaceshipRampBoardOpening',
(game) async { (game) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
whenListen(
bloc,
streamController.stream,
initialState: SpaceshipRampState.initial(),
);
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp();
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
expect( expect(
game.descendants().whereType<SpaceshipRampBoardOpening>().length, game.descendants().whereType<SpaceshipRampBoardOpening>().length,
equals(1), equals(1),
@ -117,18 +103,9 @@ void main() {
flameTester.test( flameTester.test(
'a SpaceshipRampArrowSpriteComponent', 'a SpaceshipRampArrowSpriteComponent',
(game) async { (game) async {
final bloc = _MockSpaceshipRampCubit();
final streamController = StreamController<SpaceshipRampState>();
whenListen(
bloc,
streamController.stream,
initialState: SpaceshipRampState.initial(),
);
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp();
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
expect( expect(
game game
.descendants() .descendants()
@ -155,11 +132,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: SpaceshipRampState.initial(), initialState: SpaceshipRampState.initial(),
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
await tester.pump(); await tester.pump();
@ -194,11 +168,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: state, initialState: state,
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
streamController.add( streamController.add(
state.copyWith(lightState: ArrowLightState.active1), state.copyWith(lightState: ArrowLightState.active1),
@ -238,11 +209,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: state, initialState: state,
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
streamController.add( streamController.add(
state.copyWith(lightState: ArrowLightState.active2), state.copyWith(lightState: ArrowLightState.active2),
@ -282,11 +250,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: state, initialState: state,
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
streamController.add( streamController.add(
state.copyWith(lightState: ArrowLightState.active3), state.copyWith(lightState: ArrowLightState.active3),
@ -326,11 +291,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: state, initialState: state,
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
streamController.add( streamController.add(
state.copyWith(lightState: ArrowLightState.active4), state.copyWith(lightState: ArrowLightState.active4),
@ -370,11 +332,8 @@ void main() {
streamController.stream, streamController.stream,
initialState: state, initialState: state,
); );
final ramp = SpaceshipRamp(); final ramp = SpaceshipRamp.test(bloc: bloc);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc,
);
streamController.add( streamController.add(
state.copyWith(lightState: ArrowLightState.active5), state.copyWith(lightState: ArrowLightState.active5),
@ -413,12 +372,10 @@ void main() {
initialState: SpaceshipRampState.initial(), initialState: SpaceshipRampState.initial(),
); );
final component = Component(); final component = Component();
final ramp = SpaceshipRamp(children: [component]); final ramp = SpaceshipRamp.test(bloc: bloc, children: [component]);
await game.pump( await game.ensureAdd(ramp);
ramp,
spaceshipRampCubit: bloc, expect(ramp.descendants(), contains(component));
);
expect(ramp.children, contains(component));
}); });
}); });
}); });
@ -472,23 +429,19 @@ 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(); final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit());
await game.pump( await game.ensureAdd(parent);
parent,
spaceshipRampCubit: _MockSpaceshipRampCubit(),
);
await parent.ensureAdd(component); await parent.ensureAdd(component);
expect(parent.children, contains(component)); expect(parent.children, contains(component));
}); });
flameTester.test('adds a RampBallAscendingContactBehavior', (game) async { flameTester.test('adds a RampBallAscendingContactBehavior', (game) async {
final component = SpaceshipRampBoardOpening(); final component = SpaceshipRampBoardOpening();
final ramp = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(bloc: _MockSpaceshipRampCubit());
await game.pump( await game.ensureAdd(parent);
ramp,
spaceshipRampCubit: _MockSpaceshipRampCubit(), await parent.ensureAdd(component);
);
await ramp.ensureAdd(component);
expect( expect(
component.children.whereType<RampBallAscendingContactBehavior>().length, component.children.whereType<RampBallAscendingContactBehavior>().length,
equals(1), equals(1),

@ -37,21 +37,15 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
required GameBloc gameBloc, required GameBloc gameBloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
), ),
],
),
); );
} }
} }
@ -86,14 +80,15 @@ void main() {
initialState: state, initialState: state,
); );
final behavior = RampBonusBehavior(points: shotPoints); final behavior = RampBonusBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 10)); streamController.add(state.copyWith(hits: 10));
@ -116,14 +111,15 @@ void main() {
initialState: state, initialState: state,
); );
final behavior = RampBonusBehavior(points: shotPoints); final behavior = RampBonusBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 9)); streamController.add(state.copyWith(hits: 9));

@ -36,21 +36,15 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
required GameBloc gameBloc, required GameBloc gameBloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
), ),
],
),
); );
} }
} }
@ -94,14 +88,15 @@ 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(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -130,14 +125,15 @@ void main() {
); );
final behavior = RampMultiplierBehavior(); final behavior = RampMultiplierBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -166,14 +162,15 @@ void main() {
); );
final behavior = RampMultiplierBehavior(); final behavior = RampMultiplierBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 1)); streamController.add(state.copyWith(hits: 1));

@ -36,21 +36,15 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
required GameBloc gameBloc, required GameBloc gameBloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
), ),
],
),
); );
} }
} }
@ -93,14 +87,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -129,14 +124,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: 5)); streamController.add(state.copyWith(hits: 5));
@ -165,14 +161,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add( streamController.add(
state.copyWith( state.copyWith(
@ -206,14 +203,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add( streamController.add(
state.copyWith( state.copyWith(
@ -247,14 +245,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add( streamController.add(
state.copyWith( state.copyWith(
@ -288,14 +287,15 @@ void main() {
); );
final behavior = RampProgressBehavior(); final behavior = RampProgressBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add( streamController.add(
state.copyWith(hits: 4), state.copyWith(hits: 4),

@ -36,21 +36,15 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
required GameBloc gameBloc, required GameBloc gameBloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
), ),
],
),
); );
} }
} }
@ -83,14 +77,15 @@ void main() {
initialState: state, initialState: state,
); );
final behavior = RampResetBehavior(); final behavior = RampResetBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(rounds: state.rounds - 1)); streamController.add(state.copyWith(rounds: state.rounds - 1));
await game.ready(); await game.ready();
@ -111,14 +106,15 @@ void main() {
initialState: state, initialState: state,
); );
final behavior = RampResetBehavior(); final behavior = RampResetBehavior();
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController streamController
.add(state.copyWith(roundScore: state.roundScore + 100)); .add(state.copyWith(roundScore: state.roundScore + 100));

@ -37,21 +37,15 @@ class _TestGame extends Forge2DGame {
Future<void> pump( Future<void> pump(
SpaceshipRamp child, { SpaceshipRamp child, {
required SpaceshipRampCubit spaceshipRampCubit,
required GameBloc gameBloc, required GameBloc gameBloc,
}) async { }) async {
await ensureAdd( await ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: gameBloc, value: gameBloc,
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>.value(
value: spaceshipRampCubit,
children: [ children: [
ZCanvasComponent(children: [child]), ZCanvasComponent(children: [child]),
], ],
), ),
],
),
); );
} }
} }
@ -86,14 +80,15 @@ void main() {
initialState: state, initialState: state,
); );
final behavior = RampShotBehavior(points: shotPoints); final behavior = RampShotBehavior(points: shotPoints);
final parent = SpaceshipRamp.test(); final parent = SpaceshipRamp.test(
bloc: bloc,
children: [behavior],
);
await game.pump( await game.pump(
parent, parent,
gameBloc: gameBloc, gameBloc: gameBloc,
spaceshipRampCubit: bloc,
); );
await parent.ensureAdd(behavior);
streamController.add(state.copyWith(hits: state.hits + 1)); streamController.add(state.copyWith(hits: state.hits + 1));

Loading…
Cancel
Save