generated from wpilibsuite/vendor-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add builder pattern and fix tests for Monitor
- Loading branch information
Showing
2 changed files
with
221 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,91 @@ | ||
package coppercore.monitors.test; | ||
|
||
import coppercore.monitors.Monitor; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MonitorTests { | ||
// TODO: FIGURE OUT HOW TO TEST THIS | ||
// Callbacks are hard :( | ||
|
||
// Mimics a changing robot state | ||
// boolean isStateValid = true; | ||
|
||
// @Test | ||
// public void nonStickyTest() { | ||
// Monitor exampleMonitor = | ||
// new Monitor("exampleMonitor", false, () -> isStateValid, 1.0, () -> {}); | ||
|
||
// exampleMonitor.periodic(0.0); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// isStateValid = false; | ||
// exampleMonitor.periodic(1.0); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// exampleMonitor.periodic(1.25); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// exampleMonitor.periodic(2.0); | ||
// Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
// Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
// isStateValid = true; | ||
// exampleMonitor.periodic(3.0); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
// } | ||
|
||
// @Test | ||
// public void stickyTest() { | ||
// // Mimics a changing robot state | ||
// boolean isStateValid = true; | ||
|
||
// Monitor exampleMonitor = | ||
// new Monitor( | ||
// "exampleMonitor", | ||
// true, | ||
// () -> isStateValid, | ||
// 1.0, | ||
// () -> {}); // TODO: Figure out how to test callback | ||
|
||
// exampleMonitor.periodic(0.0); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// isStateValid = false; | ||
// exampleMonitor.periodic(1.0); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// exampleMonitor.periodic(1.25); | ||
// Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
// exampleMonitor.periodic(2.0); | ||
// Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
// Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
// isStateValid = true; | ||
// exampleMonitor.periodic(3.0); | ||
// Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
// Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
// } | ||
private boolean isStateValid = true; | ||
|
||
private boolean getIsStateValid() { | ||
return isStateValid; | ||
} | ||
|
||
@Test | ||
public void nonStickyTest() { | ||
isStateValid = true; | ||
|
||
Monitor exampleMonitor = | ||
new Monitor.MonitorBuilder() | ||
.withName("exampleMonitor") | ||
.withStickyness(false) | ||
.withIsStateValidSupplier(() -> getIsStateValid()) | ||
.withTimeToFault(1.0) | ||
.withFaultCallback(() -> {}) | ||
.build(); | ||
|
||
exampleMonitor.periodic(0.0); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
isStateValid = false; | ||
exampleMonitor.periodic(1.0); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
exampleMonitor.periodic(1.25); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
exampleMonitor.periodic(2.0); | ||
Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
isStateValid = true; | ||
exampleMonitor.periodic(3.0); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
} | ||
|
||
@Test | ||
public void stickyTest() { | ||
// Mimics a changing robot state | ||
isStateValid = true; | ||
|
||
Monitor exampleMonitor = | ||
new Monitor.MonitorBuilder() | ||
.withName("exampleMonitor") | ||
.withStickyness(true) | ||
.withIsStateValidSupplier(() -> getIsStateValid()) | ||
.withTimeToFault(1.0) | ||
.withFaultCallback(() -> {}) | ||
.build(); | ||
|
||
exampleMonitor.periodic(0.0); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
|
||
isStateValid = false; | ||
exampleMonitor.periodic(1.0); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertFalse(getIsStateValid()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
exampleMonitor.periodic(1.25); | ||
Assertions.assertFalse(exampleMonitor.isFaulted()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
exampleMonitor.periodic(2.0); | ||
Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
Assertions.assertTrue(exampleMonitor.isTriggered()); | ||
|
||
isStateValid = true; | ||
exampleMonitor.periodic(3.0); | ||
Assertions.assertTrue(exampleMonitor.isFaulted()); | ||
Assertions.assertFalse(exampleMonitor.isTriggered()); | ||
} | ||
} |