c++ - Stubbing WinBase.h in cppunit test -


i'm writing unit test class uses named pipes. need stub createnamedpipe, connectnamedpipe, writefile, readfile, flushfilebuffers, disconnectnamedpipe, closehandle, , getlasterror

these defined in winbase.h dll imports.

unfortunately winbase.h gigantic file that's used everywhere, 1 can't merely stub out items want test...

i tried copying winbase.h , making inline versions of functions:

bool connectnamedpipe(     __in        handle hnamedpipe,     __inout_opt lpoverlapped lpoverlapped     ) { return false; } 

but compile error every overridden function/object pair:

3>test.obj : error lnk2005: readfile defined in main.obj 

main minimal, contain

#include <file_templates/cppunit/generic_cppunit_main.cpp> 

which include winbase.h somewhere in it's depths...

even if resolve compile error, there's possibility break cppunit in process?

any solutions short of abstracting out pipe calls , stubbing abstract instead of winbase.h?

stubbing out via abstract redirection is, pretty best way this, in opinion, leaving remaining sanity possible. it's possible entirely using preprocessor, , no runtime overhead. use following design pattern in class:

#ifndef my_create_named_pipe #define my_create_named_pipe createnamedpipe #endif  #ifndef my_connect_named_pipe #define my_connect_named_pipe connectnamedpipe #endif 

... , on. then, have code invoke system calls using #defined aliases.

feel free use whatever munging convention prefer.

then, in unit test module:

#define my_create_named_pipe my_create_named_pipe #define my_connect_named_pipe my_connect_named_pipe  // , on, include source module directly:  #include "source.cpp" 

your unit test module implement my_create_named_pipe() stubs, et al. essentially, end building separate copy of source module, unit test, that's identical in every way, except names of invoked system calls.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -