From 18675d6e1cae42ce79ac7d6d993a76e6ba6a6f7f Mon Sep 17 00:00:00 2001 From: Gold856 <117957790+Gold856@users.noreply.github.com> Date: Fri, 19 Jan 2024 08:45:02 -0500 Subject: [PATCH] No op if real, use SendableChooserSim in existing tests --- .../cpp/simulation/SendableChooserSim.cpp | 4 ++- .../frc/simulation/SendableChooserSim.h | 17 +++++++++++ .../smartdashboard/SendableChooserTest.cpp | 28 ++----------------- .../simulation/SendableChooserSim.java | 15 ++++++++-- .../smartdashboard/SendableChooserTest.java | 9 +++--- 5 files changed, 39 insertions(+), 34 deletions(-) diff --git a/wpilibc/src/main/native/cpp/simulation/SendableChooserSim.cpp b/wpilibc/src/main/native/cpp/simulation/SendableChooserSim.cpp index c114293904f..bb19ff65b05 100644 --- a/wpilibc/src/main/native/cpp/simulation/SendableChooserSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/SendableChooserSim.cpp @@ -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); + } } diff --git a/wpilibc/src/main/native/include/frc/simulation/SendableChooserSim.h b/wpilibc/src/main/native/include/frc/simulation/SendableChooserSim.h index bcb619fa5d4..49c8c4a12d8 100644 --- a/wpilibc/src/main/native/include/frc/simulation/SendableChooserSim.h +++ b/wpilibc/src/main/native/include/frc/simulation/SendableChooserSim.h @@ -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: diff --git a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp index 0d927b93fd9..7ff86fc34ef 100644 --- a/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp +++ b/wpilibc/src/test/native/cpp/smartdashboard/SendableChooserTest.cpp @@ -17,22 +17,18 @@ class SendableChooserTest : public ::testing::TestWithParam {}; TEST_P(SendableChooserTest, ReturnsSelected) { frc::SendableChooser 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()); } @@ -78,23 +74,5 @@ TEST(SendableChooserTest, ChangeListener) { EXPECT_EQ(3, currentVal); } -TEST_P(SendableChooserTest, SetSelected) { - frc::SendableChooser 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)); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SendableChooserSim.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SendableChooserSim.java index 75e3139cf71..acf527ea8a1 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SendableChooserSim.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/simulation/SendableChooserSim.java @@ -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) { @@ -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); + } } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java index 270e520239b..d01af545712 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SendableChooserTest.java @@ -29,10 +29,9 @@ void setUp() { @ParameterizedTest void returnsSelected(int toSelect) { try (var chooser = new SendableChooser(); - 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); } @@ -40,7 +39,7 @@ void returnsSelected(int toSelect) { SmartDashboard.putData("returnsSelectedChooser" + toSelect, chooser); SmartDashboard.updateValues(); - publisher.set(String.valueOf(toSelect)); + chooserSim.setSelected(String.valueOf(toSelect)); SmartDashboard.updateValues(); assertEquals(toSelect, chooser.getSelected()); }