You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
http://www.drdobbs.com/cpp/184401572
class Foo<br />
{<br />
public:<br />
bool Bar()<br />
{<br />
IUnknown * pUnk = NULL;<br />
HRESULT hr = CoCreateInstance(SOME_CLSID, NULL, CLSCTX_LOCAL_SERVER, SOME_IID,<br />
(LPVOID*)&pUnk);<br />
if (SUCCEEDED(hr)) {<br />
// do some work ...<br />
pUnk->Release();<br />
return true;<br />
}<br />
return false; // do some other work ...<br />
}<br />
};
class Foo<br />
{<br />
public:<br />
bool Bar()<br />
{<br />
IUnknown * pUnk = NULL;<br />
HRESULT hr = CoCreateInstance(SOME_CLSID, NULL, CLSCTX_LOCAL_SERVER, SOME_IID,<br />
(LPVOID*)&pUnk);<br />
if (SUCCEEDED(hr)) {<br />
// do some work ...<br />
pUnk->Release();<br />
return true;<br />
}<br />
return false; // do some other work ...<br />
}<br />
private:<br />
virtual HRESULT CoCreateInstance(__in REFCLSID rclsid, __in_opt LPUNKNOWN pUnkOuter, __i<br />
{ return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv); }<br />
};
TESTMETHOD(TestSubclass)<br />
{<br />
class TestFoo : public Foo<br />
{<br />
private:<br />
virtual HRESULT CoCreateInstance(__in REFCLSID rclsid, __in_opt LPUNKNOWN pUnkOuter,<br />
{ return E_NOINTERFACE; }<br />
};<br />
assert(false == TestFoo().Bar());<br />
}
class Foo<br />
{<br />
public:<br />
static bool Bar() {<br />
IUnknown * pUnk = NULL;<br />
HRESULT hr = CoCreateInstance(SOME_CLSID, NULL, CLSCTX_LOCAL_SERVER, SOME_IID,<br />
(LPVOID*)&pUnk);<br />
if (SUCCEEDED(hr)) {<br />
// do some work ...<br />
pUnk->Release();<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};
struct Empty{};<br />
template class FooT : public Base<br />
{<br />
public:<br />
static bool Bar() {<br />
IUnknown * pUnk = NULL;<br />
HRESULT hr = CoCreateInstance(SOME_CLSID, NULL, CLSCTX_LOCAL_SERVER, SOME_IID,<br />
(LPVOID*)&pUnk);<br />
if (SUCCEEDED(hr)) {<br />
// do some work ...<br />
pUnk->Release();<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};<br />
typedef FooT Foo;
TESTMETHOD(TBCIMockingCAPIWithEllipsis)<br />
{<br />
struct TestBase<br />
{<br />
static HRESULT CoCreateInstance(...) { return E_NOINTERFACE; }<br />
};<br />
assert(false == FooT().Bar());<br />
};
TESTMETHOD(TBCIMockingCAPIWithData)<br />
{<br />
struct TestBase {<br />
struct MyUnknown : public IUnknown {<br />
ULONG ul;<br />
MyUnknown() : ul(1) {}<br />
virtual HRESULT QueryInterface(REFIID riid, void**ppvObject) { return E_FAIL; }<br />
virtual ULONG STDMETHODCALLTYPE AddRef () { return ++ul; }<br />
virtual ULONG STDMETHODCALLTYPE Release() { return --ul; }<br />
} m_unk;<br />
HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, R<br />
*ppv = &m_unk;<br />
return S_OK;<br />
}<br />
};<br />
FooT foo;<br />
assert(true == foo.Bar());<br />
assert(0 == foo.m_unk.ul);<br />
};
class Foo<br />
{<br />
public:<br />
bool Bar2()<br />
{<br />
Baz baz;<br />
if (baz.IsItTrue()) {<br />
// do some work ...<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};
struct Empty{};<br />
template<br />
class FooT : public Base<br />
{<br />
public:<br />
bool Bar2()<br />
{<br />
Baz baz;<br />
if (baz.IsItTrue()) {<br />
// do some work ...<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};<br />
typedef FooT Foo;
TESTMETHOD(TBCIMockingAnAutomaticVariable)<br />
{<br />
struct TestBase<br />
{<br />
struct Baz<br />
{<br />
bool IsIsTrue() { return true; }<br />
};<br />
};<br />
assert(true == FooT().Bar2());<br />
};
class Foo<br />
{<br />
public:<br />
bool Bar3()<br />
{<br />
Abc abc;<br />
HRESULT hr1 = abc.DoWork();<br />
Def def;<br />
HRESULT hr2 = def.DoWork();<br />
return (SUCCEEDED(hr1) && SUCCEEDED(hr2));<br />
}<br />
};
struct Empty{};<br />
template<br />
class FooT : public Base<br />
{<br />
public:<br />
bool Bar3()<br />
{<br />
Abc abc;<br />
HRESULT hr1 = abc.DoWork();<br />
Def def;<br />
HRESULT hr2 = def.DoWork();<br />
return (SUCCEEDED(hr1) && SUCCEEDED(hr2));<br />
}<br />
};<br />
typedef FooT Foo;
struct AbcFailing { HRESULT DoWork() { return E_FAIL; } };<br />
struct AbcSucceed { HRESULT DoWork() { return S_OK; } };<br />
struct DefFailing { HRESULT DoWork() { return E_FAIL; } };<br />
struct DefSucceed { HRESULT DoWork() { return S_OK; } };<br />
template struct TestBaseT<br />
{<br />
typedef A Abc;<br />
typedef D Def;<br />
};<br />
TESTMETHOD(CompositingMocks)<br />
{<br />
assert(false == (FooT().Bar3()));<br />
assert(false == (FooT().Bar3()));<br />
assert(false == (FooT().Bar3()));<br />
assert( true == (FooT().Bar3()));<br />
}
http://msdn.microsoft.com/en-us/library/w98s4hs8.aspx
struct Empty { typedef ::Baz Baz; };<br />
template class FooT : public Base<br />
{<br />
using typename Base::Baz;<br />
public:<br />
bool Bar2()<br />
{<br />
Baz baz;<br />
if (baz.IsItTrue()) {<br />
// do some work ...<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};<br />
typedef FooT Foo;
struct Empty {<br />
static inline HRESULT CoCreateInstance(__in REFCLSID rclsid, __in_opt LPUNKNOWN pUnkOuter,<br />
{ return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv); }<br />
};<br />
template class FooT : public Base {<br />
using Base::CoCreateInstance;<br />
public:<br />
static bool Bar() {<br />
IUnknown * pUnk = NULL;<br />
HRESULT hr = CoCreateInstance(SOME_CLSID NULL, CLSCTX_LOCAL_SERVER, SOME_IID, (LPVOI<br />
if (SUCCEEDED(hr)) { // do some work ...<br />
pUnk->Release();<br />
return true;<br />
}<br />
return false; // do some other work ...<br />
}<br />
};<br />
typedef FooT Foo;
#ifdef TESTING<br />
#define TESTABLESTRUCT(c) template struct c : public Base<br />
#else<br />
#define TESTABLESTRUCT(c) struct c<br />
#endif<br />
// kernel.c<br />
TESTABLESTRUCT(Foo)<br />
{<br />
bool Bar2() {<br />
Baz baz;<br />
if (baz.IsItTrue()) {<br />
// do some work ...<br />
return true;<br />
}<br />
// do some other work ...<br />
return false;<br />
}<br />
};
kerneltests.cpp<br />
#define TESTING<br />
#include “kernel.c”<br />
TESTMETHOD(KernelModeExample)<br />
{<br />
struct TestBase<br />
{<br />
struct Baz<br />
{<br />
bool IsIsTrue() { return true; }<br />
};<br />
};<br />
assert(true == Foo().Bar2());<br />
}