expect.t.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // expect.t.h
  3. // expect
  4. //
  5. // Created by Sam Jaffe on 1/10/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "expect.hpp"
  10. struct my_error : public std::logic_error {
  11. using std::logic_error::logic_error;
  12. };
  13. class expect_TestSuite : public CxxTest::TestSuite {
  14. public:
  15. void test_expect_simple( ) const {
  16. int i = 1;
  17. expects( i == 1 );
  18. }
  19. void test_expect_fails( ) const {
  20. int i = 1;
  21. TS_ASSERT_THROWS( expects( i == 2 ), std::logic_error );
  22. }
  23. void test_expect_fails_with_error_type( ) const {
  24. int i = 1;
  25. TS_ASSERT_THROWS( (expects( i == 2, my_error, "error" )), my_error );
  26. }
  27. void test_expect_fails_with_error_message( ) const {
  28. int i = 1;
  29. try {
  30. expects( i == 2 );
  31. TS_FAIL( "expected exception" );
  32. } catch ( std::exception const & e ) {
  33. std::string emsg = e.what();
  34. emsg = emsg.substr( 0, emsg.find(".") );
  35. TS_ASSERT_EQUALS( emsg, "precondition failed: i == 2" );
  36. }
  37. }
  38. void test_expect_fails_with_custom_error_message( ) const {
  39. int i = 1;
  40. try {
  41. expects( i == 2, "example error message" );
  42. TS_FAIL( "expected exception" );
  43. } catch ( std::exception const & e ) {
  44. TS_ASSERT_EQUALS( e.what(), "example error message" );
  45. }
  46. }
  47. void test_expect_graceful( ) const {
  48. int i = 1;
  49. expects_graceful( i == 2 );
  50. TS_FAIL( "expected return" );
  51. }
  52. int impl_expect_graceful_rval( ) const {
  53. int i = 1;
  54. expects_graceful( i == 2, 7 );
  55. return 1;
  56. }
  57. void test_expect_graceful_rval( ) const {
  58. TS_ASSERT_EQUALS( impl_expect_graceful_rval(), 7 );
  59. }
  60. void test_return_ensure( ) const {
  61. int i = 6;
  62. TS_ASSERT_EQUALS( return_ensures( i + 1, _ > 5 ), 7 );
  63. }
  64. void test_return_ensure_throws( ) const {
  65. TS_ASSERT_THROWS( return_ensures( 7, _ < 5 ), std::runtime_error );
  66. }
  67. int impl_return_ensure_throws_message( ) const {
  68. int i = 6;
  69. return return_ensures( i + 1, _ < 5 );
  70. }
  71. void test_return_ensure_throws_message( ) const {
  72. try {
  73. (void) impl_return_ensure_throws_message();
  74. TS_FAIL( "expected exception" );
  75. } catch ( std::exception const & e ) {
  76. std::string emsg = e.what();
  77. emsg = emsg.substr( 0, emsg.find("].") + 1 );
  78. TS_ASSERT_EQUALS( emsg, "postcondition failed: _ < 5 [ with _ = i + 1 ]")
  79. }
  80. }
  81. void test_return_ensure_throws_custom_message( ) const {
  82. try {
  83. (void) return_ensures( 7, _ < 5, "example error message" );
  84. TS_FAIL( "expected exception" );
  85. } catch ( std::exception const & e ) {
  86. TS_ASSERT_EQUALS( e.what(), "example error message")
  87. }
  88. }
  89. void test_return_ensure_throws_custom( ) const {
  90. TS_ASSERT_THROWS( return_ensures( 7, _ < 5, my_error, "error" ), my_error );
  91. }
  92. };