1 #include "../test_helpers.hxx"
2 
3 namespace
4 {
empty()5 void empty() {}
6 
7 
test_check_notreached()8 void test_check_notreached()
9 {
10   // At a minimum, PQXX_CHECK_NOTREACHED must work.
11   bool failed{true};
12   try
13   {
14     PQXX_CHECK_NOTREACHED("(expected)");
15     failed = false;
16   }
17   catch (pqxx::test::test_failure const &)
18   {
19     // This is what we expect.
20   }
21   if (not failed)
22     throw pqxx::test::test_failure(
23       __FILE__, __LINE__, "PQXX_CHECK_NOTREACHED is broken.");
24 }
25 
26 
27 // Test PQXX_CHECK.
test_check()28 void test_check()
29 {
30   PQXX_CHECK(true, "PQXX_CHECK is broken.");
31 
32   bool failed{true};
33   try
34   {
35     PQXX_CHECK(false, "(expected)");
36     failed = false;
37   }
38   catch (pqxx::test::test_failure const &)
39   {}
40   if (not failed)
41     PQXX_CHECK_NOTREACHED("PQXX_CHECK failed to notice failure.");
42 }
43 
44 
45 // Test PQXX_CHECK_THROWS_EXCEPTION.
test_check_throws_exception()46 void test_check_throws_exception()
47 {
48   // PQXX_CHECK_THROWS_EXCEPTION expects std::exception...
49   PQXX_CHECK_THROWS_EXCEPTION(
50     throw std::exception(),
51     "PQXX_CHECK_THROWS_EXCEPTION did not catch std::exception.");
52 
53   // ...or any exception type derived from it.
54   PQXX_CHECK_THROWS_EXCEPTION(
55     throw pqxx::test::test_failure(__FILE__, __LINE__, "(expected)"),
56     "PQXX_CHECK_THROWS_EXCEPTION() failed to catch expected exception.");
57 
58   // Any other type is an error.
59   bool failed{true};
60   try
61   {
62     PQXX_CHECK_THROWS_EXCEPTION(throw 1, "(expected)");
63     failed = false;
64   }
65   catch (pqxx::test::test_failure const &)
66   {}
67   PQXX_CHECK(
68     failed,
69     "PQXX_CHECK_THROWS_EXCEPTION did not complain about non-exception.");
70 
71   // But there _must_ be an exception.
72   failed = true;
73   try
74   {
75     // If the test fails to throw, this throws a failure.
76     PQXX_CHECK_THROWS_EXCEPTION(empty(), "(expected)");
77     // So we shouldn't get to this point.
78     failed = false;
79   }
80   catch (pqxx::test::test_failure const &)
81   {
82     // Instead, we go straight here.
83   }
84   PQXX_CHECK(
85     failed, "PQXX_CHECK_THROWS_EXCEPTION did not notice missing exception.");
86 
87   // PQXX_CHECK_THROWS_EXCEPTION can test itself...
88   PQXX_CHECK_THROWS_EXCEPTION(
89     PQXX_CHECK_THROWS_EXCEPTION(empty(), "(expected)"),
90     "PQXX_CHECK_THROWS_EXCEPTION failed to throw for missing exception.");
91 
92   PQXX_CHECK_THROWS_EXCEPTION(
93     PQXX_CHECK_THROWS_EXCEPTION(throw 1, "(expected)"),
94     "PQXX_CHECK_THROWS_EXCEPTION ignored wrong exception type.");
95 }
96 
97 
98 // Test PQXX_CHECK_THROWS.
test_check_throws()99 void test_check_throws()
100 {
101   PQXX_CHECK_THROWS(
102     throw pqxx::test::test_failure(__FILE__, __LINE__, "(expected)"),
103     pqxx::test::test_failure,
104     "PQXX_CHECK_THROWS() failed to catch expected exception.");
105 
106   // Even if it's not std::exception-derived.
107   PQXX_CHECK_THROWS(throw 1, int, "(expected)");
108 
109   // PQXX_CHECK_THROWS means there _must_ be an exception.
110   bool failed{true};
111   try
112   {
113     // If the test fails to throw, PQXX_CHECK_THROWS throws a failure.
114     PQXX_CHECK_THROWS(empty(), std::runtime_error, "(expected)");
115     // So we shouldn't get to this point.
116     failed = false;
117   }
118   catch (pqxx::test::test_failure const &)
119   {
120     // Instead, we go straight here.
121   }
122   PQXX_CHECK(failed, "PQXX_CHECK_THROWS did not notice missing exception.");
123 
124   // The exception must be of the right type (or a subclass of the right type).
125   failed = true;
126   try
127   {
128     // If the test throws the wrong type, PQXX_CHECK_THROWS throws a failure.
129     PQXX_CHECK_THROWS(
130       throw std::exception(), pqxx::test::test_failure, "(expected)");
131     failed = false;
132   }
133   catch (pqxx::test::test_failure const &)
134   {
135     // Instead, we go straight here.
136   }
137   PQXX_CHECK(failed, "PQXX_CHECK_THROWS did not notice wrong exception type.");
138 
139   // PQXX_CHECK_THROWS can test itself...
140   PQXX_CHECK_THROWS(
141     PQXX_CHECK_THROWS(empty(), pqxx::test::test_failure, "(expected)"),
142     pqxx::test::test_failure,
143     "PQXX_CHECK_THROWS failed to throw for missing exception.");
144 
145   PQXX_CHECK_THROWS(
146     PQXX_CHECK_THROWS(throw 1, std::runtime_error, "(expected)"),
147     pqxx::test::test_failure,
148     "PQXX_CHECK_THROWS failed to throw for wrong exception type.");
149 }
150 
151 
test_test_helpers()152 void test_test_helpers()
153 {
154   test_check_notreached();
155   test_check();
156   test_check_throws_exception();
157   test_check_throws();
158 
159   // Test other helpers against PQXX_CHECK_THROWS.
160   PQXX_CHECK_THROWS(
161     PQXX_CHECK_NOTREACHED("(expected)"), pqxx::test::test_failure,
162     "PQXX_CHECK_THROWS did not catch PQXX_CHECK_NOTREACHED.");
163 
164   PQXX_CHECK_THROWS(
165     PQXX_CHECK(false, "(expected)"), pqxx::test::test_failure,
166     "PQXX_CHECK_THROWS did not catch failing PQXX_CHECK.");
167 
168   PQXX_CHECK_THROWS(
169     PQXX_CHECK_THROWS(
170       PQXX_CHECK(true, "(shouldn't happen)"), pqxx::test::test_failure,
171       "(expected)"),
172     pqxx::test::test_failure,
173     "PQXX_CHECK_THROWS on successful PQXX_CHECK failed to throw.");
174 
175   // PQXX_CHECK_EQUAL tests for equality.  Its arguments need not be of the
176   // same type, as long as equality between them is defined.
177   PQXX_CHECK_EQUAL(1, 1, "PQXX_CHECK_EQUAL is broken.");
178   PQXX_CHECK_EQUAL(1, 1L, "PQXX_CHECK_EQUAL breaks on type mismatch.");
179 
180   PQXX_CHECK_THROWS(
181     PQXX_CHECK_EQUAL(1, 2, "(expected)"), pqxx::test::test_failure,
182     "PQXX_CHECK_EQUAL fails to spot inequality.");
183 
184   // PQXX_CHECK_NOT_EQUAL is like PQXX_CHECK_EQUAL, but tests for inequality.
185   PQXX_CHECK_NOT_EQUAL(1, 2, "PQXX_CHECK_NOT_EQUAL is broken.");
186   PQXX_CHECK_THROWS(
187     PQXX_CHECK_NOT_EQUAL(1, 1, "(expected)"), pqxx::test::test_failure,
188     "PQXX_CHECK_NOT_EQUAL fails to fail when arguments are equal.");
189   PQXX_CHECK_THROWS(
190     PQXX_CHECK_NOT_EQUAL(1, 1L, "(expected)"), pqxx::test::test_failure,
191     "PQXX_CHECK_NOT_EQUAL breaks on type mismatch.");
192 
193   // PQXX_CHECK_BOUNDS checks a value against a range.
194   PQXX_CHECK_BOUNDS(2, 1, 3, "PQXX_CHECK_BOUNDS wrongly finds fault.");
195 
196   PQXX_CHECK_THROWS(
197     PQXX_CHECK_BOUNDS(1, 2, 3, "(Expected)"), pqxx::test::test_failure,
198     "PQXX_CHECK_BOUNDS did not detect value below permitted range.");
199 
200   // PQXX_CHECK_BOUNDS tests against a half-open interval.
201   PQXX_CHECK_BOUNDS(1, 1, 3, "PQXX_CHECK_BOUNDS goes wrong on lower bound.");
202   PQXX_CHECK_THROWS(
203     PQXX_CHECK_BOUNDS(3, 1, 3, "(Expected)"), pqxx::test::test_failure,
204     "PQXX_CHECK_BOUNDS interval is not half-open.");
205 
206   // PQXX_CHECK_BOUNDS deals well with empty intervals.
207   PQXX_CHECK_THROWS(
208     PQXX_CHECK_BOUNDS(1, 2, 1, "(Expected)"), pqxx::test::test_failure,
209     "PQXX_CHECK_BOUNDS did not detect empty interval.");
210 }
211 
212 
213 PQXX_REGISTER_TEST(test_test_helpers);
214 } // namespace
215