Gtest - test pattern class

I want to check template class with gtest. I read about TYPED_TESTs in the gtest manual and looked at the official example they reference, but still can't wrap my head around getting the template class object created in my test.

Suppose the following simple template class:

template <typename T> class Foo { public: T data ; }; 

In the testing class, we announce

 typedef ::testing::Types<int, float> MyTypes ; 

Now, how can I instantiate an object of the Foo class for Ts specified in MyTypes in the test? For instance.

 TYPED_TEST(TestFoo, test1) { Foo<T> object ; object.data = 1.0 ; ASSERT_FLOAT_EQ(object.data, 1.0) ; } 
+6
source share
1 answer

Inside the test, refer to the special name TypeParam to get the type parameter. So you could do

 TYPED_TEST(TestFoo, test1) { Foo<TypeParam> object ; // not Foo<T> object.data = 1.0 ; ASSERT_FLOAT_EQ(object.data, 1.0) ; } 
+6
source

Source: https://habr.com/ru/post/947183/


All Articles