mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[ntcore] NetworkTables 4 (#3217)
This commit is contained in:
@@ -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 <networktables/BooleanTopic.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
|
||||
#include "frc/event/EventLoop.h"
|
||||
#include "frc/event/NetworkBooleanEvent.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
class NetworkBooleanEventTest : public ::testing::Test {
|
||||
public:
|
||||
NetworkBooleanEventTest() {
|
||||
m_inst = nt::NetworkTableInstance::Create();
|
||||
m_inst.StartLocal();
|
||||
}
|
||||
|
||||
~NetworkBooleanEventTest() override {
|
||||
nt::NetworkTableInstance::Destroy(m_inst);
|
||||
}
|
||||
|
||||
nt::NetworkTableInstance m_inst;
|
||||
};
|
||||
|
||||
TEST_F(NetworkBooleanEventTest, Set) {
|
||||
EventLoop loop;
|
||||
int counter = 0;
|
||||
|
||||
auto pub = m_inst.GetTable("TestTable")->GetBooleanTopic("Test").Publish();
|
||||
|
||||
NetworkBooleanEvent(&loop, m_inst, "TestTable", "Test").IfHigh([&] {
|
||||
++counter;
|
||||
});
|
||||
pub.Set(false);
|
||||
loop.Poll();
|
||||
EXPECT_EQ(0, counter);
|
||||
pub.Set(true);
|
||||
loop.Poll();
|
||||
EXPECT_EQ(1, counter);
|
||||
pub.Set(false);
|
||||
loop.Poll();
|
||||
EXPECT_EQ(1, counter);
|
||||
}
|
||||
@@ -25,14 +25,14 @@ TEST(ShuffleboardInstanceTest, PathFluent) {
|
||||
NTWrapper ntInst;
|
||||
frc::detail::ShuffleboardInstance shuffleboardInst{ntInst.inst};
|
||||
|
||||
auto entry = shuffleboardInst.GetTab("Tab Title")
|
||||
.GetLayout("List", "List Layout")
|
||||
.Add("Data", "string")
|
||||
.WithWidget("Text View")
|
||||
.GetEntry();
|
||||
auto& entry = shuffleboardInst.GetTab("Tab Title")
|
||||
.GetLayout("List", "List Layout")
|
||||
.Add("Data", "string")
|
||||
.WithWidget("Text View")
|
||||
.GetEntry();
|
||||
|
||||
EXPECT_EQ("string", entry.GetString("")) << "Wrong entry value";
|
||||
EXPECT_EQ("/Shuffleboard/Tab Title/List/Data", entry.GetName())
|
||||
EXPECT_EQ("/Shuffleboard/Tab Title/List/Data", entry.GetTopic().GetName())
|
||||
<< "Entry path generated incorrectly";
|
||||
}
|
||||
|
||||
@@ -40,17 +40,17 @@ TEST(ShuffleboardInstanceTest, NestedLayoutsFluent) {
|
||||
NTWrapper ntInst;
|
||||
frc::detail::ShuffleboardInstance shuffleboardInst{ntInst.inst};
|
||||
|
||||
auto entry = shuffleboardInst.GetTab("Tab")
|
||||
.GetLayout("First", "List")
|
||||
.GetLayout("Second", "List")
|
||||
.GetLayout("Third", "List")
|
||||
.GetLayout("Fourth", "List")
|
||||
.Add("Value", "string")
|
||||
.GetEntry();
|
||||
auto& entry = shuffleboardInst.GetTab("Tab")
|
||||
.GetLayout("First", "List")
|
||||
.GetLayout("Second", "List")
|
||||
.GetLayout("Third", "List")
|
||||
.GetLayout("Fourth", "List")
|
||||
.Add("Value", "string")
|
||||
.GetEntry();
|
||||
|
||||
EXPECT_EQ("string", entry.GetString("")) << "Wrong entry value";
|
||||
EXPECT_EQ("/Shuffleboard/Tab/First/Second/Third/Fourth/Value",
|
||||
entry.GetName())
|
||||
entry.GetTopic().GetName())
|
||||
<< "Entry path generated incorrectly";
|
||||
}
|
||||
|
||||
@@ -64,11 +64,11 @@ TEST(ShuffleboardInstanceTest, NestedLayoutsOop) {
|
||||
frc::ShuffleboardLayout& third = second.GetLayout("Third", "List");
|
||||
frc::ShuffleboardLayout& fourth = third.GetLayout("Fourth", "List");
|
||||
frc::SimpleWidget& widget = fourth.Add("Value", "string");
|
||||
auto entry = widget.GetEntry();
|
||||
auto& entry = widget.GetEntry();
|
||||
|
||||
EXPECT_EQ("string", entry.GetString("")) << "Wrong entry value";
|
||||
EXPECT_EQ("/Shuffleboard/Tab/First/Second/Third/Fourth/Value",
|
||||
entry.GetName())
|
||||
entry.GetTopic().GetName())
|
||||
<< "Entry path generated incorrectly";
|
||||
}
|
||||
|
||||
@@ -97,12 +97,12 @@ TEST(ShuffleboardInstanceTest, NestedActuatorWidgetsAreDisabled) {
|
||||
// Note: we use the unsafe `GetBoolean()` method because if the value is NOT
|
||||
// a boolean, or if it is not present, then something has clearly gone very,
|
||||
// very wrong
|
||||
bool controllable = controllableEntry.GetValue()->GetBoolean();
|
||||
bool controllable = controllableEntry.GetValue().GetBoolean();
|
||||
// Sanity check
|
||||
EXPECT_TRUE(controllable)
|
||||
<< "The nested actuator widget should be enabled by default";
|
||||
shuffleboardInst.DisableActuatorWidgets();
|
||||
controllable = controllableEntry.GetValue()->GetBoolean();
|
||||
controllable = controllableEntry.GetValue().GetBoolean();
|
||||
EXPECT_FALSE(controllable)
|
||||
<< "The nested actuator widget should have been disabled";
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST_F(SuppliedValueWidgetTest, AddString) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/String");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
EXPECT_EQ("foo", entry.GetValue()->GetString());
|
||||
EXPECT_EQ("foo", entry.GetValue().GetString());
|
||||
}
|
||||
|
||||
TEST_F(SuppliedValueWidgetTest, AddNumber) {
|
||||
@@ -42,7 +42,7 @@ TEST_F(SuppliedValueWidgetTest, AddNumber) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Num");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
EXPECT_FLOAT_EQ(1.0, entry.GetValue()->GetDouble());
|
||||
EXPECT_FLOAT_EQ(1.0, entry.GetValue().GetDouble());
|
||||
}
|
||||
|
||||
TEST_F(SuppliedValueWidgetTest, AddBoolean) {
|
||||
@@ -51,7 +51,7 @@ TEST_F(SuppliedValueWidgetTest, AddBoolean) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Bool");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
EXPECT_EQ(true, entry.GetValue()->GetBoolean());
|
||||
EXPECT_EQ(true, entry.GetValue().GetBoolean());
|
||||
}
|
||||
|
||||
TEST_F(SuppliedValueWidgetTest, AddStringArray) {
|
||||
@@ -60,7 +60,7 @@ TEST_F(SuppliedValueWidgetTest, AddStringArray) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Strings");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
auto actual = entry.GetValue()->GetStringArray();
|
||||
auto actual = entry.GetValue().GetStringArray();
|
||||
|
||||
EXPECT_EQ(strings.size(), actual.size());
|
||||
for (size_t i = 0; i < strings.size(); i++) {
|
||||
@@ -74,7 +74,7 @@ TEST_F(SuppliedValueWidgetTest, AddNumberArray) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Numbers");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
auto actual = entry.GetValue()->GetDoubleArray();
|
||||
auto actual = entry.GetValue().GetDoubleArray();
|
||||
|
||||
EXPECT_EQ(nums.size(), actual.size());
|
||||
for (size_t i = 0; i < nums.size(); i++) {
|
||||
@@ -88,7 +88,7 @@ TEST_F(SuppliedValueWidgetTest, AddBooleanArray) {
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Booleans");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
auto actual = entry.GetValue()->GetBooleanArray();
|
||||
auto actual = entry.GetValue().GetBooleanArray();
|
||||
|
||||
EXPECT_EQ(bools.size(), actual.size());
|
||||
for (size_t i = 0; i < bools.size(); i++) {
|
||||
@@ -97,11 +97,11 @@ TEST_F(SuppliedValueWidgetTest, AddBooleanArray) {
|
||||
}
|
||||
|
||||
TEST_F(SuppliedValueWidgetTest, AddRaw) {
|
||||
std::string_view bytes = "\1\2\3";
|
||||
std::vector<uint8_t> bytes = {1, 2, 3};
|
||||
m_tab->AddRaw("Raw", [&bytes]() { return bytes; });
|
||||
auto entry = m_ntInst.inst.GetEntry("/Shuffleboard/Tab/Raw");
|
||||
|
||||
m_shuffleboardInst.Update();
|
||||
auto actual = entry.GetValue()->GetRaw();
|
||||
EXPECT_EQ(bytes, actual);
|
||||
auto actual = entry.GetValue().GetRaw();
|
||||
EXPECT_EQ(bytes, std::vector<uint8_t>(actual.begin(), actual.end()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user