From 7be290147cff1b495f66d60fa040f4fe82629e8b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 18 Sep 2023 00:11:36 -0700 Subject: [PATCH] [wpiutil] Refactor SpanMatcher and TestPrinters from ntcore (#5658) --- ntcore/CMakeLists.txt | 2 +- .../src/test/native/cpp/LocalStorageTest.cpp | 2 +- ntcore/src/test/native/cpp/TestPrinters.cpp | 8 ---- ntcore/src/test/native/cpp/TestPrinters.h | 28 +------------ .../test/native/cpp/net/ServerImplTest.cpp | 2 +- .../test/native/cpp/net/WireEncoderTest.cpp | 2 +- .../test/native/cpp/net3/WireDecoder3Test.cpp | 2 +- .../test/native/cpp/net3/WireEncoder3Test.cpp | 2 +- shared/javacpp/setupBuild.gradle | 15 +++++++ shared/jni/setupBuild.gradle | 14 +++++++ wpiutil/.styleguide | 1 + wpiutil/CMakeLists.txt | 7 +++- .../test/native/include/wpi}/SpanMatcher.h | 5 ++- .../test/native/include/wpi/TestPrinters.h | 41 +++++++++++++++++++ 14 files changed, 86 insertions(+), 45 deletions(-) rename {ntcore/src/test/native/cpp => wpiutil/src/test/native/include/wpi}/SpanMatcher.h (97%) create mode 100644 wpiutil/src/test/native/include/wpi/TestPrinters.h diff --git a/ntcore/CMakeLists.txt b/ntcore/CMakeLists.txt index 1a97ac0c07..ff2d403850 100644 --- a/ntcore/CMakeLists.txt +++ b/ntcore/CMakeLists.txt @@ -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() diff --git a/ntcore/src/test/native/cpp/LocalStorageTest.cpp b/ntcore/src/test/native/cpp/LocalStorageTest.cpp index 0aee590f54..64c1785388 100644 --- a/ntcore/src/test/native/cpp/LocalStorageTest.cpp +++ b/ntcore/src/test/native/cpp/LocalStorageTest.cpp @@ -3,12 +3,12 @@ // the WPILib BSD license file in the root directory of this project. #include +#include #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" diff --git a/ntcore/src/test/native/cpp/TestPrinters.cpp b/ntcore/src/test/native/cpp/TestPrinters.cpp index 66afe4e0d5..708573f74b 100644 --- a/ntcore/src/test/native/cpp/TestPrinters.cpp +++ b/ntcore/src/test/native/cpp/TestPrinters.cpp @@ -4,8 +4,6 @@ #include "TestPrinters.h" -#include - #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) { diff --git a/ntcore/src/test/native/cpp/TestPrinters.h b/ntcore/src/test/native/cpp/TestPrinters.h index ac0229d621..3481e762cf 100644 --- a/ntcore/src/test/native/cpp/TestPrinters.h +++ b/ntcore/src/test/native/cpp/TestPrinters.h @@ -10,33 +10,7 @@ #include #include - -namespace wpi { - -class json; - -inline void PrintTo(std::string_view str, ::std::ostream* os) { - ::testing::internal::PrintStringTo(std::string{str}, os); -} - -template -void PrintTo(std::span 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 namespace nt { diff --git a/ntcore/src/test/native/cpp/net/ServerImplTest.cpp b/ntcore/src/test/native/cpp/net/ServerImplTest.cpp index c8f876b162..6660c653a5 100644 --- a/ntcore/src/test/native/cpp/net/ServerImplTest.cpp +++ b/ntcore/src/test/native/cpp/net/ServerImplTest.cpp @@ -10,10 +10,10 @@ #include #include +#include #include "../MockLogger.h" #include "../PubSubOptionsMatcher.h" -#include "../SpanMatcher.h" #include "../TestPrinters.h" #include "../ValueMatcher.h" #include "Handle.h" diff --git a/ntcore/src/test/native/cpp/net/WireEncoderTest.cpp b/ntcore/src/test/native/cpp/net/WireEncoderTest.cpp index 44915d6398..222a5b7000 100644 --- a/ntcore/src/test/native/cpp/net/WireEncoderTest.cpp +++ b/ntcore/src/test/native/cpp/net/WireEncoderTest.cpp @@ -8,10 +8,10 @@ #include #include +#include #include #include -#include "../SpanMatcher.h" #include "../TestPrinters.h" #include "Handle.h" #include "PubSubOptions.h" diff --git a/ntcore/src/test/native/cpp/net3/WireDecoder3Test.cpp b/ntcore/src/test/native/cpp/net3/WireDecoder3Test.cpp index 23a9f889b8..74f0d5462b 100644 --- a/ntcore/src/test/native/cpp/net3/WireDecoder3Test.cpp +++ b/ntcore/src/test/native/cpp/net3/WireDecoder3Test.cpp @@ -10,8 +10,8 @@ #include #include +#include -#include "../SpanMatcher.h" #include "../TestPrinters.h" #include "../ValueMatcher.h" #include "gmock/gmock.h" diff --git a/ntcore/src/test/native/cpp/net3/WireEncoder3Test.cpp b/ntcore/src/test/native/cpp/net3/WireEncoder3Test.cpp index d284ff4577..de0593467e 100644 --- a/ntcore/src/test/native/cpp/net3/WireEncoder3Test.cpp +++ b/ntcore/src/test/native/cpp/net3/WireEncoder3Test.cpp @@ -9,9 +9,9 @@ #include #include +#include #include -#include "../SpanMatcher.h" #include "../TestPrinters.h" #include "net3/Message3.h" #include "net3/WireEncoder3.h" diff --git a/shared/javacpp/setupBuild.gradle b/shared/javacpp/setupBuild.gradle index 0c1ad31db6..6ea7d6f151 100644 --- a/shared/javacpp/setupBuild.gradle +++ b/shared/javacpp/setupBuild.gradle @@ -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') } diff --git a/shared/jni/setupBuild.gradle b/shared/jni/setupBuild.gradle index cec30b7af3..8c2f4e929d 100644 --- a/shared/jni/setupBuild.gradle +++ b/shared/jni/setupBuild.gradle @@ -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') } diff --git a/wpiutil/.styleguide b/wpiutil/.styleguide index 9a2f4376f6..882d9588aa 100644 --- a/wpiutil/.styleguide +++ b/wpiutil/.styleguide @@ -41,6 +41,7 @@ repoRootNameOverride { includeOtherLibs { ^fmt/ + ^gmock/ ^gtest/ } diff --git a/wpiutil/CMakeLists.txt b/wpiutil/CMakeLists.txt index 015dc227cf..8e35001b57 100644 --- a/wpiutil/CMakeLists.txt +++ b/wpiutil/CMakeLists.txt @@ -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() diff --git a/ntcore/src/test/native/cpp/SpanMatcher.h b/wpiutil/src/test/native/include/wpi/SpanMatcher.h similarity index 97% rename from ntcore/src/test/native/cpp/SpanMatcher.h rename to wpiutil/src/test/native/include/wpi/SpanMatcher.h index 0d6c6b5065..247c142956 100644 --- a/ntcore/src/test/native/cpp/SpanMatcher.h +++ b/wpiutil/src/test/native/include/wpi/SpanMatcher.h @@ -13,8 +13,9 @@ #include #include -#include "TestPrinters.h" -#include "gmock/gmock.h" +#include + +#include "wpi/TestPrinters.h" namespace wpi { diff --git a/wpiutil/src/test/native/include/wpi/TestPrinters.h b/wpiutil/src/test/native/include/wpi/TestPrinters.h new file mode 100644 index 0000000000..72d7da37ca --- /dev/null +++ b/wpiutil/src/test/native/include/wpi/TestPrinters.h @@ -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 +#include +#include +#include + +#include + +#include "wpi/json.h" + +namespace wpi { + +inline void PrintTo(std::string_view str, ::std::ostream* os) { + ::testing::internal::PrintStringTo(std::string{str}, os); +} + +template +void PrintTo(std::span 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