Skip to content

Commit 6f2bf61

Browse files
committed
feat: 'borrow' 1155's testUtil class
Signed-off-by: Dasun Abeykoon <[email protected]>
1 parent fcf3fcc commit 6f2bf61

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

src/main/java/org/frc6423/lib/utilities/TestUtils.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
package org.frc6423.lib.utilities;
88

9+
import static edu.wpi.first.units.Units.Seconds;
10+
911
import edu.wpi.first.hal.AllianceStationID;
1012
import edu.wpi.first.hal.HAL;
13+
import edu.wpi.first.units.measure.Time;
1114
import edu.wpi.first.wpilibj.simulation.DriverStationSim;
1215
import edu.wpi.first.wpilibj.simulation.SimHooks;
16+
import edu.wpi.first.wpilibj2.command.Command;
1317
import edu.wpi.first.wpilibj2.command.CommandScheduler;
18+
import org.frc6423.robot.Constants.Flags;
1419

15-
/** Contains static methods for creating tests */
20+
/** Contains static methods for creating tests "Inspired" by FRC 1155 */
1621
public class TestUtils {
1722
/** Set up DS and initalizes HAL with default values and asserts that it doesn't fail */
1823
public static void setupTest() {
@@ -38,4 +43,67 @@ public static void reset(AutoCloseable... subsystems) throws Exception {
3843
subsystem.close();
3944
}
4045
}
46+
47+
/**
48+
* Runs CommandScheduler and updates timer repeatedly to fast forward subsystems and run commands.
49+
*
50+
* @param ticks The number of times CommandScheduler is run
51+
*/
52+
public static void fastForward(int ticks) {
53+
for (int i = 0; i < ticks; i++) {
54+
CommandScheduler.getInstance().run();
55+
SimHooks.stepTiming(Flags.PERIOD.in(Seconds));
56+
}
57+
}
58+
59+
/**
60+
* Fasts forward in time by running CommandScheduler and updating timer.
61+
*
62+
* @param time
63+
*/
64+
public static void fastForward(Time time) {
65+
fastForward((int) (time.in(Seconds) / Flags.PERIOD.in(Seconds)));
66+
}
67+
68+
/**
69+
* Runs CommandScheduler and updates timer to fast forward subsystems by 4 seconds and run
70+
* commands.
71+
*/
72+
public static void fastForward() {
73+
fastForward(Seconds.of(4));
74+
}
75+
76+
/**
77+
* Schedules and runs a command.
78+
*
79+
* @param command The command to run.
80+
*/
81+
public static void run(Command command) {
82+
run(command, 1);
83+
}
84+
85+
/**
86+
* Schedules and runs a command.
87+
*
88+
* @param command The command to run.
89+
* @param runs The number of times CommandScheduler is run.
90+
*/
91+
public static void run(Command command, int runs) {
92+
command.schedule();
93+
fastForward(runs);
94+
}
95+
96+
/**
97+
* Schedules a command and runs it until it ends. Be careful -- if the command you give never
98+
* ends, this will be an infinite loop!
99+
*
100+
* @param command
101+
*/
102+
public static void runToCompletion(Command command) {
103+
command.schedule();
104+
fastForward(1);
105+
while (command.isScheduled()) {
106+
fastForward(1);
107+
}
108+
}
41109
}

0 commit comments

Comments
 (0)