mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpiutil] Refactor SpanMatcher and TestPrinters from ntcore (#5658)
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
// 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 <algorithm>
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <span>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "TestPrinters.h"
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
namespace wpi {
|
||||
|
||||
template <typename T>
|
||||
class SpanMatcher : public ::testing::MatcherInterface<std::span<T>> {
|
||||
public:
|
||||
explicit SpanMatcher(std::span<T> good_) : good{good_.begin(), good_.end()} {}
|
||||
|
||||
bool MatchAndExplain(std::span<T> val,
|
||||
::testing::MatchResultListener* listener) const override;
|
||||
void DescribeTo(::std::ostream* os) const override;
|
||||
void DescribeNegationTo(::std::ostream* os) const override;
|
||||
|
||||
private:
|
||||
std::vector<std::remove_cv_t<T>> good;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline ::testing::Matcher<std::span<const typename T::value_type>> SpanEq(
|
||||
const T& good) {
|
||||
return ::testing::MakeMatcher(
|
||||
new SpanMatcher(std::span<const typename T::value_type>(good)));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline ::testing::Matcher<std::span<const T>> SpanEq(
|
||||
std::initializer_list<const T> good) {
|
||||
return ::testing::MakeMatcher(
|
||||
new SpanMatcher<const T>({good.begin(), good.end()}));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool SpanMatcher<T>::MatchAndExplain(
|
||||
std::span<T> val, ::testing::MatchResultListener* listener) const {
|
||||
if (val.size() != good.size() ||
|
||||
!std::equal(val.begin(), val.end(), good.begin())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SpanMatcher<T>::DescribeTo(::std::ostream* os) const {
|
||||
PrintTo(std::span<T>{good}, os);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SpanMatcher<T>::DescribeNegationTo(::std::ostream* os) const {
|
||||
*os << "is not equal to ";
|
||||
PrintTo(std::span<T>{good}, os);
|
||||
}
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
inline std::span<const uint8_t> operator"" _us(const char* str, size_t len) {
|
||||
return {reinterpret_cast<const uint8_t*>(str), len};
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user