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,57 +140,29 @@ void main() {
); );
}); });
testRawKeyUpEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test( flameTester.test(
'moves upwards when $keyLabel is released ' 'moves upwards when release is called '
'and plunger is below its starting position', 'and plunger is below its starting position', (game) async {
(game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
plunger.body.setTransform(Vector2(0, -1), 0); plunger.body.setTransform(Vector2(0, -1), 0);
plunger.onKeyEvent(event, {}); plunger.release();
expect(plunger.body.linearVelocity.y, isPositive); expect(plunger.body.linearVelocity.y, isPositive);
expect(plunger.body.linearVelocity.x, isZero); expect(plunger.body.linearVelocity.x, isZero);
},
);
}); });
testRawKeyUpEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test( flameTester.test(
'does not move when $keyLabel is released ' 'does not move when release is called '
'and plunger is in its starting position', 'and plunger is in its starting position',
(game) async { (game) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
plunger.onKeyEvent(event, {}); plunger.release();
expect(plunger.body.linearVelocity.y, isZero); expect(plunger.body.linearVelocity.y, isZero);
expect(plunger.body.linearVelocity.x, isZero); expect(plunger.body.linearVelocity.x, isZero);
}, },
); );
}); });
testRawKeyDownEvents(keys, (event) {
final keyLabel = (event.logicalKey != LogicalKeyboardKey.space)
? event.logicalKey.keyLabel
: 'Space';
flameTester.test(
'moves downwards when $keyLabel is pressed',
(game) async {
await game.ensureAdd(plunger);
plunger.onKeyEvent(event, {});
expect(plunger.body.linearVelocity.y, isNegative);
expect(plunger.body.linearVelocity.x, isZero);
},
);
});
});
}); });
group('PlungerAnchor', () { group('PlungerAnchor', () {
@ -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,12 +284,11 @@ void main() {
); );
}); });
testRawKeyUpEvents([LogicalKeyboardKey.space], (event) {
late final anchor = PlungerAnchor(plunger: plunger);
flameTester.testGameWidget( flameTester.testGameWidget(
'plunger cannot go below anchor', 'plunger cannot go below anchor',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ensureAdd(plunger); await game.ensureAdd(plunger);
anchor = PlungerAnchor(plunger: 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.
@ -326,14 +306,12 @@ void main() {
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);
final anchor = PlungerAnchor(plunger: plunger); anchor = PlungerAnchor(plunger: plunger);
await game.ensureAdd(anchor); await game.ensureAdd(anchor);
final jointDef = PlungerAnchorPrismaticJointDef( final jointDef = PlungerAnchorPrismaticJointDef(
@ -351,5 +329,4 @@ void main() {
}, },
); );
}); });
});
} }

Loading…
Cancel
Save