-
Notifications
You must be signed in to change notification settings - Fork 0
Add CommandTester and WPILibExtension #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/CommandTester.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.team2813.lib2813.testing.junit.jupiter; | ||
|
|
||
| import edu.wpi.first.wpilibj2.command.Command; | ||
|
|
||
| /** | ||
| * Allows tests to run commands. | ||
| * | ||
| * <p>Tests can get an instance by using {@link WPILibExtension}. | ||
| */ | ||
| public interface CommandTester { | ||
|
|
||
| /** Schedules the provided command and runs it until it completes. */ | ||
| void runUntilComplete(Command command); | ||
| } |
96 changes: 96 additions & 0 deletions
96
testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.team2813.lib2813.testing.junit.jupiter; | ||
|
|
||
| import edu.wpi.first.hal.HAL; | ||
| import edu.wpi.first.wpilibj.simulation.DriverStationSim; | ||
| import edu.wpi.first.wpilibj2.command.CommandScheduler; | ||
| import org.junit.jupiter.api.extension.AfterAllCallback; | ||
| import org.junit.jupiter.api.extension.AfterEachCallback; | ||
| import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
| import org.junit.jupiter.api.extension.Extension; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.ParameterContext; | ||
| import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
| import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
|
||
| /** | ||
| * JUnit Jupiter extension for testing code that depends on WPILib. | ||
| * | ||
| * <p>Also provides a {@link CommandTester} for tests. | ||
| * | ||
| * <p>Example use: | ||
| * | ||
| * <pre>{@code | ||
| * @ExtendWith(WPILibExtension.class) | ||
| * public final class FlightSubsystemTest { | ||
| * | ||
| * @Test | ||
| * public void initiallyNotInAir() { | ||
| * var flight = new FlightSubsystem(); | ||
| * | ||
| * assertThat(flight.inAir()).isFalse(); | ||
| * } | ||
| * | ||
| * @Test | ||
| * public void takesFlight(CommandTester commandTester) { | ||
| * var flight = new FlightSubsystem(); | ||
| * Command takeOff = flight.createTakeOffCommandCommand(); | ||
| * | ||
| * commandTester.runUntilComplete(takeOff); | ||
| * | ||
| * assertThat(flight.inAir()).isTrue(); | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| */ | ||
| public final class WPILibExtension | ||
| implements Extension, | ||
| AfterAllCallback, | ||
| AfterEachCallback, | ||
| BeforeAllCallback, | ||
| ParameterResolver { | ||
|
|
||
| @Override | ||
| public void beforeAll(ExtensionContext context) { | ||
| // See https://www.chiefdelphi.com/t/driverstation-getalliance-in-gradle-test/ | ||
| HAL.initialize(500, 0); | ||
| DriverStationSim.setEnabled(true); | ||
| DriverStationSim.notifyNewData(); | ||
| CommandScheduler.getInstance().enable(); | ||
| CommandScheduler.getInstance().cancelAll(); | ||
| CommandScheduler.getInstance().unregisterAllSubsystems(); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterEach(ExtensionContext context) { | ||
| CommandScheduler.getInstance().cancelAll(); | ||
| CommandScheduler.getInstance().unregisterAllSubsystems(); | ||
| } | ||
|
|
||
| @Override | ||
| public void afterAll(ExtensionContext context) { | ||
| CommandScheduler.getInstance().cancelAll(); | ||
| CommandScheduler.getInstance().unregisterAllSubsystems(); | ||
| CommandScheduler.getInstance().disable(); | ||
| DriverStationSim.setEnabled(false); | ||
| DriverStationSim.notifyNewData(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean supportsParameter( | ||
| ParameterContext parameterContext, ExtensionContext extensionContext) | ||
| throws ParameterResolutionException { | ||
| return CommandTester.class.equals(parameterContext.getParameter().getType()); | ||
| } | ||
|
|
||
| @Override | ||
| public CommandTester resolveParameter( | ||
| ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
| return command -> { | ||
| CommandScheduler scheduler = CommandScheduler.getInstance(); | ||
| command.schedule(); | ||
| do { | ||
| scheduler.run(); | ||
| } while (scheduler.isScheduled(command)); | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "fileName": "WPILibNewCommands.json", | ||
| "name": "WPILib-New-Commands", | ||
| "version": "1.0.0", | ||
| "uuid": "111e20f7-815e-48f8-9dd6-e675ce75b266", | ||
| "frcYear": "2025", | ||
| "mavenUrls": [], | ||
| "jsonUrl": "", | ||
| "javaDependencies": [ | ||
| { | ||
| "groupId": "edu.wpi.first.wpilibNewCommands", | ||
| "artifactId": "wpilibNewCommands-java", | ||
| "version": "wpilib" | ||
| } | ||
| ], | ||
| "jniDependencies": [], | ||
| "cppDependencies": [ | ||
| { | ||
| "groupId": "edu.wpi.first.wpilibNewCommands", | ||
| "artifactId": "wpilibNewCommands-cpp", | ||
| "version": "wpilib", | ||
| "libName": "wpilibNewCommands", | ||
| "headerClassifier": "headers", | ||
| "sourcesClassifier": "sources", | ||
| "sharedLibrary": true, | ||
| "skipInvalidPlatforms": true, | ||
| "binaryPlatforms": [ | ||
| "linuxathena", | ||
| "linuxarm32", | ||
| "linuxarm64", | ||
| "windowsx86-64", | ||
| "windowsx86", | ||
| "linuxx86-64", | ||
| "osxuniversal" | ||
| ] | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.