473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The TEST_() macro

Newsgroupies:

This sample code tests a WTL dialog box. (Fear not, topic police, we see
little Windows Template Library code here, and the actual TEST_() macro
itself is quite portable.)

I like the TEST_() macro better than the usual Test Suite system (where you
must write extra code to push each Test Case into its Suite's list), so I
wrote a CppUnit clone that uses _only_ the TEST_() macro.

Firstly, all TestCases obey the Abstract Template pattern:

class
TestCase
{
public:
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

Their caller will iterate a polymorphic list of cases, and call setUp(),
runCase(), and tearDown() on each one.

To test a WTL dialog box called ProjectDlg, with a name and address on it,
we write a Suite called TestDialog:

class
TestDialog: public TestCase
{
public:
ProjectDlg m_aDlg;

virtual void setUp()
{ m_aDlg.Create(HWND_TOP); }

virtual void tearDown()
{ m_aDlg.DestroyWindow(); }

TestDialog(): m_aDlg(xml) {}
};

Now we naively derive one concrete class for each of two test cases:

class
TestDialog_first_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}
};

class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};

Then we get this primordial test rig to pass, using an unrolled version of
the loop we will soon write:

int
main()
{
try {
TestDialog_first_name test1;
test1.setUp();
test1.runCase();
test1.tearDown();

TestDialog_last_name test2;
test2.setUp();
test2.runCase();
test2.tearDown();
}
catch(_com_error & e)
{
guard_(e);
ELIDE_(__asm { int 3 });
return 1;
}
return 0;
}

(Notice, despite I'm using that evil OLE COM ActiveX whatever library from
Microsoft, my code is not full of extra crud like CComBSTR or
CoCreateInstance(). Something chased it all away!)

From here, we need to upgrade TestCase et al to automatically collect the
Test Cases. They should not appear twice - once among the test suites and
again in main(). Removing behavioral duplication tends to improve code's
style.

Make each TestCase instance push itself into a static member list:

#include <list>

class
TestCase
{
public:
typedef std::list<TestCase *> TestCases_t;
TestCases_t static cases;

TestCase() { cases.push_back(this); }
virtual void setUp() {}
virtual void runCase() = 0;
virtual void tearDown() {}
};

TestCase::TestCases_t TestCase::cases;
....
class
TestDialog_last_name: public TestDialog
{
public:
void
runCase()
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}
};
TestDialog_last_name test2;

(Remember that after each few edits all tests must still pass!) Now upgrade
main() to use the list:

int
main()
{
try {
TestCase::TestCases_t::iterator it (TestCase::cases.begin());

for ( ; it != TestCase::cases.end(); ++it )
{
TestCase & aCase = **it;
aCase.setUp();
aCase.runCase();
aCase.tearDown();
}
}

We continue to seek things that look similar, make them the same, and merge
their duplication.

The classes TestDialog_first_name (not shown) and TestDialog_last_name now
look very similar, but so do their object instances. To make them all the
same, we need to declare a class and instantiate an object in one command:

#define TEST_(suite, target) \
struct suite##target: public suite \
{ void runCase(); } \
a##suite##target; \
void suite##target::runCase()

TEST_(TestDialog, first_name)
{
CPPUNIT_ASSERT_EQUAL( "Ignatz",
m_aDlg.getText(IDC_EDIT_FIRST_NAME) );
}

TEST_(TestDialog, last_name)
{
CPPUNIT_ASSERT_EQUAL( "Mouse",
m_aDlg.getText(IDC_EDIT_LAST_NAME) );
}

The object instances went inside the TEST_() macro, which uses
token##pasting to give them unique names. But they all must construct,
globally, so their constructors can register them with the list of cases.

This pattern makes Test Fixtures easy. Classes like TestDialog take care of
setting up and tearing down tested objects, providing a framework to share
other common operations on the tested objects. Test cases are now easy. The
Test Collector pattern ensures when we write new cases, we can't forget to
add them to a remote list of cases.

But these Test Cases no longer look like normal C++ functions!

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #1
0 1436

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3210
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
33323
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
3462
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
23528
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
8459
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
10848
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
6
6424
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
3473
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
2033
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.