如何在<T>测试中实例化 MyType 中列出的 T 类 Foo 的对象?
How can I instantiate an object of class Foo<T> for Ts listed in MyTypes in a test?
我想用gtest测试一个模板类。我在谷歌测试手册中阅读了TYPED_TEST
,并查看了他们引用的官方示例,但仍然无法在我的测试中实例化模板类的对象。
template <typename T>
class Foo
{
public:
T data ;
};
在测试类中声明
typedef ::testing::Types<int, float> MyTypes ;
现在我如何在测试中为MyTypes
中列出的T
实例化类Foo<T>
的对象?
TYPED_TEST(TestFoo, test1)
{
Foo<T> object ;
object.data = 1.0 ;
ASSERT_FLOAT_EQ(object.data, 1.0) ;
}
在测试中,参考特殊名称TypeParam
来获取类型参数。你可以输入
TYPED_TEST(TestFoo, test1)
{
Foo<TypeParam> object ; // not Foo<T>
object.data = 1.0 ;
ASSERT_FLOAT_EQ(object.data, 1.0) ;
}
由于我无法自己弄清楚如何将TemplateRex的答案与Puchatek的原始代码片段结合起来,因此这里有一个完整的可编译示例。我通过阅读GTest自己的评论得到了这个,这很有帮助。
将以下内容放入test.cpp
:
#include <gtest/gtest.h>
#include <regex>
#include <vector>
using vector_typelist = testing::Types<char, int*, std::regex>;
template<class> struct vector_suite : testing::Test {};
TYPED_TEST_SUITE(vector_suite, vector_typelist);
TYPED_TEST(vector_suite, bigness)
{
std::vector<TypeParam> v;
EXPECT_TRUE(sizeof(v) >= sizeof(TypeParam));
}
使用适当的-I
和-l
选项编译。这可能看起来像:
g++ -std=c++14 -I/usr/local/include test.cpp -L/usr/local/lib -lgtest -lgtest_main
或者(如果您使用pkg-config
):
g++ -std=c++14 $(pkg-config --cflags gtest) test.cpp $(pkg-config --libs gtest_main)
运行可执行文件,你应该看到如下内容:
$ ./a.out
Running main() from /foobar/gtest_main.cc
[==========] Running 3 tests from 3 test suites.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN ] vector_suite/0.bigness
[ OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)
[----------] 1 test from vector_suite/1, where TypeParam = int*
[ RUN ] vector_suite/1.bigness
[ OK ] vector_suite/1.bigness (0 ms)
[----------] 1 test from vector_suite/1 (0 ms total)
[----------] 1 test from vector_suite/2, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >
[ RUN ] vector_suite/2.bigness
test.cpp:12: Failure
Value of: sizeof(v) >= sizeof(TypeParam)
Actual: false
Expected: true
[ FAILED ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> > (0 ms)
[----------] 1 test from vector_suite/2 (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 3 test suites ran. (0 ms total)
[ PASSED ] 2 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >
1 FAILED TEST
(我放入这个失败的测试纯粹是为了演示当类型化测试失败时会发生什么。事实上,sizeof(std::vector<std::regex>)
确实比sizeof(std::regex)
小。
请注意,TYPED_TEST
产生了一系列具有相关但不相同名称的测试套件。没有单独的vector_suite
了:
$ ./a.out --gtest_filter=vector_suite.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite.*
[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[ PASSED ] 0 tests.
有vector_suite/0
, vector_suite/1
和vector_suite/2
:
$ ./a.out --gtest_filter=vector_suite/0.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite/0.*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN ] vector_suite/0.bigness
[ OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 有没有办法删除传递给函数"foo(新对象())"的对象?
- 为什么 clang++ 只销毁一个 foo 对象
- OOP 设计 - 处理整个 Foo 对象序列
- 在Foo中声明对象Foo
- 如何在<T>测试中实例化 MyType 中列出的 T 类 Foo 的对象?
- 命名空间中的'Foo' 'bar' 不命名头文件中对象成员的类型
- 属性错误: 'Foo'对象没有属性'max'
- 以下对象的名称未知:类Foo::Bar