Last active
January 22, 2019 16:47
-
-
Save charterchap/3113980f7ddd8c0915609492c5323ca6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Use of lambda functions for fff custom_fake | |
* | |
* this file contains an example of how to have "nested functions" | |
* within a test case for setting an fff custom_fake for pass by reference args | |
* | |
* This method for custom fakes allows all your test code to be self contained | |
* inside a test case instead of having global functions cluttering your test | |
* code. | |
* | |
* note that lambda captures are not supported | |
*/ | |
#include "gtest/gtest.h" | |
#include "fff.h" | |
#include <functional> | |
DEFINE_FFF_GLOBALS; | |
/* The time structure */ | |
typedef struct { | |
int hour, min; | |
} Time; | |
/* Our fake function */ | |
FAKE_VOID_FUNC(getTime, Time*); | |
template<typename L> auto to_function_pointer(L l)->decltype(&*l) | |
{ | |
return &*l; | |
} | |
/* A test using the getTime fake function */ | |
TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output) | |
{ | |
Time t; | |
auto getTime_cf = [](Time *now) { | |
now->hour = 2; | |
now->min = 3; | |
}; | |
getTime_fake.custom_fake = to_function_pointer(getTime_cf); | |
getTime(&t); | |
ASSERT_EQ(t.hour, 2); | |
ASSERT_EQ(t.min, 3); | |
} | |
/* custiom fake with built in static sequence */ | |
TEST(FFFTestSuite, when_value_custom_fake_called_THEN_it_returns_custom_output_seq) | |
{ | |
Time t; | |
auto getTime_cf = [](Time *now) { | |
static size_t call_number = 0; | |
static std::vector<Time> v = { | |
{2,30}, | |
{4,30} | |
}; | |
now->hour = v[call_number].hour; | |
now->min = v[call_number].min; | |
call_number++; | |
}; | |
void( *fn)(Time*) = to_function_pointer(getTime_cf); | |
getTime_fake.custom_fake = fn; | |
getTime(&t); | |
ASSERT_EQ(t.hour, 2); | |
ASSERT_EQ(t.min, 30); | |
getTime(&t); | |
ASSERT_EQ(t.hour, 4); | |
ASSERT_EQ(t.min, 30); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment