From 520266df237c0e022f6bbee80de2d88eee6d5c26 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 29 Oct 2020 23:13:45 +0100 Subject: [PATCH] Stabilize MoveEffect tests --- lib/components/component.dart | 2 +- lib/effects/move_effect.dart | 3 --- test/effects/effect_test_utils.dart | 12 ++++++------ test/effects/move_effect_test.dart | 10 +++------- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/components/component.dart b/lib/components/component.dart index 01d014d3a..acc40ff3a 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -22,8 +22,8 @@ abstract class Component { /// All components on [BaseGame] are always updated by the same amount. The time each one takes to update adds up to the next update cycle. @mustCallSuper void update(double dt) { - _effects.forEach((e) => e.update(dt)); _effects.removeWhere((e) => e.hasFinished()); + _effects.forEach((e) => e.update(dt)); } /// Renders this component on the provided Canvas [c]. diff --git a/lib/effects/move_effect.dart b/lib/effects/move_effect.dart index 2f399fefa..3a65f8a68 100644 --- a/lib/effects/move_effect.dart +++ b/lib/effects/move_effect.dart @@ -109,9 +109,6 @@ class MoveEffect extends SimplePositionComponentEffect { @override void update(double dt) { - if (hasFinished()) { - return; - } super.update(dt); _currentSubPath ??= _percentagePath.first; if (!curveDirection.isNegative && _currentSubPath.endAt < curveProgress || diff --git a/test/effects/effect_test_utils.dart b/test/effects/effect_test_utils.dart index ba702e2ec..d387186b7 100644 --- a/test/effects/effect_test_utils.dart +++ b/test/effects/effect_test_utils.dart @@ -19,7 +19,6 @@ void effectTest( PositionComponent component, PositionComponentEffect effect, { bool hasFinished = true, - bool hitEdges = false, double iterations = 1.0, double expectedAngle = 0.0, Vector2 expectedPosition, @@ -30,21 +29,22 @@ void effectTest( final Callback callback = Callback(); effect.onComplete = callback.call; final BaseGame game = BaseGame(); - final double duration = (random.nextDouble() * 100).roundToDouble(); game.add(component); component.addEffect(effect); + final double duration = effect.totalTravelTime; await tester.pumpWidget(game.widget); double timeLeft = iterations * duration; while(timeLeft > 0) { - final double stepDelta = hitEdges ? 0.01 : random.nextDouble() / 10; + final double stepDelta = random.nextInt(100) / 1000; game.update(stepDelta); timeLeft -= stepDelta; } + expect(component.position, expectedPosition); + expect(component.angle, expectedAngle); + expect(component.size, expectedSize); expect(effect.hasFinished(), hasFinished); expect(callback.isCalled, hasFinished); - expect(component.angle, expectedAngle); - expect(component.position, expectedPosition); - expect(component.size, expectedSize); + game.update(0.0); // Since effects are removed before they are updated expect(component.effects.isEmpty, hasFinished); } diff --git a/test/effects/move_effect_test.dart b/test/effects/move_effect_test.dart index 664b9a27b..d483cf60b 100644 --- a/test/effects/move_effect_test.dart +++ b/test/effects/move_effect_test.dart @@ -16,7 +16,7 @@ void main() { MoveEffect effect(bool isInfinite, bool isAlternating) { return MoveEffect( path: path, - duration: random.nextDouble() * 10, + duration: random.nextDouble() * 100, isInfinite: isInfinite, isAlternating: isAlternating, ); @@ -60,23 +60,19 @@ void main() { testWidgets('MoveEffect alternation can peak', (WidgetTester tester) async { final MoveEffect moveEffect = effect(false, true); final PositionComponent positionComponent = component(); - print(path); - print(positionComponent.position); effectTest( tester, positionComponent, moveEffect, expectedPosition: path.last, - hitEdges: true, - iterations: 1.4, + hasFinished: false, + iterations: 0.5, ); }); testWidgets('MoveEffect can be infinite', (WidgetTester tester) async { final MoveEffect moveEffect = effect(true, false); final PositionComponent positionComponent = component(); - print(path); - print(positionComponent.position); effectTest( tester, positionComponent,