Create a function pointer with the same signature as the problem function. By default initialize the pointer with the problem function. Change clients to call through the function pointer. Override the function in the tests where needed.
--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
If the Tau is the only function in the object file that you use, then you might be able to link the object files in a library and link the library.
#ifdef UNIT_TESTS
#include <test_timer.h>
namespace prod {
#endif
void timer_init(void);
#ifdef UNIT_TESTS
}
#endif#include <timer.h>
#ifdef UNIT_TESTS
namespace prod {
#endif
void timer_init(void) {}
#ifdef UNIT_TESTS
}
#endif
#include <test_timer.h>
namespace stub {
void timer_init(void) {
mock().actualCall("timer_init");
}
}
#include <timer.h>
namespace stub {
void timer_init(void);
}
extern void (*timer_init)(void);
#include <CppUTest/TestHarness.h>
#include <CppUTestExt/MockSupport.h>
#include <test_timer.h>
#include <timer.h>
void (*timer_init)(void) = stub::timer_init;
TEST_GROUP(timer) {};
TEST(timer, init) {
prod::timer_init();
}
#include <test_timer.h>
namespace stub {
void timer_init(void) {
mock().actualCall("timer_init");
}
}
namespace prod {
#include "timer.c" // sic! This makes it prod::timer_init() only for testing
}
extern "C" {
void timer_init(void) {
// This is the stub that gets linked instead of the original.
// It calls a function pointer that is set to prod::timer_init()
// and can be set to stub::timer_init() when needed.
}
}
and sorry for the late reply. There are some worked examples at FakeFunctions. Regarding Pierre's example - one fundamental thing I'd recommend to do differently is this:
Hello, sorry to bring an old post back up but I thought my issue was relevant so I reposted here.
#include "FakeAdd.h"
int (*FakeAdd::add)(int,int) = Prod::add;
int (*FakeAdd::actualAdd)(AddStruct_t *) = Prod::actualAdd;
extern "C" int add(int a,int b)
{
return FakeAdd::add(a,b);
}
extern "C" int actualAdd(AddStruct_t *pAddStruct)
{
return FakeAdd::actualAdd(pAddStruct);
}
namespace Stub
{
int add(int a,int b)
{
AddStruct_t stubStruct;
stubStruct.a = a;
stubStruct.b = b;
return actualAdd(&stubStruct); // ambigous
}
int actualAdd(AddStruct_t *pAddStruct)
{
return pAddStruct->a + pAddStruct->b;
}
}
namespace Prod
{
#include "Add.c"
}
#ifndef FAKEADD_H
#define FAKEADD_H
extern "C"
{
#include "Add.h"
};
class FakeAdd
{
public:
static int (*add)(int,int);
static int (*actualAdd)(AddStruct_t *);
};
namespace Stub
{
int add(int a,int b);
int actualAdd(AddStruct_t *pAddStruct);
}
namespace Prod
{
int add(int a,int b);
int actualAdd(AddStruct_t *pAddStruct);
}
#endif //FAKEADD_H
#include "Add.h"
int add(int a,int b) { AddStruct_t addStruct; addStruct.a = a; addStruct.b = b; return actualAdd(&addStruct); }int actualAdd(AddStruct_t *pAddStruct){ return *pAddStruct.a + *pAddStruct.b;}
#ifndef _ADD_H#define _ADD_H
typedef struct AddStruct_s{ int a; int b;} AddStruct_t;
extern int add(int a,int b);extern int actualAdd(AddStruct_t *pAddStruct);
#endif