test: fix tests and groups

pull/40/head
RuiAlonso 4 years ago
parent a527a1390a
commit a5fb9ed8df

@ -18,6 +18,7 @@ void main() {
'loads correctly', 'loads correctly',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball(position: Vector2.zero());
await game.ready();
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(game.contains(ball), isTrue); expect(game.contains(ball), isTrue);

@ -7,8 +7,8 @@ import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart'; import '../../helpers/helpers.dart';
class FakeRampArea extends RampArea { class TestRampArea extends RampArea {
FakeRampArea({ TestRampArea({
required Vector2 position, required Vector2 position,
required RampOrientation orientation, required RampOrientation orientation,
required int categoryBits, required int categoryBits,
@ -30,8 +30,8 @@ class FakeRampArea extends RampArea {
]); ]);
} }
class FakeRampAreaCallback extends RampAreaCallback<FakeRampArea> { class TestRampAreaCallback extends RampAreaCallback<TestRampArea> {
FakeRampAreaCallback() : super(); TestRampAreaCallback() : super();
final _ballsInside = <Ball>{}; final _ballsInside = <Ball>{};
@ -67,7 +67,7 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final ramp = FakeRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, categoryBits: RampType.all.maskBits,
@ -84,7 +84,7 @@ void main() {
'positions correctly', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ramp = FakeRampArea( final ramp = TestRampArea(
position: position, position: position,
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, categoryBits: RampType.all.maskBits,
@ -99,7 +99,7 @@ void main() {
flameTester.test( flameTester.test(
'is static', 'is static',
(game) async { (game) async {
final ramp = FakeRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, categoryBits: RampType.all.maskBits,
@ -111,36 +111,48 @@ void main() {
); );
group('fixtures', () { group('fixtures', () {
const maskBits = 1234;
flameTester.test( flameTester.test(
'has only one shape', 'exists',
(game) async { (game) async {
final ramp = FakeRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, categoryBits: maskBits,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
expect(ramp.body.fixtures.length, 1); expect(ramp.body.fixtures[0], isA<Fixture>());
},
);
flameTester.test(
'shape is a polygon',
(game) async {
final ramp = TestRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
categoryBits: maskBits,
);
await game.ensureAdd(ramp);
for (final fixture in ramp.body.fixtures) { final fixture = ramp.body.fixtures[0];
expect(fixture, isA<Fixture>()); expect(fixture.shape.shapeType, equals(ShapeType.polygon));
expect(fixture.shape, isA<PolygonShape>());
}
}, },
); );
flameTester.test( flameTester.test(
'is sensor', 'is sensor',
(game) async { (game) async {
final ramp = FakeRampArea( final ramp = TestRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: RampType.all.maskBits, categoryBits: maskBits,
); );
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first; final fixture = ramp.body.fixtures[0];
expect(fixture.isSensor, isTrue); expect(fixture.isSensor, isTrue);
}, },
); );
@ -148,16 +160,15 @@ void main() {
flameTester.test( flameTester.test(
'sets filter categoryBits correctly', 'sets filter categoryBits correctly',
(game) async { (game) async {
const maskBits = 1234; final ramp = TestRampArea(
final ramp = FakeRampArea(
position: Vector2.zero(), position: Vector2.zero(),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: maskBits, categoryBits: maskBits,
); );
await game.ready();
await game.ensureAdd(ramp); await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first; final fixture = ramp.body.fixtures[0];
expect( expect(
fixture.filterData.categoryBits, fixture.filterData.categoryBits,
equals(maskBits), equals(maskBits),
@ -176,12 +187,12 @@ void main() {
'is saved into collection and set maskBits to path', () { 'is saved into collection and set maskBits to path', () {
final ball = MockBall(); final ball = MockBall();
final body = MockBody(); final body = MockBody();
final area = FakeRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, categoryBits: categoryBits,
); );
final callback = FakeRampAreaCallback(); final callback = TestRampAreaCallback();
when(() => body.position).thenReturn(Vector2(0, 20)); when(() => body.position).thenReturn(Vector2(0, 20));
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);
@ -203,12 +214,12 @@ void main() {
'is saved into collection and set maskBits to path', () { 'is saved into collection and set maskBits to path', () {
final ball = MockBall(); final ball = MockBall();
final body = MockBody(); final body = MockBody();
final area = FakeRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.up, orientation: RampOrientation.up,
categoryBits: categoryBits, categoryBits: categoryBits,
); );
final callback = FakeRampAreaCallback(); final callback = TestRampAreaCallback();
when(() => body.position).thenReturn(Vector2.zero()); when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);
@ -230,12 +241,12 @@ void main() {
'is removed from collection and set maskBits to collide all', () { 'is removed from collection and set maskBits to collide all', () {
final ball = MockBall(); final ball = MockBall();
final body = MockBody(); final body = MockBody();
final area = FakeRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, categoryBits: categoryBits,
); );
final callback = FakeRampAreaCallback(); final callback = TestRampAreaCallback();
when(() => body.position).thenReturn(Vector2.zero()); when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);
@ -257,12 +268,12 @@ void main() {
'is removed from collection and set maskBits to collide all', () { 'is removed from collection and set maskBits to collide all', () {
final ball = MockBall(); final ball = MockBall();
final body = MockBody(); final body = MockBody();
final area = FakeRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.down, orientation: RampOrientation.down,
categoryBits: categoryBits, categoryBits: categoryBits,
); );
final callback = FakeRampAreaCallback()..ballsInside.add(ball); final callback = TestRampAreaCallback()..ballsInside.add(ball);
when(() => body.position).thenReturn(Vector2.zero()); when(() => body.position).thenReturn(Vector2.zero());
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);
@ -282,12 +293,12 @@ void main() {
'is removed from collection and set maskBits to collide all', () { 'is removed from collection and set maskBits to collide all', () {
final ball = MockBall(); final ball = MockBall();
final body = MockBody(); final body = MockBody();
final area = FakeRampArea( final area = TestRampArea(
position: Vector2(0, 10), position: Vector2(0, 10),
orientation: RampOrientation.up, orientation: RampOrientation.up,
categoryBits: categoryBits, categoryBits: categoryBits,
); );
final callback = FakeRampAreaCallback()..ballsInside.add(ball); final callback = TestRampAreaCallback()..ballsInside.add(ball);
when(() => body.position).thenReturn(Vector2(0, 20)); when(() => body.position).thenReturn(Vector2(0, 20));
when(() => ball.body).thenReturn(body); when(() => ball.body).thenReturn(body);

@ -16,76 +16,64 @@ void main() {
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
group('JetpackRamp', () { group('JetpackRamp', () {
group('body', () { flameTester.test(
bool Function(Component) pathwaySelector(Vector2 position) => 'loads correctly',
(component) => (game) async {
component is Pathway && component.body.position == position; final ramp = JetpackRamp(
position: Vector2.zero(),
flameTester.test( );
'has a Pathway.arc at position', await game.ready();
(game) async { await game.ensureAdd(ramp);
final position = Vector2.all(10);
final jetpackRamp = JetpackRamp(
position: position,
);
await game.ready();
await game.ensureAdd(jetpackRamp);
expect( expect(game.contains(ramp), isTrue);
() => jetpackRamp.children.singleWhere( },
pathwaySelector(position), );
),
returnsNormally,
);
},
);
group('constructor', () {
flameTester.test( flameTester.test(
'path is static', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.zero();
final jetpackRamp = JetpackRamp( final ramp = JetpackRamp(
position: position, position: position,
); );
await game.ready(); await game.ensureAdd(ramp);
await game.ensureAdd(jetpackRamp);
final pathways = jetpackRamp.children.whereType<Pathway>().toList(); expect(ramp.position, equals(position));
for (final pathway in pathways) {
expect(pathway.body.bodyType, equals(BodyType.static));
}
}, },
); );
});
group('children', () {
flameTester.test( flameTester.test(
'has a two sensors for the ramp', 'has only one Pathway.arc',
(game) async { (game) async {
final jetpackRamp = JetpackRamp( final ramp = JetpackRamp(
position: Vector2.zero(), position: Vector2.zero(),
); );
await game.ready(); await game.ready();
await game.ensureAdd(jetpackRamp); await game.ensureAdd(ramp);
final rampAreas = expect(
jetpackRamp.children.whereType<JetpackRampArea>().toList(); () => ramp.children.singleWhere(
expect(rampAreas.length, 2); (component) => component is Pathway,
),
returnsNormally,
);
}, },
); );
flameTester.test( flameTester.test(
'sensors are static', 'has a two sensors for the ramp',
(game) async { (game) async {
final jetpackRamp = JetpackRamp( final ramp = JetpackRamp(
position: Vector2.zero(), position: Vector2.zero(),
); );
await game.ready(); await game.ready();
await game.ensureAdd(jetpackRamp); await game.ensureAdd(ramp);
final rampAreas = final rampAreas = ramp.children.whereType<JetpackRampArea>().toList();
jetpackRamp.children.whereType<JetpackRampArea>().toList(); expect(rampAreas.length, 2);
for (final rampArea in rampAreas) {
expect(rampArea.body.bodyType, equals(BodyType.static));
}
}, },
); );
}); });
@ -93,21 +81,7 @@ void main() {
group('JetpackRampArea', () { group('JetpackRampArea', () {
flameTester.test( flameTester.test(
'loads correctly', 'orientation is down',
(game) async {
final ramp = JetpackRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(game.contains(ramp), isTrue);
},
);
flameTester.test(
'orientation correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ramp = JetpackRampArea( final ramp = JetpackRampArea(
@ -120,91 +94,6 @@ void main() {
expect(ramp.orientation, RampOrientation.down); expect(ramp.orientation, RampOrientation.down);
}, },
); );
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final ramp = JetpackRampArea(
position: position,
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.position, position);
},
);
flameTester.test(
'is static',
(game) async {
final ramp = JetpackRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.bodyType, equals(BodyType.static));
},
);
group('fixtures', () {
flameTester.test(
'has only one shape',
(game) async {
final ramp = JetpackRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.fixtures.length, 1);
for (final fixture in ramp.body.fixtures) {
expect(fixture, isA<Fixture>());
expect(fixture.shape, isA<PolygonShape>());
}
},
);
flameTester.test(
'is sensor',
(game) async {
final ramp = JetpackRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first;
expect(fixture.isSensor, isTrue);
},
);
flameTester.test(
'sets correctly filter categoryBits to RampType.jetpack',
(game) async {
final ramp = JetpackRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first;
expect(
fixture.filterData.categoryBits,
equals(RampType.jetpack.maskBits),
);
},
);
});
});
}); });
group('JetpackRampAreaCallback', () { group('JetpackRampAreaCallback', () {

@ -16,76 +16,64 @@ void main() {
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
group('SparkyRamp', () { group('SparkyRamp', () {
group('body', () { flameTester.test(
bool Function(Component) pathwaySelector(Vector2 position) => 'loads correctly',
(component) => (game) async {
component is Pathway && component.body.position == position; final ramp = SparkyRamp(
position: Vector2.zero(),
flameTester.test( );
'has a Pathway.arc at position', await game.ready();
(game) async { await game.ensureAdd(ramp);
final position = Vector2.all(10);
final sparkyRamp = SparkyRamp(
position: position,
);
await game.ready();
await game.ensureAdd(sparkyRamp);
expect( expect(game.contains(ramp), isTrue);
() => sparkyRamp.children.singleWhere( },
pathwaySelector(position), );
),
returnsNormally,
);
},
);
group('constructor', () {
flameTester.test( flameTester.test(
'path is static', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.zero();
final sparkyRamp = SparkyRamp( final ramp = SparkyRamp(
position: position, position: position,
); );
await game.ready(); await game.ensureAdd(ramp);
await game.ensureAdd(sparkyRamp);
final pathways = game.children.whereType<Pathway>().toList(); expect(ramp.position, equals(position));
for (final pathway in pathways) {
expect(pathway.body.bodyType, equals(BodyType.static));
}
}, },
); );
});
group('children', () {
flameTester.test( flameTester.test(
'has a two sensors for the ramp', 'has only one Pathway.arc',
(game) async { (game) async {
final sparkyRamp = SparkyRamp( final ramp = SparkyRamp(
position: Vector2.zero(), position: Vector2.zero(),
); );
await game.ready(); await game.ready();
await game.ensureAdd(sparkyRamp); await game.ensureAdd(ramp);
final rampAreas = expect(
sparkyRamp.children.whereType<SparkyRampArea>().toList(); () => ramp.children.singleWhere(
expect(rampAreas.length, 2); (component) => component is Pathway,
),
returnsNormally,
);
}, },
); );
flameTester.test( flameTester.test(
'sensors are static', 'has a two sensors for the ramp',
(game) async { (game) async {
final sparkyRamp = SparkyRamp( final ramp = SparkyRamp(
position: Vector2.zero(), position: Vector2.zero(),
); );
await game.ready(); await game.ready();
await game.ensureAdd(sparkyRamp); await game.ensureAdd(ramp);
final rampAreas = final rampAreas = ramp.children.whereType<SparkyRampArea>().toList();
sparkyRamp.children.whereType<SparkyRampArea>().toList(); expect(rampAreas.length, 2);
for (final rampArea in rampAreas) {
expect(rampArea.body.bodyType, equals(BodyType.static));
}
}, },
); );
}); });
@ -93,21 +81,7 @@ void main() {
group('SparkyRampArea', () { group('SparkyRampArea', () {
flameTester.test( flameTester.test(
'loads correctly', 'orientation is down',
(game) async {
final ramp = SparkyRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(game.contains(ramp), isTrue);
},
);
flameTester.test(
'orientation correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ramp = SparkyRampArea( final ramp = SparkyRampArea(
@ -120,91 +94,6 @@ void main() {
expect(ramp.orientation, RampOrientation.down); expect(ramp.orientation, RampOrientation.down);
}, },
); );
group('body', () {
flameTester.test(
'positions correctly',
(game) async {
final position = Vector2.all(10);
final ramp = SparkyRampArea(
position: position,
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.position, position);
},
);
flameTester.test(
'is static',
(game) async {
final ramp = SparkyRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.bodyType, equals(BodyType.static));
},
);
group('fixtures', () {
flameTester.test(
'has only one shape',
(game) async {
final ramp = SparkyRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
expect(ramp.body.fixtures.length, 1);
for (final fixture in ramp.body.fixtures) {
expect(fixture, isA<Fixture>());
expect(fixture.shape, isA<PolygonShape>());
}
},
);
flameTester.test(
'is sensor',
(game) async {
final ramp = SparkyRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first;
expect(fixture.isSensor, isTrue);
},
);
flameTester.test(
'sets correctly filter categoryBits to RampType.sparky',
(game) async {
final ramp = SparkyRampArea(
position: Vector2.zero(),
orientation: RampOrientation.down,
);
await game.ready();
await game.ensureAdd(ramp);
final fixture = ramp.body.fixtures.first;
expect(
fixture.filterData.categoryBits,
equals(RampType.sparky.maskBits),
);
},
);
});
});
}); });
group('SparkyRampAreaCallback', () { group('SparkyRampAreaCallback', () {

Loading…
Cancel
Save