test: fixed tests for spaceship priority changes

pull/83/head
RuiAlonso 4 years ago
parent a0b8bad13c
commit a2b809effd

@ -36,8 +36,8 @@ class Spaceship extends Forge2DBlueprint {
SpaceshipEntrance()..initialPosition = position, SpaceshipEntrance()..initialPosition = position,
AndroidHead()..initialPosition = position, AndroidHead()..initialPosition = position,
SpaceshipHole( SpaceshipHole(
onExitLayer: Layer.spaceshipExitRail, outsideLayer: Layer.spaceshipExitRail,
onExitElevation: 2, outsidePriority: 2,
)..initialPosition = position - Vector2(5.2, 4.8), )..initialPosition = position - Vector2(5.2, 4.8),
SpaceshipHole()..initialPosition = position - Vector2(-7.2, 0.8), SpaceshipHole()..initialPosition = position - Vector2(-7.2, 0.8),
SpaceshipWall()..initialPosition = position, SpaceshipWall()..initialPosition = position,
@ -181,19 +181,16 @@ class SpaceshipEntrance extends RampOpening {
/// {@endtemplate} /// {@endtemplate}
class SpaceshipHole extends RampOpening { class SpaceshipHole extends RampOpening {
/// {@macro spaceship_hole} /// {@macro spaceship_hole}
SpaceshipHole({Layer? onExitLayer, this.onExitElevation = 1}) SpaceshipHole({Layer? outsideLayer, int? outsidePriority = 1})
: super( : super(
pathwayLayer: Layer.spaceship, pathwayLayer: Layer.spaceship,
outsideLayer: onExitLayer, outsideLayer: outsideLayer,
outsidePriority: onExitElevation, outsidePriority: outsidePriority,
orientation: RampOrientation.up, orientation: RampOrientation.up,
) { ) {
layer = Layer.spaceship; layer = Layer.spaceship;
} }
/// Priority order for [SpaceshipHole] on exit.
final int onExitElevation;
@override @override
Shape get shape { Shape get shape {
return ArcShape( return ArcShape(

@ -1,5 +1,6 @@
import 'dart:ui'; import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:mocktail/mocktail.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
@ -24,3 +25,5 @@ class MockContact extends Mock implements Contact {}
class MockContactCallback extends Mock class MockContactCallback extends Mock
implements ContactCallback<Object, Object> {} implements ContactCallback<Object, Object> {}
class MockComponent extends Mock implements Component {}

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

@ -59,7 +59,7 @@ void main() {
group('SpaceshipEntranceBallContactCallback', () { group('SpaceshipEntranceBallContactCallback', () {
test('changes the ball priority on contact', () { test('changes the ball priority on contact', () {
when(() => ball.priority).thenReturn(2); when(() => ball.priority).thenReturn(2);
when(() => entrance.priority).thenReturn(3); when(() => entrance.pathwayPriority).thenReturn(3);
SpaceshipEntranceBallContactCallback().begin( SpaceshipEntranceBallContactCallback().begin(
entrance, entrance,
@ -67,14 +67,14 @@ void main() {
MockContact(), MockContact(),
); );
verify(() => ball.showInFrontOf(entrance)).called(1); verify(() => ball.sendTo(entrance.pathwayPriority)).called(1);
}); });
}); });
group('SpaceshipHoleBallContactCallback', () { group('SpaceshipHoleBallContactCallback', () {
test('changes the ball priority on contact', () { test('changes the ball priority on contact', () {
when(() => hole.outsideLayer).thenReturn(Layer.board); when(() => hole.outsideLayer).thenReturn(Layer.board);
when(() => hole.onExitElevation).thenReturn(1); when(() => hole.outsidePriority).thenReturn(1);
SpaceshipHoleBallContactCallback().begin( SpaceshipHoleBallContactCallback().begin(
hole, hole,
@ -82,20 +82,7 @@ void main() {
MockContact(), MockContact(),
); );
verify(() => ball.priority = hole.onExitElevation).called(1); verify(() => ball.sendTo(hole.outsidePriority)).called(1);
});
test('re order the game children', () {
when(() => hole.outsideLayer).thenReturn(Layer.board);
when(() => hole.onExitElevation).thenReturn(1);
SpaceshipHoleBallContactCallback().begin(
hole,
ball,
MockContact(),
);
verify(() => ball.sendToBack()).called(1);
}); });
}); });
}); });

@ -2,10 +2,10 @@
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart'; import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockingjay/mockingjay.dart'; import 'package:mocktail/mocktail.dart';
import 'package:pinball_components/src/flame/priority.dart'; import 'package:pinball_components/src/flame/priority.dart';
import '../helpers/helpers.dart'; import '../../helpers/helpers.dart';
class TestBodyComponent extends BodyComponent { class TestBodyComponent extends BodyComponent {
@override @override
@ -19,13 +19,52 @@ void main() {
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(Forge2DGame.new);
group('ComponentPriorityX', () { group('ComponentPriorityX', () {
group('sendTo', () {
flameTester.test(
'changes the priority correctly to other level',
(game) async {
const newPriority = 5;
final component = TestBodyComponent()..priority = 4;
component.sendTo(newPriority);
expect(component.priority, equals(newPriority));
},
);
flameTester.test(
'calls reorderChildren if the new priority is different',
(game) async {
const newPriority = 5;
final component = MockComponent();
when(() => component.priority).thenReturn(4);
component.sendTo(newPriority);
verify(component.reorderChildren).called(1);
},
);
flameTester.test(
"doesn't call reorderChildren if the priority is the same",
(game) async {
const newPriority = 5;
final component = MockComponent();
when(() => component.priority).thenReturn(newPriority);
component.sendTo(newPriority);
verifyNever(component.reorderChildren);
},
);
});
group('sendToBack', () { group('sendToBack', () {
flameTester.test( flameTester.test(
'changes the priority correctly to board level', 'changes the priority correctly to board level',
(game) async { (game) async {
final component = TestBodyComponent()..priority = 4; final component = TestBodyComponent()..priority = 4;
await game.ensureAdd(component);
component.sendToBack(); component.sendToBack();
expect(component.priority, equals(0)); expect(component.priority, equals(0));
@ -38,7 +77,6 @@ void main() {
final component = MockComponent(); final component = MockComponent();
when(() => component.priority).thenReturn(4); when(() => component.priority).thenReturn(4);
await game.ensureAdd(component);
component.sendToBack(); component.sendToBack();
verify(component.reorderChildren).called(1); verify(component.reorderChildren).called(1);
@ -51,7 +89,6 @@ void main() {
final component = MockComponent(); final component = MockComponent();
when(() => component.priority).thenReturn(0); when(() => component.priority).thenReturn(0);
await game.ensureAdd(component);
component.sendToBack(); component.sendToBack();
verifyNever(component.reorderChildren); verifyNever(component.reorderChildren);
@ -68,7 +105,6 @@ void main() {
final otherComponent = TestBodyComponent() final otherComponent = TestBodyComponent()
..priority = startPriority - 1; ..priority = startPriority - 1;
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent); component.showBehindOf(otherComponent);
expect(component.priority, equals(otherComponent.priority - 1)); expect(component.priority, equals(otherComponent.priority - 1));
@ -83,7 +119,6 @@ void main() {
final otherComponent = TestBodyComponent() final otherComponent = TestBodyComponent()
..priority = startPriority + 1; ..priority = startPriority + 1;
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent); component.showBehindOf(otherComponent);
expect(component.priority, equals(startPriority)); expect(component.priority, equals(startPriority));
@ -99,7 +134,6 @@ void main() {
when(() => component.priority).thenReturn(startPriority); when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority - 1); when(() => otherComponent.priority).thenReturn(startPriority - 1);
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent); component.showBehindOf(otherComponent);
verify(component.reorderChildren).called(1); verify(component.reorderChildren).called(1);
@ -116,7 +150,6 @@ void main() {
when(() => component.priority).thenReturn(startPriority); when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority + 1); when(() => otherComponent.priority).thenReturn(startPriority + 1);
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent); component.showBehindOf(otherComponent);
verifyNever(component.reorderChildren); verifyNever(component.reorderChildren);
@ -133,7 +166,6 @@ void main() {
final otherComponent = TestBodyComponent() final otherComponent = TestBodyComponent()
..priority = startPriority + 1; ..priority = startPriority + 1;
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent); component.showInFrontOf(otherComponent);
expect(component.priority, equals(otherComponent.priority + 1)); expect(component.priority, equals(otherComponent.priority + 1));
@ -148,7 +180,6 @@ void main() {
final otherComponent = TestBodyComponent() final otherComponent = TestBodyComponent()
..priority = startPriority - 1; ..priority = startPriority - 1;
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent); component.showInFrontOf(otherComponent);
expect(component.priority, equals(startPriority)); expect(component.priority, equals(startPriority));
@ -164,7 +195,6 @@ void main() {
when(() => component.priority).thenReturn(startPriority); when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority + 1); when(() => otherComponent.priority).thenReturn(startPriority + 1);
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent); component.showInFrontOf(otherComponent);
verify(component.reorderChildren).called(1); verify(component.reorderChildren).called(1);
@ -181,7 +211,6 @@ void main() {
when(() => component.priority).thenReturn(startPriority); when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority - 1); when(() => otherComponent.priority).thenReturn(startPriority - 1);
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent); component.showInFrontOf(otherComponent);
verifyNever(component.reorderChildren); verifyNever(component.reorderChildren);

@ -70,6 +70,4 @@ class MockSpaceshipExitRailEnd extends Mock implements SpaceshipExitRailEnd {}
class MockComponentSet extends Mock implements ComponentSet {} class MockComponentSet extends Mock implements ComponentSet {}
class MockComponent extends Mock implements Component {}
class MockDashNestBumper extends Mock implements DashNestBumper {} class MockDashNestBumper extends Mock implements DashNestBumper {}

Loading…
Cancel
Save