From df6f4ace740e2a82f8fee3c543fe33eadad75e16 Mon Sep 17 00:00:00 2001 From: Andreas Waidler Date: Sun, 7 Mar 2010 22:20:56 +0100 Subject: [PATCH] Added creation macros and their tests. --- include/libsex/Makefile.am | 3 ++- include/libsex/create.hxx | 25 +++++++++++++++++++++++++ tests/AutoCreatedException.cxx | 7 +++++++ tests/AutoCreatedException.hxx | 11 +++++++++++ tests/Makefile.am | 4 +++- tests/TestMacroCreate.cxx | 10 ++++++++++ tests/TestMacroCreate.hxx | 16 ++++++++++++++++ tests/testRegistry.cxx | 3 +++ 8 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 include/libsex/create.hxx create mode 100644 tests/AutoCreatedException.cxx create mode 100644 tests/AutoCreatedException.hxx create mode 100644 tests/TestMacroCreate.cxx create mode 100644 tests/TestMacroCreate.hxx diff --git a/include/libsex/Makefile.am b/include/libsex/Makefile.am index 8588d68..545d83a 100644 --- a/include/libsex/Makefile.am +++ b/include/libsex/Makefile.am @@ -2,7 +2,8 @@ SUBDIRS = installdir = $(includedir)/$(PACKAGE) install_HEADERS = Exception.hxx \ - utility.hxx utility.ixx + utility.hxx utility.ixx \ + create.hxx uninstall-hook: rm -rf $(includedir)/$(PACKAGE) diff --git a/include/libsex/create.hxx b/include/libsex/create.hxx new file mode 100644 index 0000000..bae9ba3 --- /dev/null +++ b/include/libsex/create.hxx @@ -0,0 +1,25 @@ +#ifndef CREATE_HXX +#define CREATE_HXX + +#include // Needed in ERR_INST. + +#define ERR_DEFINE(parent, name) \ +class name : public parent\ +{\ +public:\ + static const char* const TEMPLATE;\ + name(const char* const errorMessage);\ +}; + +#define ERR_IMPL(parent, name, message) \ +const char* const name::TEMPLATE = message;\ +\ +name::name(const char* const errorMessage)\ +: parent(errorMessage)\ +{\ +}\ + +#define ERR_INST(type, var, args...) \ +type var = libsex::formatted(__FILE__, __LINE__, args); + +#endif diff --git a/tests/AutoCreatedException.cxx b/tests/AutoCreatedException.cxx new file mode 100644 index 0000000..6f6209d --- /dev/null +++ b/tests/AutoCreatedException.cxx @@ -0,0 +1,7 @@ +#include + +// Do not change this! Here we're testing whether +// defining new exception classes using macros works. +ERR_IMPL(libsex::Exception, + AutoCreatedException, + "%s is %d"); diff --git a/tests/AutoCreatedException.hxx b/tests/AutoCreatedException.hxx new file mode 100644 index 0000000..f04d5f1 --- /dev/null +++ b/tests/AutoCreatedException.hxx @@ -0,0 +1,11 @@ +#ifndef AUTOCREATEDEXCEPTION_HXX +#define AUTOCREATEDEXCEPTION_HXX + +#include +#include + +// Do not change this! Here we're testing whether +// defining new exception classes using macros works. +ERR_DEFINE(libsex::Exception, AutoCreatedException); + +#endif diff --git a/tests/Makefile.am b/tests/Makefile.am index d6e4530..30201d4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,10 +10,12 @@ tests_CXXFLAGS = $(CPPUNIT_LIBS) tests_LDADD = $(top_srcdir)/src/$(PACKAGE).la tests_SOURCES = main.cxx testRegistry.cxx \ MockException.cxx \ + AutoCreatedException.cxx \ CountingException.cxx \ UncloneableException.cxx \ TestException.cxx \ - TestUtility.cxx + TestUtility.cxx \ + TestMacroCreate.cxx else diff --git a/tests/TestMacroCreate.cxx b/tests/TestMacroCreate.cxx new file mode 100644 index 0000000..5753a5f --- /dev/null +++ b/tests/TestMacroCreate.cxx @@ -0,0 +1,10 @@ +#include +#include + +void TestMacroCreate::testInstantiation() +{ + ERR_INST(AutoCreatedException, e, "foo", 42); + CPPUNIT_ASSERT_EQUAL( + std::string("TestMacroCreate.cxx:6: foo is 42"), + std::string(e.what())); +} diff --git a/tests/TestMacroCreate.hxx b/tests/TestMacroCreate.hxx new file mode 100644 index 0000000..b08743e --- /dev/null +++ b/tests/TestMacroCreate.hxx @@ -0,0 +1,16 @@ +#ifndef TESTMACROCREATE_HXX +#define TESTMACROCREATE_HXX + +#include + +class TestMacroCreate : public CppUnit::TestFixture +{ +CPPUNIT_TEST_SUITE(TestMacroCreate); + CPPUNIT_TEST(testInstantiation); +CPPUNIT_TEST_SUITE_END(); + +public: + void testInstantiation(); +}; + +#endif diff --git a/tests/testRegistry.cxx b/tests/testRegistry.cxx index 99338a8..281ef9b 100644 --- a/tests/testRegistry.cxx +++ b/tests/testRegistry.cxx @@ -8,3 +8,6 @@ CPPUNIT_TEST_SUITE_REGISTRATION(TestException); #include CPPUNIT_TEST_SUITE_REGISTRATION(TestUtility); + +#include +CPPUNIT_TEST_SUITE_REGISTRATION(TestMacroCreate); -- 2.11.4.GIT