4 * Shows how this framework is supposed to be used.
7 // Needed for the tests in this file, not part of the examples.
8 #include <tests/UsageExamples.hxx>
11 // Usage examples begin now.
13 #include <libsex/throw.hxx>
14 #include <tests/framework/ExampleException1.hxx>
15 #include <tests/framework/ExampleException2.hxx>
18 * How to simply create an exception.
20 void tests::UsageExamples::testSimpleConstruction()
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 example::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
);
41 * How to throw a single exception without placeholders in
42 * its message template.
44 void tests::UsageExamples::testSimpleThrowing()
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(example::ExampleException1
);
56 } catch (example::ExampleException1
& e
) {
57 // Check whether result is as expected.
59 std::string
exp(__FILE__
);
61 std::string
act(e
.what());
63 CPPUNIT_ASSERT_EQUAL(exp
, act
);
67 CPPUNIT_ASSERT(success
);
71 * How to throw a single exception.
73 void tests::UsageExamples::testThrowing()
78 // Fill the parameters into the message
79 // template and throw an exception
80 // containing the resulting string.
81 THROW(example::ExampleException2
, "foobar", 42);
82 } catch (example::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
);
93 CPPUNIT_ASSERT(success
);
97 * How to chain exceptions.
99 void tests::UsageExamples::testChaining()
101 bool success
= false;
105 THROW(example::ExampleException2
, "foo", 21);
106 } catch (example::ExampleException2
& e
) {
107 // Note that CHAIN expects the
108 // variable name of the previous
109 // exception to be "e"!
110 CHAIN(example::ExampleException2
, "bar", 42);
112 } catch (example::ExampleException2
& e
) {
113 // Check whether result is as expected.
115 std::string
exp(__FILE__
);
116 exp
+= ":110: bar is 42\n";
118 exp
+= ":105: foo is 21";
120 std::stringstream act
;
123 CPPUNIT_ASSERT_EQUAL(
130 CPPUNIT_ASSERT(success
);