test: not layered nested children

pull/87/head
RuiAlonso 4 years ago
parent 9faba244b5
commit 3b0aa615b7

@ -1,12 +1,21 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'dart:math' as math; import 'dart:math' as math;
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:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
class TestBodyComponent extends BodyComponent with Layered { class TestLayeredBodyComponent extends BodyComponent with Layered {
@override
Body createBody() {
final fixtureDef = FixtureDef(CircleShape());
return world.createBody(BodyDef())..createFixture(fixtureDef);
}
}
class TestBodyComponent extends BodyComponent {
@override @override
Body createBody() { Body createBody() {
final fixtureDef = FixtureDef(CircleShape()); final fixtureDef = FixtureDef(CircleShape());
@ -38,7 +47,7 @@ void main() {
}); });
test('correctly sets and gets', () { test('correctly sets and gets', () {
final component = TestBodyComponent()..layer = Layer.jetpack; final component = TestLayeredBodyComponent()..layer = Layer.jetpack;
expect(component.layer, Layer.jetpack); expect(component.layer, Layer.jetpack);
}); });
@ -46,7 +55,7 @@ void main() {
'layers correctly before being loaded', 'layers correctly before being loaded',
(game) async { (game) async {
const expectedLayer = Layer.jetpack; const expectedLayer = Layer.jetpack;
final component = TestBodyComponent()..layer = expectedLayer; final component = TestLayeredBodyComponent()..layer = expectedLayer;
await game.ensureAdd(component); await game.ensureAdd(component);
// TODO(alestiago): modify once component.loaded is available. // TODO(alestiago): modify once component.loaded is available.
await component.mounted; await component.mounted;
@ -63,7 +72,7 @@ void main() {
'when multiple different sets', 'when multiple different sets',
(game) async { (game) async {
const expectedLayer = Layer.launcher; const expectedLayer = Layer.launcher;
final component = TestBodyComponent()..layer = Layer.jetpack; final component = TestLayeredBodyComponent()..layer = Layer.jetpack;
expect(component.layer, isNot(equals(expectedLayer))); expect(component.layer, isNot(equals(expectedLayer)));
component.layer = expectedLayer; component.layer = expectedLayer;
@ -83,7 +92,7 @@ void main() {
'layers correctly after being loaded', 'layers correctly after being loaded',
(game) async { (game) async {
const expectedLayer = Layer.jetpack; const expectedLayer = Layer.jetpack;
final component = TestBodyComponent(); final component = TestLayeredBodyComponent();
await game.ensureAdd(component); await game.ensureAdd(component);
component.layer = expectedLayer; component.layer = expectedLayer;
_expectLayerOnFixtures( _expectLayerOnFixtures(
@ -98,7 +107,7 @@ void main() {
'when multiple different sets', 'when multiple different sets',
(game) async { (game) async {
const expectedLayer = Layer.launcher; const expectedLayer = Layer.launcher;
final component = TestBodyComponent(); final component = TestLayeredBodyComponent();
await game.ensureAdd(component); await game.ensureAdd(component);
component.layer = Layer.jetpack; component.layer = Layer.jetpack;
@ -116,27 +125,52 @@ void main() {
'defaults to Layer.all ' 'defaults to Layer.all '
'when no layer is given', 'when no layer is given',
(game) async { (game) async {
final component = TestBodyComponent(); final component = TestLayeredBodyComponent();
await game.ensureAdd(component); await game.ensureAdd(component);
expect(component.layer, equals(Layer.all)); expect(component.layer, equals(Layer.all));
}, },
); );
flameTester.test( flameTester.test(
'nested children will keep their layer', 'nested Layered children will keep their layer',
(game) async { (game) async {
const parentLayer = Layer.jetpack; const parentLayer = Layer.jetpack;
const childLayer = Layer.board; const childLayer = Layer.board;
final component = TestBodyComponent()..layer = parentLayer; final component = TestLayeredBodyComponent()..layer = parentLayer;
final childComponent = TestBodyComponent()..layer = childLayer; final childComponent = TestLayeredBodyComponent()..layer = childLayer;
await component.add(childComponent); await component.add(childComponent);
await game.ensureAdd(component); await game.ensureAdd(component);
expect(childLayer, isNot(equals(parentLayer))); expect(childLayer, isNot(equals(parentLayer)));
for (final child in component.children) { for (final child in component.children) {
expect((child as TestBodyComponent).layer, equals(childLayer)); expect((child as TestLayeredBodyComponent).layer, equals(childLayer));
}
},
);
flameTester.test(
'nested children will keep their layer',
(game) async {
const parentLayer = Layer.jetpack;
final component = TestLayeredBodyComponent()..layer = parentLayer;
final childComponent = TestBodyComponent();
await component.add(childComponent);
await game.ensureAdd(component);
for (final child in component.children) {
expect(
(child as TestBodyComponent)
.body
.fixtures
.first
.filterData
.maskBits,
equals(Filter().maskBits),
);
} }
}, },
); );

Loading…
Cancel
Save