Assertions.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_TESTING__ASSERTIONS
18 #define COM_BORA_SOFTWARE__BALAU_TESTING__ASSERTIONS
19 
21 #include <Balau/Type/SourceCodeLocation.hpp>
22 
23 namespace Balau::Testing {
24 
28 inline void assertFail(const SourceCodeLocation & location, const std::string & failMessage) {
29  ThrowBalauException(Exception::AssertionException, toString(location) + " - " + failMessage + "\n");
30 }
31 
35 inline void assertFail(const std::string & failMessage) {
37 }
38 
44 template <typename A, typename E, typename C, typename V, typename F>
45 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
46  MatcherFunction<A, E, V, C> matcherFunction(expected.value());
47 
48  if (!matcherFunction.matches(actual, expected.expected)) {
51  , toString(location) + " - " + failMessage + "\n" + TestRenderers::render(actual, expected.expected)
52  );
53  }
54 }
55 
61 template <typename A, typename E, typename C, typename V, typename F>
62 inline void assertThat(const std::string & failMessage, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
63  MatcherFunction<A, E, V, C> matcherFunction(expected.value());
64 
65  if (!matcherFunction.matches(actual, expected.expected)) {
68  , failMessage + "\n" + TestRenderers::render(actual, expected.expected)
69  );
70  }
71 }
72 
76 template <typename A, typename E, typename C, typename V, typename F>
77 inline void assertThat(const SourceCodeLocation & location, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
78  assertThat(location, "", actual, expected);
79 }
80 
84 template <typename A, typename E, typename C, typename V, typename F>
85 inline void assertThat(const A & actual, const ExpectedValue<E, C, V, F> & expected) {
86  assertThat("", actual, expected);
87 }
88 
97 template <typename A, typename E, typename C, typename V, typename F>
98 inline void assertThatNP(const SourceCodeLocation & location, const std::string & failMessage, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
99  MatcherFunction<A, E, V, C> matcherFunction(expected.value());
100 
101  if (!matcherFunction.matches(actual, expected.expected)) {
102  ThrowBalauException(Exception::AssertionException, toString(location) + " - " + failMessage + "\n");
103  }
104 }
105 
114 template <typename A, typename E, typename C, typename V, typename F>
115 inline void assertThatNP(const std::string & failMessage, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
116  MatcherFunction<A, E, V, C> matcherFunction(expected.value());
117 
118  if (!matcherFunction.matches(actual, expected.expected)) {
120  }
121 }
122 
129 template <typename A, typename E, typename C, typename V, typename F>
130 inline void assertThatNP(const SourceCodeLocation & location, const A & actual, const ExpectedValue<E, C, V, F> & expected) {
131  assertThatNP(location, "", actual, expected);
132 }
133 
140 template <typename A, typename E, typename C, typename V, typename F>
141 inline void assertThatNP(const A & actual, const ExpectedValue<E, C, V, F> & expected) {
142  assertThatNP("", actual, expected);
143 }
144 
150 template <typename A, typename T>
151 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, const A & actual, const IsAExpectation<T> & ) {
152  if (!dynamic_cast<const T *>(&actual)) {
155  , toString(location) + " - " + failMessage + "\nIncompatible types: " + boost::core::demangle(typeid(actual).name()) + " is not a " + boost::core::demangle(typeid(T).name())
156  );
157  }
158 }
159 
165 template <typename A, typename T>
166 inline void assertThat(const std::string & failMessage, const A & actual, const IsAExpectation<T> & ) {
167  if (!dynamic_cast<const T *>(&actual)) {
170  , failMessage + "\nIncompatible types: " + boost::core::demangle(typeid(actual).name()) + " is not a " + boost::core::demangle(typeid(T).name())
171  );
172  }
173 }
174 
178 template <typename A, typename T>
179 inline void assertThat(const SourceCodeLocation & location, const A & actual, const IsAExpectation<T> & expected) {
180  assertThat(location, "", actual, expected);
181 }
182 
186 template <typename A, typename T>
187 inline void assertThat(const A & actual, const IsAExpectation<T> & expected) {
188  assertThat("", actual, expected);
189 }
190 
199 template <typename A, typename T>
200 inline void assertThatNP(const SourceCodeLocation & location, const std::string & failMessage, const A & actual, const IsAExpectation<T> & ) {
201  if (!dynamic_cast<const T *>(&actual)) {
202  ThrowBalauException(Exception::AssertionException, toString(location) + " - " + failMessage + "\n");
203  }
204 }
205 
214 template <typename A, typename T>
215 inline void assertThatNP(const std::string & failMessage, const A & actual, const IsAExpectation<T> & ) {
216  if (!dynamic_cast<const T *>(&actual)) {
218  }
219 }
220 
227 template <typename A, typename T>
228 inline void assertThatNP(const SourceCodeLocation & location, const A & actual, const IsAExpectation<T> & expected) {
229  assertThatNP(location, "", actual, expected);
230 }
231 
238 template <typename A, typename T>
239 inline void assertThatNP(const A & actual, const IsAExpectation<T> & expected) {
240  assertThatNP("", actual, expected);
241 }
242 
252 template <typename F, typename E>
253 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, F function, const ThrowExpectation<E> & e) {
254  try {
255  function();
256  } catch (const E & actualException) {
257  assertThat(location, failMessage, actualException, is(e.e));
258  return;
259  } catch (const std::exception & actualException) {
262  , toString(location) + " - " + failMessage + "\nA different standard exception was thrown: " + actualException.what()
263  );
264  }
265 
268  , toString(location) + " - " + failMessage + "\nThe expected exception was not thrown."
269  );
270 }
271 
281 template <typename F, typename E>
282 inline void assertThat(const std::string & failMessage, F function, const ThrowExpectation<E> & e) {
283  try {
284  function();
285  } catch (const E & actualException) {
286  assertThat(failMessage, actualException, is(e.e));
287  return;
288  } catch (const std::exception & actualException) {
291  , failMessage + "\nA different standard exception was thrown: " + actualException.what()
292  );
293  }
294 
297  , failMessage + "\nThe expected exception was not thrown."
298  );
299 }
300 
310 template <typename F, typename E, typename C>
311 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, F function, const ThrowExpectationWithFunction<E, C> & e) {
312  try {
313  function();
314  } catch (const E & actualException) {
315  if (!e.compare(actualException)) {
318  , ::toString(
319  location
320  , " - "
321  , failMessage
322  , "\nThe exception's state was not identical to the supplied exception: "
323  , e
324  , " != "
325  , actualException
326  )
327  );
328  } else {
329  return;
330  }
331  } catch (const std::exception & actualException) {
334  , toString(location) + " - " + failMessage + "\nA different standard exception was thrown: " + actualException.what()
335  );
336  }
337 
340  , toString(location) + " - " + failMessage + "\nThe expected exception was not thrown."
341  );
342 }
343 
352 template <typename F, typename E>
353 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, F function, const ThrowExpectationWithPredicate<E> & predicate) {
354  try {
355  function();
356  } catch (const E & actualException) {
357  if (!predicate.compare(actualException)) {
360  , ::toString(
361  location
362  , " - "
363  , failMessage
364  , "\nThe exception's state was unexpected: "
365  , actualException
366  )
367  );
368  } else {
369  return;
370  }
371  } catch (const std::exception & actualException) {
374  , toString(location) + " - " + failMessage + "\nA different standard exception was thrown: " + actualException.what()
375  );
376  }
377 
380  , toString(location) + " - " + failMessage + "\nThe expected exception was not thrown."
381  );
382 }
383 
393 template <typename F, typename E, typename C>
394 inline void assertThat(const std::string & failMessage, F function, const ThrowExpectationWithFunction<E, C> & e) {
395  try {
396  function();
397  } catch (const E & actualException) {
398  if (!e.compare(actualException)) {
401  , ::toString(
402  failMessage
403  , "\nThe exception's state was not identical to the supplied exception: "
404  , e
405  , " != "
406  , actualException
407  )
408  );
409  } else {
410  return;
411  }
412  } catch (const std::exception & actualException) {
415  , failMessage + "\nA different standard exception was thrown: " + actualException.what()
416  );
417  }
418 
421  , failMessage + "\nThe expected exception was not thrown."
422  );
423 }
424 
433 template <typename F, typename E>
434 inline void assertThat(const std::string & failMessage, F function, const ThrowExpectationWithPredicate<E> & predicate) {
435  try {
436  function();
437  } catch (const E & actualException) {
438  if (!predicate.compare(actualException)) {
441  , ::toString(
442  failMessage
443  , "\nThe exception's state was unexpected: "
444  , actualException
445  )
446  );
447  } else {
448  return;
449  }
450  } catch (const std::exception & actualException) {
453  , failMessage + "\nA different standard exception was thrown: " + actualException.what()
454  );
455  }
456 
459  , failMessage + "\nThe expected exception was not thrown."
460  );
461 }
462 
471 template <typename F, typename E>
472 inline void assertThat(const SourceCodeLocation & location, const std::string & failMessage, F function, const ThrowTypeExpectation<E> &) {
473  try {
474  function();
475  } catch (const E & actualException) {
476  // Assertion passed.
477  return;
478  } catch (const std::exception & actualException) {
481  , toString(location) + " - " + failMessage + "\nA different standard exception was thrown: " + actualException.what()
482  );
483  }
484 
487  , toString(location) + " - " + failMessage + "\nThe expected exception was not thrown."
488  );
489 }
490 
499 template <typename F, typename E>
500 inline void assertThat(const std::string & failMessage, F function, const ThrowTypeExpectation<E> &) {
501  try {
502  function();
503  } catch (const E & actualException) {
504  // Assertion passed.
505  return;
506  } catch (const std::exception & actualException) {
509  , failMessage + "\nA different standard exception was thrown: " + actualException.what()
510  );
511  }
512 
515  , failMessage + "\nThe expected exception was not thrown."
516  );
517 }
518 
527 template <typename F, typename E>
528 inline void assertThat(const SourceCodeLocation & location, F function, const ThrowExpectation<E> & expectedException) {
529  assertThat(location, "", function, expectedException);
530 }
531 
540 template <typename F, typename E>
541 inline void assertThat(F function, const ThrowExpectation<E> & expectedException) {
542  assertThat("", function, expectedException);
543 }
544 
553 template <typename F, typename E, typename C>
554 inline void assertThat(const SourceCodeLocation & location, F function, const ThrowExpectationWithFunction<E, C> & expectedException) {
555  assertThat(location, "", function, expectedException);
556 }
557 
564 template <typename F, typename E>
565 inline void assertThat(const SourceCodeLocation & location, F function, const ThrowExpectationWithPredicate<E> & predicate) {
566  assertThat(location, "", function, predicate);
567 }
568 
577 template <typename F, typename E, typename C>
578 inline void assertThat(F function, const ThrowExpectationWithFunction<E, C> & expectedException) {
579  assertThat("", function, expectedException);
580 }
581 
588 template <typename F, typename E>
589 inline void assertThat(F function, const ThrowExpectationWithPredicate<E> & predicate) {
590  assertThat("", function, predicate);
591 }
592 
599 template <typename F, typename E>
600 inline void assertThat(const SourceCodeLocation & location, F function, const ThrowTypeExpectation<E> & expectedExceptionType) {
601  assertThat(location, "", function, expectedExceptionType);
602 }
603 
610 template <typename F, typename E>
611 inline void assertThat(F function, const ThrowTypeExpectation<E> & expectedExceptionType) {
612  assertThat("", function, expectedExceptionType);
613 }
614 
615 } // namespace Balau::Testing
616 
620 #define AssertFail(...) ::Balau::Testing::assertFail(::Balau::SourceCodeLocation(__FILE__, __LINE__), __VA_ARGS__)
621 
625 #define AssertThat(...) ::Balau::Testing::assertThat(::Balau::SourceCodeLocation(__FILE__, __LINE__), __VA_ARGS__)
626 
630 #define AssertThatNP(...) ::Balau::Testing::assertThatNP(::Balau::SourceCodeLocation(__FILE__, __LINE__), __VA_ARGS__)
631 
632 #endif // COM_BORA_SOFTWARE__BALAU_TESTING__ASSERTIONS
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
std::string render(const ContainerA< char > &actual, const ContainerE< char > &expected)
Overload for rendering char containers as hex values.
Definition: HexCharRenderer.hpp:28
The exception thrown by the test runner when an assertion fails.
Definition: TestExceptions.hpp:27
void assertThat(const SourceCodeLocation &location, const std::string &failMessage, const A &actual, const ExpectedValue< E, C, V, F > &expected)
Assert that the actual value supplied matches that in the matcher.
Definition: Assertions.hpp:45
A type used to representing a source code file and line number pair, obtained via the FILE and LINE m...
Definition: SourceCodeLocation.hpp:27
Matcher functions for test assertions.
ExpectedValue< E, MatcherCompareEquals, EvNotUsed, EvNotUsed > is(const E &expected)
Is the actual value equal to the supplied expected value?
Definition: Matchers.hpp:28
The test runner and test assertion functions.
Definition: Assertions.hpp:23
Balau::U8String< AllocatorT > toString(ExecutionModel model)
Print the execution model value as a UTF-8 string.
Definition: ExecutionModel.hpp:92
void assertThatNP(const SourceCodeLocation &location, const std::string &failMessage, const A &actual, const ExpectedValue< E, C, V, F > &expected)
Assertion without printing a failure rendering.
Definition: Assertions.hpp:98
void assertFail(const SourceCodeLocation &location, const std::string &failMessage)
Cause an assertion failure with a message.
Definition: Assertions.hpp:28