TYPED_TEST 对于 class 模板 <typename T, size_t size>

TYPED_TEST for a class template <typename T, size_t size>

我正在尝试为模板化的 class 命名客户端编写单元测试。无法成功编译 unitTests 代码。不确定如何在单元测试中同时传递 class / typename Tsize_t size 参数。我做了一个 git 回购 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

这就是我的 google 测试的样子

#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

模板化客户端 class 需要 <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 class,它将 std::tuple(或 std::pair)作为单个模板参数并将其拆分为两个模板参数 Tsize 再次使用 std::tuple_element。然后它将 Client<T, size> 实例化为 class 成员。然后可以使用它来执行所需的测试:

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);
  }
}