c++ - TYPED_TEST 用于类模板
问题描述
我正在尝试为名为 Client 的模板类编写单元测试。无法成功编译 unitTests 代码。不确定如何在单元测试中同时传递 theclass / typename T
和参数。如果您希望在本地机器上编译并查看问题,size_t size
我已经制作了一个 git repo 。git clone https://github.com/BhanuKiranChaluvadi/gtest-minimal-example.git
#pragma once
#include <array>
namespace inputs
{
template <typename T, size_t size>
class IClient
{
public:
using ClientType = std::array<T, size>;
virtual const ClientType &getID() const = 0;
virtual bool isID(const ClientType &anotherID) const = 0;
};
template <typename T, size_t size>
class Client : public IClient<T, size>
{
public:
using ClientT = std::array<T, size>;
Client(const ClientT &ID) : ID(ID) {}
Client(const ClientT &&ID) : ID(std::move(ID)) {}
inline const ClientT &getID() const override { return ID; }
inline bool isID(const ClientT &anotherID) const override { return ID == anotherID; }
inline bool operator==(const Client &anotherClient) { return ID == anotherClient.getID(); }
private:
ClientT ID;
};
} // namespace inputs
这就是我的谷歌测试的样子
#include "gtest/gtest.h"
#include <type_traits>
#include "client.hpp"
#include <memory>
#include <utility>
#include <tuple>
using namespace inputs;
namespace inputs_test
{
template <class T, size_t size>
class ClientTest : public testing::Test
{
};
using testing::Types;
// The list of types we want to test.
typedef Types<std::pair<int8_t, std::integral_constant<std::size_t, 16>>,
std::pair<uint8_t, std::integral_constant<std::size_t, 24>>>
Implementations;
TYPED_TEST_CASE(ClientTest, Implementations);
TYPED_TEST(ClientTest, shouldReturnSetID)
{
typename TypeParam::first_type data_type;
typename TypeParam::second_type size;
// static constexpr std::size_t n = TypeParam::value;
EXPECT_TRUE(true);
}
} // namespace inputs_test
模板化的 Client 类应为<int8_t, 16>
或<uint8_t, 24>
。我不确定如何传递size_t
模板化参数。
test/googletest-src/googletest/include/gtest/gtest-typed-test.h:174:38: error: wrong number of template arguments (1, should be 2)
174 | typedef CaseName<gtest_TypeParam_> TestFixture; \
解决方案
一种可能的解决方案是编写一个ClientTest
类,该类将std::tuple
(or std::pair
) 作为单个模板参数并将其拆分为两个模板参数T
,然后size
再次使用std::tuple_element
. 然后它实例Client<T, size>
化为类成员。然后可以使用它来执行所需的测试:
namespace inputs_test {
template <typename Tup>
class ClientTest: public testing::Test {
using T = typename std::tuple_element_t<0, Tup>;
static constexpr std::size_t size = std::tuple_element_t<1, Tup>::value;
using ClientT = typename inputs::Client<T, size>::ClientT;
protected:
ClientTest() noexcept
: id{new ClientT{}}, client{new inputs::Client<T, size>{*id}} {
return;
}
~ClientTest() noexcept {
delete id;
delete client;
}
ClientT* id;
inputs::Client<T, size>* client;
};
typedef testing::Types<std::tuple<std::int8_t, std::integral_constant<std::size_t, 16>>,
std::tuple<std::uint8_t, std::integral_constant<std::size_t, 24>>>
Implementations;
TYPED_TEST_CASE(ClientTest, Implementations);
TYPED_TEST(ClientTest, shouldReturnSetID) {
EXPECT_EQ(this->client->getID(), *this->id);
}
}
推荐阅读
- arduino - Beaglebone 黑色 UART
- swift - URL(fileURLWithPath: snap.urlImage) 命令未按预期工作
- jquery - 使用 JQuery 滑块未正确设置最小值最大值
- php - 当图像已上传到文件夹中时如何解决laravel中的“文件不存在或不可读”
- android - 无法在 IntentIntegrator 中添加Extra
- python - Python strptime 在数据框中给出 int 而不是 datetime 对象
- swift - 在 iOS13 中,是否允许在收到 VoIP Push 时报告 CXCallUpdate 或 CXEndCallAction?
- c# - 将 WPF 应用程序移植到 NetCore 3 时缺少对 MSTSCLib 的引用
- ruby-on-rails - 哎呀!!找不到资源项的方法 index.en
- google-cloud-platform - 是否可以查看具有特定权限的所有资源的列表?