Skip to content

Commit

Permalink
No op if real, use SendableChooserSim in existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Jan 19, 2024
1 parent f20a408 commit 18675d6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ SendableChooserSim::SendableChooserSim(nt::NetworkTableInstance inst,
}

void SendableChooserSim::SetSelected(std::string_view option) {
m_publisher.Set(option);
if (RobotBase::IsSimulation()) {
m_publisher.Set(option);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,25 @@ namespace frc::sim {

class SendableChooserSim {
public:
/**
* Constructs a SendableChooserSim.
*
* @param path The path where the SendableChooser is published.
*/
explicit SendableChooserSim(std::string_view path);

/**
* Constructs a SendableChooserSim.
*
* @param inst The NetworkTables instance.
* @param path The path where the SendableChooser is published.
*/
SendableChooserSim(nt::NetworkTableInstance inst, std::string_view path);

/**
* Set the selected option.
* @param option The option.
*/
void SetSelected(std::string_view option);

private:
Expand Down
28 changes: 3 additions & 25 deletions wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ class SendableChooserTest : public ::testing::TestWithParam<int> {};

TEST_P(SendableChooserTest, ReturnsSelected) {
frc::SendableChooser<int> chooser;
frc::sim::SendableChooserSim chooserSim = frc::sim::SendableChooserSim{
fmt::format("/SmartDashboard/ReturnsSelectedChooser{}/", GetParam())};

for (int i = 1; i <= 3; i++) {
chooser.AddOption(std::to_string(i), i);
}
chooser.SetDefaultOption("0", 0);

auto pub =
nt::NetworkTableInstance::GetDefault()
.GetStringTopic(fmt::format(
"/SmartDashboard/ReturnsSelectedChooser{}/selected", GetParam()))
.Publish();

frc::SmartDashboard::PutData(
fmt::format("ReturnsSelectedChooser{}", GetParam()), &chooser);
frc::SmartDashboard::UpdateValues();
pub.Set(std::to_string(GetParam()));
chooserSim.SetSelected(std::to_string(GetParam()));
frc::SmartDashboard::UpdateValues();
EXPECT_EQ(GetParam(), chooser.GetSelected());
}
Expand Down Expand Up @@ -78,23 +74,5 @@ TEST(SendableChooserTest, ChangeListener) {
EXPECT_EQ(3, currentVal);
}

TEST_P(SendableChooserTest, SetSelected) {
frc::SendableChooser<int> chooser;
frc::sim::SendableChooserSim chooserSim = frc::sim::SendableChooserSim{
fmt::format("/SmartDashboard/SetSelectedChooser{}/", GetParam())};

for (int i = 1; i <= 3; i++) {
chooser.AddOption(std::to_string(i), i);
}
chooser.SetDefaultOption("0", 0);

frc::SmartDashboard::PutData(fmt::format("SetSelectedChooser{}", GetParam()),
&chooser);
frc::SmartDashboard::UpdateValues();
chooserSim.SetSelected(std::to_string(GetParam()));
frc::SmartDashboard::UpdateValues();
EXPECT_EQ(GetParam(), chooser.GetSelected());
}

INSTANTIATE_TEST_SUITE_P(SendableChooserTests, SendableChooserTest,
::testing::Values(0, 1, 2, 3));
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public SendableChooserSim(String path) {
/**
* Constructs a SendableChooserSim.
*
* @param inst The NetworkTable instance.
* @param inst The NetworkTables instance.
* @param path The path where the SendableChooser is published.
*/
public SendableChooserSim(NetworkTableInstance inst, String path) {
Expand All @@ -35,10 +35,19 @@ public SendableChooserSim(NetworkTableInstance inst, String path) {

@Override
public void close() {
m_publisher.close();
if (RobotBase.isSimulation()) {
m_publisher.close();
}
}

/**
* Set the selected option.
*
* @param option The option.
*/
public void setSelected(String option) {
m_publisher.set(option);
if (RobotBase.isSimulation()) {
m_publisher.set(option);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ void setUp() {
@ParameterizedTest
void returnsSelected(int toSelect) {
try (var chooser = new SendableChooser<Integer>();
var publisher =
m_inst
.getStringTopic("/SmartDashboard/returnsSelectedChooser" + toSelect + "/selected")
.publish()) {
var chooserSim =
new SendableChooserSim(
m_inst, "/SmartDashboard/returnsSelectedChooser" + toSelect + "/")) {
for (int i = 1; i <= 3; i++) {
chooser.addOption(String.valueOf(i), i);
}
chooser.setDefaultOption(String.valueOf(0), 0);

SmartDashboard.putData("returnsSelectedChooser" + toSelect, chooser);
SmartDashboard.updateValues();
publisher.set(String.valueOf(toSelect));
chooserSim.setSelected(String.valueOf(toSelect));
SmartDashboard.updateValues();
assertEquals(toSelect, chooser.getSelected());
}
Expand Down

0 comments on commit 18675d6

Please sign in to comment.