mirror of
https://github.com/Team4388/Robot-Essentials.git
synced 2026-06-09 00:38:01 -06:00
Merge pull request #19 from Team4388/templates
Add Custom Templates for Easy Class Creation
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 frc4388.robot.commands;
|
||||
|
||||
import edu.wpi.first.wpilibj2.command.*;
|
||||
import frc4388.robot.subsystems.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
public class CommandTest {
|
||||
private CommandScheduler scheduler = null;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
scheduler = CommandScheduler.getInstance();
|
||||
}
|
||||
|
||||
// TODO: Update this to use an actual command. Won't work with inline commands for some reason
|
||||
|
||||
@Test
|
||||
public void testExample() {
|
||||
// Arrange
|
||||
Drive drive = mock(Drive.class);
|
||||
RunCommand command = new RunCommand(() -> drive.driveWithInput(0, 0), drive);
|
||||
|
||||
// Act
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
|
||||
// Assert
|
||||
verify(drive).driveWithInput(0, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 frc4388.robot.subsystems;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import edu.wpi.first.wpilibj.Spark;
|
||||
import frc4388.robot.Constants.LEDConstants;
|
||||
import frc4388.utility.LEDPatterns;
|
||||
|
||||
/**
|
||||
* Based off the LEDSubsystemTest class
|
||||
*/
|
||||
public class SubsystemTest {
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
// Arrange
|
||||
Spark ledController = mock(Spark.class);
|
||||
|
||||
// Act
|
||||
LED led = new LED(ledController);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDConstants.DEFAULT_PATTERN.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPatterns() {
|
||||
// Arrange
|
||||
Spark ledController = mock(Spark.class);
|
||||
LED led = new LED(ledController);
|
||||
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.RAINBOW_RAINBOW);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.RAINBOW_RAINBOW.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.BLUE_BREATH);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.BLUE_BREATH.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.SOLID_BLACK);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.SOLID_BLACK.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 frc4388.utility;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import com.kauailabs.navx.frc.AHRS;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import frc4388.mocks.MockPigeonIMU;
|
||||
import frc4388.robot.Constants.DriveConstants;
|
||||
|
||||
/**
|
||||
* Based on the RobotGyroUtilityTest class
|
||||
*/
|
||||
public class UtilityTest {
|
||||
private RobotGyro gyroPigeon;
|
||||
private RobotGyro gyroNavX;
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
AHRS navX = mock(AHRS.class);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
gyroNavX = new RobotGyro(navX);
|
||||
|
||||
// Assert
|
||||
assertEquals(true, gyroPigeon.m_isGyroAPigeon);
|
||||
assertEquals(pigeon, gyroPigeon.getPigeon());
|
||||
assertEquals(null, gyroPigeon.getNavX());
|
||||
assertEquals(false, gyroNavX.m_isGyroAPigeon);
|
||||
assertEquals(navX, gyroNavX.getNavX());
|
||||
assertEquals(null, gyroNavX.getPigeon());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeadingPigeon() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
|
||||
// Act & Assert
|
||||
assertEquals(-90, gyroPigeon.getHeading(270), 0.0001);
|
||||
assertEquals(-45, gyroPigeon.getHeading(315), 0.0001);
|
||||
assertEquals(-60, gyroPigeon.getHeading(-60), 0.0001);
|
||||
assertEquals(30, gyroPigeon.getHeading(30), 0.0001);
|
||||
assertEquals(0, gyroPigeon.getHeading(0), 0.0001);
|
||||
assertEquals(180, gyroPigeon.getHeading(180), 0.0001);
|
||||
assertEquals(-180, gyroPigeon.getHeading(-180), 0.0001);
|
||||
assertEquals(97, gyroPigeon.getHeading(1537), 0.0001);
|
||||
assertEquals(99, gyroPigeon.getHeading(-2781), 0.0001);
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,11 @@
|
||||
|
||||
package frc4388.robot.subsystems;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.junit.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import edu.wpi.first.wpilibj.*;
|
||||
import frc4388.robot.Constants.LEDConstants;
|
||||
import frc4388.utility.LEDPatterns;
|
||||
@@ -18,24 +20,40 @@ import frc4388.utility.LEDPatterns;
|
||||
* Add your docs here.
|
||||
*/
|
||||
public class LEDSubsystemTest {
|
||||
Spark ledController = mock(Spark.class);
|
||||
LED led = new LED(ledController);
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
// Arrange
|
||||
Spark ledController = mock(Spark.class);
|
||||
|
||||
// Act
|
||||
LED led = new LED(ledController);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDConstants.DEFAULT_PATTERN.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPatterns() {
|
||||
// TEST 1
|
||||
assertEquals(LEDConstants.DEFAULT_PATTERN.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
// Arrange
|
||||
Spark ledController = mock(Spark.class);
|
||||
LED led = new LED(ledController);
|
||||
|
||||
// TEST 2
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.RAINBOW_RAINBOW);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.RAINBOW_RAINBOW.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
|
||||
// TEST 3
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.BLUE_BREATH);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.BLUE_BREATH.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
|
||||
// TEST 4
|
||||
// Act
|
||||
led.setPattern(LEDPatterns.SOLID_BLACK);
|
||||
|
||||
// Assert
|
||||
assertEquals(LEDPatterns.SOLID_BLACK.getValue(), led.getPattern().getValue(), 0.0001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,44 +9,47 @@ package frc4388.utility;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.junit.*;
|
||||
|
||||
import edu.wpi.first.wpilibj.*;
|
||||
import com.ctre.phoenix.sensors.PigeonIMU;
|
||||
import com.kauailabs.navx.frc.AHRS;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import frc4388.mocks.MockPigeonIMU;
|
||||
import frc4388.robot.Constants.DriveConstants;
|
||||
import frc4388.utility.RobotGyro;
|
||||
|
||||
/**
|
||||
* Add your docs here.
|
||||
*/
|
||||
public class RobotGyroUtilityTest {
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
RobotGyro gyroPigeon = new RobotGyro(pigeon);
|
||||
AHRS navX = mock(AHRS.class);
|
||||
RobotGyro gyroNavX = new RobotGyro(navX);
|
||||
RobotTime robotTime = RobotTime.getInstance();
|
||||
|
||||
// TODO UNTESTED: most functions for NavX
|
||||
|
||||
private RobotGyro gyroPigeon;
|
||||
private RobotGyro gyroNavX;
|
||||
|
||||
@Test
|
||||
public void testConfig() {
|
||||
// TEST 1
|
||||
public void testConstructor() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
AHRS navX = mock(AHRS.class);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
gyroNavX = new RobotGyro(navX);
|
||||
|
||||
// Assert
|
||||
assertEquals(true, gyroPigeon.m_isGyroAPigeon);
|
||||
assertEquals(pigeon, gyroPigeon.getPigeon());
|
||||
assertEquals(null, gyroPigeon.getNavX());
|
||||
|
||||
// TEST 2
|
||||
assertEquals(false, gyroNavX.m_isGyroAPigeon);
|
||||
assertEquals(navX, gyroNavX.getNavX());
|
||||
assertEquals(null, gyroNavX.getPigeon());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeading() {
|
||||
// TESTS
|
||||
public void testHeadingPigeon() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
|
||||
// Act & Assert
|
||||
assertEquals(-90, gyroPigeon.getHeading(270), 0.0001);
|
||||
assertEquals(-45, gyroPigeon.getHeading(315), 0.0001);
|
||||
assertEquals(-60, gyroPigeon.getHeading(-60), 0.0001);
|
||||
@@ -59,97 +62,123 @@ public class RobotGyroUtilityTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testYawPitchRoll() {
|
||||
// TEST 1
|
||||
public void testYawPitchRollPigeon() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
|
||||
// Assert
|
||||
assertEquals(0, gyroPigeon.getAngle(), 0.0001);
|
||||
|
||||
// TEST 2
|
||||
// Act
|
||||
pigeon.setYaw(40);
|
||||
|
||||
// Assert
|
||||
assertEquals(40, gyroPigeon.getAngle(), 0.0001);
|
||||
|
||||
// TEST 3
|
||||
// Act
|
||||
gyroPigeon.reset();
|
||||
|
||||
// Assert
|
||||
assertEquals(0, gyroPigeon.getAngle(), 0.0001);
|
||||
|
||||
// TEST 4
|
||||
// Act
|
||||
pigeon.setYaw(-1457);
|
||||
pigeon.setCurrentPitch(100);
|
||||
pigeon.setCurrentRoll(100);
|
||||
|
||||
// Assert
|
||||
assertEquals(-1457, gyroPigeon.getAngle(), 0.0001);
|
||||
assertEquals(90, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(90, gyroPigeon.getRoll(), 0.0001);
|
||||
|
||||
// TEST 5
|
||||
// Act
|
||||
pigeon.setCurrentPitch(45);
|
||||
pigeon.setCurrentRoll(45);
|
||||
|
||||
// Assert
|
||||
assertEquals(45, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(45, gyroPigeon.getRoll(), 0.0001);
|
||||
|
||||
// TEST 6
|
||||
// Act
|
||||
pigeon.setCurrentPitch(0);
|
||||
pigeon.setCurrentRoll(0);
|
||||
|
||||
// Assert
|
||||
assertEquals(0, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(0, gyroPigeon.getRoll(), 0.0001);
|
||||
|
||||
// TEST 7
|
||||
// Act
|
||||
pigeon.setCurrentPitch(-60);
|
||||
pigeon.setCurrentRoll(-60);
|
||||
|
||||
// Assert
|
||||
assertEquals(-60, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(-60, gyroPigeon.getRoll(), 0.0001);
|
||||
|
||||
// TEST 8
|
||||
// Act
|
||||
pigeon.setCurrentPitch(-90);
|
||||
pigeon.setCurrentRoll(-90);
|
||||
|
||||
// Assert
|
||||
assertEquals(-90, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(-90, gyroPigeon.getRoll(), 0.0001);
|
||||
|
||||
// TEST 9
|
||||
// Act
|
||||
pigeon.setCurrentPitch(-100);
|
||||
pigeon.setCurrentRoll(-100);
|
||||
|
||||
// Assert
|
||||
assertEquals(-90, gyroPigeon.getPitch(), 0.0001);
|
||||
assertEquals(-90, gyroPigeon.getRoll(), 0.0001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRates() {
|
||||
// SETUP
|
||||
pigeon.setYaw(0);
|
||||
public void testRatesPigeon() {
|
||||
// Arrange
|
||||
MockPigeonIMU pigeon = new MockPigeonIMU(DriveConstants.DRIVE_PIGEON_ID);
|
||||
gyroPigeon = new RobotGyro(pigeon);
|
||||
RobotTime robotTime = RobotTime.getInstance();
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// TEST 1
|
||||
// Act
|
||||
robotTime.m_deltaTime = 5;
|
||||
pigeon.setYaw(0);
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// Assert
|
||||
assertEquals(0, gyroPigeon.getRate(), 1);
|
||||
|
||||
// TEST 2
|
||||
// Act
|
||||
robotTime.m_deltaTime = 5;
|
||||
pigeon.setYaw(90);
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// Assert
|
||||
assertEquals(18000, gyroPigeon.getRate(), 0.001);
|
||||
|
||||
// TEST 3
|
||||
// Act
|
||||
robotTime.m_deltaTime = 5;
|
||||
pigeon.setYaw(90);
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// Assert
|
||||
assertEquals(0, gyroPigeon.getRate(), 0.001);
|
||||
|
||||
// TEST 4
|
||||
// Act
|
||||
robotTime.m_deltaTime = 3;
|
||||
pigeon.setYaw(-30);
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// Assert
|
||||
assertEquals(-40000, gyroPigeon.getRate(), 0.001);
|
||||
|
||||
// TEST 5
|
||||
// Act
|
||||
robotTime.m_deltaTime = 6;
|
||||
pigeon.setYaw(690);
|
||||
gyroPigeon.updatePigeonDeltas();
|
||||
|
||||
// Assert
|
||||
assertEquals(120000, gyroPigeon.getRate(), 0.001);
|
||||
}
|
||||
|
||||
private void wait(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,8 @@
|
||||
package frc4388.utility;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import org.junit.*;
|
||||
|
||||
import edu.wpi.first.wpilibj.*;
|
||||
import org.junit.*;
|
||||
|
||||
/**
|
||||
* Add your docs here.
|
||||
@@ -21,7 +19,7 @@ public class RobotTimeUtilityTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateTimes() {
|
||||
// SETUP
|
||||
// Arrange
|
||||
long lastTime;
|
||||
robotTime.m_deltaTime = 0;
|
||||
robotTime.m_robotTime = 0;
|
||||
@@ -30,25 +28,29 @@ public class RobotTimeUtilityTest {
|
||||
robotTime.endMatchTime();
|
||||
robotTime.m_lastMatchTime = 0;
|
||||
|
||||
// TEST 1
|
||||
// Assert
|
||||
assertEquals(0, robotTime.m_deltaTime);
|
||||
assertEquals(0, robotTime.m_robotTime);
|
||||
assertEquals(0, robotTime.m_lastRobotTime);
|
||||
assertEquals(0, robotTime.m_frameNumber);
|
||||
lastTime = robotTime.m_robotTime;
|
||||
|
||||
// TEST 2
|
||||
// Act
|
||||
wait(1);
|
||||
robotTime.updateTimes();
|
||||
|
||||
// Assert
|
||||
assertEquals(true, robotTime.m_deltaTime > 0);
|
||||
assertEquals(true, robotTime.m_robotTime > 0);
|
||||
assertEquals(lastTime, robotTime.m_lastRobotTime);
|
||||
assertEquals(1, robotTime.m_frameNumber);
|
||||
lastTime = robotTime.m_robotTime;
|
||||
|
||||
// TEST 3
|
||||
// Act
|
||||
wait(1);
|
||||
robotTime.updateTimes();
|
||||
|
||||
// Assert
|
||||
assertEquals(true, robotTime.m_deltaTime > 0);
|
||||
assertEquals(true, robotTime.m_robotTime > 0);
|
||||
assertEquals(lastTime, robotTime.m_lastRobotTime);
|
||||
@@ -57,33 +59,39 @@ public class RobotTimeUtilityTest {
|
||||
|
||||
@Test
|
||||
public void testMatchTime() {
|
||||
// SETUP
|
||||
// Arrange
|
||||
long lastTime;
|
||||
|
||||
// TEST 1
|
||||
// Assert
|
||||
assertEquals(0, robotTime.m_matchTime);
|
||||
assertEquals(0, robotTime.m_lastMatchTime);
|
||||
lastTime = robotTime.m_matchTime;
|
||||
|
||||
// TEST 2
|
||||
// Act
|
||||
robotTime.startMatchTime();
|
||||
wait(1);
|
||||
robotTime.updateTimes();
|
||||
|
||||
// Assert
|
||||
assertEquals(true, robotTime.m_matchTime > 0);
|
||||
assertEquals(lastTime, robotTime.m_lastMatchTime);
|
||||
lastTime = robotTime.m_matchTime;
|
||||
|
||||
// TEST 3
|
||||
// Act
|
||||
wait(1);
|
||||
robotTime.updateTimes();
|
||||
robotTime.endMatchTime();
|
||||
|
||||
// Assert
|
||||
assertEquals(0, robotTime.m_matchTime);
|
||||
assertEquals(lastTime, robotTime.m_lastMatchTime);
|
||||
lastTime = robotTime.m_matchTime;
|
||||
|
||||
// TEST 4
|
||||
// Act
|
||||
wait(1);
|
||||
robotTime.updateTimes();
|
||||
|
||||
// Assert
|
||||
assertEquals(0, robotTime.m_matchTime);
|
||||
assertEquals(lastTime, robotTime.m_lastMatchTime);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* This file is a configuration file generated by the `Template` extension on `vscode`
|
||||
* @see https://marketplace.visualstudio.com/items?itemName=yongwoo.template
|
||||
*/
|
||||
module.exports = {
|
||||
// You can change the template path to another path
|
||||
templateRootPath: "./.templates",
|
||||
// After copying the template file the `replaceFileTextFn` function is executed
|
||||
replaceFileTextFn: (fileText, templateName, utils) => {
|
||||
// @see https://www.npmjs.com/package/change-case
|
||||
const { changeCase } = utils;
|
||||
// You can change the text in the file
|
||||
return fileText
|
||||
.replace(/__templateName__/gm, templateName)
|
||||
.replace(
|
||||
/__templateNameToPascalCase__/gm,
|
||||
changeCase.pascalCase(templateName)
|
||||
)
|
||||
.replace(
|
||||
/__templateNameToParamCase__/gm,
|
||||
changeCase.paramCase(templateName)
|
||||
);
|
||||
},
|
||||
replaceFileNameFn: (fileName, templateName, utils) => {
|
||||
const { path } = utils;
|
||||
// @see https://nodejs.org/api/path.html#path_path_parse_path
|
||||
const { base } = path.parse(fileName);
|
||||
// You can change the file name
|
||||
return base;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user