| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- //
- // expect.t.h
- // expect
- //
- // Created by Sam Jaffe on 1/10/17.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "expect.hpp"
- struct my_error : public std::logic_error {
- using std::logic_error::logic_error;
- };
- class expect_TestSuite : public CxxTest::TestSuite {
- public:
- void test_expect_simple( ) const {
- int i = 1;
- expects( i == 1 );
- }
-
- void test_expect_fails( ) const {
- int i = 1;
- TS_ASSERT_THROWS( expects( i == 2 ), std::logic_error );
- }
-
- void test_expect_fails_with_error_type( ) const {
- int i = 1;
- TS_ASSERT_THROWS( (expects( i == 2, my_error, "error" )), my_error );
- }
-
- void test_expect_fails_with_error_message( ) const {
- int i = 1;
- try {
- expects( i == 2 );
- TS_FAIL( "expected exception" );
- } catch ( std::exception const & e ) {
- std::string emsg = e.what();
- emsg = emsg.substr( 0, emsg.find(".") );
- TS_ASSERT_EQUALS( emsg, "precondition failed: i == 2" );
- }
- }
-
- void test_expect_fails_with_custom_error_message( ) const {
- int i = 1;
- try {
- expects( i == 2, "example error message" );
- TS_FAIL( "expected exception" );
- } catch ( std::exception const & e ) {
- TS_ASSERT_EQUALS( e.what(), "example error message" );
- }
- }
-
- void test_expect_graceful( ) const {
- int i = 1;
- expects_graceful( i == 2 );
- TS_FAIL( "expected return" );
- }
-
- int impl_expect_graceful_rval( ) const {
- int i = 1;
- expects_graceful( i == 2, 7 );
- return 1;
- }
-
- void test_expect_graceful_rval( ) const {
- TS_ASSERT_EQUALS( impl_expect_graceful_rval(), 7 );
- }
-
- void test_return_ensure( ) const {
- int i = 6;
- TS_ASSERT_EQUALS( return_ensures( i + 1, _ > 5 ), 7 );
- }
- void test_return_ensure_throws( ) const {
- TS_ASSERT_THROWS( return_ensures( 7, _ < 5 ), std::runtime_error );
- }
-
- int impl_return_ensure_throws_message( ) const {
- int i = 6;
- return return_ensures( i + 1, _ < 5 );
- }
- void test_return_ensure_throws_message( ) const {
- try {
- (void) impl_return_ensure_throws_message();
- TS_FAIL( "expected exception" );
- } catch ( std::exception const & e ) {
- std::string emsg = e.what();
- emsg = emsg.substr( 0, emsg.find("].") + 1 );
- TS_ASSERT_EQUALS( emsg, "postcondition failed: _ < 5 [ with _ = i + 1 ]")
- }
- }
-
- void test_return_ensure_throws_custom_message( ) const {
- try {
- (void) return_ensures( 7, _ < 5, "example error message" );
- TS_FAIL( "expected exception" );
- } catch ( std::exception const & e ) {
- TS_ASSERT_EQUALS( e.what(), "example error message")
- }
- }
-
- void test_return_ensure_throws_custom( ) const {
- TS_ASSERT_THROWS( return_ensures( 7, _ < 5, my_error, "error" ), my_error );
- }
- };
|