Updated .gitignore.
[libsex.git] / tests / src / UsageExamples.cxx
blob602afbf5851611d091eb5a60e611c21fa6d593c2
1 /**
2 * Shows how this framework is supposed to be used.
3 */
5 // Needed for the tests in this file, not part of the
6 // examples.
7 #include <tests/src/UsageExamples.hxx>
8 #include <sstream>
10 // Usage examples begin now.
12 // This is the way to _declare_ an exception class.
13 // --- Start MyException.hxx ---
15 #ifndef MYEXCEPTION_HXX
16 #define MYEXCEPTION_HXX
18 // Remember to adjust include path.
19 #include <source/declare.hxx>
20 #include <source/Exception.hxx>
22 // MyException inherits from libsex::Exception.
23 LIBSEX_DECLARE(libsex::Exception, MyException)
25 #endif
27 // --- End MyException.hxx ---
31 // This is the way to _define_ an exception class.
32 // --- Start MyException.cxx ---
34 #include <source/define.hxx>
35 #include <source/Exception.hxx>
36 // Remember to include the header file (MyException.hxx)!
38 // Define MyException and provide a message template (see
39 // printf for a list and explanation of placeholders).
41 // If you want to localize your exceptions, you just have
42 // to localize the following line.
44 LIBSEX_DEFINE(libsex::Exception, MyException, "%s is %d")
46 // --- End MyException.cxx ---
50 // Lets create an exception with a fixed message:
51 LIBSEX_DECLARE(libsex::Exception, Error)
52 LIBSEX_DEFINE(libsex::Exception, Error, "Error!")
55 #include <source/throw.hxx> // THROW and CHAIN
57 /**
58 * How to simply create an exception.
60 void UsageExamples::testSimpleConstruction()
62 // The error message.
63 std::string msg("Someone has set us up the bomb!");
65 // Create an exception, do not throw it. Note we
66 // can pass an arbitrary string as message --
67 // the Exception subsystem itself does not depend
68 // on message templates.
69 MyException e(msg.c_str());
71 // Turn description into an instance of std::string
72 // (needed for comparing).
73 std::string what(e.what());
77 CPPUNIT_ASSERT_EQUAL(msg, what);
80 /**
81 * How to throw a single exception without placeholders in
82 * its message template.
84 void UsageExamples::testSimpleThrowing()
86 bool success = false;
88 try {
89 // Create an instance and throw it. Note
90 // that THROW uses the message template, so
91 // we can not pass an arbitrary string.
92 // Since Error's message template does not
93 // expect any arguments, we can use
94 // THROW_ARGLESS.
95 THROW_ARGLESS(Error);
96 } catch (Error& e) {
97 // Check whether result is as expected.
99 std::string exp(__FILE__); exp += ":95: Error!";
100 std::string act(e.what());
102 CPPUNIT_ASSERT_EQUAL(exp, act);
103 success = true;
106 CPPUNIT_ASSERT(success);
110 * How to throw a single exception.
112 void UsageExamples::testThrowing()
114 bool success = false;
116 try {
117 // Fill the parameters into the message
118 // template and throw an exception
119 // containing the resulting string.
120 THROW(MyException, "foobar", 42);
121 } catch (MyException& e) {
122 // Check whether result is as expected.
124 std::string exp(__FILE__); exp += ":120: foobar is 42";
125 std::string act(e.what());
127 CPPUNIT_ASSERT_EQUAL(exp, act);
128 success = true;
131 CPPUNIT_ASSERT(success);
135 * How to chain exceptions.
137 void UsageExamples::testChaining()
139 bool success = false;
141 try {
142 try {
143 THROW(MyException, "foo", 21);
144 } catch (MyException& e) {
145 // Note that CHAIN expects the
146 // variable name of the previous
147 // exception to be "e"!
148 CHAIN(MyException, "bar", 42);
150 } catch (MyException& e) {
151 // Check whether result is as expected.
153 std::string exp(__FILE__);
154 exp += ":148: bar is 42\n";
155 exp += __FILE__;
156 exp += ":143: foo is 21";
158 std::stringstream act;
159 e.backtrace(act);
161 CPPUNIT_ASSERT_EQUAL(
162 exp,
163 act.str());
165 success = true;
168 CPPUNIT_ASSERT(success);