From a5fb9ed8df8efbcf9360ecdee9039596087e121c Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Tue, 15 Mar 2022 10:53:54 +0100 Subject: [PATCH] test: fix tests and groups --- test/game/components/ball_test.dart | 1 + test/game/components/crossing_ramp_test.dart | 75 ++++---- test/game/components/jetpack_ramp_test.dart | 179 ++++--------------- test/game/components/sparky_ramp_test.dart | 179 ++++--------------- 4 files changed, 112 insertions(+), 322 deletions(-) diff --git a/test/game/components/ball_test.dart b/test/game/components/ball_test.dart index 0fb009ba..6fedd6df 100644 --- a/test/game/components/ball_test.dart +++ b/test/game/components/ball_test.dart @@ -18,6 +18,7 @@ void main() { 'loads correctly', (game) async { final ball = Ball(position: Vector2.zero()); + await game.ready(); await game.ensureAdd(ball); expect(game.contains(ball), isTrue); diff --git a/test/game/components/crossing_ramp_test.dart b/test/game/components/crossing_ramp_test.dart index 95099e06..af2b84e6 100644 --- a/test/game/components/crossing_ramp_test.dart +++ b/test/game/components/crossing_ramp_test.dart @@ -7,8 +7,8 @@ import 'package:pinball/game/game.dart'; import '../../helpers/helpers.dart'; -class FakeRampArea extends RampArea { - FakeRampArea({ +class TestRampArea extends RampArea { + TestRampArea({ required Vector2 position, required RampOrientation orientation, required int categoryBits, @@ -30,8 +30,8 @@ class FakeRampArea extends RampArea { ]); } -class FakeRampAreaCallback extends RampAreaCallback { - FakeRampAreaCallback() : super(); +class TestRampAreaCallback extends RampAreaCallback { + TestRampAreaCallback() : super(); final _ballsInside = {}; @@ -67,7 +67,7 @@ void main() { flameTester.test( 'loads correctly', (game) async { - final ramp = FakeRampArea( + final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, categoryBits: RampType.all.maskBits, @@ -84,7 +84,7 @@ void main() { 'positions correctly', (game) async { final position = Vector2.all(10); - final ramp = FakeRampArea( + final ramp = TestRampArea( position: position, orientation: RampOrientation.down, categoryBits: RampType.all.maskBits, @@ -99,7 +99,7 @@ void main() { flameTester.test( 'is static', (game) async { - final ramp = FakeRampArea( + final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, categoryBits: RampType.all.maskBits, @@ -111,36 +111,48 @@ void main() { ); group('fixtures', () { + const maskBits = 1234; + flameTester.test( - 'has only one shape', + 'exists', (game) async { - final ramp = FakeRampArea( + final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: RampType.all.maskBits, + categoryBits: maskBits, ); await game.ensureAdd(ramp); - expect(ramp.body.fixtures.length, 1); + expect(ramp.body.fixtures[0], isA()); + }, + ); + + flameTester.test( + 'shape is a polygon', + (game) async { + final ramp = TestRampArea( + position: Vector2.zero(), + orientation: RampOrientation.down, + categoryBits: maskBits, + ); + await game.ensureAdd(ramp); - for (final fixture in ramp.body.fixtures) { - expect(fixture, isA()); - expect(fixture.shape, isA()); - } + final fixture = ramp.body.fixtures[0]; + expect(fixture.shape.shapeType, equals(ShapeType.polygon)); }, ); flameTester.test( 'is sensor', (game) async { - final ramp = FakeRampArea( + final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, - categoryBits: RampType.all.maskBits, + categoryBits: maskBits, ); await game.ensureAdd(ramp); - final fixture = ramp.body.fixtures.first; + final fixture = ramp.body.fixtures[0]; expect(fixture.isSensor, isTrue); }, ); @@ -148,16 +160,15 @@ void main() { flameTester.test( 'sets filter categoryBits correctly', (game) async { - const maskBits = 1234; - final ramp = FakeRampArea( + final ramp = TestRampArea( position: Vector2.zero(), orientation: RampOrientation.down, categoryBits: maskBits, ); - await game.ready(); + await game.ensureAdd(ramp); - final fixture = ramp.body.fixtures.first; + final fixture = ramp.body.fixtures[0]; expect( fixture.filterData.categoryBits, equals(maskBits), @@ -176,12 +187,12 @@ void main() { 'is saved into collection and set maskBits to path', () { final ball = MockBall(); final body = MockBody(); - final area = FakeRampArea( + final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, categoryBits: categoryBits, ); - final callback = FakeRampAreaCallback(); + final callback = TestRampAreaCallback(); when(() => body.position).thenReturn(Vector2(0, 20)); when(() => ball.body).thenReturn(body); @@ -203,12 +214,12 @@ void main() { 'is saved into collection and set maskBits to path', () { final ball = MockBall(); final body = MockBody(); - final area = FakeRampArea( + final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.up, categoryBits: categoryBits, ); - final callback = FakeRampAreaCallback(); + final callback = TestRampAreaCallback(); when(() => body.position).thenReturn(Vector2.zero()); when(() => ball.body).thenReturn(body); @@ -230,12 +241,12 @@ void main() { 'is removed from collection and set maskBits to collide all', () { final ball = MockBall(); final body = MockBody(); - final area = FakeRampArea( + final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, categoryBits: categoryBits, ); - final callback = FakeRampAreaCallback(); + final callback = TestRampAreaCallback(); when(() => body.position).thenReturn(Vector2.zero()); when(() => ball.body).thenReturn(body); @@ -257,12 +268,12 @@ void main() { 'is removed from collection and set maskBits to collide all', () { final ball = MockBall(); final body = MockBody(); - final area = FakeRampArea( + final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.down, categoryBits: categoryBits, ); - final callback = FakeRampAreaCallback()..ballsInside.add(ball); + final callback = TestRampAreaCallback()..ballsInside.add(ball); when(() => body.position).thenReturn(Vector2.zero()); when(() => ball.body).thenReturn(body); @@ -282,12 +293,12 @@ void main() { 'is removed from collection and set maskBits to collide all', () { final ball = MockBall(); final body = MockBody(); - final area = FakeRampArea( + final area = TestRampArea( position: Vector2(0, 10), orientation: RampOrientation.up, categoryBits: categoryBits, ); - final callback = FakeRampAreaCallback()..ballsInside.add(ball); + final callback = TestRampAreaCallback()..ballsInside.add(ball); when(() => body.position).thenReturn(Vector2(0, 20)); when(() => ball.body).thenReturn(body); diff --git a/test/game/components/jetpack_ramp_test.dart b/test/game/components/jetpack_ramp_test.dart index e6256b4c..8b6e3321 100644 --- a/test/game/components/jetpack_ramp_test.dart +++ b/test/game/components/jetpack_ramp_test.dart @@ -16,76 +16,64 @@ void main() { final flameTester = FlameTester(PinballGameTest.create); group('JetpackRamp', () { - group('body', () { - bool Function(Component) pathwaySelector(Vector2 position) => - (component) => - component is Pathway && component.body.position == position; - - flameTester.test( - 'has a Pathway.arc at position', - (game) async { - final position = Vector2.all(10); - final jetpackRamp = JetpackRamp( - position: position, - ); - await game.ready(); - await game.ensureAdd(jetpackRamp); + flameTester.test( + 'loads correctly', + (game) async { + final ramp = JetpackRamp( + position: Vector2.zero(), + ); + await game.ready(); + await game.ensureAdd(ramp); - expect( - () => jetpackRamp.children.singleWhere( - pathwaySelector(position), - ), - returnsNormally, - ); - }, - ); + expect(game.contains(ramp), isTrue); + }, + ); + group('constructor', () { flameTester.test( - 'path is static', + 'positions correctly', (game) async { - final position = Vector2.all(10); - final jetpackRamp = JetpackRamp( + final position = Vector2.zero(); + final ramp = JetpackRamp( position: position, ); - await game.ready(); - await game.ensureAdd(jetpackRamp); + await game.ensureAdd(ramp); - final pathways = jetpackRamp.children.whereType().toList(); - for (final pathway in pathways) { - expect(pathway.body.bodyType, equals(BodyType.static)); - } + expect(ramp.position, equals(position)); }, ); + }); + group('children', () { flameTester.test( - 'has a two sensors for the ramp', + 'has only one Pathway.arc', (game) async { - final jetpackRamp = JetpackRamp( + final ramp = JetpackRamp( position: Vector2.zero(), ); await game.ready(); - await game.ensureAdd(jetpackRamp); + await game.ensureAdd(ramp); - final rampAreas = - jetpackRamp.children.whereType().toList(); - expect(rampAreas.length, 2); + expect( + () => ramp.children.singleWhere( + (component) => component is Pathway, + ), + returnsNormally, + ); }, ); flameTester.test( - 'sensors are static', + 'has a two sensors for the ramp', (game) async { - final jetpackRamp = JetpackRamp( + final ramp = JetpackRamp( position: Vector2.zero(), ); await game.ready(); - await game.ensureAdd(jetpackRamp); + await game.ensureAdd(ramp); - final rampAreas = - jetpackRamp.children.whereType().toList(); - for (final rampArea in rampAreas) { - expect(rampArea.body.bodyType, equals(BodyType.static)); - } + final rampAreas = ramp.children.whereType().toList(); + expect(rampAreas.length, 2); }, ); }); @@ -93,21 +81,7 @@ void main() { group('JetpackRampArea', () { flameTester.test( - 'loads correctly', - (game) async { - final ramp = JetpackRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(game.contains(ramp), isTrue); - }, - ); - - flameTester.test( - 'orientation correctly', + 'orientation is down', (game) async { final position = Vector2.all(10); final ramp = JetpackRampArea( @@ -120,91 +94,6 @@ void main() { expect(ramp.orientation, RampOrientation.down); }, ); - - group('body', () { - flameTester.test( - 'positions correctly', - (game) async { - final position = Vector2.all(10); - final ramp = JetpackRampArea( - position: position, - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.position, position); - }, - ); - - flameTester.test( - 'is static', - (game) async { - final ramp = JetpackRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.bodyType, equals(BodyType.static)); - }, - ); - - group('fixtures', () { - flameTester.test( - 'has only one shape', - (game) async { - final ramp = JetpackRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.fixtures.length, 1); - - for (final fixture in ramp.body.fixtures) { - expect(fixture, isA()); - expect(fixture.shape, isA()); - } - }, - ); - - flameTester.test( - 'is sensor', - (game) async { - final ramp = JetpackRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - final fixture = ramp.body.fixtures.first; - expect(fixture.isSensor, isTrue); - }, - ); - - flameTester.test( - 'sets correctly filter categoryBits to RampType.jetpack', - (game) async { - final ramp = JetpackRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - final fixture = ramp.body.fixtures.first; - expect( - fixture.filterData.categoryBits, - equals(RampType.jetpack.maskBits), - ); - }, - ); - }); - }); }); group('JetpackRampAreaCallback', () { diff --git a/test/game/components/sparky_ramp_test.dart b/test/game/components/sparky_ramp_test.dart index 88fac578..fd24bd48 100644 --- a/test/game/components/sparky_ramp_test.dart +++ b/test/game/components/sparky_ramp_test.dart @@ -16,76 +16,64 @@ void main() { final flameTester = FlameTester(PinballGameTest.create); group('SparkyRamp', () { - group('body', () { - bool Function(Component) pathwaySelector(Vector2 position) => - (component) => - component is Pathway && component.body.position == position; - - flameTester.test( - 'has a Pathway.arc at position', - (game) async { - final position = Vector2.all(10); - final sparkyRamp = SparkyRamp( - position: position, - ); - await game.ready(); - await game.ensureAdd(sparkyRamp); + flameTester.test( + 'loads correctly', + (game) async { + final ramp = SparkyRamp( + position: Vector2.zero(), + ); + await game.ready(); + await game.ensureAdd(ramp); - expect( - () => sparkyRamp.children.singleWhere( - pathwaySelector(position), - ), - returnsNormally, - ); - }, - ); + expect(game.contains(ramp), isTrue); + }, + ); + group('constructor', () { flameTester.test( - 'path is static', + 'positions correctly', (game) async { - final position = Vector2.all(10); - final sparkyRamp = SparkyRamp( + final position = Vector2.zero(); + final ramp = SparkyRamp( position: position, ); - await game.ready(); - await game.ensureAdd(sparkyRamp); + await game.ensureAdd(ramp); - final pathways = game.children.whereType().toList(); - for (final pathway in pathways) { - expect(pathway.body.bodyType, equals(BodyType.static)); - } + expect(ramp.position, equals(position)); }, ); + }); + group('children', () { flameTester.test( - 'has a two sensors for the ramp', + 'has only one Pathway.arc', (game) async { - final sparkyRamp = SparkyRamp( + final ramp = SparkyRamp( position: Vector2.zero(), ); await game.ready(); - await game.ensureAdd(sparkyRamp); + await game.ensureAdd(ramp); - final rampAreas = - sparkyRamp.children.whereType().toList(); - expect(rampAreas.length, 2); + expect( + () => ramp.children.singleWhere( + (component) => component is Pathway, + ), + returnsNormally, + ); }, ); flameTester.test( - 'sensors are static', + 'has a two sensors for the ramp', (game) async { - final sparkyRamp = SparkyRamp( + final ramp = SparkyRamp( position: Vector2.zero(), ); await game.ready(); - await game.ensureAdd(sparkyRamp); + await game.ensureAdd(ramp); - final rampAreas = - sparkyRamp.children.whereType().toList(); - for (final rampArea in rampAreas) { - expect(rampArea.body.bodyType, equals(BodyType.static)); - } + final rampAreas = ramp.children.whereType().toList(); + expect(rampAreas.length, 2); }, ); }); @@ -93,21 +81,7 @@ void main() { group('SparkyRampArea', () { flameTester.test( - 'loads correctly', - (game) async { - final ramp = SparkyRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(game.contains(ramp), isTrue); - }, - ); - - flameTester.test( - 'orientation correctly', + 'orientation is down', (game) async { final position = Vector2.all(10); final ramp = SparkyRampArea( @@ -120,91 +94,6 @@ void main() { expect(ramp.orientation, RampOrientation.down); }, ); - - group('body', () { - flameTester.test( - 'positions correctly', - (game) async { - final position = Vector2.all(10); - final ramp = SparkyRampArea( - position: position, - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.position, position); - }, - ); - - flameTester.test( - 'is static', - (game) async { - final ramp = SparkyRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.bodyType, equals(BodyType.static)); - }, - ); - - group('fixtures', () { - flameTester.test( - 'has only one shape', - (game) async { - final ramp = SparkyRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - expect(ramp.body.fixtures.length, 1); - - for (final fixture in ramp.body.fixtures) { - expect(fixture, isA()); - expect(fixture.shape, isA()); - } - }, - ); - - flameTester.test( - 'is sensor', - (game) async { - final ramp = SparkyRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - final fixture = ramp.body.fixtures.first; - expect(fixture.isSensor, isTrue); - }, - ); - - flameTester.test( - 'sets correctly filter categoryBits to RampType.sparky', - (game) async { - final ramp = SparkyRampArea( - position: Vector2.zero(), - orientation: RampOrientation.down, - ); - await game.ready(); - await game.ensureAdd(ramp); - - final fixture = ramp.body.fixtures.first; - expect( - fixture.filterData.categoryBits, - equals(RampType.sparky.maskBits), - ); - }, - ); - }); - }); }); group('SparkyRampAreaCallback', () {