refactor: ballsInside private and removed from tests

pull/40/head
RuiAlonso 4 years ago
parent 2168aa499e
commit 6f331544b3

@ -68,25 +68,24 @@ abstract class RampOpening extends BodyComponent with InitialPosition, Layered {
class RampOpeningBallContactCallback<Opening extends RampOpening> class RampOpeningBallContactCallback<Opening extends RampOpening>
extends ContactCallback<Ball, Opening> { extends ContactCallback<Ball, Opening> {
/// [Ball]s currently inside the ramp. /// [Ball]s currently inside the ramp.
@visibleForTesting final _ballsInside = <Ball>{};
final ballsInside = <Ball>{};
@override @override
void begin(Ball ball, Opening opening, Contact _) { void begin(Ball ball, Opening opening, Contact _) {
Layer layer; Layer layer;
if (!ballsInside.contains(ball)) { if (!_ballsInside.contains(ball)) {
layer = opening.pathwayLayer; layer = opening.pathwayLayer;
ballsInside.add(ball); _ballsInside.add(ball);
ball.layer = layer; ball.layer = layer;
} else { } else {
ballsInside.remove(ball); _ballsInside.remove(ball);
} }
} }
@override @override
void end(Ball ball, Opening opening, Contact _) { void end(Ball ball, Opening opening, Contact _) {
if (!ballsInside.contains(ball)) { if (!_ballsInside.contains(ball)) {
ball.layer = Layer.board; ball.layer = Layer.board;
} else { } else {
// TODO(ruimiguel): change this code. Check what happens with ball that // TODO(ruimiguel): change this code. Check what happens with ball that
@ -100,7 +99,7 @@ class RampOpeningBallContactCallback<Opening extends RampOpening>
if (isBallOutsideOpening) { if (isBallOutsideOpening) {
ball.layer = Layer.board; ball.layer = Layer.board;
ballsInside.remove(ball); _ballsInside.remove(ball);
} }
} }
} }

@ -116,13 +116,6 @@ void main() {
}); });
group('RampOpeningBallContactCallback', () { group('RampOpeningBallContactCallback', () {
test('ballsInside is empty when initialized', () {
expect(
RampOpeningBallContactCallback<TestRampOpening>().ballsInside,
isEmpty,
);
});
flameTester.test( flameTester.test(
'changes ball layer ' 'changes ball layer '
'when a ball enters upwards into a downward ramp opening', 'when a ball enters upwards into a downward ramp opening',
@ -146,30 +139,6 @@ void main() {
verify(() => ball.layer = area.pathwayLayer).called(1); 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( flameTester.test(
'changes ball layer ' 'changes ball layer '
'when a ball enters downwards into a upward ramp opening', 'when a ball enters downwards into a upward ramp opening',
@ -193,59 +162,6 @@ void main() {
verify(() => ball.layer = area.pathwayLayer).called(1); 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( flameTester.test(
'changes ball layer ' 'changes ball layer '
'when a ball exits from a downward oriented ramp', (game) async { 'when a ball exits from a downward oriented ramp', (game) async {
@ -272,35 +188,6 @@ void main() {
verify(() => ball.layer = Layer.board); 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( flameTester.test(
'changes ball layer ' 'changes ball layer '
'when a ball exits from a upward oriented ramp', (game) async { 'when a ball exits from a upward oriented ramp', (game) async {
@ -327,39 +214,6 @@ void main() {
verify(() => ball.layer = Layer.board); 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( flameTester.test(
'change ball layer between pathwayLayer and again to Layer.board ' 'change ball layer between pathwayLayer and again to Layer.board '
'when a ball enters and exits from ramp', (game) async { 'when a ball enters and exits from ramp', (game) async {

Loading…
Cancel
Save