diff --git a/packages/forge2d/lib/src/dynamics/joints/prismatic_joint.dart b/packages/forge2d/lib/src/dynamics/joints/prismatic_joint.dart index 077cf574..b5775049 100644 --- a/packages/forge2d/lib/src/dynamics/joints/prismatic_joint.dart +++ b/packages/forge2d/lib/src/dynamics/joints/prismatic_joint.dart @@ -275,20 +275,14 @@ class PrismaticJoint extends Joint { } /// Set the motor speed, usually in meters per second. - /// - /// @param speed - void setMotorSpeed(double speed) { + set motorSpeed(double speed) { bodyA.setAwake(true); bodyB.setAwake(true); _motorSpeed = speed; } /// Get the motor speed, usually in meters per second. - /// - /// @return - double getMotorSpeed() { - return _motorSpeed; - } + double get motorSpeed => _motorSpeed; /// Set the maximum motor force, usually in N. /// diff --git a/packages/forge2d/lib/src/dynamics/joints/revolute_joint.dart b/packages/forge2d/lib/src/dynamics/joints/revolute_joint.dart index c919dc5d..efb77687 100644 --- a/packages/forge2d/lib/src/dynamics/joints/revolute_joint.dart +++ b/packages/forge2d/lib/src/dynamics/joints/revolute_joint.dart @@ -461,7 +461,7 @@ class RevoluteJoint extends Joint { _enableMotor = flag; } - void setMotorSpeed(final double speed) { + set motorSpeed(double speed) { bodyA.setAwake(true); bodyB.setAwake(true); _motorSpeed = speed; diff --git a/packages/forge2d/lib/src/dynamics/joints/wheel_joint.dart b/packages/forge2d/lib/src/dynamics/joints/wheel_joint.dart index be3f190d..ed165e85 100644 --- a/packages/forge2d/lib/src/dynamics/joints/wheel_joint.dart +++ b/packages/forge2d/lib/src/dynamics/joints/wheel_joint.dart @@ -137,15 +137,13 @@ class WheelJoint extends Joint { _enableMotor = flag; } - void setMotorSpeed(double speed) { + set motorSpeed(double speed) { bodyA.setAwake(true); bodyB.setAwake(true); _motorSpeed = speed; } - double getMotorSpeed() { - return _motorSpeed; - } + double get motorSpeed => _motorSpeed; double getMaxMotorTorque() { return _maxMotorTorque; diff --git a/packages/forge2d/test/dynamics/joints/prismatic_joint_test.dart b/packages/forge2d/test/dynamics/joints/prismatic_joint_test.dart index c1c4444c..04a8ce5f 100644 --- a/packages/forge2d/test/dynamics/joints/prismatic_joint_test.dart +++ b/packages/forge2d/test/dynamics/joints/prismatic_joint_test.dart @@ -2,37 +2,63 @@ import 'package:forge2d/forge2d.dart'; import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; -import '../../helpers/helpers.dart'; +class _MockBody extends Mock implements Body {} + +class _MockDebugDraw extends Mock implements DebugDraw {} void main() { group('PrismaticJoint', () { - test('can be instantiated', () { - final world = World(); - final jointDef = PrismaticJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world); + late PrismaticJointDef jointDef; + setUp( + () { + jointDef = PrismaticJointDef() + ..bodyA = _MockBody() + ..bodyB = _MockBody(); + }, + ); + + test('can be instantiated', () { expect(PrismaticJoint(jointDef), isA()); }); + group('motorSpeed', () { + test('can change motor speed', () { + final joint = PrismaticJoint(jointDef); + + final oldMotorSpeed = joint.motorSpeed; + final newMotorSpeed = oldMotorSpeed + 1; + joint.motorSpeed = newMotorSpeed; + + expect(joint.motorSpeed, equals(newMotorSpeed)); + }); + + test('wakes up both bodies', () { + final joint = PrismaticJoint(jointDef); + joint.motorSpeed = 1; + + verify(() => joint.bodyA.setAwake(true)).called(1); + verify(() => joint.bodyB.setAwake(true)).called(1); + }); + }); + group('render', () { - late World world; late DebugDraw debugDraw; setUp(() { - world = World(); - debugDraw = MockDebugDraw(); + debugDraw = _MockDebugDraw(); registerFallbackValue(Vector2.zero()); registerFallbackValue(Color3i.black); }); test('draws three segments', () { - final joint = PrismaticJoint( - PrismaticJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world), - ); + final joint = PrismaticJoint(jointDef); + when(() => joint.bodyA.transform).thenReturn(Transform.zero()); + when(() => joint.bodyB.transform).thenReturn(Transform.zero()); + when(() => joint.bodyA.worldPoint(any())).thenReturn(Vector2.zero()); + when(() => joint.bodyB.worldPoint(any())).thenReturn(Vector2.zero()); + joint.render(debugDraw); verify(() => debugDraw.drawSegment(any(), any(), any())).called(3); }); diff --git a/packages/forge2d/test/dynamics/joints/revolute_joint_test.dart b/packages/forge2d/test/dynamics/joints/revolute_joint_test.dart index 8139e32f..1af79891 100644 --- a/packages/forge2d/test/dynamics/joints/revolute_joint_test.dart +++ b/packages/forge2d/test/dynamics/joints/revolute_joint_test.dart @@ -2,37 +2,61 @@ import 'package:forge2d/forge2d.dart'; import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; -import '../../helpers/helpers.dart'; +class _MockBody extends Mock implements Body {} + +class _MockDebugDraw extends Mock implements DebugDraw {} void main() { group('RevoluteJoint', () { - test('can be instantiated', () { - final world = World(); - final jointDef = RevoluteJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world); + late RevoluteJointDef jointDef; + + setUp(() { + jointDef = RevoluteJointDef() + ..bodyA = _MockBody() + ..bodyB = _MockBody(); + }); + test('can be instantiated', () { expect(RevoluteJoint(jointDef), isA()); }); + group('motorSpeed', () { + test('can change motor speed', () { + final joint = RevoluteJoint(jointDef); + + final oldMotorSpeed = joint.motorSpeed; + final newMotorSpeed = oldMotorSpeed + 1; + joint.motorSpeed = newMotorSpeed; + + expect(joint.motorSpeed, equals(newMotorSpeed)); + }); + + test('wakes up both bodies', () { + final joint = RevoluteJoint(jointDef); + joint.motorSpeed = 1; + + verify(() => joint.bodyA.setAwake(true)).called(1); + verify(() => joint.bodyB.setAwake(true)).called(1); + }); + }); + group('render', () { - late World world; late DebugDraw debugDraw; setUp(() { - world = World(); - debugDraw = MockDebugDraw(); + debugDraw = _MockDebugDraw(); registerFallbackValue(Vector2.zero()); registerFallbackValue(Color3i.black); }); test('draws three segments', () { - final joint = RevoluteJoint( - RevoluteJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world), - ); + final joint = RevoluteJoint(jointDef); + when(() => joint.bodyA.transform).thenReturn(Transform.zero()); + when(() => joint.bodyB.transform).thenReturn(Transform.zero()); + when(() => joint.bodyA.worldPoint(any())).thenReturn(Vector2.zero()); + when(() => joint.bodyB.worldPoint(any())).thenReturn(Vector2.zero()); + joint.render(debugDraw); verify(() => debugDraw.drawSegment(any(), any(), any())).called(3); }); diff --git a/packages/forge2d/test/dynamics/joints/wheel_joint_test.dart b/packages/forge2d/test/dynamics/joints/wheel_joint_test.dart index 4019637c..d2e193c6 100644 --- a/packages/forge2d/test/dynamics/joints/wheel_joint_test.dart +++ b/packages/forge2d/test/dynamics/joints/wheel_joint_test.dart @@ -2,37 +2,61 @@ import 'package:forge2d/forge2d.dart'; import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; -import '../../helpers/helpers.dart'; +class _MockBody extends Mock implements Body {} + +class _MockDebugDraw extends Mock implements DebugDraw {} void main() { group('WheelJoint', () { - test('can be instantiated', () { - final world = World(); - final jointDef = WheelJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world); + late WheelJointDef jointDef; + + setUp(() { + jointDef = WheelJointDef() + ..bodyA = _MockBody() + ..bodyB = _MockBody(); + }); + test('can be instantiated', () { expect(WheelJoint(jointDef), isA()); }); + group('motorSpeed', () { + test('can change motor speed', () { + final joint = WheelJoint(jointDef); + + final oldMotorSpeed = joint.motorSpeed; + final newMotorSpeed = oldMotorSpeed + 1; + joint.motorSpeed = newMotorSpeed; + + expect(joint.motorSpeed, equals(newMotorSpeed)); + }); + + test('wakes up both bodies', () { + final joint = WheelJoint(jointDef); + joint.motorSpeed = 1; + + verify(() => joint.bodyA.setAwake(true)).called(1); + verify(() => joint.bodyB.setAwake(true)).called(1); + }); + }); + group('render', () { - late World world; late DebugDraw debugDraw; setUp(() { - world = World(); - debugDraw = MockDebugDraw(); + debugDraw = _MockDebugDraw(); registerFallbackValue(Vector2.zero()); registerFallbackValue(Color3i.black); }); test('draws three segments', () { - final joint = WheelJoint( - WheelJointDef() - ..bodyA = Body(BodyDef(), world) - ..bodyB = Body(BodyDef(), world), - ); + final joint = WheelJoint(jointDef); + when(() => joint.bodyA.transform).thenReturn(Transform.zero()); + when(() => joint.bodyB.transform).thenReturn(Transform.zero()); + when(() => joint.bodyA.worldPoint(any())).thenReturn(Vector2.zero()); + when(() => joint.bodyB.worldPoint(any())).thenReturn(Vector2.zero()); + joint.render(debugDraw); verify(() => debugDraw.drawSegment(any(), any(), any())).called(3); });