Add tests for SendableChooser and Command Sendable functionality (#5179)

This commit is contained in:
Starlight220
2023-06-23 18:18:38 +03:00
committed by GitHub
parent 663bf25aaf
commit 89fc51f0d4
4 changed files with 344 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
// 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 <frc/smartdashboard/SendableChooser.h>
#include <frc/smartdashboard/SmartDashboard.h>
#include <string>
#include <networktables/NetworkTableInstance.h>
#include "gtest/gtest.h"
class SendableChooserTest : public ::testing::TestWithParam<int> {};
TEST_P(SendableChooserTest, ReturnsSelected) {
frc::SendableChooser<int> chooser;
for (int i = 1; i <= 3; i++) {
chooser.AddOption(std::to_string(i), i);
}
chooser.SetDefaultOption("0", 0);
auto pub = nt::NetworkTableInstance::GetDefault()
.GetStringTopic("/SmartDashboard/chooser/selected")
.Publish();
frc::SmartDashboard::PutData("chooser", &chooser);
frc::SmartDashboard::UpdateValues();
pub.Set(std::to_string(GetParam()));
frc::SmartDashboard::UpdateValues();
EXPECT_EQ(GetParam(), chooser.GetSelected());
}
TEST(SendableChooserTest, DefaultIsReturnedOnNoSelect) {
frc::SendableChooser<int> chooser;
for (int i = 1; i <= 3; i++) {
chooser.AddOption(std::to_string(i), i);
}
// Use 4 here rather than 0 to make sure it's not default-init int.
chooser.SetDefaultOption("4", 4);
EXPECT_EQ(4, chooser.GetSelected());
}
TEST(SendableChooserTest,
DefaultConstructableIsReturnedOnNoSelectAndNoDefault) {
frc::SendableChooser<int> chooser;
for (int i = 1; i <= 3; i++) {
chooser.AddOption(std::to_string(i), i);
}
EXPECT_EQ(0, chooser.GetSelected());
}
INSTANTIATE_TEST_SUITE_P(SendableChooserTests, SendableChooserTest,
::testing::Values(0, 1, 2, 3));