Skip to content

Commit 426e82c

Browse files
committed
bugfixes and better tests
1 parent 88a7b49 commit 426e82c

10 files changed

Lines changed: 164 additions & 146 deletions

File tree

src/main/java/org/carlmontrobotics/lib199/safeMode/SafeCommand.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@
77

88
public class SafeCommand extends FunctionalCommand {
99

10+
private final Command command;
11+
1012
public SafeCommand(Command command) {
1113
super(
12-
command::initialize,
13-
command::execute,
14+
() -> { if(SafeMode.isEnabled()) command.initialize(); },
15+
() -> { if(SafeMode.isEnabled()) command.execute(); },
1416
command::end,
1517
() -> command.isFinished() || !SafeMode.isEnabled(),
1618
command.getRequirements().toArray(Subsystem[]::new)
1719
);
18-
CommandScheduler.getInstance().registerComposedCommands(command);
20+
CommandScheduler.getInstance().registerComposedCommands(this.command = command);
21+
}
22+
23+
@Override
24+
public boolean runsWhenDisabled() {
25+
return command.runsWhenDisabled();
26+
}
27+
28+
@Override
29+
public InterruptionBehavior getInterruptionBehavior() {
30+
return command.getInterruptionBehavior();
1931
}
2032

2133
}

src/main/java/org/carlmontrobotics/lib199/safeMode/SafeExecuteBlockingCommand.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,27 @@
88
// These are suitable for cases where the command should keep running such as a default command where isFinished must always return false
99
public class SafeExecuteBlockingCommand extends FunctionalCommand {
1010

11+
private final Command command;
12+
1113
public SafeExecuteBlockingCommand(Command command) {
1214
super(
13-
command::initialize,
14-
() -> { if (SafeMode.isEnabled()) command.execute(); },
15+
() -> command.initialize(),
16+
() -> { if(SafeMode.isEnabled()) command.execute(); },
1517
command::end,
1618
command::isFinished,
1719
command.getRequirements().toArray(Subsystem[]::new)
1820
);
19-
CommandScheduler.getInstance().registerComposedCommands(command);
21+
CommandScheduler.getInstance().registerComposedCommands(this.command = command);
22+
}
23+
24+
@Override
25+
public boolean runsWhenDisabled() {
26+
return command.runsWhenDisabled();
27+
}
28+
29+
@Override
30+
public InterruptionBehavior getInterruptionBehavior() {
31+
return command.getInterruptionBehavior();
2032
}
2133

2234
}

src/main/java/org/carlmontrobotics/lib199/safeMode/UnsafeCommand.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@
77

88
public class UnsafeCommand extends FunctionalCommand {
99

10+
private final Command command;
11+
1012
public UnsafeCommand(Command command) {
1113
super(
12-
command::initialize,
13-
command::execute,
14+
() -> { if(!SafeMode.isEnabled()) command.initialize(); },
15+
() -> { if(!SafeMode.isEnabled()) command.execute(); },
1416
command::end,
1517
() -> command.isFinished() || SafeMode.isEnabled(),
1618
command.getRequirements().toArray(Subsystem[]::new)
1719
);
18-
CommandScheduler.getInstance().registerComposedCommands(command);
20+
CommandScheduler.getInstance().registerComposedCommands(this.command = command);
21+
}
22+
23+
@Override
24+
public boolean runsWhenDisabled() {
25+
return command.runsWhenDisabled();
26+
}
27+
28+
@Override
29+
public InterruptionBehavior getInterruptionBehavior() {
30+
return command.getInterruptionBehavior();
1931
}
2032

2133
}

src/main/java/org/carlmontrobotics/lib199/safeMode/UnsafeExecuteBlockingCommand.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// These are suitable for cases where the command should keep running such as a default command where isFinished must always return false
99
public class UnsafeExecuteBlockingCommand extends FunctionalCommand {
1010

11+
private final Command command;
12+
1113
public UnsafeExecuteBlockingCommand(Command command) {
1214
super(
1315
command::initialize,
@@ -16,7 +18,17 @@ public UnsafeExecuteBlockingCommand(Command command) {
1618
command::isFinished,
1719
command.getRequirements().toArray(Subsystem[]::new)
1820
);
19-
CommandScheduler.getInstance().registerComposedCommands(command);
21+
CommandScheduler.getInstance().registerComposedCommands(this.command = command);
22+
}
23+
24+
@Override
25+
public boolean runsWhenDisabled() {
26+
return command.runsWhenDisabled();
27+
}
28+
29+
@Override
30+
public InterruptionBehavior getInterruptionBehavior() {
31+
return command.getInterruptionBehavior();
2032
}
2133

2234
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.carlmontrobotics.lib199.safeMode;
2+
3+
import edu.wpi.first.wpilibj2.command.CommandBase;
4+
5+
public class LoggingCommand extends CommandBase {
6+
7+
private int initializedCount = 0, executeCount = 0;
8+
9+
@Override
10+
public void initialize() {
11+
initializedCount++;
12+
}
13+
14+
@Override
15+
public void execute() {
16+
executeCount++;
17+
}
18+
19+
public void reset() {
20+
initializedCount = executeCount = 0;
21+
}
22+
23+
public int getInitializedCount() {
24+
return initializedCount;
25+
}
26+
27+
public int getExecuteCount() {
28+
return executeCount;
29+
}
30+
31+
@Override
32+
public boolean isFinished() {
33+
return false;
34+
}
35+
36+
@Override
37+
public boolean runsWhenDisabled() {
38+
return true;
39+
}
40+
41+
}

src/test/java/org/carlmontrobotics/lib199/safeMode/SafeCommandTest.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/test/java/org/carlmontrobotics/lib199/safeMode/SafeExecuteBlockingCommandTest.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.carlmontrobotics.lib199.safeMode;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.function.Function;
8+
9+
import org.junit.Test;
10+
11+
import edu.wpi.first.wpilibj2.command.Command;
12+
import edu.wpi.first.wpilibj2.command.CommandScheduler;
13+
14+
public class SafeModeCommandsTest {
15+
16+
@Test
17+
public void testSafeCommand() {
18+
testCommand(SafeCommand::new, true, false);
19+
}
20+
21+
@Test
22+
public void testSafeBlockingCommand() {
23+
testCommand(SafeExecuteBlockingCommand::new, true, true);
24+
}
25+
26+
@Test
27+
public void testUnsafeCommand() {
28+
testCommand(UnsafeCommand::new, false, false);
29+
}
30+
31+
@Test
32+
public void testUnsafeExecuteBlockingCommand() {
33+
testCommand(UnsafeExecuteBlockingCommand::new, false, true);
34+
}
35+
36+
public void testCommand(Function<Command, Command> constructor, boolean isSafe, boolean staysEnabled) {
37+
LoggingCommand loggingCommand = new LoggingCommand();
38+
Command command = constructor.apply(loggingCommand);
39+
40+
loggingCommand.reset();
41+
42+
SafeMode.enable();
43+
command.schedule();
44+
CommandScheduler.getInstance().run();
45+
if (isSafe || staysEnabled)
46+
assertTrue(command.isScheduled());
47+
else
48+
assertFalse(command.isScheduled());
49+
if (!staysEnabled)
50+
assertEquals(isSafe ? 1 : 0, loggingCommand.getInitializedCount());
51+
assertEquals(isSafe ? 1 : 0, loggingCommand.getExecuteCount());
52+
53+
SafeMode.disable();
54+
command.schedule();
55+
CommandScheduler.getInstance().run();
56+
if (!isSafe || staysEnabled)
57+
assertTrue(command.isScheduled());
58+
else
59+
assertFalse(command.isScheduled());
60+
if (!staysEnabled)
61+
assertEquals(1, loggingCommand.getInitializedCount());
62+
assertEquals(1, loggingCommand.getExecuteCount());
63+
}
64+
65+
}

src/test/java/org/carlmontrobotics/lib199/safeMode/UnsafeCommandTest.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/test/java/org/carlmontrobotics/lib199/safeMode/UnsafeExecuteBlockingCommandTest.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)