Split the two command implementations into separate libraries (#2012)

This will allow us at the user code side to determine to include old commands, new commands or both.
This commit is contained in:
Thad House
2019-11-01 21:58:54 -07:00
committed by Peter Johnson
parent 2ad15cae19
commit 509819d83f
271 changed files with 470 additions and 91 deletions

View File

@@ -0,0 +1,40 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.sim.DriverStationSim;
public final class MockHardwareExtension implements BeforeAllCallback {
private static ExtensionContext getRoot(ExtensionContext context) {
return context.getParent().map(MockHardwareExtension::getRoot).orElse(context);
}
@Override
public void beforeAll(ExtensionContext context) {
getRoot(context).getStore(Namespace.GLOBAL).getOrComputeIfAbsent("HAL Initalized", key -> {
initializeHardware();
return true;
}, Boolean.class);
}
private void initializeHardware() {
HAL.initialize(500, 0);
DriverStationSim dsSim = new DriverStationSim();
dsSim.setDsAttached(true);
dsSim.setAutonomous(false);
dsSim.setEnabled(true);
dsSim.setTest(true);
}
}

View File

@@ -0,0 +1,56 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* The basic test for all {@link Command} tests.
*/
public abstract class AbstractCommandTest {
@BeforeEach
void commandSetup() {
Scheduler.getInstance().removeAll();
Scheduler.getInstance().enable();
}
public class ASubsystem extends Subsystem {
Command m_command;
@Override
protected void initDefaultCommand() {
if (m_command != null) {
setDefaultCommand(m_command);
}
}
public void init(Command command) {
m_command = command;
}
}
protected void assertCommandState(MockCommand command, int initialize, int execute,
int isFinished, int end, int interrupted) {
assertAll(
() -> assertEquals(initialize, command.getInitializeCount()),
() -> assertEquals(execute, command.getExecuteCount()),
() -> assertEquals(isFinished, command.getIsFinishedCount()),
() -> assertEquals(end, command.getEndCount()),
() -> assertEquals(interrupted, command.getInterruptedCount())
);
}
protected void sleep(int time) {
assertDoesNotThrow(() -> Thread.sleep(time));
}
}

View File

@@ -0,0 +1,116 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import edu.wpi.first.wpilibj.buttons.InternalButton;
/**
* Test that covers the {@link edu.wpi.first.wpilibj.buttons.Button} with the {@link Command}
* library.
*/
class ButtonTest extends AbstractCommandTest {
private InternalButton m_button1;
private InternalButton m_button2;
@BeforeEach
void setUp() {
m_button1 = new InternalButton();
m_button2 = new InternalButton();
}
/**
* Simple Button Test.
*/
@Test
void buttonTest() {
final MockCommand command1 = new MockCommand();
final MockCommand command2 = new MockCommand();
final MockCommand command3 = new MockCommand();
final MockCommand command4 = new MockCommand();
m_button1.whenPressed(command1);
m_button1.whenReleased(command2);
m_button1.whileHeld(command3);
m_button2.whileHeld(command4);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
m_button1.setPressed(true);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 1, 1, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 2, 2, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
m_button2.setPressed(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 3, 3, 0, 0);
assertCommandState(command4, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 0);
assertCommandState(command4, 1, 1, 1, 0, 0);
m_button1.setPressed(false);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 5, 5, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 6, 6, 0, 0);
assertCommandState(command2, 1, 1, 1, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 0);
m_button2.setPressed(false);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 0);
assertCommandState(command2, 1, 2, 2, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
command1.cancel();
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 3, 3, 0, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
command2.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 4, 4, 1, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 7, 7, 0, 1);
assertCommandState(command2, 1, 4, 4, 1, 0);
assertCommandState(command3, 1, 4, 4, 0, 1);
assertCommandState(command4, 1, 3, 3, 0, 1);
}
}

View File

@@ -0,0 +1,55 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.Test;
/**
* Ported from the old CrioTest Classes.
*/
class CommandParallelGroupTest extends AbstractCommandTest {
/**
* Simple Parallel Command Group With 2 commands one command terminates first.
*/
@Test
void parallelCommandGroupWithTwoCommandsTest() {
final MockCommand command1 = new MockCommand();
final MockCommand command2 = new MockCommand();
final CommandGroup commandGroup = new CommandGroup();
commandGroup.addParallel(command1);
commandGroup.addParallel(command2);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
commandGroup.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 1, 2, 2, 0, 0);
command1.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 3, 3, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 4, 4, 0, 0);
command2.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 1, 0);
assertCommandState(command2, 1, 5, 5, 1, 0);
}
}

View File

@@ -0,0 +1,61 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.Test;
/**
* Ported from the old CrioTest Classes.
*/
class CommandScheduleTest extends AbstractCommandTest {
/**
* Simple scheduling of a command and making sure the command is run and successfully terminates.
*/
@Test
void runAndTerminateTest() {
final MockCommand command = new MockCommand();
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
command.setHasFinished(true);
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 1, 0);
}
/**
* Simple scheduling of a command and making sure the command is run and cancels correctly.
*/
@Test
void runAndCancelTest() {
final MockCommand command = new MockCommand();
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 0);
command.cancel();
assertCommandState(command, 1, 3, 3, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 1);
}
}

View File

@@ -0,0 +1,93 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
/**
* Ported from the old CrioTest Classes.
*/
class CommandSequentialGroupTest extends AbstractCommandTest {
private static final Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName());
/**
* Simple Command Group With 3 commands that all depend on a subsystem. Some commands have a
* timeout.
*/
@Test
public void testThreeCommandOnSubSystem() {
logger.fine("Begining Test");
final ASubsystem subsystem = new ASubsystem();
logger.finest("Creating Mock Command1");
final MockCommand command1 = new MockCommand(subsystem);
logger.finest("Creating Mock Command2");
final MockCommand command2 = new MockCommand(subsystem);
logger.finest("Creating Mock Command3");
final MockCommand command3 = new MockCommand(subsystem);
logger.finest("Creating Command Group");
final CommandGroup commandGroup = new CommandGroup();
commandGroup.addSequential(command1, 1.0);
commandGroup.addSequential(command2, 2.0);
commandGroup.addSequential(command3);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
logger.finest("Starting Command group");
commandGroup.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
sleep(1250); // command 1 timeout
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 1, 1, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 0);
assertCommandState(command3, 0, 0, 0, 0, 0);
sleep(2500); // command 2 timeout
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 2, 2, 0, 0);
command3.setHasFinished(true);
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 1);
assertCommandState(command3, 1, 3, 3, 1, 0);
}
}

View File

@@ -0,0 +1,102 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.Test;
/**
* Ported from the old CrioTest Classes.
*/
class CommandSupersedeTest extends AbstractCommandTest {
/**
* Testing one command superseding another because of dependencies.
*/
@Test
void oneCommandSupersedingAnotherBecauseOfDependenciesTest() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command1 = new MockCommand(subsystem);
final MockCommand command2 = new MockCommand(subsystem);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command1.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command2.start();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 1);
assertCommandState(command2, 1, 3, 3, 0, 0);
}
/**
* Testing one command failing superseding another because of dependencies because the first
* command cannot be interrupted.
*/
@Test
@SuppressWarnings("PMD.NonStaticInitializer")
void commandFailingSupersedingBecauseFirstCanNotBeInterruptedTest() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command1 = new MockCommand(subsystem) {
{
setInterruptible(false);
}
};
final MockCommand command2 = new MockCommand(subsystem);
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command1.start();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 0, 0, 0, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 1, 1, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 2, 2, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
command2.start();
assertCommandState(command1, 1, 3, 3, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command1, 1, 4, 4, 0, 0);
assertCommandState(command2, 0, 0, 0, 0, 0);
}
}

View File

@@ -0,0 +1,47 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.Test;
/**
* Test a {@link Command} that times out.
*/
class CommandTimeoutTest extends AbstractCommandTest {
/**
* Command 2 second Timeout Test.
*/
@Test
void twoSecondTimeoutTest() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand command = new MockCommand(subsystem, 2) {
@Override
public boolean isFinished() {
return super.isFinished() || isTimedOut();
}
};
command.start();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 3, 3, 0, 0);
sleep(2500);
Scheduler.getInstance().run();
assertCommandState(command, 1, 4, 4, 1, 0);
Scheduler.getInstance().run();
assertCommandState(command, 1, 4, 4, 1, 0);
}
}

View File

@@ -0,0 +1,345 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ConditionalCommandTest extends AbstractCommandTest {
MockConditionalCommand m_command;
MockConditionalCommand m_commandNull;
MockCommand m_onTrue;
MockCommand m_onFalse;
MockSubsystem m_subsys;
Boolean m_condition;
@BeforeEach
void initCommands() {
m_subsys = new MockSubsystem();
m_onTrue = new MockCommand(m_subsys);
m_onFalse = new MockCommand(m_subsys);
m_command = new MockConditionalCommand(m_onTrue, m_onFalse);
m_commandNull = new MockConditionalCommand(m_onTrue, null);
}
protected void assertConditionalCommandState(MockConditionalCommand command, int initialize,
int execute, int isFinished, int end,
int interrupted) {
assertEquals(initialize, command.getInitializeCount());
assertEquals(execute, command.getExecuteCount());
assertEquals(isFinished, command.getIsFinishedCount());
assertEquals(end, command.getEndCount());
assertEquals(interrupted, command.getInterruptedCount());
}
@Test
void onTrueTest() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
assertTrue(m_onTrue.getInitializeCount() > 0, "Did not initialize the true command");
assertSame(m_onFalse.getInitializeCount(), 0, "Initialized the false command");
}
@Test
void onFalseTest() {
m_command.setCondition(false);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onFalse
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onFalse
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 1, 1, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 2, 2, 0, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onFalse.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 3, 3, 1, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onFalse, 1, 3, 3, 1, 0);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
assertTrue(m_onFalse.getInitializeCount() > 0, "Did not initialize the false command");
assertSame(m_onTrue.getInitializeCount(), 0, "Initialized the true command");
}
@Test
void cancelSubCommandTest() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.cancel();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
}
@Test
void cancelRequiresTest() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onFalse.start();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 1);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 0, 1);
assertCommandState(m_onFalse, 1, 1, 1, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 1);
}
@Test
void cancelCondCommandTest() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_command.cancel();
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 1);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 1);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 1);
}
@Test
void onTrueTwiceTest() {
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
m_onTrue.resetCounters();
m_command.resetCounters();
m_command.setCondition(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 2, 2, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 0, 0);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 4, 4, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 3, 3, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 5, 5, 1, 0);
}
@Test
void onTrueInstantTest() {
m_command.setCondition(true);
m_onTrue.setHasFinished(true);
Scheduler.getInstance().add(m_command);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onTrue
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 1, 1, 1, 1, 0);
assertCommandState(m_onFalse, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_command, 1, 3, 3, 1, 0);
}
@Test
void onFalseNullTest() {
m_commandNull.setCondition(false);
Scheduler.getInstance().add(m_commandNull);
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init command and select m_onFalse
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 0, 0, 0, 0, 0);
Scheduler.getInstance().run(); // init m_onFalse
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 1, 1, 1, 1, 0);
Scheduler.getInstance().run();
assertCommandState(m_onTrue, 0, 0, 0, 0, 0);
assertConditionalCommandState(m_commandNull, 1, 1, 1, 1, 0);
}
}

View File

@@ -0,0 +1,111 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.Test;
/**
* Tests the {@link Command} library.
*/
public class DefaultCommandTest extends AbstractCommandTest {
/**
* Testing of default commands where the interrupting command ends itself.
*/
@Test
void defaultCommandWhereTheInteruptingCommandEndsItselfTest() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand defaultCommand = new MockCommand(subsystem);
final MockCommand anotherCommand = new MockCommand(subsystem);
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
subsystem.init(defaultCommand);
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
anotherCommand.start();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
anotherCommand.setHasFinished(true);
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 4, 4, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 5, 5, 0, 1);
assertCommandState(anotherCommand, 1, 3, 3, 1, 0);
}
/**
* Testing of default commands where the interrupting command is canceled.
*/
@Test
void defaultCommandsInterruptingCommandCanceledTest() {
final ASubsystem subsystem = new ASubsystem();
final MockCommand defaultCommand = new MockCommand(subsystem);
final MockCommand anotherCommand = new MockCommand(subsystem);
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
subsystem.init(defaultCommand);
subsystem.initDefaultCommand();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
anotherCommand.start();
assertCommandState(defaultCommand, 1, 2, 2, 0, 0);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 0, 0, 0, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 1, 1, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
anotherCommand.cancel();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 0);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 1, 3, 3, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 4, 4, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
Scheduler.getInstance().run();
assertCommandState(defaultCommand, 2, 5, 5, 0, 1);
assertCommandState(anotherCommand, 1, 2, 2, 0, 1);
}
}

View File

@@ -0,0 +1,149 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
/**
* A class to simulate a simple command. The command keeps track of how many times each method was
* called.
*/
public class MockCommand extends Command {
private int m_initializeCount;
private int m_executeCount;
private int m_isFinishedCount;
private boolean m_hasFinished;
private int m_endCount;
private int m_interruptedCount;
public MockCommand(Subsystem subsys) {
super();
requires(subsys);
}
public MockCommand(Subsystem subsys, double timeout) {
this(subsys);
setTimeout(timeout);
}
public MockCommand() {
super();
}
@Override
protected void initialize() {
++m_initializeCount;
}
@Override
protected void execute() {
++m_executeCount;
}
@Override
protected boolean isFinished() {
++m_isFinishedCount;
return isHasFinished();
}
@Override
protected void end() {
++m_endCount;
}
@Override
protected void interrupted() {
++m_interruptedCount;
}
/**
* How many times the initialize method has been called.
*/
public int getInitializeCount() {
return m_initializeCount;
}
/**
* If the initialize method has been called at least once.
*/
public boolean hasInitialized() {
return getInitializeCount() > 0;
}
/**
* How many time the execute method has been called.
*/
public int getExecuteCount() {
return m_executeCount;
}
/**
* How many times the isFinished method has been called.
*/
public int getIsFinishedCount() {
return m_isFinishedCount;
}
/**
* Get what value the isFinished method will return.
*
* @return what value the isFinished method will return.
*/
public boolean isHasFinished() {
return m_hasFinished;
}
/**
* Set what value the isFinished method will return.
*
* @param hasFinished set what value the isFinished method will return.
*/
public void setHasFinished(boolean hasFinished) {
m_hasFinished = hasFinished;
}
/**
* How many times the end method has been called.
*/
public int getEndCount() {
return m_endCount;
}
/**
* If the end method has been called at least once.
*/
public boolean hasEnd() {
return getEndCount() > 0;
}
/**
* How many times the interrupted method has been called.
*/
public int getInterruptedCount() {
return m_interruptedCount;
}
/**
* If the interrupted method has been called at least once.
*/
public boolean hasInterrupted() {
return getInterruptedCount() > 0;
}
/**
* Reset internal counters.
*/
public void resetCounters() {
m_initializeCount = 0;
m_executeCount = 0;
m_isFinishedCount = 0;
m_hasFinished = false;
m_endCount = 0;
m_interruptedCount = 0;
}
}

View File

@@ -0,0 +1,125 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
public class MockConditionalCommand extends ConditionalCommand {
private boolean m_condition;
private int m_initializeCount;
private int m_executeCount;
private int m_isFinishedCount;
private int m_endCount;
private int m_interruptedCount;
public MockConditionalCommand(MockCommand onTrue, MockCommand onFalse) {
super(onTrue, onFalse);
}
@Override
protected boolean condition() {
return m_condition;
}
public void setCondition(boolean condition) {
this.m_condition = condition;
}
@Override
protected void initialize() {
++m_initializeCount;
}
@Override
protected void execute() {
++m_executeCount;
}
@Override
protected boolean isFinished() {
++m_isFinishedCount;
return super.isFinished();
}
@Override
protected void end() {
++m_endCount;
}
@Override
protected void interrupted() {
++m_interruptedCount;
}
/**
* How many times the initialize method has been called.
*/
public int getInitializeCount() {
return m_initializeCount;
}
/**
* If the initialize method has been called at least once.
*/
public boolean hasInitialized() {
return getInitializeCount() > 0;
}
/**
* How many time the execute method has been called.
*/
public int getExecuteCount() {
return m_executeCount;
}
/**
* How many times the isFinished method has been called.
*/
public int getIsFinishedCount() {
return m_isFinishedCount;
}
/**
* How many times the end method has been called.
*/
public int getEndCount() {
return m_endCount;
}
/**
* If the end method has been called at least once.
*/
public boolean hasEnd() {
return getEndCount() > 0;
}
/**
* How many times the interrupted method has been called.
*/
public int getInterruptedCount() {
return m_interruptedCount;
}
/**
* If the interrupted method has been called at least once.
*/
public boolean hasInterrupted() {
return getInterruptedCount() > 0;
}
/**
* Reset internal counters.
*/
public void resetCounters() {
m_condition = false;
m_initializeCount = 0;
m_executeCount = 0;
m_isFinishedCount = 0;
m_endCount = 0;
m_interruptedCount = 0;
}
}

View File

@@ -0,0 +1,16 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.command;
/**
* A class to simulate a simple subsystem.
*/
public class MockSubsystem extends Subsystem {
@Override
protected void initDefaultCommand() {}
}

View File

@@ -0,0 +1,152 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.shuffleboard;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.Sendable;
import edu.wpi.first.wpilibj.command.InstantCommand;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SuppressWarnings({"PMD.TooManyMethods"})
public class ShuffleboardTabTest {
private NetworkTableInstance m_ntInstance;
private ShuffleboardTab m_tab;
private ShuffleboardInstance m_instance;
@BeforeEach
void setup() {
m_ntInstance = NetworkTableInstance.create();
m_instance = new ShuffleboardInstance(m_ntInstance);
m_tab = m_instance.getTab("Tab");
}
@AfterEach
void tearDown() {
m_ntInstance.close();
}
@Test
void testAddDouble() {
NetworkTableEntry entry = m_tab.add("Double", 1.0).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/Double", entry.getName()),
() -> assertEquals(1.0, entry.getValue().getDouble()));
}
@Test
void testAddInteger() {
NetworkTableEntry entry = m_tab.add("Int", 1).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/Int", entry.getName()),
() -> assertEquals(1.0, entry.getValue().getDouble()));
}
@Test
void testAddLong() {
NetworkTableEntry entry = m_tab.add("Long", 1L).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/Long", entry.getName()),
() -> assertEquals(1.0, entry.getValue().getDouble()));
}
@Test
void testAddBoolean() {
NetworkTableEntry entry = m_tab.add("Bool", false).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/Bool", entry.getName()),
() -> assertFalse(entry.getValue().getBoolean()));
}
@Test
void testAddString() {
NetworkTableEntry entry = m_tab.add("String", "foobar").getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/String", entry.getName()),
() -> assertEquals("foobar", entry.getValue().getString()));
}
@Test
void testAddNamedSendableWithProperties() {
Sendable sendable = new InstantCommand("Command");
String widgetType = "Command Widget";
m_tab.add(sendable)
.withWidget(widgetType)
.withProperties(mapOf("foo", 1234, "bar", "baz"));
m_instance.update();
String meta = "/Shuffleboard/.metadata/Tab/Command";
assertAll(
() -> assertEquals(1234,
m_ntInstance.getEntry(meta + "/Properties/foo").getDouble(-1),
"Property 'foo' not set correctly"),
() -> assertEquals("baz",
m_ntInstance.getEntry(meta + "/Properties/bar").getString(null),
"Property 'bar' not set correctly"),
() -> assertEquals(widgetType,
m_ntInstance.getEntry(meta + "/PreferredComponent").getString(null),
"Preferred component not set correctly"));
}
@Test
void testAddNumberArray() {
NetworkTableEntry entry = m_tab.add("DoubleArray", new double[]{1, 2, 3}).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/DoubleArray", entry.getName()),
() -> assertArrayEquals(new double[]{1, 2, 3}, entry.getValue().getDoubleArray()));
}
@Test
void testAddBooleanArray() {
NetworkTableEntry entry = m_tab.add("BoolArray", new boolean[]{true, false}).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/BoolArray", entry.getName()),
() -> assertArrayEquals(new boolean[]{true, false}, entry.getValue().getBooleanArray()));
}
@Test
void testAddStringArray() {
NetworkTableEntry entry = m_tab.add("StringArray", new String[]{"foo", "bar"}).getEntry();
assertAll(
() -> assertEquals("/Shuffleboard/Tab/StringArray", entry.getName()),
() -> assertArrayEquals(new String[]{"foo", "bar"}, entry.getValue().getStringArray()));
}
@Test
void testTitleDuplicates() {
m_tab.add("foo", "bar");
assertThrows(IllegalArgumentException.class, () -> m_tab.add("foo", "baz"));
}
/**
* Stub for Java 9 {@code Map.of()}.
*/
@SuppressWarnings({"unchecked", "PMD"})
private static <K, V> Map<K, V> mapOf(Object... entries) {
Map<K, V> map = new HashMap<>();
for (int i = 0; i < entries.length; i += 2) {
map.put((K) entries[i], (V) entries[i + 1]);
}
return map;
}
}