Class PretendClock
java.lang.Object
com.crossbotics.ftchelper.testkit.PretendClock
- All Implemented Interfaces:
LongSupplier
A pretend clock for JUnit tests: the time only moves when your test says so. The robot's parts read
it (through
Time) instead of the real clock, so a test of timing does not wait and gives the
same result on every computer.
@After public void realClock() { PretendClock.stopUsing(); }
@Test public void theArmPushesAfterStaging() {
PretendClock clock = PretendClock.use();
PretendMotor fly = devices.motor("launcher");
robot.launcher.spinUp();
for (int i = 0; i < 100; i++) { // 0.5 s, 5 ms a loop
clock.addSec(0.005);
fly.velocity = fly.velocityCommand; // the pretend wheel reaches the speed asked
robot.all().update();
if (i == 60) robot.launcher.fire(); // at 0.3 s: the wheel is ready
}
assertEquals(0.7, devices.servo("feeder_arm").position, 1e-9);
}
Docs: docs/modules/testkit.md, docs/tests/03-checks-before-practice-and-matches.md-
Method Summary
Modifier and TypeMethodDescriptionvoidaddSec(double sec) Moves the time forward.longdoublesec()The time now, seconds since this clock was made (it starts at 1).static voidThe real clock again (put it in@After).static PretendClockuse()Makes a pretend clock and has the robot's parts use it.
-
Method Details
-
use
Makes a pretend clock and has the robot's parts use it. CallstopUsing()in@After. -
stopUsing
public static void stopUsing()The real clock again (put it in@After). -
addSec
public void addSec(double sec) Moves the time forward. @param sec seconds (0 or more) -
sec
public double sec()The time now, seconds since this clock was made (it starts at 1). -
getAsLong
public long getAsLong()- Specified by:
getAsLongin interfaceLongSupplier
-