[wpiutil] Refactor SpanMatcher and TestPrinters from ntcore (#5658)

This commit is contained in:
Peter Johnson
2023-09-18 00:11:36 -07:00
committed by GitHub
parent 9fe258427a
commit 7be290147c
14 changed files with 86 additions and 45 deletions

View File

@@ -91,5 +91,5 @@ target_link_libraries(ntcoredev ntcore)
if (WITH_TESTS)
wpilib_add_test(ntcore src/test/native/cpp)
target_include_directories(ntcore_test PRIVATE src/main/native/cpp)
target_link_libraries(ntcore_test ntcore gmock_main)
target_link_libraries(ntcore_test ntcore gmock_main wpiutil_testlib)
endif()

View File

@@ -3,12 +3,12 @@
// the WPILib BSD license file in the root directory of this project.
#include <gtest/gtest.h>
#include <wpi/SpanMatcher.h>
#include "LocalStorage.h"
#include "MockListenerStorage.h"
#include "MockLogger.h"
#include "PubSubOptionsMatcher.h"
#include "SpanMatcher.h"
#include "TestPrinters.h"
#include "ValueMatcher.h"
#include "gmock/gmock.h"

View File

@@ -4,8 +4,6 @@
#include "TestPrinters.h"
#include <wpi/json.h>
#include "Handle.h"
#include "PubSubOptions.h"
#include "net/Message.h"
@@ -13,12 +11,6 @@
#include "networktables/NetworkTableValue.h"
#include "ntcore_cpp.h"
namespace wpi {
void PrintTo(const json& val, ::std::ostream* os) {
*os << val.dump();
}
} // namespace wpi
namespace nt {
void PrintTo(const Event& event, std::ostream* os) {

View File

@@ -10,33 +10,7 @@
#include <string_view>
#include <gtest/gtest.h>
namespace wpi {
class json;
inline void PrintTo(std::string_view str, ::std::ostream* os) {
::testing::internal::PrintStringTo(std::string{str}, os);
}
template <typename T>
void PrintTo(std::span<T> val, ::std::ostream* os) {
*os << '{';
bool first = true;
for (auto v : val) {
if (first) {
first = false;
} else {
*os << ", ";
}
*os << ::testing::PrintToString(v);
}
*os << '}';
}
void PrintTo(const json& val, ::std::ostream* os);
} // namespace wpi
#include <wpi/TestPrinters.h>
namespace nt {

View File

@@ -10,10 +10,10 @@
#include <vector>
#include <gtest/gtest.h>
#include <wpi/SpanMatcher.h>
#include "../MockLogger.h"
#include "../PubSubOptionsMatcher.h"
#include "../SpanMatcher.h"
#include "../TestPrinters.h"
#include "../ValueMatcher.h"
#include "Handle.h"

View File

@@ -8,10 +8,10 @@
#include <vector>
#include <gtest/gtest.h>
#include <wpi/SpanMatcher.h>
#include <wpi/json.h>
#include <wpi/raw_ostream.h>
#include "../SpanMatcher.h"
#include "../TestPrinters.h"
#include "Handle.h"
#include "PubSubOptions.h"

View File

@@ -10,8 +10,8 @@
#include <string_view>
#include <gtest/gtest.h>
#include <wpi/SpanMatcher.h>
#include "../SpanMatcher.h"
#include "../TestPrinters.h"
#include "../ValueMatcher.h"
#include "gmock/gmock.h"

View File

@@ -9,9 +9,9 @@
#include <vector>
#include <gtest/gtest.h>
#include <wpi/SpanMatcher.h>
#include <wpi/raw_ostream.h>
#include "../SpanMatcher.h"
#include "../TestPrinters.h"
#include "net3/Message3.h"
#include "net3/WireEncoder3.h"

View File

@@ -91,6 +91,19 @@ model {
}
}
}
"${nativeName}TestLib"(NativeLibrarySpec) {
sources {
cpp {
source {
srcDirs 'src/testlib/native/cpp'
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/test/native/include'
}
}
}
}
}
testSuites {
"${nativeName}Test"(GoogleTestTestSuiteSpec) {
@@ -116,8 +129,10 @@ model {
binaries {
withType(GoogleTestTestSuiteBinarySpec) {
lib library: nativeName, linkage: 'shared'
lib library: "${nativeName}TestLib", linkage: 'shared'
if (!project.hasProperty('noWpiutil')) {
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutilTestLib', linkage: 'shared'
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(it, 'ni_link_libraries', 'ni_runtime_libraries')
}

View File

@@ -225,6 +225,19 @@ model {
}
}
}
"${nativeName}TestLib"(NativeLibrarySpec) {
sources {
cpp {
source {
srcDirs 'src/testlib/native/cpp'
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/test/native/include'
}
}
}
}
}
testSuites {
"${nativeName}Test"(GoogleTestTestSuiteSpec) {
@@ -255,6 +268,7 @@ model {
lib library: nativeName, linkage: 'shared'
if (!project.hasProperty('noWpiutil')) {
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutilTestLib', linkage: 'shared'
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(it, 'ni_link_libraries', 'ni_runtime_libraries')
}

View File

@@ -41,6 +41,7 @@ repoRootNameOverride {
includeOtherLibs {
^fmt/
^gmock/
^gtest/
}

View File

@@ -189,7 +189,10 @@ foreach(example ${wpiutil_examples})
endforeach()
if (WITH_TESTS)
file(GLOB_RECURSE wpiutil_testlib_src src/test/native/include/*.h)
add_library(wpiutil_testlib INTERFACE ${wpiutil_test_src})
target_include_directories(wpiutil_testlib INTERFACE src/test/native/include)
wpilib_add_test(wpiutil src/test/native/cpp)
target_include_directories(wpiutil_test PRIVATE src/test/native/include)
target_link_libraries(wpiutil_test wpiutil gmock_main)
target_link_libraries(wpiutil_test wpiutil gmock_main wpiutil_testlib)
endif()

View File

@@ -13,8 +13,9 @@
#include <utility>
#include <vector>
#include "TestPrinters.h"
#include "gmock/gmock.h"
#include <gmock/gmock.h>
#include "wpi/TestPrinters.h"
namespace wpi {

View File

@@ -0,0 +1,41 @@
// 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.
#pragma once
#include <ostream>
#include <span>
#include <string>
#include <string_view>
#include <gtest/gtest.h>
#include "wpi/json.h"
namespace wpi {
inline void PrintTo(std::string_view str, ::std::ostream* os) {
::testing::internal::PrintStringTo(std::string{str}, os);
}
template <typename T>
void PrintTo(std::span<T> val, ::std::ostream* os) {
*os << '{';
bool first = true;
for (auto v : val) {
if (first) {
first = false;
} else {
*os << ", ";
}
*os << ::testing::PrintToString(v);
}
*os << '}';
}
inline void PrintTo(const json& val, ::std::ostream* os) {
*os << val.dump();
}
} // namespace wpi