From 88fd7fdef8985c2f26f9554e6be72a502f8012c4 Mon Sep 17 00:00:00 2001 From: Kaya <95276965+kytpbs@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:39:57 +0300 Subject: [PATCH] Create LEDWaitingCommandTest.java --- .../LEDWaitingCommandTest.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/test/java/command_tests/simple_command_tests/LEDWaitingCommandTest.java diff --git a/src/test/java/command_tests/simple_command_tests/LEDWaitingCommandTest.java b/src/test/java/command_tests/simple_command_tests/LEDWaitingCommandTest.java new file mode 100644 index 0000000..0fff8fc --- /dev/null +++ b/src/test/java/command_tests/simple_command_tests/LEDWaitingCommandTest.java @@ -0,0 +1,64 @@ +package command_tests.simple_command_tests; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static subsystem_tests.led_tests.utils.LEDTestUtils.testAtTime; + +import command_tests.utils.CommandTestBase; +import edu.wpi.first.wpilibj.Timer; +import frc.robot.commands.led_commands.LEDLoadingWaitCommand; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LEDWaitingCommandTest extends CommandTestBase { + private static final double kWaitTime = 2.0; + private LEDLoadingWaitCommand ledLoadingWaitCommand; + + @BeforeEach + public void setUp() { + super.setUp(); + ledLoadingWaitCommand = new LEDLoadingWaitCommand(kWaitTime); + commandScheduler.schedule(ledLoadingWaitCommand); + } + + @AfterEach + public void tearDown() { + super.tearDown(); + } + + void testLEDLoading(double waitTime) { + double startTime = Timer.getFPGATimestamp(); + for (double i = 0.1; Timer.getFPGATimestamp() - startTime + i < waitTime; i += 0.1) { + Timer.delay(i); + commandScheduler.run(); + testAtTime(ledSubsystem, startTime, waitTime); + } + } + + /** + * This test takes a lot of time as it tries a lot of differently timed {@link + * LEDLoadingWaitCommand}s + */ + @Test + void testLEDLoading() { + // try multiple wait times, just so we can do it + for (double i = 0.0; i <= 6; i += 1.2) { + commandScheduler.cancelAll(); + commandScheduler.schedule(new LEDLoadingWaitCommand(i)); + commandScheduler.run(); + testLEDLoading(i); + } + } + + @Test + void itActuallyEnds() { + commandScheduler.run(); + assertEquals( + false, + ledLoadingWaitCommand.isFinished(), + "led wait command shouldn't finish before wait time"); + Timer.delay(kWaitTime); // just in case add a little bit extra time + assertEquals( + true, ledLoadingWaitCommand.isFinished(), "led wait command should finish after wait time"); + } +}