test: fixed test for ball added

pull/213/head
RuiAlonso 3 years ago
parent 069ceaa3d6
commit d4387d27fe

@ -4,25 +4,70 @@ import 'package:pinball/game/game.dart';
void main() { void main() {
group('GameBloc', () { group('GameBloc', () {
test('initial state has 3 rounds, 1 ball and empty score', () { test('initial state has 3 rounds, 0 ball and empty score', () {
final gameBloc = GameBloc(); final gameBloc = GameBloc();
expect(gameBloc.state.score, equals(0)); expect(gameBloc.state.score, equals(0));
expect(gameBloc.state.balls, equals(1)); expect(gameBloc.state.balls, equals(0));
expect(gameBloc.state.rounds, equals(3)); expect(gameBloc.state.rounds, equals(3));
}); });
group('BallLost', () { group('BallAdded', () {
blocTest<GameBloc, GameState>( blocTest<GameBloc, GameState>(
'decreases number of balls', 'increases number of balls',
build: GameBloc.new, build: GameBloc.new,
act: (bloc) { act: (bloc) {
bloc.add(const BallLost(ballsLeft: 0)); bloc.add(const BallAdded());
}, },
expect: () => [ expect: () => [
const GameState( const GameState(
score: 0, score: 0,
multiplier: 1, multiplier: 1,
balls: 1, balls: 1,
rounds: 3,
bonusHistory: [],
),
],
);
});
group('BallLost', () {
blocTest<GameBloc, GameState>(
'decreases only number of balls '
'when there are more than 1',
build: GameBloc.new,
seed: () => const GameState(
score: 0,
multiplier: 1,
balls: 3,
rounds: 3,
bonusHistory: [],
),
act: (bloc) {
bloc.add(const BallLost());
},
expect: () => [
const GameState(
score: 0,
multiplier: 1,
balls: 2,
rounds: 3,
bonusHistory: [],
),
],
);
blocTest<GameBloc, GameState>(
'decreases number of rounds '
'when there are no more balls in current round',
build: GameBloc.new,
act: (bloc) {
bloc.add(const BallLost());
},
expect: () => [
const GameState(
score: 0,
multiplier: 1,
balls: 0,
rounds: 2, rounds: 2,
bonusHistory: [], bonusHistory: [],
), ),
@ -41,7 +86,7 @@ void main() {
bonusHistory: [], bonusHistory: [],
), ),
act: (bloc) { act: (bloc) {
bloc.add(const BallLost(ballsLeft: 0)); bloc.add(const BallLost());
}, },
expect: () => [ expect: () => [
isA<GameState>() isA<GameState>()
@ -62,7 +107,7 @@ void main() {
bonusHistory: [], bonusHistory: [],
), ),
act: (bloc) { act: (bloc) {
bloc.add(const BallLost(ballsLeft: 0)); bloc.add(const BallLost());
}, },
expect: () => [ expect: () => [
isA<GameState>() isA<GameState>()
@ -81,20 +126,12 @@ void main() {
..add(const Scored(points: 2)) ..add(const Scored(points: 2))
..add(const Scored(points: 3)), ..add(const Scored(points: 3)),
expect: () => [ expect: () => [
const GameState( isA<GameState>()
score: 2, ..having((state) => state.score, 'score', 2)
multiplier: 1, ..having((state) => state.isGameOver, 'isGameOver', false),
balls: 1, isA<GameState>()
rounds: 3, ..having((state) => state.score, 'score', 5)
bonusHistory: [], ..having((state) => state.isGameOver, 'isGameOver', false),
),
const GameState(
score: 5,
multiplier: 1,
balls: 1,
rounds: 3,
bonusHistory: [],
),
], ],
); );
@ -104,32 +141,23 @@ void main() {
build: GameBloc.new, build: GameBloc.new,
act: (bloc) { act: (bloc) {
for (var i = 0; i < bloc.state.rounds; i++) { for (var i = 0; i < bloc.state.rounds; i++) {
bloc.add(const BallLost(ballsLeft: 0)); bloc.add(const BallLost());
} }
bloc.add(const Scored(points: 2)); bloc.add(const Scored(points: 2));
}, },
expect: () => [ expect: () => [
const GameState( isA<GameState>()
score: 0, ..having((state) => state.score, 'score', 0)
multiplier: 1, ..having((state) => state.rounds, 'rounds', 2)
balls: 1, ..having((state) => state.isGameOver, 'isGameOver', false),
rounds: 2, isA<GameState>()
bonusHistory: [], ..having((state) => state.score, 'score', 0)
), ..having((state) => state.rounds, 'rounds', 1)
const GameState( ..having((state) => state.isGameOver, 'isGameOver', false),
score: 0, isA<GameState>()
multiplier: 1, ..having((state) => state.score, 'score', 0)
balls: 1, ..having((state) => state.rounds, 'rounds', 0)
rounds: 1, ..having((state) => state.isGameOver, 'isGameOver', true),
bonusHistory: [],
),
const GameState(
score: 0,
multiplier: 1,
balls: 1,
rounds: 0,
bonusHistory: [],
),
], ],
); );
}); });
@ -137,26 +165,20 @@ void main() {
group('MultiplierIncreased', () { group('MultiplierIncreased', () {
blocTest<GameBloc, GameState>( blocTest<GameBloc, GameState>(
'increases multiplier ' 'increases multiplier '
'when game is not over', 'when multiplier is below 6 and game is not over',
build: GameBloc.new, build: GameBloc.new,
act: (bloc) => bloc act: (bloc) => bloc
..add(const MultiplierIncreased()) ..add(const MultiplierIncreased())
..add(const MultiplierIncreased()), ..add(const MultiplierIncreased()),
expect: () => [ expect: () => [
const GameState( isA<GameState>()
score: 0, ..having((state) => state.score, 'score', 0)
multiplier: 2, ..having((state) => state.multiplier, 'multiplier', 2)
balls: 1, ..having((state) => state.isGameOver, 'isGameOver', false),
rounds: 3, isA<GameState>()
bonusHistory: [], ..having((state) => state.score, 'score', 0)
), ..having((state) => state.multiplier, 'multiplier', 3)
const GameState( ..having((state) => state.isGameOver, 'isGameOver', false),
score: 0,
multiplier: 3,
balls: 1,
rounds: 3,
bonusHistory: [],
),
], ],
); );
@ -166,23 +188,13 @@ void main() {
build: GameBloc.new, build: GameBloc.new,
seed: () => const GameState( seed: () => const GameState(
score: 0, score: 0,
multiplier: 5, multiplier: 6,
balls: 1, balls: 1,
rounds: 3, rounds: 3,
bonusHistory: [], bonusHistory: [],
), ),
act: (bloc) => bloc act: (bloc) => bloc..add(const MultiplierIncreased()),
..add(const MultiplierIncreased()) expect: () => const <GameState>[],
..add(const MultiplierIncreased()),
expect: () => [
const GameState(
score: 0,
multiplier: 6,
balls: 1,
rounds: 3,
bonusHistory: [],
),
],
); );
blocTest<GameBloc, GameState>( blocTest<GameBloc, GameState>(
@ -191,32 +203,23 @@ void main() {
build: GameBloc.new, build: GameBloc.new,
act: (bloc) { act: (bloc) {
for (var i = 0; i < bloc.state.rounds; i++) { for (var i = 0; i < bloc.state.rounds; i++) {
bloc.add(const BallLost(ballsLeft: 0)); bloc.add(const BallLost());
} }
bloc.add(const MultiplierIncreased()); bloc.add(const MultiplierIncreased());
}, },
expect: () => [ expect: () => [
const GameState( isA<GameState>()
score: 0, ..having((state) => state.score, 'score', 0)
multiplier: 1, ..having((state) => state.multiplier, 'multiplier', 1)
balls: 1, ..having((state) => state.isGameOver, 'isGameOver', false),
rounds: 2, isA<GameState>()
bonusHistory: [], ..having((state) => state.score, 'score', 0)
), ..having((state) => state.multiplier, 'multiplier', 1)
const GameState( ..having((state) => state.isGameOver, 'isGameOver', false),
score: 0, isA<GameState>()
multiplier: 1, ..having((state) => state.score, 'score', 0)
balls: 1, ..having((state) => state.multiplier, 'multiplier', 1)
rounds: 1, ..having((state) => state.isGameOver, 'isGameOver', true),
bonusHistory: [],
),
const GameState(
score: 0,
multiplier: 1,
balls: 1,
rounds: 0,
bonusHistory: [],
),
], ],
); );
}); });
@ -230,21 +233,19 @@ void main() {
act: (bloc) => bloc act: (bloc) => bloc
..add(const BonusActivated(GameBonus.googleWord)) ..add(const BonusActivated(GameBonus.googleWord))
..add(const BonusActivated(GameBonus.dashNest)), ..add(const BonusActivated(GameBonus.dashNest)),
expect: () => const [ expect: () => [
GameState( isA<GameState>()
score: 0, ..having(
multiplier: 1, (state) => state.bonusHistory,
balls: 1, 'bonusHistory',
rounds: 3, [GameBonus.googleWord],
bonusHistory: [GameBonus.googleWord], ),
), isA<GameState>()
GameState( ..having(
score: 0, (state) => state.bonusHistory,
multiplier: 1, 'bonusHistory',
balls: 1, [GameBonus.googleWord, GameBonus.dashNest],
rounds: 3, ),
bonusHistory: [GameBonus.googleWord, GameBonus.dashNest],
),
], ],
); );
}, },
@ -255,14 +256,13 @@ void main() {
'adds game bonus', 'adds game bonus',
build: GameBloc.new, build: GameBloc.new,
act: (bloc) => bloc..add(const SparkyTurboChargeActivated()), act: (bloc) => bloc..add(const SparkyTurboChargeActivated()),
expect: () => const [ expect: () => [
GameState( isA<GameState>()
score: 0, ..having(
multiplier: 1, (state) => state.bonusHistory,
balls: 1, 'bonusHistory',
rounds: 3, [GameBonus.sparkyTurboCharge],
bonusHistory: [GameBonus.sparkyTurboCharge], ),
),
], ],
); );
}); });

@ -5,19 +5,28 @@ import 'package:pinball/game/game.dart';
void main() { void main() {
group('GameEvent', () { group('GameEvent', () {
group('BallLost', () { group('BallAdded', () {
test('can be instantiated', () { test('can be instantiated', () {
expect(const BallLost(ballsLeft: 1), isNotNull); expect(const BallAdded(), isNotNull);
}); });
test('supports value equality', () { test('supports value equality', () {
expect( expect(
BallLost(ballsLeft: 1), BallAdded(),
equals(const BallLost(ballsLeft: 1)), equals(const BallAdded()),
); );
});
});
group('BallLost', () {
test('can be instantiated', () {
expect(const BallLost(), isNotNull);
});
test('supports value equality', () {
expect( expect(
BallLost(ballsLeft: 2), BallLost(),
isNot(equals(const BallLost(ballsLeft: 1))), equals(const BallLost()),
); );
}); });
}); });

@ -52,6 +52,20 @@ void main() {
); );
}); });
flameBlocTester.testGameWidget(
'when loaded adds BallAdded to GameBloc',
setUp: (game, tester) async {
final controller = BallController(ball);
await ball.add(controller);
await game.ensureAdd(ball);
controller.lost();
},
verify: (game, tester) async {
verify(() => gameBloc.add(const BallAdded())).called(1);
},
);
flameBlocTester.testGameWidget( flameBlocTester.testGameWidget(
'lost adds BallLost to GameBloc', 'lost adds BallLost to GameBloc',
setUp: (game, tester) async { setUp: (game, tester) async {
@ -62,7 +76,7 @@ void main() {
controller.lost(); controller.lost();
}, },
verify: (game, tester) async { verify: (game, tester) async {
verify(() => gameBloc.add(const BallLost(ballsLeft: 0))).called(1); verify(() => gameBloc.add(const BallLost())).called(1);
}, },
); );

Loading…
Cancel
Save