fix: reached coverage

pull/89/head
alestiago 4 years ago
parent 45abc8ae16
commit bd0484ae38

@ -3,16 +3,17 @@
import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball/flame/blueprint.dart';
import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart';
/// {@template flutter_forest}
/// Area positioned at the top right of the [Board] where the [Ball]
/// can bounce off [_DashNestBumper]s.
/// can bounce off [DashNestBumper]s.
///
/// When all [_DashNestBumper]s are hit at least once, the [GameBonus.dashNest]
/// is awarded, and the [_BigDashNestBumper] releases a new [Ball].
/// When all [DashNestBumper]s are hit at least once, the [GameBonus.dashNest]
/// is awarded, and the [BigDashNestBumper] releases a new [Ball].
/// {@endtemplate}
// TODO(alestiago): Make a [Blueprint] once nesting [Blueprint] is implemented.
class FlutterForest extends Component
@ -40,14 +41,14 @@ class FlutterForest extends Component
@override
Future<void> onLoad() async {
gameRef.addContactCallback(_DashNestBumperBallContactCallback());
gameRef.addContactCallback(DashNestBumperBallContactCallback());
// TODO(alestiago): adjust positioning once sprites are added.
final smallLeftNest = _SmallDashNestBumper(id: 'small_left_nest')
final smallLeftNest = SmallDashNestBumper(id: 'small_left_nest')
..initialPosition = position + Vector2(-4.8, 2.8);
final smallRightNest = _SmallDashNestBumper(id: 'small_right_nest')
final smallRightNest = SmallDashNestBumper(id: 'small_right_nest')
..initialPosition = position + Vector2(0.5, -5.5);
final bigNest = _BigDashNestBumper(id: 'big_nest')
final bigNest = BigDashNestBumper(id: 'big_nest')
..initialPosition = position;
await addAll([
@ -63,26 +64,29 @@ class FlutterForest extends Component
///
/// When hit, the [GameState.score] is increased.
/// {@endtemplate}
abstract class _DashNestBumper extends BodyComponent<PinballGame>
@visibleForTesting
abstract class DashNestBumper extends BodyComponent<PinballGame>
with ScorePoints, InitialPosition {
/// {@template dash_nest_bumper}
_DashNestBumper({required this.id});
DashNestBumper({required this.id});
final String id;
}
class _DashNestBumperBallContactCallback
extends ContactCallback<_DashNestBumper, Ball> {
@visibleForTesting
class DashNestBumperBallContactCallback
extends ContactCallback<DashNestBumper, Ball> {
@override
void begin(_DashNestBumper dashNestBumper, Ball ball, Contact _) {
void begin(DashNestBumper dashNestBumper, Ball ball, Contact _) {
dashNestBumper.gameRef.read<GameBloc>().add(
DashNestActivated(dashNestBumper.id),
);
}
}
class _BigDashNestBumper extends _DashNestBumper {
_BigDashNestBumper({required String id}) : super(id: id);
@visibleForTesting
class BigDashNestBumper extends DashNestBumper {
BigDashNestBumper({required String id}) : super(id: id);
@override
int get points => 20;
@ -101,8 +105,9 @@ class _BigDashNestBumper extends _DashNestBumper {
}
}
class _SmallDashNestBumper extends _DashNestBumper {
_SmallDashNestBumper({required String id}) : super(id: id);
@visibleForTesting
class SmallDashNestBumper extends DashNestBumper {
SmallDashNestBumper({required String id}) : super(id: id);
@override
int get points => 10;

@ -194,6 +194,7 @@ void main() {
group('bonus letter activation', () {
final gameBloc = MockGameBloc();
final tester = flameBlocTester(gameBloc: () => gameBloc);
BonusLetter _getBonusLetter(PinballGame game) {
return game.children
@ -212,8 +213,6 @@ void main() {
);
});
final tester = flameBlocTester(gameBloc: () => gameBloc);
tester.widgetTest(
'adds BonusLetterActivated to GameBloc when not activated',
(game, tester) async {

@ -1,8 +1,10 @@
// ignore_for_file: cascade_invocations
import 'package:bloc_test/bloc_test.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/game/game.dart';
import 'package:pinball_components/pinball_components.dart';
@ -10,10 +12,9 @@ import '../../helpers/helpers.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create);
group('FlutterForest', () {
final flameTester = FlameTester(PinballGameTest.create);
flameTester.test(
'loads correctly',
(game) async {
@ -26,10 +27,10 @@ void main() {
);
flameTester.test(
'adds a new ball onNewState',
'onNewState adds a new ball',
(game) async {
await game.ready();
final flutterForest = FlutterForest(position: Vector2(0, 0));
await game.ready();
await game.ensureAdd(flutterForest);
final previousBalls = game.descendants().whereType<Ball>().length;
@ -42,5 +43,83 @@ void main() {
);
},
);
group('listenWhen', () {
final gameBloc = MockGameBloc();
final tester = flameBlocTester(gameBloc: () => gameBloc);
setUp(() {
whenListen(
gameBloc,
const Stream<GameState>.empty(),
initialState: const GameState.initial(),
);
});
tester.widgetTest(
'listens when a Bonus.dashNest is added',
(game, tester) async {
await game.ready();
final flutterForest =
game.descendants().whereType<FlutterForest>().first;
const state = GameState(
score: 0,
balls: 3,
activatedBonusLetters: [],
activatedDashNests: {},
bonusHistory: [GameBonus.dashNest],
);
expect(
flutterForest.listenWhen(const GameState.initial(), state),
isTrue,
);
},
);
});
});
group('DashNestBumperBallContactCallback', () {
final gameBloc = MockGameBloc();
final tester = flameBlocTester(gameBloc: () => gameBloc);
setUp(() {
whenListen(
gameBloc,
const Stream<GameState>.empty(),
initialState: const GameState.initial(),
);
});
tester.widgetTest(
'adds a DashNestActivated event with DashNestBumper.id',
(game, tester) async {
final contactCallback = DashNestBumperBallContactCallback();
const id = '0';
final dashNestBumper = MockDashNestBumper();
when(() => dashNestBumper.id).thenReturn(id);
when(() => dashNestBumper.gameRef).thenReturn(game);
contactCallback.begin(dashNestBumper, MockBall(), MockContact());
verify(() => gameBloc.add(DashNestActivated(dashNestBumper.id)))
.called(1);
},
);
});
group('BigDashNestBumper', () {
test('has points', () {
final dashNestBumper = BigDashNestBumper(id: '');
expect(dashNestBumper.points, greaterThan(0));
});
});
group('SmallDashNestBumper', () {
test('has points', () {
final dashNestBumper = SmallDashNestBumper(id: '');
expect(dashNestBumper.points, greaterThan(0));
});
});
}

@ -68,3 +68,5 @@ class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {}
class MockSpaceshipHole extends Mock implements SpaceshipHole {}
class MockComponentSet extends Mock implements ComponentSet {}
class MockDashNestBumper extends Mock implements DashNestBumper {}

Loading…
Cancel
Save