Removed autotools cruft.
[libsex.git] / tests / UsageExamples.cxx
blob20cb3d0a9aff57f227dc5ca26861b99375a5b435
1 /**
2 * @file
4 * Shows how this framework is supposed to be used.
5 */
7 // Needed for the tests in this file, not part of the examples.
8 #include <tests/UsageExamples.hxx>
9 #include <sstream>
11 // Usage examples begin now.
13 #include <source/throw.hxx>
14 #include <tests/framework/ExampleException1.hxx>
15 #include <tests/framework/ExampleException2.hxx>
17 /**
18 * How to simply create an exception.
20 void tests::UsageExamples::testSimpleConstruction()
22 // The error message.
23 std::string msg("Someone has set us up the bomb!");
25 // Create an exception, do not throw it. Note we
26 // can pass an arbitrary string as message --
27 // the Exception subsystem itself does not depend
28 // on message templates.
29 ExampleException2 e(msg.c_str());
31 // Turn description into an instance of std::string
32 // (needed for comparing).
33 std::string what(e.what());
37 CPPUNIT_ASSERT_EQUAL(msg, what);
40 /**
41 * How to throw a single exception without placeholders in
42 * its message template.
44 void tests::UsageExamples::testSimpleThrowing()
46 bool success = false;
48 try {
49 // Create an instance and throw it. Note
50 // that THROW uses the message template, so
51 // we can not pass an arbitrary string.
52 // Since ExampleException1's message
53 // template does not expect any arguments,
54 // we can use THROW_ARGLESS.
55 THROW_ARGLESS(ExampleException1);
56 } catch (ExampleException1& e) {
57 // Check whether result is as expected.
59 std::string exp(__FILE__);
60 exp += ":55: Error!";
61 std::string act(e.what());
63 CPPUNIT_ASSERT_EQUAL(exp, act);
64 success = true;
67 CPPUNIT_ASSERT(success);
70 /**
71 * How to throw a single exception.
73 void tests::UsageExamples::testThrowing()
75 bool success = false;
77 try {
78 // Fill the parameters into the message
79 // template and throw an exception
80 // containing the resulting string.
81 THROW(ExampleException2, "foobar", 42);
82 } catch (ExampleException2& e) {
83 // Check whether result is as expected.
85 std::string exp(__FILE__);
86 exp += ":81: foobar is 42";
87 std::string act(e.what());
89 CPPUNIT_ASSERT_EQUAL(exp, act);
90 success = true;
93 CPPUNIT_ASSERT(success);
96 /**
97 * How to chain exceptions.
99 void tests::UsageExamples::testChaining()
101 bool success = false;
103 try {
104 try {
105 THROW(ExampleException2, "foo", 21);
106 } catch (ExampleException2& e) {
107 // Note that CHAIN expects the
108 // variable name of the previous
109 // exception to be "e"!
110 CHAIN(ExampleException2, "bar", 42);
112 } catch (ExampleException2& e) {
113 // Check whether result is as expected.
115 std::string exp(__FILE__);
116 exp += ":110: bar is 42\n";
117 exp += __FILE__;
118 exp += ":105: foo is 21";
120 std::stringstream act;
121 e.backtrace(act);
123 CPPUNIT_ASSERT_EQUAL(
124 exp,
125 act.str());
127 success = true;
130 CPPUNIT_ASSERT(success);