From 6f331544b31f7b9f323ecba553546673f7745fac Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Fri, 18 Mar 2022 09:36:56 +0100 Subject: [PATCH] refactor: ballsInside private and removed from tests --- lib/game/components/ramp_opening.dart | 13 +- test/game/components/ramp_opening_test.dart | 146 -------------------- 2 files changed, 6 insertions(+), 153 deletions(-) diff --git a/lib/game/components/ramp_opening.dart b/lib/game/components/ramp_opening.dart index 9d5286cd..a6517083 100644 --- a/lib/game/components/ramp_opening.dart +++ b/lib/game/components/ramp_opening.dart @@ -68,25 +68,24 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered { class RampOpeningBallContactCallback extends ContactCallback { /// [Ball]s currently inside the ramp. - @visibleForTesting - final ballsInside = {}; + final _ballsInside = {}; @override void begin(Ball ball, Opening opening, Contact _) { Layer layer; - if (!ballsInside.contains(ball)) { + if (!_ballsInside.contains(ball)) { layer = opening.pathwayLayer; - ballsInside.add(ball); + _ballsInside.add(ball); ball.layer = layer; } else { - ballsInside.remove(ball); + _ballsInside.remove(ball); } } @override void end(Ball ball, Opening opening, Contact _) { - if (!ballsInside.contains(ball)) { + if (!_ballsInside.contains(ball)) { ball.layer = Layer.board; } else { // TODO(ruimiguel): change this code. Check what happens with ball that @@ -100,7 +99,7 @@ class RampOpeningBallContactCallback if (isBallOutsideOpening) { ball.layer = Layer.board; - ballsInside.remove(ball); + _ballsInside.remove(ball); } } } diff --git a/test/game/components/ramp_opening_test.dart b/test/game/components/ramp_opening_test.dart index 5163ebe0..6f69fe0b 100644 --- a/test/game/components/ramp_opening_test.dart +++ b/test/game/components/ramp_opening_test.dart @@ -116,13 +116,6 @@ void main() { }); group('RampOpeningBallContactCallback', () { - test('ballsInside is empty when initialized', () { - expect( - RampOpeningBallContactCallback().ballsInside, - isEmpty, - ); - }); - flameTester.test( 'changes ball layer ' 'when a ball enters upwards into a downward ramp opening', @@ -146,30 +139,6 @@ void main() { verify(() => ball.layer = area.pathwayLayer).called(1); }); - flameTester.test( - 'adds ball to ballsInside ' - 'when a ball enters upwards into a downward oriented ramp', - (game) async { - final ball = MockBall(); - final body = MockBody(); - final area = TestRampOpening( - orientation: RampOrientation.down, - pathwayLayer: Layer.jetpack, - ); - final callback = TestRampOpeningBallContactCallback(); - - when(() => ball.body).thenReturn(body); - when(() => body.position).thenReturn(Vector2.zero()); - when(() => ball.layer).thenReturn(Layer.board); - - await game.ready(); - await game.ensureAdd(area); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.length, equals(1)); - expect(callback.ballsInside.first, ball); - }); - flameTester.test( 'changes ball layer ' 'when a ball enters downwards into a upward ramp opening', @@ -193,59 +162,6 @@ void main() { verify(() => ball.layer = area.pathwayLayer).called(1); }); - flameTester.test( - 'adds ball to ballsInside ' - 'when a ball enters downwards into an upward oriented ramp', - (game) async { - final ball = MockBall(); - final body = MockBody(); - final area = TestRampOpening( - orientation: RampOrientation.up, - pathwayLayer: Layer.jetpack, - ); - final callback = TestRampOpeningBallContactCallback(); - - when(() => ball.body).thenReturn(body); - when(() => body.position).thenReturn(Vector2.zero()); - when(() => ball.layer).thenReturn(Layer.board); - - await game.ready(); - await game.ensureAdd(area); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.contains(ball), isTrue); - }, - ); - - flameTester.test( - 'removes ball from ballsInside ' - 'when a ball exits from a downward oriented ramp', (game) async { - final ball = MockBall(); - final body = MockBody(); - final area = TestRampOpening( - orientation: RampOrientation.down, - pathwayLayer: Layer.jetpack, - )..initialPosition = Vector2(0, 10); - final callback = TestRampOpeningBallContactCallback(); - - when(() => ball.body).thenReturn(body); - when(() => body.position).thenReturn(Vector2.zero()); - when(() => body.linearVelocity).thenReturn(Vector2(0, -1)); - when(() => ball.layer).thenReturn(Layer.board); - - await game.ready(); - await game.ensureAdd(area); - - expect(callback.ballsInside.isEmpty, isTrue); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.length, equals(1)); - expect(callback.ballsInside.first, ball); - - callback.end(ball, area, MockContact()); - expect(callback.ballsInside.isEmpty, true); - }); - flameTester.test( 'changes ball layer ' 'when a ball exits from a downward oriented ramp', (game) async { @@ -272,35 +188,6 @@ void main() { verify(() => ball.layer = Layer.board); }); - flameTester.test( - 'removes ball from ballsInside ' - 'when a ball exits from a upward oriented ramp', (game) async { - final ball = MockBall(); - final body = MockBody(); - final area = TestRampOpening( - orientation: RampOrientation.up, - pathwayLayer: Layer.jetpack, - )..initialPosition = Vector2(0, 10); - final callback = TestRampOpeningBallContactCallback(); - - when(() => ball.body).thenReturn(body); - when(() => body.position).thenReturn(Vector2.zero()); - when(() => body.linearVelocity).thenReturn(Vector2(0, 1)); - when(() => ball.layer).thenReturn(Layer.board); - - await game.ready(); - await game.ensureAdd(area); - - expect(callback.ballsInside.isEmpty, isTrue); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.length, equals(1)); - expect(callback.ballsInside.first, ball); - - callback.end(ball, area, MockContact()); - expect(callback.ballsInside.isEmpty, true); - }); - flameTester.test( 'changes ball layer ' 'when a ball exits from a upward oriented ramp', (game) async { @@ -327,39 +214,6 @@ void main() { verify(() => ball.layer = Layer.board); }); - flameTester.test( - 'removes added ball from ballsInside ' - 'when a ball enters and exits from ramp', (game) async { - final ball = MockBall(); - final body = MockBody(); - final area = TestRampOpening( - orientation: RampOrientation.down, - pathwayLayer: Layer.jetpack, - )..initialPosition = Vector2(0, 10); - final callback = TestRampOpeningBallContactCallback(); - - when(() => ball.body).thenReturn(body); - when(() => body.position).thenReturn(Vector2.zero()); - when(() => body.linearVelocity).thenReturn(Vector2(0, 1)); - when(() => ball.layer).thenReturn(Layer.board); - - await game.ready(); - await game.ensureAdd(area); - - expect(callback.ballsInside.isEmpty, isTrue); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.length, equals(1)); - expect(callback.ballsInside.first, ball); - - callback.end(ball, area, MockContact()); - expect(callback.ballsInside.length, equals(1)); - expect(callback.ballsInside.first, ball); - - callback.begin(ball, area, MockContact()); - expect(callback.ballsInside.isEmpty, isTrue); - }); - flameTester.test( 'change ball layer between pathwayLayer and again to Layer.board ' 'when a ball enters and exits from ramp', (game) async {