mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples] Add unit testing infrastructure (#4646)
This commit is contained in:
17
wpilibcExamples/src/test/cpp/examples/UnitTest/cpp/main.cpp
Normal file
17
wpilibcExamples/src/test/cpp/examples/UnitTest/cpp/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <hal/HALBase.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
/**
|
||||
* Runs all unit tests.
|
||||
*/
|
||||
int main(int argc, char** argv) {
|
||||
HAL_Initialize(500, 0);
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
int ret = RUN_ALL_TESTS();
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <frc/DoubleSolenoid.h>
|
||||
#include <frc/simulation/DoubleSolenoidSim.h>
|
||||
#include <frc/simulation/PWMSim.h>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "subsystems/Intake.h"
|
||||
|
||||
class IntakeTest : public testing::Test {
|
||||
protected:
|
||||
Intake intake; // create our intake
|
||||
frc::sim::PWMSim simMotor{
|
||||
IntakeConstants::kMotorPort}; // create our simulation PWM
|
||||
frc::sim::DoubleSolenoidSim simPiston{
|
||||
frc::PneumaticsModuleType::CTREPCM, IntakeConstants::kPistonFwdChannel,
|
||||
IntakeConstants::kPistonRevChannel}; // create our simulation solenoid
|
||||
};
|
||||
|
||||
TEST_F(IntakeTest, DoesntWorkWhenClosed) {
|
||||
intake.Retract(); // close the intake
|
||||
intake.Activate(0.5); // try to activate the motor
|
||||
EXPECT_DOUBLE_EQ(
|
||||
0.0,
|
||||
simMotor.GetSpeed()); // make sure that the value set to the motor is 0
|
||||
}
|
||||
|
||||
TEST_F(IntakeTest, WorksWhenOpen) {
|
||||
intake.Deploy();
|
||||
intake.Activate(0.5);
|
||||
EXPECT_DOUBLE_EQ(0.5, simMotor.GetSpeed());
|
||||
}
|
||||
|
||||
TEST_F(IntakeTest, Retract) {
|
||||
intake.Retract();
|
||||
EXPECT_EQ(frc::DoubleSolenoid::Value::kReverse, simPiston.Get());
|
||||
}
|
||||
|
||||
TEST_F(IntakeTest, Deploy) {
|
||||
intake.Deploy();
|
||||
EXPECT_EQ(frc::DoubleSolenoid::Value::kForward, simPiston.Get());
|
||||
}
|
||||
Reference in New Issue
Block a user