Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用google test将typename和字符串传递给参数化测试_C++_Googletest - Fatal编程技术网

C++ 使用google test将typename和字符串传递给参数化测试

C++ 使用google test将typename和字符串传递给参数化测试,c++,googletest,C++,Googletest,是否有一种方法可以使用google的测试将类型和字符串传递给参数化测试 我想: template <typename T> class RawTypesTest : public ::testing::TestWithParam<const char * type> { protected: virtual void SetUp() { message = type; } }; TEST_P(RawTypesTest, Foo) { ASSE

是否有一种方法可以使用google的测试将类型和字符串传递给参数化测试

我想:

template <typename T>
class RawTypesTest : public ::testing::TestWithParam<const char * type> {
protected:
  virtual void SetUp() {
       message = type;
  }
}; 

TEST_P(RawTypesTest, Foo) {
  ASSERT_STREQ(message, type);
  ParamType * data = ..;
  ...
}
模板
类RawTypesTest:public::testing::TestWithParam{
受保护的:
虚拟空间设置(){
消息=类型;
}
}; 
测试P(RawTypesTest,Foo){
断言(消息、类型);
参数类型*数据=。。;
...
}

提前感谢

值参数化测试不适用于传递类型信息;您只能通过类型化或类型参数化测试来实现这一点。在这两种情况下,您都必须将类型和字符串信息打包到特殊的结构中。以下是如何通过以下方式完成:

然后,参数结构的定义变得更短:

MY_PARAM_TYPE(TypeAndString1, Type1, "my string 1");
MY_PARAM_TYPE(TypeAndString2, Type2, "my string 2");

这是相当复杂的,但没有简单的方法来做到这一点。我最好的建议是尝试重新考虑您的测试,以避免同时需要类型和值信息。但是如果你必须的话,这里是方法。

我没有研究类型化的宏家族在内部做什么,但是我发现使用它们对于目标来说非常复杂。只需将参数编码为类型的一部分,就可以实现相同的功能

#define TSLP(name, value) \
struct Tslp##name \
{ \
   std::string operator()() \
   { \
      return value; \
   } \
}

TSLP(Empty, "");
TSLP(Foo, "foo");
template<class Type, class Param>
class ParamTextFixture : public Type {
    static std::string message() {
        return Param()();
    }
}
#定义TSLP(名称、值)\
结构Tslp##名称\
{ \
std::字符串运算符()\
{ \
返回值\
} \
}
TSLP(空,“”);
TSLP(Foo,“Foo”);
模板
类ParamTextFixture:公共类型{
静态std::string消息(){
返回参数();
}
}
然后,您只需要在类型中使用ParamTextFixture替换类

typedef ::testing::Types<
    ParamTextFixture<MyClass, TslpEmpty>
    , ParamTextFixture<MyClass, TslpFoo> >
...
typedef::测试::类型<
参数固定装置
,ParamTextFixture>
...

TestParamgtest类吗?对不起,应该是TestWithParam
MY_PARAM_TYPE(TypeAndString1, Type1, "my string 1");
MY_PARAM_TYPE(TypeAndString2, Type2, "my string 2");
#define TSLP(name, value) \
struct Tslp##name \
{ \
   std::string operator()() \
   { \
      return value; \
   } \
}

TSLP(Empty, "");
TSLP(Foo, "foo");
template<class Type, class Param>
class ParamTextFixture : public Type {
    static std::string message() {
        return Param()();
    }
}
typedef ::testing::Types<
    ParamTextFixture<MyClass, TslpEmpty>
    , ParamTextFixture<MyClass, TslpFoo> >
...