Skip to content

Commit

Permalink
yay unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan1522 committed Nov 6, 2024
1 parent 0d45bba commit 9ba4c8d
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 110 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.10.2'


// Mockito for mocking objects in unit tests
testImplementation 'org.mockito:mockito-core:4.8.1' // Use the latest version

// Optional: If you need Mockito extensions for JUnit5
testImplementation 'org.mockito:mockito-junit-jupiter:4.8.1' // Or the latest version

implementation 'gov.nist.math:jama:1.0.3'

implementation "io.grpc:grpc-protobuf:${grpcVersion}"
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/frc/robot/BuildConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public final class BuildConstants {
public static final String MAVEN_GROUP = "";
public static final String MAVEN_NAME = "4829-BaseRobotCode";
public static final String VERSION = "unspecified";
public static final int GIT_REVISION = 8;
public static final String GIT_SHA = "52fbf88798bd786d25dffff6b4769182ad9ea51c";
public static final String GIT_DATE = "2024-10-29 17:58:37 EDT";
public static final String GIT_BRANCH = "main";
public static final String BUILD_DATE = "2024-11-05 07:52:13 EST";
public static final long BUILD_UNIX_TIME = 1730811133569L;
public static final int DIRTY = 0;
public static final int GIT_REVISION = 9;
public static final String GIT_SHA = "0d45bba6ffc24373aef17c97030333ba903faf7f";
public static final String GIT_DATE = "2024-11-05 08:00:30 EST";
public static final String GIT_BRANCH = "actual-unit-tests";
public static final String BUILD_DATE = "2024-11-06 12:16:06 EST";
public static final long BUILD_UNIX_TIME = 1730913366161L;
public static final int DIRTY = 1;

private BuildConstants(){}
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/subsystems/swerve/SwerveDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public double getCharacterizationVelocity() {
}

/** Processes odometry inputs */
private void fetchOdometryInputs() {
void fetchOdometryInputs() {
odometryThread.lockOdometry();
odometryThread.updateInputs(odometryThreadInputs);
Logger.processInputs("Drive/OdometryThread", odometryThreadInputs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SwerveModule extends SubsystemBase {
private final String name;
private final ModuleInputsAutoLogged inputs = new ModuleInputsAutoLogged();

private SwerveModulePosition[] odometryPositions = new SwerveModulePosition[] {};
SwerveModulePosition[] odometryPositions = new SwerveModulePosition[] {};

private final Alert hardwareFaultAlert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public static class GyroInputs {
*
* @param inputs inputs to update
*/
void updateInputs(GyroInputs inputs);
default void updateInputs(GyroInputs inputs) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ class ModuleInputs {
*
* @param inputs the inputs to update
*/
void updateInputs(ModuleInputs inputs);
default void updateInputs(ModuleInputs inputs) {}

/**
* Sets the desired state for the module and sends calculated output from controller to the motor.
*
* @param desiredState Desired state with speed and angle.
*/
void setDesiredState(SwerveModuleState desiredState);
default void setDesiredState(SwerveModuleState desiredState) {}

void setDriveVoltage(Voltage voltage);
default void setDriveVoltage(Voltage voltage) {}

void setTurnVoltage(Voltage voltage);
default void setTurnVoltage(Voltage voltage) {}

void stopModule();
default void stopModule() {}

double getTurnRotations();
default double getTurnRotations() {return 0.0;}
}
94 changes: 0 additions & 94 deletions src/test/java/RobotTest.java

This file was deleted.

157 changes: 157 additions & 0 deletions src/test/java/SwerveDriveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@


import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.junit.Before;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.subsystems.swerve.*;
import frc.robot.subsystems.swerve.SwerveConstants.DriveConstants;
import frc.robot.subsystems.swerve.moduleIO.ModuleInterface;
import frc.robot.subsystems.swerve.gyroIO.GyroInputsAutoLogged;
import frc.robot.subsystems.swerve.gyroIO.GyroInterface;
import frc.robot.subsystems.swerve.odometryThread.OdometryThread;

class SwerveDriveTest {

private SwerveDrive swerveDrive;
private GyroInterface mockGyroIO;
private ModuleInterface mockFrontLeftModule;
private ModuleInterface mockFrontRightModule;
private ModuleInterface mockBackLeftModule;
private ModuleInterface mockBackRightModule;
private OdometryThread mockOdometryThread;

@BeforeEach
void setUp() {
// Mock the dependencies
mockGyroIO = mock(GyroInterface.class);
mockFrontLeftModule = mock(ModuleInterface.class);
mockFrontRightModule = mock(ModuleInterface.class);
mockBackLeftModule = mock(ModuleInterface.class);
mockBackRightModule = mock(ModuleInterface.class);
mockOdometryThread = mock(OdometryThread.class);

// Initialize the SwerveDrive with the mocked dependencies
swerveDrive = new SwerveDrive(mockGyroIO, mockFrontLeftModule, mockFrontRightModule, mockBackLeftModule, mockBackRightModule);
}

@Test
void testConstructorInitialization() {
// Ensure that the constructor correctly initializes the pose estimator and modules
assertNotNull(swerveDrive);
// assertNotNull(swerveDrive.poseEstimator);
// assertNotNull(swerveDrive.swerveModules);
// assertEquals(4, swerveDrive.swerveModules.length); // 4 modules
}

@Test
void testGetGyroRate() {
// Mock gyro inputs
GyroInputsAutoLogged inputsAutoLogged = new GyroInputsAutoLogged();
when(inputsAutoLogged.yawVelocity).thenReturn(0.5);

// Test the getGyroRate method
double gyroRate = swerveDrive.getGyroRate();
assertEquals(0.5, gyroRate, 0.001);
}

@Test
void testDrive() {
// Prepare mock module states
SwerveModuleState[] mockStates = new SwerveModuleState[4];
for (int i = 0; i < 4; i++) {
mockStates[i] = mock(SwerveModuleState.class);
}

// Mock the kinematics method to return mock states
when(DriveConstants.DRIVE_KINEMATICS.toSwerveModuleStates(any(ChassisSpeeds.class))).thenReturn(mockStates);

// Call the drive method
swerveDrive.drive(1.0, 1.0, 0.5, true);

// Verify that setModuleStates was called
verify(swerveDrive).setModuleStates(mockStates);
}

@Test
void testGetPose() {
// Mock the pose estimator to return a specific pose
Pose2d mockPose = new Pose2d(1.0, 2.0, new Rotation2d(Math.toRadians(45)));
when(swerveDrive.getPose()).thenReturn(mockPose);

// Call the getPose method and verify the returned pose
Pose2d pose = swerveDrive.getPose();
assertEquals(1.0, pose.getX(), 0.001);
assertEquals(2.0, pose.getY(), 0.001);
assertEquals(Math.toRadians(45), pose.getRotation().getRadians(), 0.001);
}

@Test
void testSetPose() {
// Mock pose setter
Pose2d newPose = new Pose2d(3.0, 4.0, Rotation2d.fromDegrees(90));
swerveDrive.setPose(newPose);

// Verify that the resetPosition method was called with the new pose
verify(swerveDrive).setPose(eq(newPose));
}

// @Test
// void testCharacterization() {
// // Mock voltage setting on modules
// doNothing().when(mockFrontLeftModule).setVoltage(any());
// doNothing().when(mockFrontRightModule).setVoltage(any());
// doNothing().when(mockBackLeftModule).setVoltage(any());
// doNothing().when(mockBackRightModule).setVoltage(any());

// // Call the runCharacterization method
// swerveDrive.runCharacterization(12.0);

// // Verify that the setVoltage method was called on all modules
// verify(mockFrontLeftModule).setVoltage(-12.0);
// verify(mockFrontRightModule).setVoltage(-12.0);
// verify(mockBackLeftModule).setVoltage(-12.0);
// verify(mockBackRightModule).setVoltage(-12.0);
// }

// @Test
// void testFetchOdometryInputs() {
// // Mock inputs and behavior
// when(mockGyroIO.updateInputs(any())).thenReturn(null);
// doNothing().when(mockOdometryThread).lockOdometry();
// doNothing().when(mockOdometryThread).unlockOdometry();

// // Call the fetchOdometryInputs method
// swerveDrive.fetchOdometryInputs();

// // Verify the behavior of the method
// verify(mockOdometryThread).lockOdometry();
// verify(mockOdometryThread).unlockOdometry();
// verify(mockGyroIO).updateInputs(any());
// }

@Test
void testAllianceAngleOffset() {
// Test for Blue Alliance
when(DriverStation.getAlliance()).thenReturn(Optional.of(DriverStation.Alliance.Blue));
assertEquals(0.0, swerveDrive.getAllianceAngleOffset(), 0.001);

// Test for Red Alliance
when(DriverStation.getAlliance()).thenReturn(Optional.of(DriverStation.Alliance.Red));
assertEquals(180.0, swerveDrive.getAllianceAngleOffset(), 0.001);
}
}
Loading

0 comments on commit 9ba4c8d

Please sign in to comment.