Skip to content

Commit

Permalink
refactor!: used setters and getters for motorSpeed (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago authored May 14, 2022
1 parent c032f9c commit 96103f1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 55 deletions.
10 changes: 2 additions & 8 deletions packages/forge2d/lib/src/dynamics/joints/prismatic_joint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions packages/forge2d/lib/src/dynamics/joints/wheel_joint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 40 additions & 14 deletions packages/forge2d/test/dynamics/joints/prismatic_joint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrismaticJoint>());
});

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);
});
Expand Down
52 changes: 38 additions & 14 deletions packages/forge2d/test/dynamics/joints/revolute_joint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<RevoluteJoint>());
});

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);
});
Expand Down
52 changes: 38 additions & 14 deletions packages/forge2d/test/dynamics/joints/wheel_joint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<WheelJoint>());
});

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);
});
Expand Down

0 comments on commit 96103f1

Please sign in to comment.