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

@@ -1,56 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,116 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,55 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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

@@ -1,61 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,93 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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

@@ -1,102 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,47 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,345 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 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

@@ -1,111 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 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

@@ -1,149 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2018 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

@@ -1,125 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 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

@@ -1,16 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 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

@@ -1,152 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 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;
}
}

View File

@@ -1,179 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import edu.wpi.first.wpilibj2.command.button.InternalButton;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class ButtonTest extends CommandTestBase {
@Test
void whenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whenPressed(command1);
scheduler.run();
verify(command1, never()).schedule(true);
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).schedule(true);
}
@Test
void whenReleasedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(true);
button.whenReleased(command1);
scheduler.run();
verify(command1, never()).schedule(true);
button.setPressed(false);
scheduler.run();
scheduler.run();
verify(command1).schedule(true);
}
@Test
void whileHeldTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whileHeld(command1);
scheduler.run();
verify(command1, never()).schedule(true);
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1, times(2)).schedule(true);
button.setPressed(false);
scheduler.run();
verify(command1).cancel();
}
@Test
void whenHeldTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.whenHeld(command1);
scheduler.run();
verify(command1, never()).schedule(true);
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).schedule(true);
button.setPressed(false);
scheduler.run();
verify(command1).cancel();
}
@Test
void toggleWhenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.toggleWhenPressed(command1);
scheduler.run();
verify(command1, never()).schedule(true);
button.setPressed(true);
scheduler.run();
when(command1.isScheduled()).thenReturn(true);
scheduler.run();
verify(command1).schedule(true);
button.setPressed(false);
scheduler.run();
verify(command1, never()).cancel();
button.setPressed(true);
scheduler.run();
verify(command1).cancel();
}
@Test
void cancelWhenPressedTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
InternalButton button = new InternalButton();
button.setPressed(false);
button.cancelWhenPressed(command1);
scheduler.run();
verify(command1, never()).cancel();
button.setPressed(true);
scheduler.run();
scheduler.run();
verify(command1).cancel();
}
@Test
void runnableBindingTest() {
InternalButton buttonWhenPressed = new InternalButton();
InternalButton buttonWhileHeld = new InternalButton();
InternalButton buttonWhenReleased = new InternalButton();
buttonWhenPressed.setPressed(false);
buttonWhileHeld.setPressed(true);
buttonWhenReleased.setPressed(true);
Counter counter = new Counter();
buttonWhenPressed.whenPressed(counter::increment);
buttonWhileHeld.whileHeld(counter::increment);
buttonWhenReleased.whenReleased(counter::increment);
CommandScheduler scheduler = CommandScheduler.getInstance();
scheduler.run();
buttonWhenPressed.setPressed(true);
buttonWhenReleased.setPressed(false);
scheduler.run();
assertEquals(counter.m_counter, 4);
}
@Test
void buttonCompositionTest() {
InternalButton button1 = new InternalButton();
InternalButton button2 = new InternalButton();
button1.setPressed(true);
button2.setPressed(false);
assertFalse(button1.and(button2).get());
assertTrue(button1.or(button2).get());
assertFalse(button1.negate().get());
assertTrue(button1.and(button2.negate()).get());
}
}

View File

@@ -1,182 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import edu.wpi.first.wpilibj.Timer;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CommandDecoratorTest extends CommandTestBase {
@Test
void withTimeoutTest() {
CommandScheduler scheduler = new CommandScheduler();
Command command1 = new WaitCommand(10);
Command timeout = command1.withTimeout(2);
scheduler.schedule(timeout);
scheduler.run();
assertFalse(scheduler.isScheduled(command1));
assertTrue(scheduler.isScheduled(timeout));
Timer.delay(3);
scheduler.run();
assertFalse(scheduler.isScheduled(timeout));
}
@Test
void withInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
Command command = new WaitCommand(10).withInterrupt(condition::getCondition);
scheduler.schedule(command);
scheduler.run();
assertTrue(scheduler.isScheduled(command));
condition.setCondition(true);
scheduler.run();
assertFalse(scheduler.isScheduled(command));
}
@Test
void beforeStartingTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
condition.setCondition(false);
Command command = new InstantCommand();
scheduler.schedule(command.beforeStarting(() -> condition.setCondition(true)));
assertTrue(condition.getCondition());
}
@Test
void andThenLambdaTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
condition.setCondition(false);
Command command = new InstantCommand();
scheduler.schedule(command.andThen(() -> condition.setCondition(true)));
assertFalse(condition.getCondition());
scheduler.run();
assertTrue(condition.getCondition());
}
@Test
void andThenTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
condition.setCondition(false);
Command command1 = new InstantCommand();
Command command2 = new InstantCommand(() -> condition.setCondition(true));
scheduler.schedule(command1.andThen(command2));
assertFalse(condition.getCondition());
scheduler.run();
assertTrue(condition.getCondition());
}
@Test
void deadlineWithTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
condition.setCondition(false);
Command dictator = new WaitUntilCommand(condition::getCondition);
Command endsBefore = new InstantCommand();
Command endsAfter = new WaitUntilCommand(() -> false);
Command group = dictator.deadlineWith(endsBefore, endsAfter);
scheduler.schedule(group);
scheduler.run();
assertTrue(scheduler.isScheduled(group));
condition.setCondition(true);
scheduler.run();
assertFalse(scheduler.isScheduled(group));
}
@Test
void alongWithTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
condition.setCondition(false);
Command command1 = new WaitUntilCommand(condition::getCondition);
Command command2 = new InstantCommand();
Command group = command1.alongWith(command2);
scheduler.schedule(group);
scheduler.run();
assertTrue(scheduler.isScheduled(group));
condition.setCondition(true);
scheduler.run();
assertFalse(scheduler.isScheduled(group));
}
@Test
void raceWithTest() {
CommandScheduler scheduler = new CommandScheduler();
Command command1 = new WaitUntilCommand(() -> false);
Command command2 = new InstantCommand();
Command group = command1.raceWith(command2);
scheduler.schedule(group);
scheduler.run();
assertFalse(scheduler.isScheduled(group));
}
@Test
void perpetuallyTest() {
CommandScheduler scheduler = new CommandScheduler();
Command command = new InstantCommand();
Command perpetual = command.perpetually();
scheduler.schedule(perpetual);
scheduler.run();
scheduler.run();
scheduler.run();
assertTrue(scheduler.isScheduled(perpetual));
}
}

View File

@@ -1,55 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class CommandGroupErrorTest extends CommandTestBase {
@Test
void commandInMultipleGroupsTest() {
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
@SuppressWarnings("PMD.UnusedLocalVariable")
Command group = new ParallelCommandGroup(command1, command2);
assertThrows(IllegalArgumentException.class,
() -> new ParallelCommandGroup(command1, command2));
}
@Test
void commandInGroupExternallyScheduledTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
@SuppressWarnings("PMD.UnusedLocalVariable")
Command group = new ParallelCommandGroup(command1, command2);
assertThrows(IllegalArgumentException.class,
() -> scheduler.schedule(command1));
}
@Test
void redecoratedCommandErrorTest() {
Command command = new InstantCommand();
assertDoesNotThrow(() -> command.withTimeout(10).withInterrupt(() -> false));
assertThrows(IllegalArgumentException.class, () -> command.withTimeout(10));
CommandGroupBase.clearGroupedCommand(command);
assertDoesNotThrow(() -> command.withTimeout(10));
}
}

View File

@@ -1,79 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
@SuppressWarnings("PMD.TooManyMethods")
class CommandRequirementsTest extends CommandTestBase {
@Test
void requirementInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
Subsystem requirement = new TestSubsystem();
MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement);
Command interrupted = interruptedHolder.getMock();
MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement);
Command interrupter = interrupterHolder.getMock();
scheduler.schedule(interrupted);
scheduler.run();
scheduler.schedule(interrupter);
scheduler.run();
verify(interrupted).initialize();
verify(interrupted).execute();
verify(interrupted).end(true);
verify(interrupter).initialize();
verify(interrupter).execute();
assertFalse(scheduler.isScheduled(interrupted));
assertTrue(scheduler.isScheduled(interrupter));
}
@Test
void requirementUninterruptibleTest() {
CommandScheduler scheduler = new CommandScheduler();
Subsystem requirement = new TestSubsystem();
MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement);
Command notInterrupted = interruptedHolder.getMock();
MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement);
Command interrupter = interrupterHolder.getMock();
scheduler.schedule(false, notInterrupted);
scheduler.schedule(interrupter);
assertTrue(scheduler.isScheduled(notInterrupted));
assertFalse(scheduler.isScheduled(interrupter));
}
@Test
void defaultCommandRequirementErrorTest() {
CommandScheduler scheduler = new CommandScheduler();
Subsystem system = new TestSubsystem();
Command missingRequirement = new WaitUntilCommand(() -> false);
Command ends = new InstantCommand(() -> {
}, system);
assertThrows(IllegalArgumentException.class,
() -> scheduler.setDefaultCommand(system, missingRequirement));
assertThrows(IllegalArgumentException.class,
() -> scheduler.setDefaultCommand(system, ends));
}
}

View File

@@ -1,122 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class CommandScheduleTest extends CommandTestBase {
@Test
void instantScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(true);
holder.setFinished(true);
Command mockCommand = holder.getMock();
scheduler.schedule(mockCommand);
assertTrue(scheduler.isScheduled(mockCommand));
verify(mockCommand).initialize();
scheduler.run();
verify(mockCommand).execute();
verify(mockCommand).end(false);
assertFalse(scheduler.isScheduled(mockCommand));
}
@Test
void singleIterationScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(true);
Command mockCommand = holder.getMock();
scheduler.schedule(mockCommand);
assertTrue(scheduler.isScheduled(mockCommand));
scheduler.run();
holder.setFinished(true);
scheduler.run();
verify(mockCommand).initialize();
verify(mockCommand, times(2)).execute();
verify(mockCommand).end(false);
assertFalse(scheduler.isScheduled(mockCommand));
}
@Test
void multiScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
scheduler.schedule(true, command1, command2, command3);
assertTrue(scheduler.isScheduled(command1, command2, command3));
scheduler.run();
assertTrue(scheduler.isScheduled(command1, command2, command3));
command1Holder.setFinished(true);
scheduler.run();
assertTrue(scheduler.isScheduled(command2, command3));
assertFalse(scheduler.isScheduled(command1));
command2Holder.setFinished(true);
scheduler.run();
assertTrue(scheduler.isScheduled(command3));
assertFalse(scheduler.isScheduled(command1, command2));
command3Holder.setFinished(true);
scheduler.run();
assertFalse(scheduler.isScheduled(command1, command2, command3));
}
@Test
void schedulerCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(true);
Command mockCommand = holder.getMock();
scheduler.schedule(mockCommand);
scheduler.run();
scheduler.cancel(mockCommand);
scheduler.run();
verify(mockCommand).execute();
verify(mockCommand).end(true);
verify(mockCommand, never()).end(false);
assertFalse(scheduler.isScheduled(mockCommand));
}
@Test
void notScheduledCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(true);
Command mockCommand = holder.getMock();
assertDoesNotThrow(() -> scheduler.cancel(mockCommand));
}
}

View File

@@ -1,92 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import edu.wpi.first.hal.sim.DriverStationSim;
import edu.wpi.first.wpilibj.DriverStation;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Basic setup for all {@link Command tests}."
*/
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
abstract class CommandTestBase {
@BeforeEach
void commandSetup() {
CommandScheduler.getInstance().cancelAll();
CommandScheduler.getInstance().enable();
CommandScheduler.getInstance().clearButtons();
CommandGroupBase.clearGroupedCommands();
setDSEnabled(true);
}
void setDSEnabled(boolean enabled) {
DriverStationSim sim = new DriverStationSim();
sim.setDsAttached(true);
sim.setEnabled(enabled);
sim.notifyNewData();
DriverStation.getInstance().isNewControlData();
while (DriverStation.getInstance().isEnabled() != enabled) {
try {
Thread.sleep(1);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
class TestSubsystem extends SubsystemBase {
}
protected class MockCommandHolder {
private final Command m_mockCommand = mock(Command.class);
MockCommandHolder(boolean runWhenDisabled, Subsystem... requirements) {
when(m_mockCommand.getRequirements()).thenReturn(Set.of(requirements));
when(m_mockCommand.isFinished()).thenReturn(false);
when(m_mockCommand.runsWhenDisabled()).thenReturn(runWhenDisabled);
}
Command getMock() {
return m_mockCommand;
}
void setFinished(boolean finished) {
when(m_mockCommand.isFinished()).thenReturn(finished);
}
}
protected class Counter {
int m_counter;
void increment() {
m_counter++;
}
}
protected class ConditionHolder {
private boolean m_condition;
void setCondition(boolean condition) {
m_condition = condition;
}
boolean getCondition() {
return m_condition;
}
}
}

View File

@@ -1,65 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
class ConditionalCommandTest extends CommandTestBase {
@Test
void conditionalCommandTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
command1Holder.setFinished(true);
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
ConditionalCommand conditionalCommand = new ConditionalCommand(command1, command2, () -> true);
scheduler.schedule(conditionalCommand);
scheduler.run();
verify(command1).initialize();
verify(command1).execute();
verify(command1).end(false);
verify(command2, never()).initialize();
verify(command2, never()).execute();
verify(command2, never()).end(false);
}
@Test
void conditionalCommandRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
ConditionalCommand conditionalCommand = new ConditionalCommand(command1, command2, () -> true);
scheduler.schedule(conditionalCommand);
scheduler.schedule(new InstantCommand(() -> {
}, system3));
assertFalse(scheduler.isScheduled(conditionalCommand));
verify(command1).end(true);
verify(command2, never()).end(true);
}
}

View File

@@ -1,83 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
class DefaultCommandTest extends CommandTestBase {
@Test
void defaultCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
TestSubsystem hasDefaultCommand = new TestSubsystem();
MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand);
Command defaultCommand = defaultHolder.getMock();
scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand);
scheduler.run();
assertTrue(scheduler.isScheduled(defaultCommand));
}
@Test
void defaultCommandInterruptResumeTest() {
CommandScheduler scheduler = new CommandScheduler();
TestSubsystem hasDefaultCommand = new TestSubsystem();
MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand);
Command defaultCommand = defaultHolder.getMock();
MockCommandHolder interrupterHolder = new MockCommandHolder(true, hasDefaultCommand);
Command interrupter = interrupterHolder.getMock();
scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand);
scheduler.run();
scheduler.schedule(interrupter);
assertFalse(scheduler.isScheduled(defaultCommand));
assertTrue(scheduler.isScheduled(interrupter));
scheduler.cancel(interrupter);
scheduler.run();
assertTrue(scheduler.isScheduled(defaultCommand));
assertFalse(scheduler.isScheduled(interrupter));
}
@Test
void defaultCommandDisableResumeTest() {
CommandScheduler scheduler = new CommandScheduler();
TestSubsystem hasDefaultCommand = new TestSubsystem();
MockCommandHolder defaultHolder = new MockCommandHolder(false, hasDefaultCommand);
Command defaultCommand = defaultHolder.getMock();
scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand);
scheduler.run();
assertTrue(scheduler.isScheduled(defaultCommand));
setDSEnabled(false);
scheduler.run();
assertFalse(scheduler.isScheduled(defaultCommand));
setDSEnabled(true);
scheduler.run();
assertTrue(scheduler.isScheduled(defaultCommand));
verify(defaultCommand).end(true);
}
}

View File

@@ -1,43 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class FunctionalCommandTest extends CommandTestBase {
@Test
void functionalCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder cond1 = new ConditionHolder();
ConditionHolder cond2 = new ConditionHolder();
ConditionHolder cond3 = new ConditionHolder();
ConditionHolder cond4 = new ConditionHolder();
FunctionalCommand command =
new FunctionalCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true),
interrupted -> cond3.setCondition(true), cond4::getCondition);
scheduler.schedule(command);
scheduler.run();
assertTrue(scheduler.isScheduled(command));
cond4.setCondition(true);
scheduler.run();
assertFalse(scheduler.isScheduled(command));
assertTrue(cond1.getCondition());
assertTrue(cond2.getCondition());
assertTrue(cond3.getCondition());
}
}

View File

@@ -1,30 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class InstantCommandTest extends CommandTestBase {
@Test
void instantCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder cond = new ConditionHolder();
InstantCommand command = new InstantCommand(() -> cond.setCondition(true));
scheduler.schedule(command);
scheduler.run();
assertTrue(cond.getCondition());
assertFalse(scheduler.isScheduled(command));
}
}

View File

@@ -1,34 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import edu.wpi.first.wpilibj.Timer;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DisabledOnOs(OS.MAC)
class NotifierCommandTest extends CommandTestBase {
@Test
void notifierCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
Counter counter = new Counter();
NotifierCommand command = new NotifierCommand(counter::increment, .01);
scheduler.schedule(command);
Timer.delay(.25);
scheduler.cancel(command);
assertEquals(.25, 0.01 * counter.m_counter, .025);
}
}

View File

@@ -1,131 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class ParallelCommandGroupTest extends CommandTestBase {
@Test
void parallelGroupScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelCommandGroup(command1, command2);
scheduler.schedule(group);
verify(command1).initialize();
verify(command2).initialize();
command1Holder.setFinished(true);
scheduler.run();
command2Holder.setFinished(true);
scheduler.run();
verify(command1).execute();
verify(command1).end(false);
verify(command2, times(2)).execute();
verify(command2).end(false);
assertFalse(scheduler.isScheduled(group));
}
@Test
void parallelGroupInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelCommandGroup(command1, command2);
scheduler.schedule(group);
command1Holder.setFinished(true);
scheduler.run();
scheduler.run();
scheduler.cancel(group);
verify(command1).execute();
verify(command1).end(false);
verify(command1, never()).end(true);
verify(command2, times(2)).execute();
verify(command2, never()).end(false);
verify(command2).end(true);
assertFalse(scheduler.isScheduled(group));
}
@Test
void notScheduledCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelCommandGroup(command1, command2);
assertDoesNotThrow(() -> scheduler.cancel(group));
}
@Test
void parallelGroupRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
Subsystem system4 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();
Command group = new ParallelCommandGroup(command1, command2);
scheduler.schedule(group);
scheduler.schedule(command3);
assertFalse(scheduler.isScheduled(group));
assertTrue(scheduler.isScheduled(command3));
}
@Test
void parallelGroupRequirementErrorTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system2, system3);
Command command2 = command2Holder.getMock();
assertThrows(IllegalArgumentException.class,
() -> new ParallelCommandGroup(command1, command2));
}
}

View File

@@ -1,129 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.internal.verification.VerificationModeFactory.times;
class ParallelDeadlineGroupTest extends CommandTestBase {
@Test
void parallelDeadlineScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
command2Holder.setFinished(true);
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
Command group = new ParallelDeadlineGroup(command1, command2, command3);
scheduler.schedule(group);
scheduler.run();
assertTrue(scheduler.isScheduled(group));
command1Holder.setFinished(true);
scheduler.run();
assertFalse(scheduler.isScheduled(group));
verify(command2).initialize();
verify(command2).execute();
verify(command2).end(false);
verify(command2, never()).end(true);
verify(command1).initialize();
verify(command1, times(2)).execute();
verify(command1).end(false);
verify(command1, never()).end(true);
verify(command3).initialize();
verify(command3, times(2)).execute();
verify(command3, never()).end(false);
verify(command3).end(true);
}
@Test
void parallelDeadlineInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
command2Holder.setFinished(true);
Command group = new ParallelDeadlineGroup(command1, command2);
scheduler.schedule(group);
scheduler.run();
scheduler.run();
scheduler.cancel(group);
verify(command1, times(2)).execute();
verify(command1, never()).end(false);
verify(command1).end(true);
verify(command2).execute();
verify(command2).end(false);
verify(command2, never()).end(true);
assertFalse(scheduler.isScheduled(group));
}
@Test
void parallelDeadlineRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
Subsystem system4 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();
Command group = new ParallelDeadlineGroup(command1, command2);
scheduler.schedule(group);
scheduler.schedule(command3);
assertFalse(scheduler.isScheduled(group));
assertTrue(scheduler.isScheduled(command3));
}
@Test
void parallelDeadlineRequirementErrorTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system2, system3);
Command command2 = command2Holder.getMock();
assertThrows(IllegalArgumentException.class,
() -> new ParallelDeadlineGroup(command1, command2));
}
}

View File

@@ -1,163 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class ParallelRaceGroupTest extends CommandTestBase {
@Test
void parallelRaceScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelRaceGroup(command1, command2);
scheduler.schedule(group);
verify(command1).initialize();
verify(command2).initialize();
command1Holder.setFinished(true);
scheduler.run();
command2Holder.setFinished(true);
scheduler.run();
verify(command1).execute();
verify(command1).end(false);
verify(command2).execute();
verify(command2).end(true);
verify(command2, never()).end(false);
assertFalse(scheduler.isScheduled(group));
}
@Test
void parallelRaceInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelRaceGroup(command1, command2);
scheduler.schedule(group);
scheduler.run();
scheduler.run();
scheduler.cancel(group);
verify(command1, times(2)).execute();
verify(command1, never()).end(false);
verify(command1).end(true);
verify(command2, times(2)).execute();
verify(command2, never()).end(false);
verify(command2).end(true);
assertFalse(scheduler.isScheduled(group));
}
@Test
void notScheduledCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new ParallelRaceGroup(command1, command2);
assertDoesNotThrow(() -> scheduler.cancel(group));
}
@Test
void parallelRaceRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
Subsystem system4 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();
Command group = new ParallelRaceGroup(command1, command2);
scheduler.schedule(group);
scheduler.schedule(command3);
assertFalse(scheduler.isScheduled(group));
assertTrue(scheduler.isScheduled(command3));
}
@Test
void parallelRaceRequirementErrorTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system2, system3);
Command command2 = command2Holder.getMock();
assertThrows(IllegalArgumentException.class, () -> new ParallelRaceGroup(command1, command2));
}
@Test
void parallelRaceOnlyCallsEndOnceTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system2);
Command command2 = command2Holder.getMock();
MockCommandHolder perpetualCommandHolder = new MockCommandHolder(true);
Command command3 = new PerpetualCommand(perpetualCommandHolder.getMock());
Command group1 = new SequentialCommandGroup(command1, command2);
assertNotNull(group1);
assertNotNull(command3);
Command group2 = new ParallelRaceGroup(group1, command3);
CommandScheduler scheduler = new CommandScheduler();
scheduler.schedule(group2);
scheduler.run();
command1Holder.setFinished(true);
scheduler.run();
command2Holder.setFinished(true);
// at this point the sequential group should be done
assertDoesNotThrow(() -> scheduler.run());
}
}

View File

@@ -1,26 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PerpetualCommandTest extends CommandTestBase {
@Test
void perpetualCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
PerpetualCommand command = new PerpetualCommand(new InstantCommand());
scheduler.schedule(command);
scheduler.run();
assertTrue(scheduler.isScheduled(command));
}
}

View File

@@ -1,37 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
class PrintCommandTest extends CommandTestBase {
@Test
void printCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
final PrintStream originalOut = System.out;
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
PrintCommand command = new PrintCommand("Test!");
scheduler.schedule(command);
scheduler.run();
assertFalse(scheduler.isScheduled(command));
assertEquals(testOut.toString(), "Test!" + System.lineSeparator());
System.setOut(originalOut);
}
}

View File

@@ -1,51 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
class ProxyScheduleCommandTest extends CommandTestBase {
@Test
void proxyScheduleCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command1);
scheduler.schedule(scheduleCommand);
verify(command1).schedule();
}
@Test
void proxyScheduleCommandEndTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
ConditionHolder cond = new ConditionHolder();
WaitUntilCommand command = new WaitUntilCommand(cond::getCondition);
ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command);
scheduler.schedule(scheduleCommand);
scheduler.run();
assertTrue(scheduler.isScheduled(scheduleCommand));
cond.setCondition(true);
scheduler.run();
scheduler.run();
assertFalse(scheduler.isScheduled(scheduleCommand));
}
}

View File

@@ -1,183 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static edu.wpi.first.wpilibj2.command.CommandGroupBase.parallel;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class RobotDisabledCommandTest extends CommandTestBase {
@Test
void robotDisabledCommandCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(false);
Command mockCommand = holder.getMock();
scheduler.schedule(mockCommand);
assertTrue(scheduler.isScheduled(mockCommand));
setDSEnabled(false);
scheduler.run();
assertFalse(scheduler.isScheduled(mockCommand));
setDSEnabled(true);
}
@Test
void runWhenDisabledTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder holder = new MockCommandHolder(true);
Command mockCommand = holder.getMock();
scheduler.schedule(mockCommand);
assertTrue(scheduler.isScheduled(mockCommand));
setDSEnabled(false);
scheduler.run();
assertTrue(scheduler.isScheduled(mockCommand));
}
@Test
void sequentialGroupRunWhenDisabledTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();
Command runWhenDisabled = new SequentialCommandGroup(command1, command2);
Command dontRunWhenDisabled = new SequentialCommandGroup(command3, command4);
scheduler.schedule(runWhenDisabled);
scheduler.schedule(dontRunWhenDisabled);
setDSEnabled(false);
scheduler.run();
assertTrue(scheduler.isScheduled(runWhenDisabled));
assertFalse(scheduler.isScheduled(dontRunWhenDisabled));
}
@Test
void parallelGroupRunWhenDisabledTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();
Command runWhenDisabled = new ParallelCommandGroup(command1, command2);
Command dontRunWhenDisabled = new ParallelCommandGroup(command3, command4);
scheduler.schedule(runWhenDisabled);
scheduler.schedule(dontRunWhenDisabled);
setDSEnabled(false);
scheduler.run();
assertTrue(scheduler.isScheduled(runWhenDisabled));
assertFalse(scheduler.isScheduled(dontRunWhenDisabled));
}
@Test
void conditionalRunWhenDisabledTest() {
setDSEnabled(false);
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();
CommandScheduler scheduler = new CommandScheduler();
Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true);
Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true);
scheduler.schedule(runWhenDisabled, dontRunWhenDisabled);
assertTrue(scheduler.isScheduled(runWhenDisabled));
assertFalse(scheduler.isScheduled(dontRunWhenDisabled));
}
@Test
void selectRunWhenDisabledTest() {
setDSEnabled(false);
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();
Command runWhenDisabled = new SelectCommand(Map.of(1, command1, 2, command2), () -> 1);
Command dontRunWhenDisabled = new SelectCommand(Map.of(1, command3, 2, command4), () -> 1);
CommandScheduler scheduler = new CommandScheduler();
scheduler.schedule(runWhenDisabled, dontRunWhenDisabled);
assertTrue(scheduler.isScheduled(runWhenDisabled));
assertFalse(scheduler.isScheduled(dontRunWhenDisabled));
}
@Test
void parallelConditionalRunWhenDisabledTest() {
setDSEnabled(false);
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
MockCommandHolder command4Holder = new MockCommandHolder(false);
Command command4 = command4Holder.getMock();
CommandScheduler scheduler = new CommandScheduler();
Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true);
Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true);
Command parallel = parallel(runWhenDisabled, dontRunWhenDisabled);
scheduler.schedule(parallel);
assertFalse(scheduler.isScheduled(runWhenDisabled));
}
}

View File

@@ -1,30 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class RunCommandTest extends CommandTestBase {
@Test
void runCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
Counter counter = new Counter();
RunCommand command = new RunCommand(counter::increment);
scheduler.schedule(command);
scheduler.run();
scheduler.run();
scheduler.run();
assertEquals(3, counter.m_counter);
}
}

View File

@@ -1,47 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.verify;
class ScheduleCommandTest extends CommandTestBase {
@Test
void scheduleCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
ScheduleCommand scheduleCommand = new ScheduleCommand(command1, command2);
scheduler.schedule(scheduleCommand);
verify(command1).schedule();
verify(command2).schedule();
}
@Test
void scheduleCommandDuringRunTest() {
CommandScheduler scheduler = CommandScheduler.getInstance();
InstantCommand toSchedule = new InstantCommand();
ScheduleCommand scheduleCommand = new ScheduleCommand(toSchedule);
SequentialCommandGroup group =
new SequentialCommandGroup(new InstantCommand(), scheduleCommand);
scheduler.schedule(group);
scheduler.schedule(new InstantCommand().perpetually());
scheduler.run();
assertDoesNotThrow(scheduler::run);
}
}

View File

@@ -1,57 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SchedulerTest extends CommandTestBase {
@Test
void schedulerLambdaTestNoInterrupt() {
CommandScheduler scheduler = new CommandScheduler();
Counter counter = new Counter();
scheduler.onCommandInitialize(command -> counter.increment());
scheduler.onCommandExecute(command -> counter.increment());
scheduler.onCommandFinish(command -> counter.increment());
scheduler.schedule(new InstantCommand());
scheduler.run();
assertEquals(counter.m_counter, 3);
}
@Test
void schedulerInterruptLambdaTest() {
CommandScheduler scheduler = new CommandScheduler();
Counter counter = new Counter();
scheduler.onCommandInterrupt(command -> counter.increment());
Command command = new WaitCommand(10);
scheduler.schedule(command);
scheduler.cancel(command);
assertEquals(counter.m_counter, 1);
}
@Test
void unregisterSubsystemTest() {
CommandScheduler scheduler = new CommandScheduler();
Subsystem system = new TestSubsystem();
scheduler.registerSubsystem(system);
assertDoesNotThrow(() -> scheduler.unregisterSubsystem(system));
}
}

View File

@@ -1,108 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
class SelectCommandTest extends CommandTestBase {
@Test
void selectCommandTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
command1Holder.setFinished(true);
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
SelectCommand selectCommand =
new SelectCommand(Map.ofEntries(
Map.entry("one", command1),
Map.entry("two", command2),
Map.entry("three", command3)),
() -> "one");
scheduler.schedule(selectCommand);
scheduler.run();
verify(command1).initialize();
verify(command1).execute();
verify(command1).end(false);
verify(command2, never()).initialize();
verify(command2, never()).execute();
verify(command2, never()).end(false);
verify(command3, never()).initialize();
verify(command3, never()).execute();
verify(command3, never()).end(false);
}
@Test
void selectCommandInvalidKeyTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
command1Holder.setFinished(true);
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
SelectCommand selectCommand =
new SelectCommand(Map.ofEntries(
Map.entry("one", command1),
Map.entry("two", command2),
Map.entry("three", command3)),
() -> "four");
assertDoesNotThrow(() -> scheduler.schedule(selectCommand));
}
@Test
void selectCommandRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
Subsystem system4 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();
SelectCommand selectCommand = new SelectCommand(
Map.ofEntries(Map.entry("one", command1), Map.entry("two", command2),
Map.entry("three", command3)), () -> "one");
scheduler.schedule(selectCommand);
scheduler.schedule(new InstantCommand(() -> {
}, system3));
assertFalse(scheduler.isScheduled(selectCommand));
verify(command1).end(true);
verify(command2, never()).end(true);
verify(command3, never()).end(true);
}
}

View File

@@ -1,128 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
class SequentialCommandGroupTest extends CommandTestBase {
@Test
void sequentialGroupScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new SequentialCommandGroup(command1, command2);
scheduler.schedule(group);
verify(command1).initialize();
verify(command2, never()).initialize();
command1Holder.setFinished(true);
scheduler.run();
verify(command1).execute();
verify(command1).end(false);
verify(command2).initialize();
verify(command2, never()).execute();
verify(command2, never()).end(false);
command2Holder.setFinished(true);
scheduler.run();
verify(command1).execute();
verify(command1).end(false);
verify(command2).execute();
verify(command2).end(false);
assertFalse(scheduler.isScheduled(group));
}
@Test
void sequentialGroupInterruptTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true);
Command command3 = command3Holder.getMock();
Command group = new SequentialCommandGroup(command1, command2, command3);
scheduler.schedule(group);
command1Holder.setFinished(true);
scheduler.run();
scheduler.cancel(group);
scheduler.run();
verify(command1).execute();
verify(command1, never()).end(true);
verify(command1).end(false);
verify(command2, never()).execute();
verify(command2).end(true);
verify(command2, never()).end(false);
verify(command3, never()).initialize();
verify(command3, never()).execute();
verify(command3, never()).end(true);
verify(command3, never()).end(false);
assertFalse(scheduler.isScheduled(group));
}
@Test
void notScheduledCancelTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true);
Command command2 = command2Holder.getMock();
Command group = new SequentialCommandGroup(command1, command2);
assertDoesNotThrow(() -> scheduler.cancel(group));
}
@Test
void sequentialGroupRequirementTest() {
Subsystem system1 = new TestSubsystem();
Subsystem system2 = new TestSubsystem();
Subsystem system3 = new TestSubsystem();
Subsystem system4 = new TestSubsystem();
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2);
Command command1 = command1Holder.getMock();
MockCommandHolder command2Holder = new MockCommandHolder(true, system3);
Command command2 = command2Holder.getMock();
MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4);
Command command3 = command3Holder.getMock();
Command group = new SequentialCommandGroup(command1, command2);
scheduler.schedule(group);
scheduler.schedule(command3);
assertFalse(scheduler.isScheduled(group));
assertTrue(scheduler.isScheduled(command3));
}
}

View File

@@ -1,37 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class StartEndCommandTest extends CommandTestBase {
@Test
void startEndCommandScheduleTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder cond1 = new ConditionHolder();
ConditionHolder cond2 = new ConditionHolder();
StartEndCommand command =
new StartEndCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true));
scheduler.schedule(command);
scheduler.run();
assertTrue(scheduler.isScheduled(command));
scheduler.cancel(command);
assertFalse(scheduler.isScheduled(command));
assertTrue(cond1.getCondition());
assertTrue(cond2.getCondition());
}
}

View File

@@ -1,67 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import edu.wpi.first.wpilibj.Timer;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class WaitCommandTest extends CommandTestBase {
@Test
void waitCommandTest() {
CommandScheduler scheduler = new CommandScheduler();
WaitCommand waitCommand = new WaitCommand(2);
scheduler.schedule(waitCommand);
scheduler.run();
Timer.delay(1);
scheduler.run();
assertTrue(scheduler.isScheduled(waitCommand));
Timer.delay(2);
scheduler.run();
assertFalse(scheduler.isScheduled(waitCommand));
}
@Test
void withTimeoutTest() {
CommandScheduler scheduler = new CommandScheduler();
MockCommandHolder command1Holder = new MockCommandHolder(true);
Command command1 = command1Holder.getMock();
when(command1.withTimeout(anyDouble())).thenCallRealMethod();
Command timeout = command1.withTimeout(2);
scheduler.schedule(timeout);
scheduler.run();
verify(command1).initialize();
verify(command1).execute();
assertFalse(scheduler.isScheduled(command1));
assertTrue(scheduler.isScheduled(timeout));
Timer.delay(3);
scheduler.run();
verify(command1).end(true);
verify(command1, never()).end(false);
assertFalse(scheduler.isScheduled(timeout));
}
}

View File

@@ -1,31 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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.wpilibj2.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class WaitUntilCommandTest extends CommandTestBase {
@Test
void waitUntilTest() {
CommandScheduler scheduler = new CommandScheduler();
ConditionHolder condition = new ConditionHolder();
Command command = new WaitUntilCommand(condition::getCondition);
scheduler.schedule(command);
scheduler.run();
assertTrue(scheduler.isScheduled(command));
condition.setCondition(true);
scheduler.run();
assertFalse(scheduler.isScheduled(command));
}
}