test: plunger tests refactored without keyevents

pull/152/head
RuiAlonso 4 years ago
parent 35e6ef5064
commit 92d4fe484a

@ -1,3 +1,2 @@
export 'key_testers.dart';
export 'mocks.dart'; export 'mocks.dart';
export 'test_game.dart'; export 'test_game.dart';

@ -1,24 +1,8 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.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';
class MockRawKeyDownEvent extends Mock implements RawKeyDownEvent {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return super.toString();
}
}
class MockRawKeyUpEvent extends Mock implements RawKeyUpEvent {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return super.toString();
}
}
class MockFilter extends Mock implements Filter {} class MockFilter extends Mock implements Filter {}
class MockFixture extends Mock implements Fixture {} class MockFixture extends Mock implements Fixture {}

@ -1,7 +1,5 @@
// ignore_for_file: cascade_invocations // ignore_for_file: cascade_invocations
import 'dart:collection';
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/services.dart'; import 'package:flutter/services.dart';
@ -117,13 +115,23 @@ void main() {
); );
}); });
group('onKeyEvent', () { group('pull', () {
final keys = UnmodifiableListView([ flameTester.test(
LogicalKeyboardKey.space, 'moves downwards when pull is called',
LogicalKeyboardKey.arrowDown, (game) async {
LogicalKeyboardKey.keyS, final plunger = Plunger(
]); compressionDistance: compressionDistance,
);
await game.ensureAdd(plunger);
plunger.pull();
expect(plunger.body.linearVelocity.y, isNegative);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
group('release', () {
late Plunger plunger; late Plunger plunger;
setUp(() { setUp(() {
@ -132,56 +140,28 @@ void main() {
); );
}); });
testRawKeyUpEvents(keys, (event) { flameTester.test(
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) 'moves upwards when release is called '
? event.logicalKey.keyLabel 'and plunger is below its starting position', (game) async {
: 'Space'; await game.ensureAdd(plunger);
flameTester.test( plunger.body.setTransform(Vector2(0, -1), 0);
'moves upwards when $keyLabel is released ' plunger.release();
'and plunger is below its starting position',
(game) async {
await game.ensureAdd(plunger);
plunger.body.setTransform(Vector2(0, -1), 0);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isPositive);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
testRawKeyUpEvents(keys, (event) { expect(plunger.body.linearVelocity.y, isPositive);
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) expect(plunger.body.linearVelocity.x, isZero);
? event.logicalKey.keyLabel
: 'Space';
flameTester.test(
'does not move when $keyLabel is released '
'and plunger is in its starting position',
(game) async {
await game.ensureAdd(plunger);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isZero);
expect(plunger.body.linearVelocity.x, isZero);
},
);
}); });
testRawKeyDownEvents(keys, (event) { flameTester.test(
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space) 'does not move when release is called '
? event.logicalKey.keyLabel 'and plunger is in its starting position',
: 'Space'; (game) async {
flameTester.test( await game.ensureAdd(plunger);
'moves downwards when $keyLabel is pressed', plunger.release();
(game) async {
await game.ensureAdd(plunger); expect(plunger.body.linearVelocity.y, isZero);
plunger.onKeyEvent(event, {}); expect(plunger.body.linearVelocity.x, isZero);
},
expect(plunger.body.linearVelocity.y, isNegative); );
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
}); });
}); });
@ -210,6 +190,7 @@ void main() {
group('PlungerAnchorPrismaticJointDef', () { group('PlungerAnchorPrismaticJointDef', () {
const compressionDistance = 10.0; const compressionDistance = 10.0;
late Plunger plunger; late Plunger plunger;
late PlungerAnchor anchor;
setUp(() { setUp(() {
plunger = Plunger( plunger = Plunger(
@ -222,7 +203,7 @@ void main() {
'plunger body as bodyA', 'plunger body as bodyA',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -238,7 +219,7 @@ void main() {
'anchor body as bodyB', 'anchor body as bodyB',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -255,7 +236,7 @@ void main() {
'limits enabled', 'limits enabled',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -272,7 +253,7 @@ void main() {
'lower translation limit as negative infinity', 'lower translation limit as negative infinity',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -289,7 +270,7 @@ void main() {
'connected body collison enabled', 'connected body collison enabled',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -303,53 +284,49 @@ void main() {
); );
}); });
testRawKeyUpEvents([LogicalKeyboardKey.space], (event) { flameTester.testGameWidget(
late final anchor = PlungerAnchor(plunger: plunger); 'plunger cannot go below anchor',
flameTester.testGameWidget( setUp: (game, tester) async {
'plunger cannot go below anchor', await game.ensureAdd(plunger);
setUp: (game, tester) async { anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(plunger); await game.ensureAdd(anchor);
await game.ensureAdd(anchor);
// Giving anchor a shape for the plunger to collide with. // Giving anchor a shape for the plunger to collide with.
anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1)); anchor.body.createFixtureFromShape(PolygonShape()..setAsBoxXY(2, 1));
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
anchor: anchor, anchor: anchor,
); );
game.world.createJoint(PrismaticJoint(jointDef)); game.world.createJoint(PrismaticJoint(jointDef));
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
}, },
verify: (game, tester) async { verify: (game, tester) async {
expect(plunger.body.position.y > anchor.body.position.y, isTrue); expect(plunger.body.position.y > anchor.body.position.y, isTrue);
}, },
); );
});
testRawKeyUpEvents([LogicalKeyboardKey.space], (event) { flameTester.testGameWidget(
flameTester.testGameWidget( 'plunger cannot excessively exceed starting position',
'plunger cannot excessively exceed starting position', setUp: (game, tester) async {
setUp: (game, tester) async { await game.ensureAdd(plunger);
await game.ensureAdd(plunger); anchor = PlungerAnchor(plunger: plunger);
final anchor = PlungerAnchor(plunger: plunger); await game.ensureAdd(anchor);
await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
plunger: plunger, plunger: plunger,
anchor: anchor, anchor: anchor,
); );
game.world.createJoint(PrismaticJoint(jointDef)); game.world.createJoint(PrismaticJoint(jointDef));
plunger.body.setTransform(Vector2(0, -1), 0); plunger.body.setTransform(Vector2(0, -1), 0);
await tester.pump(const Duration(seconds: 1)); await tester.pump(const Duration(seconds: 1));
}, },
verify: (game, tester) async { verify: (game, tester) async {
expect(plunger.body.position.y < 1, isTrue); expect(plunger.body.position.y < 1, isTrue);
}, },
); );
});
}); });
} }

Loading…
Cancel
Save