【发布时间】:2021-09-15 23:08:53
【问题描述】:
我正在尝试为名为 Client 的模板类编写单元测试。无法成功编译 unitTests 代码。不确定如何在单元测试中同时传递 class / 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
模板化的客户端类应为<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; \
【问题讨论】:
-
你看过
TYPED_TEST宏解析到什么吗?另外,你能提供一个minimal reproducible example吗?再加上它产生的输出,这样人们就可以将错误消息与导致它的代码相匹配。顺便说一句:我认为重要的区别是您使用的是非类型模板参数(MCVE 将验证这一点)。如果我猜对了,请考虑将其添加到您的搜索和问题标题中。 -
@UlrichEckhardt 这是我正在使用的确切代码,只需在此处复制粘贴即可。我正在使用
Ubuntu 20.04和g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0,我从最新的大师那里编译了谷歌测试 -
我不怀疑它是从您的实际代码中复制而来的。但是,它仍然不完整,仍然分为两个文件,并且包含显然不需要的代码。确保我可以将其复制粘贴到文件中并查看完全相同的问题。
-
嗨@UlrichEckhardt 我在问题的描述中添加了一个github repo。您可以克隆并执行 cmake .and make。
标签: c++ unit-testing templates c++14 googletest