Files
allwpilib/ntcore/src/test/native/cpp/TestPrinters.cpp

157 lines
4.1 KiB
C++
Raw Normal View History

Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
/*----------------------------------------------------------------------------*/
2018-05-13 17:09:56 -07:00
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
Implement independent instances. Previously, most of the classes were implemented as singletons so only one instance was possible. This change adds an instance handle-based API. In Java, this API is located in a different package than the old API (edu.wpi.first.networktables). Backwards compatibility with ITable and the old NetworkTable API is largely maintained, but a handful of classes have moved to the new package in Java (ConnectionInfo and PersistentException), and the old JNI has been completed replaced. Also: - Move SetTeam implementation to Dispatcher. - Consistently pass time through Java and C++ Value API. - Rename nt_Value.h to NetworkTableValue.h for consistency with Java. - Improve documentation - Make C++ and Java APIs more consistent - Document RPC functions and support RPC in Java. - Add polling features for entry and connection listeners and use them to move callback threads to Java level. - Remove thread start and stop hooks (as polling is available). - Make Notifiers, RpcServer, Dispatcher, and Storage mockable. - Set NOTIFY_NEW on immediate entry notifications. - Make GetTable("/") and GetTable("") equivalent. - Generate local notification for flags update when loading persistent file. And many unit test updates/changes: - Use InitGoogleMock instead of InitGoogleTest in test main. - Move test printers to TestPrinter.h/cpp. - Provide printers for StringRef, EntryNotifier, and Handle. - StorageTest: Check notifications. - Add entry notifier unit tests. - Storage: Add test for incoming entry assignment. - Update connection listener tests. - Add entry listener unit tests. Fixes #11, #140, #189, #190, #192, #193, #221
2017-04-23 10:26:17 -07:00
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "TestPrinters.h"
#include "Handle.h"
#include "Message.h"
#include "networktables/NetworkTableValue.h"
#include "ntcore_cpp.h"
namespace nt {
void PrintTo(const EntryNotification& event, std::ostream* os) {
*os << "EntryNotification{listener=";
PrintTo(Handle{event.listener}, os);
*os << ", entry=";
PrintTo(Handle{event.entry}, os);
*os << ", name=\"" << event.name << "\", flags=" << event.flags << ", value=";
PrintTo(event.value, os);
*os << '}';
}
void PrintTo(const Handle& handle, std::ostream* os) {
*os << "Handle{";
switch (handle.GetType()) {
case Handle::kConnectionListener:
*os << "kConnectionListener";
break;
case Handle::kConnectionListenerPoller:
*os << "kConnectionListenerPoller";
break;
case Handle::kEntry:
*os << "kEntry";
break;
case Handle::kEntryListener:
*os << "kEntryListener";
break;
case Handle::kEntryListenerPoller:
*os << "kEntryListenerPoller";
break;
case Handle::kInstance:
*os << "kInstance";
break;
case Handle::kLogger:
*os << "kLogger";
break;
case Handle::kLoggerPoller:
*os << "kLoggerPoller";
break;
case Handle::kRpcCall:
*os << "kRpcCall";
break;
case Handle::kRpcCallPoller:
*os << "kRpcCallPoller";
break;
default:
*os << "UNKNOWN";
break;
}
*os << ", " << handle.GetInst() << ", " << handle.GetIndex() << '}';
}
void PrintTo(const Message& msg, std::ostream* os) {
*os << "Message{";
switch (msg.type()) {
case Message::kKeepAlive:
*os << "kKeepAlive";
break;
case Message::kClientHello:
*os << "kClientHello";
break;
case Message::kProtoUnsup:
*os << "kProtoUnsup";
break;
case Message::kServerHelloDone:
*os << "kServerHelloDone";
break;
case Message::kServerHello:
*os << "kServerHello";
break;
case Message::kClientHelloDone:
*os << "kClientHelloDone";
break;
case Message::kEntryAssign:
*os << "kEntryAssign";
break;
case Message::kEntryUpdate:
*os << "kEntryUpdate";
break;
case Message::kFlagsUpdate:
*os << "kFlagsUpdate";
break;
case Message::kEntryDelete:
*os << "kEntryDelete";
break;
case Message::kClearEntries:
*os << "kClearEntries";
break;
case Message::kExecuteRpc:
*os << "kExecuteRpc";
break;
case Message::kRpcResponse:
*os << "kRpcResponse";
break;
default:
*os << "UNKNOWN";
break;
}
*os << ": str=\"" << msg.str() << "\", id=" << msg.id()
<< ", flags=" << msg.flags() << ", seq_num_uid=" << msg.seq_num_uid()
<< ", value=";
PrintTo(msg.value(), os);
*os << '}';
}
void PrintTo(const Value& value, std::ostream* os) {
*os << "Value{";
switch (value.type()) {
case NT_UNASSIGNED:
break;
case NT_BOOLEAN:
*os << (value.GetBoolean() ? "true" : "false");
break;
case NT_DOUBLE:
*os << value.GetDouble();
break;
case NT_STRING:
*os << '"' << value.GetString().str() << '"';
break;
case NT_RAW:
*os << ::testing::PrintToString(value.GetRaw());
break;
case NT_BOOLEAN_ARRAY:
*os << ::testing::PrintToString(value.GetBooleanArray());
break;
case NT_DOUBLE_ARRAY:
*os << ::testing::PrintToString(value.GetDoubleArray());
break;
case NT_STRING_ARRAY:
*os << ::testing::PrintToString(value.GetStringArray());
break;
case NT_RPC:
*os << ::testing::PrintToString(value.GetRpc());
break;
default:
*os << "UNKNOWN TYPE " << value.type();
break;
}
*os << '}';
}
} // namespace nt