From dc98eefcc1d20e56f57ad01e48f6c00c2bb75dc5 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Fri, 18 Mar 2022 09:33:57 -0300 Subject: [PATCH] feat: more tests and improving sizes --- lib/game/components/spaceship.dart | 6 +- lib/game/pinball_game.dart | 4 +- test/game/components/spaceship_test.dart | 103 +++++++++++++++++++++++ test/helpers/mocks.dart | 10 +++ 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 test/game/components/spaceship_test.dart diff --git a/lib/game/components/spaceship.dart b/lib/game/components/spaceship.dart index 999a7c19..bb6ffb90 100644 --- a/lib/game/components/spaceship.dart +++ b/lib/game/components/spaceship.dart @@ -2,9 +2,11 @@ import 'dart:async'; import 'dart:math'; +import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; // TODO(erickzanardo): change this to use the layer class @@ -207,7 +209,7 @@ class SpaceshipHole extends BodyComponent with InitialPosition { @override Body createBody() { renderBody = false; - final circleShape = CircleShape()..radius = _spaceShipSize / 14; + final circleShape = CircleShape()..radius = _spaceShipSize / 80; final bodyDef = BodyDef() ..userData = this @@ -280,7 +282,7 @@ class SpaceshipWall extends BodyComponent with InitialPosition { return world.createBody(bodyDef) ..createFixture( FixtureDef(wallShape) - ..restitution = 0.8 + ..restitution = 1 ..filter.maskBits = _spaceShipBits ..filter.categoryBits = _spaceShipBits, ); diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 71f670f3..d54f3e76 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -92,12 +92,12 @@ class PinballGame extends Forge2DGame unawaited( add( - SpaceshipHole()..initialPosition = position - Vector2(5, 5), + SpaceshipHole()..initialPosition = position - Vector2(5, 4), ), ); unawaited( add( - SpaceshipHole()..initialPosition = position - Vector2(-5, 5), + SpaceshipHole()..initialPosition = position - Vector2(-5, 4), ), ); diff --git a/test/game/components/spaceship_test.dart b/test/game/components/spaceship_test.dart new file mode 100644 index 00000000..7e16edd8 --- /dev/null +++ b/test/game/components/spaceship_test.dart @@ -0,0 +1,103 @@ +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:pinball/game/game.dart'; + +import '../../helpers/helpers.dart'; + +void main() { + group('Spaceship', () { + late Filter filterData; + late Fixture fixture; + late Body body; + late PinballGame game; + late Ball ball; + late SpaceshipEntrance entrance; + late SpaceshipHole hole; + + setUp(() { + filterData = MockFilter(); + + fixture = MockFixture(); + when(() => fixture.filterData).thenReturn(filterData); + + body = MockBody(); + when(() => body.fixtures).thenReturn([fixture]); + + game = MockPinballGame(); + + ball = MockBall(); + when(() => ball.gameRef).thenReturn(game); + when(() => ball.body).thenReturn(body); + + entrance = MockSpaceshipEntrance(); + hole = MockSpaceshipHole(); + }); + + group('SpaceshipEntranceBallContactCallback', () { + test('changes the ball priority on contact', () { + SpaceshipEntranceBallContactCallback().begin( + entrance, + ball, + MockContact(), + ); + + verify(() => ball.priority = 3).called(1); + }); + + test('re order the game children', () { + SpaceshipEntranceBallContactCallback().begin( + entrance, + ball, + MockContact(), + ); + + verify(game.reorderChildren).called(1); + }); + + test('changes the filter data from the ball fixtures', () { + SpaceshipEntranceBallContactCallback().begin( + entrance, + ball, + MockContact(), + ); + + verify(() => filterData.maskBits = 0x0002).called(1); + verify(() => filterData.categoryBits = 0x0002).called(1); + }); + }); + + group('SpaceshipHoleBallContactCallback', () { + test('changes the ball priority on contact', () { + SpaceshipHoleBallContactCallback().begin( + hole, + ball, + MockContact(), + ); + + verify(() => ball.priority = 1).called(1); + }); + + test('re order the game children', () { + SpaceshipHoleBallContactCallback().begin( + hole, + ball, + MockContact(), + ); + + verify(game.reorderChildren).called(1); + }); + + test('changes the filter data from the ball fixtures', () { + SpaceshipHoleBallContactCallback().begin( + hole, + ball, + MockContact(), + ); + + verify(() => filterData.categoryBits = 0xFFFF).called(1); + verify(() => filterData.maskBits = 0x0001).called(1); + }); + }); + }); +} diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 80820c1b..67e04c32 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -41,3 +41,13 @@ class MockTapUpInfo extends Mock implements TapUpInfo {} class MockEventPosition extends Mock implements EventPosition {} class MockBonusLetter extends Mock implements BonusLetter {} + +class MockFilter extends Mock implements Filter {} + +class MockFixture extends Mock implements Fixture {} + +class MockBody extends Mock implements Body {} + +class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {} + +class MockSpaceshipHole extends Mock implements SpaceshipHole {}