test: fixed tests for PriorityX

pull/83/head
RuiAlonso 4 years ago
parent 2b1275477c
commit 472c5f2065

@ -1,10 +1,14 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'package:flame/components.dart';
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:pinball/game/components/priority.dart'; import 'package:pinball/game/components/priority.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart';
class TestBodyComponent extends BodyComponent { class TestBodyComponent extends BodyComponent {
@override @override
Body createBody() { Body createBody() {
@ -17,108 +21,173 @@ void main() {
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(Forge2DGame.new);
group('ComponentPriorityX', () { group('ComponentPriorityX', () {
test('correctly sets and gets', () { group('sendToBack', () {
final component = TestBodyComponent()
..elevation = Elevation.spaceship.order;
expect(component.elevation, Elevation.spaceship.order);
});
flameTester.test( flameTester.test(
'elevation correctly before being loaded', 'changes the priority correctly to board level',
(game) async { (game) async {
const expectedElevation = Elevation.spaceship; final component = TestBodyComponent()..priority = 4;
final component = TestBodyComponent()
..elevation = expectedElevation.order;
await game.ensureAdd(component); await game.ensureAdd(component);
// TODO(alestiago): modify once component.loaded is available. component.sendToBack();
await component.mounted;
expect(component.elevation, equals(expectedElevation.order)); expect(component.priority, equals(0));
}, },
); );
flameTester.test( flameTester.test(
'elevation correctly before being loaded ' 'calls reorderChildren if the priority is greater than lowest level',
'when multiple different sets',
(game) async { (game) async {
const expectedElevation = Elevation.spaceship; final component = MockComponent();
final component = TestBodyComponent() when(() => component.priority).thenReturn(4);
..elevation = Elevation.jetpack.order;
expect(component.elevation, isNot(equals(expectedElevation.order)));
component.elevation = expectedElevation.order;
await game.ensureAdd(component); await game.ensureAdd(component);
// TODO(alestiago): modify once component.loaded is available. component.sendToBack();
await component.mounted;
expect(component.elevation, expectedElevation.order); verify(component.reorderChildren).called(1);
}, },
); );
flameTester.test( flameTester.test(
'elevation correctly after being loaded', "doesn't call reorderChildren if the priority is the lowest level",
(game) async { (game) async {
const expectedElevation = Elevation.spaceship; final component = MockComponent();
final component = TestBodyComponent(); when(() => component.priority).thenReturn(0);
await game.ensureAdd(component); await game.ensureAdd(component);
component.elevation = expectedElevation.order; component.sendToBack();
expect(component.elevation, expectedElevation.order);
verifyNever(component.reorderChildren);
}, },
); );
});
group('showBehindOf', () {
flameTester.test( flameTester.test(
'elevation correctly after being loaded ' 'changes the priority if is greater than other component',
'when multiple different sets',
(game) async { (game) async {
const expectedElevation = Elevation.spaceship; const startPriority = 2;
final component = TestBodyComponent(); final component = TestBodyComponent()..priority = startPriority;
await game.ensureAdd(component); final otherComponent = TestBodyComponent()
..priority = startPriority - 1;
component.elevation = Elevation.jetpack.order; await game.ensureAddAll([component, otherComponent]);
expect(component.elevation, isNot(equals(expectedElevation.order))); component.showBehindOf(otherComponent);
component.elevation = expectedElevation.order;
expect(component.elevation, expectedElevation.order); expect(component.priority, equals(otherComponent.priority - 1));
}, },
); );
flameTester.test( flameTester.test(
'defaults to Elevation.board ' "doesn't change the priority if is lower than other component",
'when no elevation is given',
(game) async { (game) async {
final component = TestBodyComponent(); const startPriority = 2;
await game.ensureAdd(component); final component = TestBodyComponent()..priority = startPriority;
expect(component.elevation, equals(Elevation.board.order)); final otherComponent = TestBodyComponent()
..priority = startPriority + 1;
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent);
expect(component.priority, equals(startPriority));
}, },
); );
});
group('ElevationOrder', () { flameTester.test(
test('board is the lowest elevation', () { 'calls reorderChildren if the priority is greater than other component',
for (final elevation in Elevation.values) { (game) async {
if (elevation != Elevation.board) { const startPriority = 2;
expect(elevation.order, greaterThan(Elevation.board.order)); final component = MockComponent();
} final otherComponent = MockComponent();
} when(() => component.priority).thenReturn(startPriority);
}); when(() => otherComponent.priority).thenReturn(startPriority - 1);
test('jetpack has greater elevation than board', () { await game.ensureAddAll([component, otherComponent]);
expect(Elevation.jetpack.order, greaterThan(Elevation.board.order)); component.showBehindOf(otherComponent);
});
test('spaceship has greater elevation than board and next ramps', () { verify(component.reorderChildren).called(1);
expect(Elevation.spaceship.order, greaterThan(Elevation.jetpack.order)); },
expect( );
Elevation.spaceship.order,
greaterThan(Elevation.spaceshipExitRail.order), flameTester.test(
"doesn't call reorderChildren if the priority is lower than other "
'component',
(game) async {
const startPriority = 2;
final component = MockComponent();
final otherComponent = MockComponent();
when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority + 1);
await game.ensureAddAll([component, otherComponent]);
component.showBehindOf(otherComponent);
verifyNever(component.reorderChildren);
},
); );
}); });
test('spaceshipExitRail has greater elevation than board', () { group('showInFrontOf', () {
expect( flameTester.test(
Elevation.spaceshipExitRail.order, 'changes the priority if is lower than other component',
greaterThan(Elevation.board.order), (game) async {
const startPriority = 2;
final component = TestBodyComponent()..priority = startPriority;
final otherComponent = TestBodyComponent()
..priority = startPriority + 1;
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent);
expect(component.priority, equals(otherComponent.priority + 1));
},
);
flameTester.test(
"doesn't change the priority if is greater than other component",
(game) async {
const startPriority = 2;
final component = TestBodyComponent()..priority = startPriority;
final otherComponent = TestBodyComponent()
..priority = startPriority - 1;
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent);
expect(component.priority, equals(startPriority));
},
);
flameTester.test(
'calls reorderChildren if the priority is lower than other component',
(game) async {
const startPriority = 2;
final component = MockComponent();
final otherComponent = MockComponent();
when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority + 1);
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent);
verify(component.reorderChildren).called(1);
},
);
flameTester.test(
"doesn't call reorderChildren if the priority is greater than other "
'component',
(game) async {
const startPriority = 2;
final component = MockComponent();
final otherComponent = MockComponent();
when(() => component.priority).thenReturn(startPriority);
when(() => otherComponent.priority).thenReturn(startPriority - 1);
await game.ensureAddAll([component, otherComponent]);
component.showInFrontOf(otherComponent);
verifyNever(component.reorderChildren);
},
); );
}); });
}); });

@ -1,3 +1,4 @@
import 'package:flame/components.dart';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -59,3 +60,5 @@ class MockFixture extends Mock implements Fixture {}
class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {} class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {}
class MockSpaceshipHole extends Mock implements SpaceshipHole {} class MockSpaceshipHole extends Mock implements SpaceshipHole {}
class MockComponent extends Mock implements Component {}

Loading…
Cancel
Save