PVData C++  8.0.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Macros | Functions
Unit testing helpers

Macros

#define TEST_METHOD(klass, method)   ::detail::test_method<klass, &klass::method>(#klass, #method)
 
#define testEqual(LHS, RHS)   ::detail::testEqualx(#LHS, #RHS, LHS, RHS)
 
#define testNotEqual(LHS, RHS)   ::detail::testNotEqualx(#LHS, #RHS, LHS, RHS)
 
#define testTrue(B)   ::detail::testPassx(!!(B))<<#B
 
#define testThrows(EXC, CODE)   try{ CODE; testFail("unexpected success of " #CODE); }catch(EXC& e){testPass("catch expected exception: %s", e.what());}
 
#define testShow()   ::detail::testPassx()
 

Functions

template<typename PVD >
::detail::testPassx testFieldEqual (const std::tr1::shared_ptr< const epics::pvData::PVStructure > &val, const char *name, typename PVD::value_type expect)
 
template<typename PVD >
::detail::testPassx testFieldEqual (const std::tr1::shared_ptr< const epics::pvData::PVStructure > &val, const char *name, typename PVD::const_svector expect)
 

Detailed Description

Helper functions for writing unit tests.

/*
* Copyright information and license terms for this software can be
* found in the file LICENSE that is included with the distribution
*/
/* c++ unittest skeleton */
#include <stdexcept>
#include <testMain.h>
#include <pv/pvUnitTest.h>
#include <pv/epicsException.h>
namespace {
void testCase1() {
testEqual(1+1, 2);
}
} // namespace
MAIN(testUnitTest)
{
testPlan(1);
try {
testCase1();
}catch(std::exception& e){
PRINT_EXCEPTION(e); // print stack trace if thrown with THROW_EXCEPTION()
testAbort("Unhandled exception: %s", e.what());
}
return testDone();
}

Macro Definition Documentation

#define TEST_METHOD (   klass,
  method 
)    ::detail::test_method<klass, &klass::method>(#klass, #method)

Run a class method as a test.

Each invocation of TEST_METHOD() constructs a new instance of 'klass' on the stack. Thus constructor and destructor can be used for common test setup and tear down.

namespace { // anon
struct MyTest {
MyTest() { } // setup
~MyTest() { } // tear down
void test1() {}
void test2() {}
};
} // namespace anon
MAIN(somename) {
testPlan(0);
TEST_METHOD(MyTest, test1)
TEST_METHOD(MyTest, test2)
return testDone();
}

Definition at line 98 of file pvUnitTest.h.

#define testEqual (   LHS,
  RHS 
)    ::detail::testEqualx(#LHS, #RHS, LHS, RHS)

Compare equality. print left and right hand values and expression strings

int x=5;
testEqual(x, 5);
// prints "ok 1 - x (5) == 5 (5)\n"
testEqual(x, 6)<<" oops";
// prints "not ok 1 - x (5) == 6 (6) oops\n"

Definition at line 110 of file pvUnitTest.h.

#define testShow ( )    ::detail::testPassx()

Print test output w/o testing

testShow()<<"Foo";

Definition at line 140 of file pvUnitTest.h.

#define testThrows (   EXC,
  CODE 
)    try{ CODE; testFail("unexpected success of " #CODE); }catch(EXC& e){testPass("catch expected exception: %s", e.what());}

Test that a given block throws an exception

Definition at line 132 of file pvUnitTest.h.

#define testTrue (   B)    ::detail::testPassx(!!(B))<<#B

Pass/fail from boolean

bool y=true;
// prints "ok 1 - y\n"
testTrue(!y)<<" oops";
// prints "not ok 1 - !y oops\n"

Definition at line 124 of file pvUnitTest.h.

Function Documentation

template<typename PVD >
::detail::testPassx testFieldEqual ( const std::tr1::shared_ptr< const epics::pvData::PVStructure > &  val,
const char *  name,
typename PVD::value_type  expect 
)

Compare value of PVStructure field

PVStructurePtr x(.....);
testFieldEqual<epics::pvData::PVInt>(x, "alarm.severity", 1);

Definition at line 151 of file pvUnitTest.h.