Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
elvizer committed Nov 24, 2024
2 parents de1615f + c417579 commit 902aef1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/lib/CTREUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static boolean attempt(Supplier<StatusCode> action, String deviceName) {
// failed attempt
else {
FaultLogger.report(
deviceName + ": Config Apply Failed: " + statusCode.getDescription(), FaultType.ERROR);
deviceName + ": Config Apply Failed - " + statusCode.getDescription(), FaultType.ERROR);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/lib/FaultsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final boolean equals(Object other) {
if (other == this) return true;

if (other instanceof Fault o) {
return o.description == description && o.type == type;
return o.description.equals(description) && o.type.equals(type);
}

return false;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/frc/lib/UnitTestingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public static void setupTests() {
Timer.delay(0.100);
}

/** Resets the CommandScheduler. */
public static void reset() {
CommandScheduler.getInstance().unregisterAllSubsystems();
CommandScheduler.getInstance().cancelAll();
}

/**
* Resets CommandScheduler and closes all subsystems. Please call in an @AfterEach method!
*
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/frc/lib/AdvancedSubsystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj2.command.Command;
import frc.lib.FaultsTable.Fault;
import frc.lib.FaultsTable.FaultType;
import java.util.function.BiConsumer;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -51,10 +52,18 @@ public void addFault() {
assert _sub.hasFault("FAULT 1", FaultType.ERROR);
}

@Test
public void faultEquality() {
Fault f1 = new Fault("FAULT 1", FaultType.ERROR);
Fault f2 = new Fault("FAULT 1", FaultType.ERROR);

assert f1.equals(f2);
}

@Test
public void duplicateFaults() {
_sub.addFault("Fault 1", FaultType.ERROR);
_sub.addFault("Fault 1", FaultType.ERROR);
_sub.addFault("FAULT 1", FaultType.ERROR);
_sub.addFault("FAULT 1", FaultType.ERROR);

assertEquals(_sub.getFaults().size(), 1, DELTA);
}
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/frc/lib/CTREUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package frc.lib;

import static frc.lib.UnitTestingUtil.reset;
import static frc.lib.UnitTestingUtil.setupTests;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.ctre.phoenix6.StatusCode;
import com.ctre.phoenix6.hardware.TalonFX;
import frc.lib.FaultsTable.Fault;
import frc.lib.FaultsTable.FaultType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CTREUtilTest {
private TalonFX _motor;

@BeforeEach
public void setup() {
setupTests();

_motor = new TalonFX(1);
}

@AfterEach
public void close() throws Exception {
reset();

_motor.close();
}

@Test
public void motorName() {
var name = CTREUtil.getName(_motor);

assertEquals(name, "TalonFX (1)");
}

@Test
public void motorAttempt() {
var name = CTREUtil.getName(_motor);

var failed = CTREUtil.attempt(() -> StatusCode.ConfigFailed, _motor);

assert failed;

FaultLogger.update();

assert FaultLogger.totalFaults()
.contains(
new Fault(
name + ": Config Apply Failed - " + StatusCode.ConfigFailed.getDescription(),
FaultType.ERROR));
}
}

0 comments on commit 902aef1

Please sign in to comment.