mirror of https://github.com/flutter/pinball.git
parent
ddec3c0f44
commit
7fe2b1183b
@ -0,0 +1,14 @@
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
|
||||
mixin InitialPosition on BodyComponent {
|
||||
late final Vector2 initialPosition;
|
||||
|
||||
@override
|
||||
void onMount() {
|
||||
super.onMount();
|
||||
assert(
|
||||
body.position == initialPosition,
|
||||
'Body position is not equal to initial position.',
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
class EmptyGame extends Forge2DGame {}
|
||||
|
||||
class TestBodyComponent extends BodyComponent with InitialPosition {
|
||||
@override
|
||||
Body createBody() {
|
||||
return world.createBody(BodyDef());
|
||||
}
|
||||
}
|
||||
|
||||
class TestPositionBodyComponent extends BodyComponent with InitialPosition {
|
||||
@override
|
||||
Body createBody() {
|
||||
return world.createBody(BodyDef()..position = initialPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final flameTester = FlameTester(EmptyGame.new);
|
||||
group('InitialPosition', () {
|
||||
test('correctly sets and gets', () {
|
||||
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
|
||||
expect(component.initialPosition, Vector2(1, 2));
|
||||
});
|
||||
|
||||
test('can only be set once', () {
|
||||
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
|
||||
expect(
|
||||
() => component.initialPosition = Vector2(3, 4),
|
||||
throwsA(isA<Error>()),
|
||||
);
|
||||
});
|
||||
|
||||
flameTester.test(
|
||||
'returns normally '
|
||||
'when the body sets the position to initial position',
|
||||
(game) async {
|
||||
final component = TestPositionBodyComponent()
|
||||
..initialPosition = Vector2.zero();
|
||||
|
||||
await expectLater(
|
||||
() async => game.ensureAdd(component),
|
||||
returnsNormally,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'throws AssertionError '
|
||||
'when not setting initialPosition to body',
|
||||
(game) async {
|
||||
final component = TestBodyComponent()..initialPosition = Vector2.zero();
|
||||
await game.ensureAdd(component);
|
||||
|
||||
await expectLater(
|
||||
() async => game.ensureAdd(component),
|
||||
throwsAssertionError,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue