chore(flame_test): Deprecate expectVector2 (#1275)

Function expectVector2 is deprecated in favor of closeToVector(). All uses of expectVector2 removed from our tests.
This commit is contained in:
Pasha Stetsenko
2021-12-27 11:17:32 -08:00
committed by GitHub
parent b1f6a34c19
commit cf577bedaf
13 changed files with 79 additions and 87 deletions

View File

@ -17,16 +17,16 @@ void main() async {
// and the component has its anchor in the top left corner (which then // and the component has its anchor in the top left corner (which then
// is were the margin will be calculated from). // is were the margin will be calculated from).
// (500, 500) - size(20, 20) - position(10, 20) = (470, 460) // (500, 500) - size(20, 20) - position(10, 20) = (470, 460)
expectVector2(marginComponent.position, Vector2(470.0, 460.0)); expect(marginComponent.position, closeToVector(470, 460));
game.onGameResize(Vector2.all(1000)); game.onGameResize(Vector2.all(1000));
game.update(0); game.update(0);
// After resizing the game, the component should still be 30 pixels from // After resizing the game, the component should still be 30 pixels from
// the right edge and 40 pixels from the bottom. // the right edge and 40 pixels from the bottom.
expectVector2(marginComponent.position, Vector2(970.0, 960.0)); expect(marginComponent.position, closeToVector(970, 960));
// After the size of the component is changed the position is also // After the size of the component is changed the position is also
// changed. // changed.
marginComponent.size.add(Vector2.all(10)); marginComponent.size.add(Vector2.all(10));
expectVector2(marginComponent.position, Vector2(960.0, 950.0)); expect(marginComponent.position, closeToVector(960, 950));
}, },
); );
@ -42,12 +42,12 @@ void main() async {
// and the component has its anchor in the top left corner (which then // and the component has its anchor in the top left corner (which then
// is were the margin will be calculated from). // is were the margin will be calculated from).
// (500, 500) - size(20, 20) - position(10, 20) = (470, 460) // (500, 500) - size(20, 20) - position(10, 20) = (470, 460)
expectVector2(marginComponent.position, Vector2(470.0, 460.0)); expect(marginComponent.position, closeToVector(470, 460));
game.update(0); game.update(0);
game.camera.zoom = 2.0; game.camera.zoom = 2.0;
game.onGameResize(Vector2.all(500)); game.onGameResize(Vector2.all(500));
game.update(0); game.update(0);
expectVector2(marginComponent.position, Vector2(470.0, 460.0)); expect(marginComponent.position, closeToVector(470, 460));
}, },
); );
}); });

View File

@ -34,7 +34,7 @@ void main() {
tileHeight: 8.0, tileHeight: 8.0,
); );
expectVector2(c.getBlockCenterPosition(const Block(0, 0)), Vector2(0, 0)); expect(c.getBlockCenterPosition(const Block(0, 0)), closeToVector(0, 0));
}); });
}); });
} }

View File

@ -51,7 +51,7 @@ void main() {
); );
await game.add(joystick); await game.add(joystick);
game.update(0); game.update(0);
expectVector2(joystick.knob!.position, Vector2(10, 10)); expect(joystick.knob!.position, closeToVector(10, 10));
// Start dragging the joystick // Start dragging the joystick
game.onDragStart( game.onDragStart(
1, 1,
@ -77,7 +77,7 @@ void main() {
), ),
); );
game.update(0); game.update(0);
expectVector2(joystick.knob!.position, Vector2(20, 10)); expect(joystick.knob!.position, closeToVector(20, 10));
// Drag the knob back towards it's base position // Drag the knob back towards it's base position
game.onDragUpdate( game.onDragUpdate(
1, 1,
@ -91,7 +91,7 @@ void main() {
), ),
); );
game.update(0); game.update(0);
expectVector2(joystick.knob!.position, Vector2(20, 10)); expect(joystick.knob!.position, closeToVector(20, 10));
}, },
); );
}); });

View File

@ -22,7 +22,7 @@ class _MyDebugComponent extends PositionComponent {
} }
void main() { void main() {
group('PositionComponent overlap test', () { group('PositionComponent', () {
test('overlap', () { test('overlap', () {
final component = PositionComponent(); final component = PositionComponent();
component.position.setValues(2.0, 2.0); component.position.setValues(2.0, 2.0);
@ -412,43 +412,45 @@ void main() {
..scale = Vector2(2.0, 3.0) ..scale = Vector2(2.0, 3.0)
..position = startPosition; ..position = startPosition;
final centerPosition = component.center; final centerPosition = component.center;
final x = centerPosition.x;
final y = centerPosition.y;
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
// Same position after one vertical flip. // Same position after one vertical flip.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
// Same position after flipping back the vertical flip. // Same position after flipping back the vertical flip.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
// Same position after one horizontal flip. // Same position after one horizontal flip.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
// Same position after flipping back the horizontal flip. // Same position after flipping back the horizontal flip.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
// Same position after flipping both vertically and horizontally. // Same position after flipping both vertically and horizontally.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
// Same position after flipping back both vertically and horizontally. // Same position after flipping back both vertically and horizontally.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
// Same position after flipping both horizontally and vertically. // Same position after flipping both horizontally and vertically.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
component.flipVerticallyAroundCenter(); component.flipVerticallyAroundCenter();
component.flipHorizontallyAroundCenter(); component.flipHorizontallyAroundCenter();
// Same position after flipping back both horizontally and vertically in // Same position after flipping back both horizontally and vertically in
// the reverse order. // the reverse order.
expectVector2(component.center, centerPosition); expect(component.center, closeToVector(x, y, epsilon: 1e-14));
}); });
test('rotations', () { test('rotations', () {

View File

@ -17,17 +17,17 @@ void main() {
ScaleEffect.by(Vector2.all(2.0), EffectController(duration: 1)), ScaleEffect.by(Vector2.all(2.0), EffectController(duration: 1)),
); );
game.update(0); game.update(0);
expectVector2(component.scale, Vector2.all(1.0)); expect(component.scale, closeToVector(1, 1));
expect(component.children.length, 1); expect(component.children.length, 1);
game.update(0.5); game.update(0.5);
expectVector2(component.scale, Vector2.all(1.5)); expect(component.scale, closeToVector(1.5, 1.5));
game.update(0.5); game.update(0.5);
expectVector2(component.scale, Vector2.all(2.0)); expect(component.scale, closeToVector(2, 2));
game.update(0); game.update(0);
expect(component.children.length, 0); expect(component.children.length, 0);
expectVector2(component.scale, Vector2.all(2.0)); expect(component.scale, closeToVector(2, 2));
}); });
flameGame.test('absolute', (game) { flameGame.test('absolute', (game) {
@ -39,17 +39,17 @@ void main() {
ScaleEffect.to(Vector2.all(3.0), EffectController(duration: 1)), ScaleEffect.to(Vector2.all(3.0), EffectController(duration: 1)),
); );
game.update(0); game.update(0);
expectVector2(component.scale, Vector2.all(1.0)); expect(component.scale, closeToVector(1, 1));
expect(component.children.length, 1); expect(component.children.length, 1);
game.update(0.5); game.update(0.5);
expectVector2(component.scale, Vector2.all(2.0)); expect(component.scale, closeToVector(2, 2));
game.update(0.5); game.update(0.5);
expectVector2(component.scale, Vector2.all(3.0)); expect(component.scale, closeToVector(3, 3));
game.update(0); game.update(0);
expect(component.children.length, 0); expect(component.children.length, 0);
expectVector2(component.scale, Vector2.all(3.0)); expect(component.scale, closeToVector(3, 3));
}); });
flameGame.test('reset relative', (game) { flameGame.test('reset relative', (game) {
@ -61,15 +61,14 @@ void main() {
EffectController(duration: 1), EffectController(duration: 1),
); );
component.add(effect..removeOnFinish = false); component.add(effect..removeOnFinish = false);
final expectedScale = Vector2.all(1.0); var expectedScale = 1.0;
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
expectVector2(component.scale, expectedScale);
// After each reset the object will be scaled up twice // After each reset the object will be scaled up twice
// relative to its scale at the start of the effect. // relative to its scale at the start of the effect.
effect.reset(); effect.reset();
game.update(1); game.update(1);
expectedScale.multiply(Vector2.all(2)); expectedScale *= 2;
expectVector2(component.scale, expectedScale); expect(component.scale, closeToVector(expectedScale, expectedScale));
} }
}); });
@ -88,7 +87,7 @@ void main() {
// `Vector2(1, 1)`, regardless of its initial orientation. // `Vector2(1, 1)`, regardless of its initial orientation.
effect.reset(); effect.reset();
game.update(1); game.update(1);
expectVector2(component.scale, Vector2.all(1.0)); expect(component.scale, closeToVector(1, 1));
} }
}); });
@ -149,7 +148,7 @@ void main() {
game.update(1000 - totalTime); game.update(1000 - totalTime);
// Typically, `component.scale` could accumulate numeric discrepancy on // Typically, `component.scale` could accumulate numeric discrepancy on
// the order of 1e-11 .. 1e-12 by now. // the order of 1e-11 .. 1e-12 by now.
expectVector2(component.scale, Vector2.all(1.0), epsilon: 1e-10); expect(component.scale, closeToVector(1, 1, epsilon: 1e-10));
}); });
}); });
} }

View File

@ -18,17 +18,17 @@ void main() {
SizeEffect.by(Vector2.all(1.0), EffectController(duration: 1)), SizeEffect.by(Vector2.all(1.0), EffectController(duration: 1)),
); );
game.update(0); game.update(0);
expectVector2(component.size, Vector2.all(1.0)); expect(component.size, closeToVector(1, 1));
expect(component.children.length, 1); expect(component.children.length, 1);
game.update(0.5); game.update(0.5);
expectVector2(component.size, Vector2.all(1.5)); expect(component.size, closeToVector(1.5, 1.5));
game.update(0.5); game.update(0.5);
expectVector2(component.size, Vector2.all(2.0)); expect(component.size, closeToVector(2, 2));
game.update(0); game.update(0);
expect(component.children.length, 0); expect(component.children.length, 0);
expectVector2(component.size, Vector2.all(2.0)); expect(component.size, closeToVector(2, 2));
}); });
flameGame.test('absolute', (game) { flameGame.test('absolute', (game) {
@ -40,17 +40,17 @@ void main() {
SizeEffect.to(Vector2.all(3.0), EffectController(duration: 1)), SizeEffect.to(Vector2.all(3.0), EffectController(duration: 1)),
); );
game.update(0); game.update(0);
expectVector2(component.size, Vector2.all(1.0)); expect(component.size, closeToVector(1, 1));
expect(component.children.length, 1); expect(component.children.length, 1);
game.update(0.5); game.update(0.5);
expectVector2(component.size, Vector2.all(2.0)); expect(component.size, closeToVector(2, 2));
game.update(0.5); game.update(0.5);
expectVector2(component.size, Vector2.all(3.0)); expect(component.size, closeToVector(3, 3));
game.update(0); game.update(0);
expect(component.children.length, 0); expect(component.children.length, 0);
expectVector2(component.size, Vector2.all(3.0)); expect(component.size, closeToVector(3, 3));
}); });
flameGame.test('reset relative', (game) { flameGame.test('reset relative', (game) {
@ -64,13 +64,12 @@ void main() {
component.add(effect..removeOnFinish = false); component.add(effect..removeOnFinish = false);
final expectedSize = Vector2.zero(); final expectedSize = Vector2.zero();
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
expectVector2(component.size, expectedSize);
// After each reset the object will be sized up by Vector2(1, 1) // After each reset the object will be sized up by Vector2(1, 1)
// relative to its size at the start of the effect. // relative to its size at the start of the effect.
effect.reset(); effect.reset();
game.update(1); game.update(1);
expectedSize.add(Vector2.all(1.0)); expectedSize.add(Vector2.all(1.0));
expectVector2(component.size, expectedSize); expect(component.size, closeToVector(expectedSize.x, expectedSize.y));
} }
}); });
@ -89,7 +88,7 @@ void main() {
// `Vector2(1, 1)`, regardless of its initial orientation. // `Vector2(1, 1)`, regardless of its initial orientation.
effect.reset(); effect.reset();
game.update(1); game.update(1);
expectVector2(component.size, Vector2.all(1.0)); expect(component.size, closeToVector(1, 1));
} }
}); });
@ -112,21 +111,13 @@ void main() {
); );
game.update(1); game.update(1);
expectVector2( expect(component.size, closeToVector(1, 1)); // 5*1/10 + 0.5*1
component.size,
Vector2.all(1),
epsilon: 1e-15,
); // 5*1/10 + 0.5*1
game.update(1); game.update(1);
expectVector2( expect(component.size, closeToVector(1, 1)); // 5*2/10 + 0.5*1 - 0.5*1
component.size,
Vector2.all(1),
epsilon: 1e-15,
); // 5*2/10 + 0.5*1 - 0.5*1
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
game.update(1); game.update(1);
} }
expectVector2(component.size, Vector2.all(5), epsilon: 1e-15); expect(component.size, closeToVector(5, 5));
expect(component.children.length, 0); expect(component.children.length, 0);
}); });
@ -154,7 +145,7 @@ void main() {
game.update(1000 - totalTime); game.update(1000 - totalTime);
// Typically, `component.size` could accumulate numeric discrepancy on the // Typically, `component.size` could accumulate numeric discrepancy on the
// order of 1e-11 .. 1e-12 by now. // order of 1e-11 .. 1e-12 by now.
expectVector2(component.size, Vector2.zero(), epsilon: 1e-10); expect(component.size, closeToVector(0, 0, epsilon: 1e-10));
}); });
}); });
} }

View File

@ -32,7 +32,7 @@ void main() {
final matrix4 = Matrix4.translation(Vector3(0, 10, 0)); final matrix4 = Matrix4.translation(Vector3(0, 10, 0));
final input = Vector2.all(10); final input = Vector2.all(10);
expectVector2(matrix4.transform2(input), Vector2(10, 20)); expect(matrix4.transform2(input), closeToVector(10, 20));
}); });
test('test transformed2', () { test('test transformed2', () {
@ -41,8 +41,8 @@ void main() {
final out = Vector2.zero(); final out = Vector2.zero();
final result = matrix4.transformed2(input, out); final result = matrix4.transformed2(input, out);
expectVector2(out, input); expect(out, closeToVector(input.x, input.y));
expectVector2(result, Vector2(10, 20)); expect(result, closeToVector(10, 20));
}); });
}); });
} }

View File

@ -149,48 +149,48 @@ void main() {
test('rotate - no center defined', () { test('rotate - no center defined', () {
final position = Vector2(0.0, 1.0); final position = Vector2(0.0, 1.0);
position.rotate(-math.pi / 2); position.rotate(-math.pi / 2);
expectVector2(position, Vector2(1.0, 0.0)); expect(position, closeToVector(1.0, 0.0));
}); });
test('rotate - no center defined, negative position', () { test('rotate - no center defined, negative position', () {
final position = Vector2(0.0, -1.0); final position = Vector2(0.0, -1.0);
position.rotate(-math.pi / 2); position.rotate(-math.pi / 2);
expectVector2(position, Vector2(-1.0, 0.0)); expect(position, closeToVector(-1.0, 0.0));
}); });
test('rotate - with center defined', () { test('rotate - with center defined', () {
final position = Vector2(0.0, 1.0); final position = Vector2(0.0, 1.0);
final center = Vector2(1.0, 1.0); final center = Vector2(1.0, 1.0);
position.rotate(-math.pi / 2, center: center); position.rotate(-math.pi / 2, center: center);
expectVector2(position, Vector2(1.0, 2.0)); expect(position, closeToVector(1.0, 2.0));
}); });
test('rotate - with positive direction', () { test('rotate - with positive direction', () {
final position = Vector2(0.0, 1.0); final position = Vector2(0.0, 1.0);
final center = Vector2(1.0, 1.0); final center = Vector2(1.0, 1.0);
position.rotate(math.pi / 2, center: center); position.rotate(math.pi / 2, center: center);
expectVector2(position, Vector2(1.0, 0.0)); expect(position, closeToVector(1.0, 0.0));
}); });
test('rotate - with a negative y position', () { test('rotate - with a negative y position', () {
final position = Vector2(2.0, -3.0); final position = Vector2(2.0, -3.0);
final center = Vector2(1.0, 1.0); final center = Vector2(1.0, 1.0);
position.rotate(math.pi / 2, center: center); position.rotate(math.pi / 2, center: center);
expectVector2(position, Vector2(5.0, 2.0)); expect(position, closeToVector(5.0, 2.0));
}); });
test('rotate - with a negative x position', () { test('rotate - with a negative x position', () {
final position = Vector2(-2.0, 3.0); final position = Vector2(-2.0, 3.0);
final center = Vector2(1.0, 1.0); final center = Vector2(1.0, 1.0);
position.rotate(math.pi / 2, center: center); position.rotate(math.pi / 2, center: center);
expectVector2(position, Vector2(-1.0, -2.0)); expect(position, closeToVector(-1.0, -2.0));
}); });
test('rotate - with a negative position', () { test('rotate - with a negative position', () {
final position = Vector2(-2.0, -3.0); final position = Vector2(-2.0, -3.0);
final center = Vector2(1.0, 0.0); final center = Vector2(1.0, 0.0);
position.rotate(math.pi / 2, center: center); position.rotate(math.pi / 2, center: center);
expectVector2(position, Vector2(4.0, -3.0)); expect(position, closeToVector(4.0, -3.0));
}); });
test('screenAngle', () { test('screenAngle', () {

View File

@ -49,7 +49,8 @@ void main() {
await Future<void>.delayed(const Duration(milliseconds: 50)); await Future<void>.delayed(const Duration(milliseconds: 50));
await tester.tapAt(tapPosition); await tester.tapAt(tapPosition);
expect(game.doubleTapRegistered, isTrue); expect(game.doubleTapRegistered, isTrue);
expectVector2(game.doubleTapPosition!, tapPosition.toVector2()); final tapVector = tapPosition.toVector2();
expect(game.doubleTapPosition, closeToVector(tapVector.x, tapVector.y));
}, },
); );
}); });

View File

@ -403,23 +403,6 @@ void main() {
// project ∘ unproject = identity = unproject ∘ project // project ∘ unproject = identity = unproject ∘ project
// by evaluating an arbitrary list of parameters. // by evaluating an arbitrary list of parameters.
void _assertIdentityOfProjector(Projector projector) { void _assertIdentityOfProjector(Projector projector) {
// unproject combined with project no-ops
Vector2 identity1(Vector2 v) {
return projector.projectVector(projector.unprojectVector(v));
}
Vector2 identity2(Vector2 v) {
return projector.unprojectVector(projector.projectVector(v));
}
Vector2 identity3(Vector2 v) {
return projector.scaleVector(projector.unscaleVector(v));
}
Vector2 identity4(Vector2 v) {
return projector.unscaleVector(projector.scaleVector(v));
}
final someValues = <double>[-1, 0, 1, 2, 10, 100]; final someValues = <double>[-1, 0, 1, 2, 10, 100];
final someVectors = [ final someVectors = [
for (final x in someValues) for (final x in someValues)
@ -427,9 +410,21 @@ void _assertIdentityOfProjector(Projector projector) {
]; ];
someVectors.forEach((e) { someVectors.forEach((e) {
expectVector2(identity1(e), e); expect(
expectVector2(identity2(e), e); projector.projectVector(projector.unprojectVector(e)),
expectVector2(identity3(e), e); closeToVector(e.x, e.y, epsilon: 1e-13),
expectVector2(identity4(e), e); );
expect(
projector.unprojectVector(projector.projectVector(e)),
closeToVector(e.x, e.y, epsilon: 1e-13),
);
expect(
projector.scaleVector(projector.unscaleVector(e)),
closeToVector(e.x, e.y, epsilon: 1e-13),
);
expect(
projector.unscaleVector(projector.scaleVector(e)),
closeToVector(e.x, e.y, epsilon: 1e-13),
);
}); });
} }

View File

@ -1,3 +1,4 @@
export 'src/close_to_vector.dart';
export 'src/expect_double.dart'; export 'src/expect_double.dart';
export 'src/expect_vector2.dart'; export 'src/expect_vector2.dart';
export 'src/fails_assert.dart'; export 'src/fails_assert.dart';

View File

@ -1,6 +1,7 @@
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
@Deprecated('Use closeToVector() instead')
void expectVector2( void expectVector2(
Vector2 actual, Vector2 actual,
Vector2 expected, { Vector2 expected, {

View File

@ -6,7 +6,9 @@ void main() {
group('expectVector2', () { group('expectVector2', () {
test('can test vector2', () { test('can test vector2', () {
final vector = Vector2.all(1.0); final vector = Vector2.all(1.0);
// ignore: deprecated_member_use_from_same_package
expectVector2(vector + Vector2.all(1.0), Vector2.all(2.0)); expectVector2(vector + Vector2.all(1.0), Vector2.all(2.0));
// ignore: deprecated_member_use_from_same_package
expectVector2(vector + Vector2.all(1.1), Vector2.all(2.0), epsilon: 0.2); expectVector2(vector + Vector2.all(1.1), Vector2.all(2.0), epsilon: 0.2);
}); });
}); });