Add float range to test suite where needed

This commit is contained in:
Lukas Klingsbo
2020-10-30 16:43:20 +01:00
parent 467181ba75
commit 5e4c466716
4 changed files with 33 additions and 16 deletions

View File

@ -23,6 +23,9 @@ void effectTest(
double expectedAngle = 0.0,
Vector2 expectedPosition,
Vector2 expectedSize,
// Only use when checking a value that is not in
// the start, end or peak of an iteration
double floatRange = 0.0,
}) async {
expectedPosition ??= Vector2.zero();
expectedSize ??= Vector2.all(100.0);
@ -39,9 +42,28 @@ void effectTest(
game.update(stepDelta);
timeLeft -= stepDelta;
}
expect(component.position, expectedPosition);
expect(component.angle, expectedAngle);
expect(component.size, expectedSize);
if (floatRange != 0) {
bool acceptableVector(Vector2 vector, Vector2 expectedVector) {
return (expectedVector - vector).length < floatRange;
}
final bool acceptablePosition =
acceptableVector(component.position, expectedPosition);
final bool acceptableSize = acceptableVector(component.size, expectedSize);
final bool acceptableAngle =
(expectedAngle - component.angle).abs() < floatRange;
assert(acceptablePosition, "Position is not correct");
assert(acceptableAngle, "Angle is not correct");
assert(acceptableSize, "Size is not correct");
} else {
expect(
component.position,
expectedPosition,
reason: "Position is not correct",
);
expect(component.angle, expectedAngle, reason: "Angle is not correct");
expect(component.size, expectedSize, reason: "Size is not correct");
}
expect(effect.hasFinished(), hasFinished);
expect(callback.isCalled, hasFinished);
game.update(0.0); // Since effects are removed before they are updated

View File

@ -94,10 +94,8 @@ void main() {
positionComponent,
moveEffect,
expectedPosition: path.last,
iterations: 1.0,
iterations: 3.0,
hasFinished: false,
);
});
// TODO: test that tests speed, and not only duration
}

View File

@ -81,14 +81,11 @@ void main() {
testWidgets('RotateEffect alternation can peak', (WidgetTester tester) async {
final RotateEffect rotateEffect = effect(false, true);
final PositionComponent positionComponent = component();
effectTest(
tester,
positionComponent,
rotateEffect,
effectTest(tester, positionComponent, rotateEffect,
expectedAngle: angleArgument,
hasFinished: false,
iterations: 0.5,
);
floatRange: 0.000001);
});
testWidgets('RotateEffect can be infinite', (WidgetTester tester) async {
@ -99,7 +96,7 @@ void main() {
positionComponent,
rotateEffect,
expectedAngle: angleArgument,
iterations: 1.0,
iterations: 3.0,
hasFinished: false,
);
});

View File

@ -94,7 +94,7 @@ void main() {
positionComponent,
scaleEffect,
expectedSize: argumentSize,
iterations: 1.0,
iterations: 3.0,
hasFinished: false,
);
});