Custom Assertions

Top  Previous  Next

To create custom assertions between parameters passed to methods, you can use the WHEN_CALLED(...).DoInstead() combo, and provide a pointer to your own verification function:

 

Assuming the following class in production code

 

class Person

{

public:

      int GetAverageChildAge(int child1, int child2) 

      { 

              return (child1 + child2) / 2 

      }

 int CallPrivate(int var) 

      { 

              return SomePrivate(var) 

      }

private:

 int SomePrivate(int var){ return var}

 int SomePrivate(string var){ return stoi(var)}

 

};

 

We'd like to create a custom assertion that GetAverageChildAge actually gets called with the parameters of the same parity, so let's create our verification function:

 

class AltPerson

{

public:

      int VerifyArguments(int param1, int param2) 

      { 

            int mod1 = param1 % 2;

            int mod2 = param2 % 2;

              ASSERT_EQ(mod1, mod2);

            return -1;

      }

};

 

Now, let's inject the verification function into the runtime:

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = new Person();   

   AltPerson* altPerson = new AltPerson(); 

 

   WHEN_CALLED(personPtr->GetAverageChildAge(_, _)).DoMemberFunctionInstead(altPerson, VerifyArguments);

 

   int child1 0;

   int child2 = 2;

   

   // We should get an assert exception since the first parameter is not 10

   personPtr->GetAverageChildAge(child1, child2); 

   ISOLATOR_CLEANUP();

}

 

For more simple custom assertions on parameters passed to methods, you can use the Conditional Checkers:

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = new Person();   

 

   WHEN_CALLED(personPtr->GetAverageChildAge(_, _)).Ignore();

 

   int child1 0;

   int child2 = 2;

 

   personPtr->GetAverageChildAge(child1, child2); 

 

   ASSERT_WAS_CALLED(personPtr->GetAverageChildAge(EQ(0), LE(10)));

   ISOLATOR_CLEANUP();

}

 

 

Using Predicates:

You can use lambda expressions using the IS<> macro like in example below:

 

Note: In order to assert a specific overload was called with matching arguments, use the IS<> macro to specify the overload and the arguments predicate.

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = new Person();  

 

   WHEN_CALLED(personPtr->GetAverageChildAge(_, _)).Ignore();

 

   int child1 0;

   int child2 = 2;

 

   personPtr->GetAverageChildAge(child1, child2); 

 

   ASSERT_WAS_CALLED(personPtr->GetAverageChildAge(EQ(0), IS<int>( [](int x) {return x == 2} )));

   ISOLATOR_CLEANUP();

}

 

Or you can extract the condition like in the next example:

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = new Person();   

 

   WHEN_CALLED(personPtr->GetAverageChildAge(_, _)).Ignore();

 

   int child1 0;

   int child2 = 2;

 

   auto predicate = [](int x)

   {

      return x == 2

   }

 

   personPtr->GetAverageChildAge(child1, child2); 

 

   ASSERT_WAS_CALLED(personPtr->GetAverageChildAge(EQ(0), IS<int>( predicate )));

   ISOLATOR_CLEANUP();

}

 

As in public methods, to assert a private overloaded method with matching arguments was called, use the IS<> macro:

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = FAKE<Person>(FakeOptions::CallOriginal);

 

   personPtr->CallPrivate(10);

 

   PRIVATE_ASSERT_WAS_CALLED(personPtr,SomePrivate,IS<int>([](int x) {return x == 10}));

}

 

 

linuxsymbol Note: When there is no lambda expressions support, you can use a predicate class with overloaded ().

You'll need to define NO_CPP_LAMBDA_SUPPORT:

 

#define NO_CPP_LAMBDA_SUPPORT

 

struct Predicate{

   bool operator()(int x){return x == 10;}

}

 

 

TEST_F(Examples, CustomAssertion)

{

   Person* personPtr = FAKE<Person>(FakeOptions::CallOriginal);

 

   personPtr->CallPrivate(10);

 

   PRIVATE_ASSERT_WAS_CALLED(personPtr,SomePrivate,IS<int>(Predicate()));

}


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