From cf577bedaff41a13ba10f86f45103ab0f02ed95c Mon Sep 17 00:00:00 2001 From: Pasha Stetsenko Date: Mon, 27 Dec 2021 11:17:32 -0800 Subject: [PATCH] chore(flame_test): Deprecate expectVector2 (#1275) Function expectVector2 is deprecated in favor of closeToVector(). All uses of expectVector2 removed from our tests. --- .../components/hud_margin_component_test.dart | 10 ++--- .../isometric_tile_map_component_test.dart | 2 +- .../components/joystick_component_test.dart | 6 +-- .../components/position_component_test.dart | 20 +++++----- .../flame/test/effects/scale_effect_test.dart | 27 +++++++------- .../flame/test/effects/size_effect_test.dart | 37 +++++++------------ .../flame/test/extensions/matrix4_test.dart | 6 +-- .../flame/test/extensions/vector2_test.dart | 14 +++---- .../game_widget/game_widget_tap_test.dart | 3 +- .../flame/test/game/projections_test.dart | 37 ++++++++----------- packages/flame_test/lib/flame_test.dart | 1 + .../flame_test/lib/src/expect_vector2.dart | 1 + .../flame_test/test/expect_vector2_test.dart | 2 + 13 files changed, 79 insertions(+), 87 deletions(-) diff --git a/packages/flame/test/components/hud_margin_component_test.dart b/packages/flame/test/components/hud_margin_component_test.dart index 93366a55a..db397f55e 100644 --- a/packages/flame/test/components/hud_margin_component_test.dart +++ b/packages/flame/test/components/hud_margin_component_test.dart @@ -17,16 +17,16 @@ void main() async { // and the component has its anchor in the top left corner (which then // is were the margin will be calculated from). // (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.update(0); // After resizing the game, the component should still be 30 pixels from // 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 // changed. 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 // is were the margin will be calculated from). // (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.camera.zoom = 2.0; game.onGameResize(Vector2.all(500)); game.update(0); - expectVector2(marginComponent.position, Vector2(470.0, 460.0)); + expect(marginComponent.position, closeToVector(470, 460)); }, ); }); diff --git a/packages/flame/test/components/isometric_tile_map_component_test.dart b/packages/flame/test/components/isometric_tile_map_component_test.dart index 9e3ce72cd..071df5905 100644 --- a/packages/flame/test/components/isometric_tile_map_component_test.dart +++ b/packages/flame/test/components/isometric_tile_map_component_test.dart @@ -34,7 +34,7 @@ void main() { tileHeight: 8.0, ); - expectVector2(c.getBlockCenterPosition(const Block(0, 0)), Vector2(0, 0)); + expect(c.getBlockCenterPosition(const Block(0, 0)), closeToVector(0, 0)); }); }); } diff --git a/packages/flame/test/components/joystick_component_test.dart b/packages/flame/test/components/joystick_component_test.dart index 518dd4249..87a099efc 100644 --- a/packages/flame/test/components/joystick_component_test.dart +++ b/packages/flame/test/components/joystick_component_test.dart @@ -51,7 +51,7 @@ void main() { ); await game.add(joystick); game.update(0); - expectVector2(joystick.knob!.position, Vector2(10, 10)); + expect(joystick.knob!.position, closeToVector(10, 10)); // Start dragging the joystick game.onDragStart( 1, @@ -77,7 +77,7 @@ void main() { ), ); 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 game.onDragUpdate( 1, @@ -91,7 +91,7 @@ void main() { ), ); game.update(0); - expectVector2(joystick.knob!.position, Vector2(20, 10)); + expect(joystick.knob!.position, closeToVector(20, 10)); }, ); }); diff --git a/packages/flame/test/components/position_component_test.dart b/packages/flame/test/components/position_component_test.dart index 0df4cf141..efed5666c 100644 --- a/packages/flame/test/components/position_component_test.dart +++ b/packages/flame/test/components/position_component_test.dart @@ -22,7 +22,7 @@ class _MyDebugComponent extends PositionComponent { } void main() { - group('PositionComponent overlap test', () { + group('PositionComponent', () { test('overlap', () { final component = PositionComponent(); component.position.setValues(2.0, 2.0); @@ -412,43 +412,45 @@ void main() { ..scale = Vector2(2.0, 3.0) ..position = startPosition; final centerPosition = component.center; + final x = centerPosition.x; + final y = centerPosition.y; component.flipVerticallyAroundCenter(); // Same position after one vertical flip. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipVerticallyAroundCenter(); // Same position after flipping back the vertical flip. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipHorizontallyAroundCenter(); // Same position after one horizontal flip. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipHorizontallyAroundCenter(); // Same position after flipping back the horizontal flip. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipVerticallyAroundCenter(); component.flipHorizontallyAroundCenter(); // Same position after flipping both vertically and horizontally. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipVerticallyAroundCenter(); component.flipHorizontallyAroundCenter(); // 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.flipVerticallyAroundCenter(); // Same position after flipping both horizontally and vertically. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); component.flipVerticallyAroundCenter(); component.flipHorizontallyAroundCenter(); // Same position after flipping back both horizontally and vertically in // the reverse order. - expectVector2(component.center, centerPosition); + expect(component.center, closeToVector(x, y, epsilon: 1e-14)); }); test('rotations', () { diff --git a/packages/flame/test/effects/scale_effect_test.dart b/packages/flame/test/effects/scale_effect_test.dart index 51ff681f4..f0e74af05 100644 --- a/packages/flame/test/effects/scale_effect_test.dart +++ b/packages/flame/test/effects/scale_effect_test.dart @@ -17,17 +17,17 @@ void main() { ScaleEffect.by(Vector2.all(2.0), EffectController(duration: 1)), ); game.update(0); - expectVector2(component.scale, Vector2.all(1.0)); + expect(component.scale, closeToVector(1, 1)); expect(component.children.length, 1); game.update(0.5); - expectVector2(component.scale, Vector2.all(1.5)); + expect(component.scale, closeToVector(1.5, 1.5)); game.update(0.5); - expectVector2(component.scale, Vector2.all(2.0)); + expect(component.scale, closeToVector(2, 2)); game.update(0); expect(component.children.length, 0); - expectVector2(component.scale, Vector2.all(2.0)); + expect(component.scale, closeToVector(2, 2)); }); flameGame.test('absolute', (game) { @@ -39,17 +39,17 @@ void main() { ScaleEffect.to(Vector2.all(3.0), EffectController(duration: 1)), ); game.update(0); - expectVector2(component.scale, Vector2.all(1.0)); + expect(component.scale, closeToVector(1, 1)); expect(component.children.length, 1); game.update(0.5); - expectVector2(component.scale, Vector2.all(2.0)); + expect(component.scale, closeToVector(2, 2)); game.update(0.5); - expectVector2(component.scale, Vector2.all(3.0)); + expect(component.scale, closeToVector(3, 3)); game.update(0); expect(component.children.length, 0); - expectVector2(component.scale, Vector2.all(3.0)); + expect(component.scale, closeToVector(3, 3)); }); flameGame.test('reset relative', (game) { @@ -61,15 +61,14 @@ void main() { EffectController(duration: 1), ); component.add(effect..removeOnFinish = false); - final expectedScale = Vector2.all(1.0); + var expectedScale = 1.0; for (var i = 0; i < 5; i++) { - expectVector2(component.scale, expectedScale); // After each reset the object will be scaled up twice // relative to its scale at the start of the effect. effect.reset(); game.update(1); - expectedScale.multiply(Vector2.all(2)); - expectVector2(component.scale, expectedScale); + expectedScale *= 2; + expect(component.scale, closeToVector(expectedScale, expectedScale)); } }); @@ -88,7 +87,7 @@ void main() { // `Vector2(1, 1)`, regardless of its initial orientation. effect.reset(); 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); // Typically, `component.scale` could accumulate numeric discrepancy on // 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)); }); }); } diff --git a/packages/flame/test/effects/size_effect_test.dart b/packages/flame/test/effects/size_effect_test.dart index 9ce2e94b7..1f90c18ae 100644 --- a/packages/flame/test/effects/size_effect_test.dart +++ b/packages/flame/test/effects/size_effect_test.dart @@ -18,17 +18,17 @@ void main() { SizeEffect.by(Vector2.all(1.0), EffectController(duration: 1)), ); game.update(0); - expectVector2(component.size, Vector2.all(1.0)); + expect(component.size, closeToVector(1, 1)); expect(component.children.length, 1); game.update(0.5); - expectVector2(component.size, Vector2.all(1.5)); + expect(component.size, closeToVector(1.5, 1.5)); game.update(0.5); - expectVector2(component.size, Vector2.all(2.0)); + expect(component.size, closeToVector(2, 2)); game.update(0); expect(component.children.length, 0); - expectVector2(component.size, Vector2.all(2.0)); + expect(component.size, closeToVector(2, 2)); }); flameGame.test('absolute', (game) { @@ -40,17 +40,17 @@ void main() { SizeEffect.to(Vector2.all(3.0), EffectController(duration: 1)), ); game.update(0); - expectVector2(component.size, Vector2.all(1.0)); + expect(component.size, closeToVector(1, 1)); expect(component.children.length, 1); game.update(0.5); - expectVector2(component.size, Vector2.all(2.0)); + expect(component.size, closeToVector(2, 2)); game.update(0.5); - expectVector2(component.size, Vector2.all(3.0)); + expect(component.size, closeToVector(3, 3)); game.update(0); expect(component.children.length, 0); - expectVector2(component.size, Vector2.all(3.0)); + expect(component.size, closeToVector(3, 3)); }); flameGame.test('reset relative', (game) { @@ -64,13 +64,12 @@ void main() { component.add(effect..removeOnFinish = false); final expectedSize = Vector2.zero(); for (var i = 0; i < 5; i++) { - expectVector2(component.size, expectedSize); // After each reset the object will be sized up by Vector2(1, 1) // relative to its size at the start of the effect. effect.reset(); game.update(1); 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. effect.reset(); 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); - expectVector2( - component.size, - Vector2.all(1), - epsilon: 1e-15, - ); // 5*1/10 + 0.5*1 + expect(component.size, closeToVector(1, 1)); // 5*1/10 + 0.5*1 game.update(1); - expectVector2( - component.size, - Vector2.all(1), - epsilon: 1e-15, - ); // 5*2/10 + 0.5*1 - 0.5*1 + expect(component.size, closeToVector(1, 1)); // 5*2/10 + 0.5*1 - 0.5*1 for (var i = 0; i < 10; i++) { game.update(1); } - expectVector2(component.size, Vector2.all(5), epsilon: 1e-15); + expect(component.size, closeToVector(5, 5)); expect(component.children.length, 0); }); @@ -154,7 +145,7 @@ void main() { game.update(1000 - totalTime); // Typically, `component.size` could accumulate numeric discrepancy on the // 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)); }); }); } diff --git a/packages/flame/test/extensions/matrix4_test.dart b/packages/flame/test/extensions/matrix4_test.dart index 5405484ce..f80f9c2ac 100644 --- a/packages/flame/test/extensions/matrix4_test.dart +++ b/packages/flame/test/extensions/matrix4_test.dart @@ -32,7 +32,7 @@ void main() { final matrix4 = Matrix4.translation(Vector3(0, 10, 0)); final input = Vector2.all(10); - expectVector2(matrix4.transform2(input), Vector2(10, 20)); + expect(matrix4.transform2(input), closeToVector(10, 20)); }); test('test transformed2', () { @@ -41,8 +41,8 @@ void main() { final out = Vector2.zero(); final result = matrix4.transformed2(input, out); - expectVector2(out, input); - expectVector2(result, Vector2(10, 20)); + expect(out, closeToVector(input.x, input.y)); + expect(result, closeToVector(10, 20)); }); }); } diff --git a/packages/flame/test/extensions/vector2_test.dart b/packages/flame/test/extensions/vector2_test.dart index 7880681cb..6ecd3e813 100644 --- a/packages/flame/test/extensions/vector2_test.dart +++ b/packages/flame/test/extensions/vector2_test.dart @@ -149,48 +149,48 @@ void main() { test('rotate - no center defined', () { final position = Vector2(0.0, 1.0); 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', () { final position = Vector2(0.0, -1.0); position.rotate(-math.pi / 2); - expectVector2(position, Vector2(-1.0, 0.0)); + expect(position, closeToVector(-1.0, 0.0)); }); test('rotate - with center defined', () { final position = Vector2(0.0, 1.0); final center = Vector2(1.0, 1.0); 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', () { final position = Vector2(0.0, 1.0); final center = Vector2(1.0, 1.0); 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', () { final position = Vector2(2.0, -3.0); final center = Vector2(1.0, 1.0); 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', () { final position = Vector2(-2.0, 3.0); final center = Vector2(1.0, 1.0); 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', () { final position = Vector2(-2.0, -3.0); final center = Vector2(1.0, 0.0); position.rotate(math.pi / 2, center: center); - expectVector2(position, Vector2(4.0, -3.0)); + expect(position, closeToVector(4.0, -3.0)); }); test('screenAngle', () { diff --git a/packages/flame/test/game/game_widget/game_widget_tap_test.dart b/packages/flame/test/game/game_widget/game_widget_tap_test.dart index 93eb850ab..b2ba4ae65 100644 --- a/packages/flame/test/game/game_widget/game_widget_tap_test.dart +++ b/packages/flame/test/game/game_widget/game_widget_tap_test.dart @@ -49,7 +49,8 @@ void main() { await Future.delayed(const Duration(milliseconds: 50)); await tester.tapAt(tapPosition); expect(game.doubleTapRegistered, isTrue); - expectVector2(game.doubleTapPosition!, tapPosition.toVector2()); + final tapVector = tapPosition.toVector2(); + expect(game.doubleTapPosition, closeToVector(tapVector.x, tapVector.y)); }, ); }); diff --git a/packages/flame/test/game/projections_test.dart b/packages/flame/test/game/projections_test.dart index cfc2dcb95..bfc0471cb 100644 --- a/packages/flame/test/game/projections_test.dart +++ b/packages/flame/test/game/projections_test.dart @@ -403,23 +403,6 @@ void main() { // project ∘ unproject = identity = unproject ∘ project // by evaluating an arbitrary list of parameters. 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 = [-1, 0, 1, 2, 10, 100]; final someVectors = [ for (final x in someValues) @@ -427,9 +410,21 @@ void _assertIdentityOfProjector(Projector projector) { ]; someVectors.forEach((e) { - expectVector2(identity1(e), e); - expectVector2(identity2(e), e); - expectVector2(identity3(e), e); - expectVector2(identity4(e), e); + expect( + projector.projectVector(projector.unprojectVector(e)), + closeToVector(e.x, e.y, epsilon: 1e-13), + ); + 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), + ); }); } diff --git a/packages/flame_test/lib/flame_test.dart b/packages/flame_test/lib/flame_test.dart index be76a0e5a..52949e855 100644 --- a/packages/flame_test/lib/flame_test.dart +++ b/packages/flame_test/lib/flame_test.dart @@ -1,3 +1,4 @@ +export 'src/close_to_vector.dart'; export 'src/expect_double.dart'; export 'src/expect_vector2.dart'; export 'src/fails_assert.dart'; diff --git a/packages/flame_test/lib/src/expect_vector2.dart b/packages/flame_test/lib/src/expect_vector2.dart index a9a72f25d..a5f12185a 100644 --- a/packages/flame_test/lib/src/expect_vector2.dart +++ b/packages/flame_test/lib/src/expect_vector2.dart @@ -1,6 +1,7 @@ import 'package:flame/extensions.dart'; import 'package:test/test.dart'; +@Deprecated('Use closeToVector() instead') void expectVector2( Vector2 actual, Vector2 expected, { diff --git a/packages/flame_test/test/expect_vector2_test.dart b/packages/flame_test/test/expect_vector2_test.dart index 448dfa141..b289db8ad 100644 --- a/packages/flame_test/test/expect_vector2_test.dart +++ b/packages/flame_test/test/expect_vector2_test.dart @@ -6,7 +6,9 @@ void main() { group('expectVector2', () { test('can test vector2', () { final vector = Vector2.all(1.0); + // ignore: deprecated_member_use_from_same_package 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); }); });