,c++,unit-testing,templates,c++14,googletest"/>

首页 > 解决方案 > 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; \

标签: c++unit-testingtemplatesc++14googletest

解决方案


一种可能的解决方案是编写一个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);
  }
}

推荐阅读