|
| 1 | +// Copyright (c) 2025 FRC 6423 - Ward Melville Iron Patriots |
| 2 | +// https://github.com/wmironpatriots |
| 3 | +// |
| 4 | +// Open Source Software; you can modify and/or share it under the terms of |
| 5 | +// MIT license file in the root directory of this project |
| 6 | + |
| 7 | +package org.frc6423.robot; |
| 8 | + |
| 9 | +import static edu.wpi.first.units.Units.Seconds; |
| 10 | +import static org.frc6423.lib.utilities.TestUtils.reset; |
| 11 | +import static org.frc6423.lib.utilities.TestUtils.runToCompletion; |
| 12 | +import static org.frc6423.lib.utilities.TestUtils.setupTest; |
| 13 | +import static org.frc6423.robot.subsystems.superstructure.elevator.Elevator.MAX_EXTENSION_HEIGHT; |
| 14 | + |
| 15 | +import edu.wpi.first.units.measure.Time; |
| 16 | +import edu.wpi.first.wpilibj.Timer; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.stream.Stream; |
| 19 | +import org.frc6423.robot.subsystems.superstructure.elevator.Elevator; |
| 20 | +import org.frc6423.robot.subsystems.superstructure.elevator.ElevatorExtension; |
| 21 | +import org.frc6423.robot.subsystems.superstructure.elevator.ElevatorIOSim; |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.RepeatedTest; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +/** {@link Elevator} subsystem test class */ |
| 30 | +class ElevatorTests { |
| 31 | + /** The max time a test can run for before auto failing */ |
| 32 | + static final Time TIMEOUT = Seconds.of(3.0); |
| 33 | + |
| 34 | + Elevator elevator; |
| 35 | + Timer timer = new Timer(); |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void init() { |
| 39 | + // Init sys |
| 40 | + setupTest(); |
| 41 | + // Create elevator |
| 42 | + elevator = new Elevator(new ElevatorIOSim()); |
| 43 | + // Start test timer |
| 44 | + timer.start(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + public void destroy() throws Exception { |
| 49 | + // Stop and record test time |
| 50 | + timer.stop(); |
| 51 | + System.out.println("FINISHED TEST IN " + timer.get() + " SECONDS"); |
| 52 | + |
| 53 | + // Reset elevator |
| 54 | + reset(elevator); |
| 55 | + } |
| 56 | + |
| 57 | + /** Run 10 randomly generated extension heights between 0.0 and the max extension height */ |
| 58 | + @RepeatedTest(10) |
| 59 | + public void runRandExtension() { |
| 60 | + runToCompletion( |
| 61 | + elevator |
| 62 | + // Picks a random extension height between 0.0 and max extension height |
| 63 | + .runExtension(MAX_EXTENSION_HEIGHT.times(Math.random())) |
| 64 | + .until(() -> elevator.isNearSetpointPose()) |
| 65 | + .withTimeout(TIMEOUT)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Run all {@link ElevatorExtension} |
| 70 | + * |
| 71 | + * @param extension {@link ElevatorExtension} |
| 72 | + */ |
| 73 | + @ParameterizedTest |
| 74 | + @MethodSource("provideExtensionHeights") |
| 75 | + public void runExtensions(ElevatorExtension extension) { |
| 76 | + runToCompletion( |
| 77 | + elevator |
| 78 | + .runExtension(extension) |
| 79 | + .until(() -> elevator.isNearSetpointPose()) |
| 80 | + .withTimeout(TIMEOUT)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return {@link Stream} of all {@link ElevatorExtension} |
| 85 | + */ |
| 86 | + private static Stream<Arguments> provideExtensionHeights() { |
| 87 | + ArrayList<Arguments> extensions = new ArrayList<>(); |
| 88 | + for (var extension : ElevatorExtension.values()) { |
| 89 | + extensions.add(Arguments.of(extension)); |
| 90 | + } |
| 91 | + |
| 92 | + return extensions.stream(); |
| 93 | + } |
| 94 | +} |
0 commit comments