在 TYPED_TEST 中获取 TypeParam()
Obtain TypeParam() inside the TYPED_TEST
我有一个模板化的 class,它将 data type T
和 size_t size
作为模板化参数,并为客户端 class 编写了一些单元测试。我在两个不同的地方重复相同类型的代码,一个在 fixture 上,另一个在 TYPED_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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
在 TYPED_TEST - ReturnTrueWhenComparedWithSameClientID
using T = typename std::tuple_element<0, decltype(TypeParam())>::type;
static constexpr std::size_t size = std::tuple_element_t<1, decltype(TypeParam())>::value;
using ClientT = typename Client<T, size>::ClientT; // std::array
客户
template <typename T, size_t size>
class Client {
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 { return ID; }
inline bool isID(const ClientT& anotherID) const { return ID == anotherID; }
inline bool operator==(const Client& anotherClient) { return ID == anotherClient.getID(); }
private:
ClientT ID;
};
单元测试文件如下所示
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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
protected:
ClientTest() : id(ClientT{1}), client(std::make_unique<Client<T, size>>(id)) {}
~ClientTest() override {}
void setClient(const ClientT& id) { client.reset(new Client<T, size>(id)); }
ClientT id;
std::unique_ptr<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, ReturnTrueWhenComparedWithSameClientID) {
using T = typename std::tuple_element<0, decltype(TypeParam())>::type;
static constexpr std::size_t size = std::tuple_element_t<1, decltype(TypeParam())>::value;
using ClientT = typename Client<T, size>::ClientT; // std::array
ClientT new_id{15};
this->setClient(new_id);
EXPECT_EQ(this->client->getID(), new_id);
EXPECT_TRUE(this->client->isID(new_id));
}
到目前为止,测试正在编译并且看起来不错。但是我不喜欢将相同的代码块重复两次
我想知道是否有一种方法可以将数据类型和大小从夹具继承到 TYPED_TEST 或比现有的更好的东西。
谢谢
你可以做的是 在夹具中创建别名 ClientT
ClientTest
public
template <typename Tup>
class ClientTest : public testing::Test {
public:
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 Client<T, size>::ClientT;
protected:
// ...
};
然后在 TYPED_TEST
中通过 ClientTest<Tup>::ClientT
访问它(模板参数 Tup
由 TypeParam()
给出)
using ClientT = typename ClientTest<decltype(TypeParam())>::ClientT;
或从 this
指针 获取 模板化 class 类型,如下所示:
using ClientT = typename std::decay_t<decltype(*this)>::ClientT
如果你像上面的代码一样只需要一次,你甚至可以只使用
typename std::decay_t<decltype(*this)>::ClientT new_id{15};
参考gtest
后的documentation
类型别名(使用或 typedef)在 TestFixturepublic
template <typename Tup>
class ClientTest : public testing::Test {
public:
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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
protected:
ClientTest() : id(ClientT{1}), client(std::make_unique<Client<T, size>>(id)) {}
~ClientTest() override {}
void setClient(const ClientT& id) { client.reset(new Client<T, size>(id)); }
ClientT id;
std::unique_ptr<Client<T, size>> client;
};
在TYPED_TEST。类型别名(使用或 typedef)从 TestFixture 范围解析 (TestFixture::)
TYPED_TEST(ClientTest, ReturnTrueWhenComparedWithSameClientID) {
typename TestFixture::ClientT id{15};
}
我有一个模板化的 class,它将 data type T
和 size_t size
作为模板化参数,并为客户端 class 编写了一些单元测试。我在两个不同的地方重复相同类型的代码,一个在 fixture 上,另一个在 TYPED_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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
在 TYPED_TEST - ReturnTrueWhenComparedWithSameClientID
using T = typename std::tuple_element<0, decltype(TypeParam())>::type;
static constexpr std::size_t size = std::tuple_element_t<1, decltype(TypeParam())>::value;
using ClientT = typename Client<T, size>::ClientT; // std::array
客户
template <typename T, size_t size>
class Client {
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 { return ID; }
inline bool isID(const ClientT& anotherID) const { return ID == anotherID; }
inline bool operator==(const Client& anotherClient) { return ID == anotherClient.getID(); }
private:
ClientT ID;
};
单元测试文件如下所示
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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
protected:
ClientTest() : id(ClientT{1}), client(std::make_unique<Client<T, size>>(id)) {}
~ClientTest() override {}
void setClient(const ClientT& id) { client.reset(new Client<T, size>(id)); }
ClientT id;
std::unique_ptr<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, ReturnTrueWhenComparedWithSameClientID) {
using T = typename std::tuple_element<0, decltype(TypeParam())>::type;
static constexpr std::size_t size = std::tuple_element_t<1, decltype(TypeParam())>::value;
using ClientT = typename Client<T, size>::ClientT; // std::array
ClientT new_id{15};
this->setClient(new_id);
EXPECT_EQ(this->client->getID(), new_id);
EXPECT_TRUE(this->client->isID(new_id));
}
到目前为止,测试正在编译并且看起来不错。但是我不喜欢将相同的代码块重复两次
我想知道是否有一种方法可以将数据类型和大小从夹具继承到 TYPED_TEST 或比现有的更好的东西。
谢谢
你可以做的是 在夹具中创建别名 ClientT
ClientTest
public
template <typename Tup>
class ClientTest : public testing::Test {
public:
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 Client<T, size>::ClientT;
protected:
// ...
};
然后在 TYPED_TEST
中通过 ClientTest<Tup>::ClientT
访问它(模板参数 Tup
由 TypeParam()
给出)
using ClientT = typename ClientTest<decltype(TypeParam())>::ClientT;
或从 this
指针 获取 模板化 class 类型,如下所示:
using ClientT = typename std::decay_t<decltype(*this)>::ClientT
如果你像上面的代码一样只需要一次,你甚至可以只使用
typename std::decay_t<decltype(*this)>::ClientT new_id{15};
参考gtest
后的documentation类型别名(使用或 typedef)在 TestFixturepublic
template <typename Tup>
class ClientTest : public testing::Test {
public:
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 Client<T, size>::ClientT; // ClientT = std::array<T, size>
protected:
ClientTest() : id(ClientT{1}), client(std::make_unique<Client<T, size>>(id)) {}
~ClientTest() override {}
void setClient(const ClientT& id) { client.reset(new Client<T, size>(id)); }
ClientT id;
std::unique_ptr<Client<T, size>> client;
};
在TYPED_TEST。类型别名(使用或 typedef)从 TestFixture 范围解析 (TestFixture::)
TYPED_TEST(ClientTest, ReturnTrueWhenComparedWithSameClientID) {
typename TestFixture::ClientT id{15};
}