Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates Faking Templates

Faking Templates

Top  Previous  Next

Isolator++ Professional supports faking template classes. The syntax is the same as faking concrete classes.

 

Let's say we want to change the behavior of a template method:

 

template <class K>

class MyTemplateClass {

   K a, b;

 

public:

   MyTemplateClass(K _a, K _b) { a = _a; b = _b; }

   K getSum() { return a + b; }

};

 

We'll use the FAKE and WHEN_CALLED macros:

 

TEST_F(TemplatesTests, MyTemplateClass_getSum_Return7) 

{

   // Arrange

   auto fake = FAKE<MyTemplateClass<int>>();

   WHEN_CALLED(fake->getSum()).Return(7);

 

   // Act

   int num = fake->getSum();

 

   // Assert

   ASSERT_EQ(num, 7);

}

 

 

Faking multiple type templates works the same way:

 

template <class T1, class T2>

class MyTemplateClass {

   T1 t1;

   T2 t2;

 

public:

   MyTemplateClass(T1 _t1, T2 _t2) { t1 = _t1; t2 = _t2; }

   T1 getSum() { return t1 + static_cast<T1>t2; }

};

 

TEST_F(TemplatesTests, MyTemplateClass_getSum_Return7) 

{

   // Arrange

   auto fake = FAKE<MyTemplateClass<long,int>>();

   WHEN_CALLED(fake->getSum()).Return(7);

 

   // Act

   int num = fake->getSum();

 

   // Assert

   ASSERT_EQ(num, 7);

}

 

NOTE: linuxsymbol To fake multiple type templates on Linux you'll need to use a typedef.

 

typedef MyTemplateClass<long,int> MyTemplate_long_int; // Necessary only on Linux

 

auto fake = FAKE<MyTemplate_long_int>();

 

 


Copyright  Typemock Ltd. 2009-2020.  All Rights Reserved.