Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ca25bce48b
Fetching contributors…

Cannot retrieve contributors at this time

file 101 lines (81 sloc) 3.597 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <cxxtest/TestSuite.h>

class Thing
{
    int _i;
public:
    Thing( int argI ) : _i(argI) {}
    int i() const { return _i; }
};

class Fail
{
public:
    bool operator()( int ) const { return false; }
    bool operator()( int, int ) const { return false; }
};

class ThrowsAssert : public CxxTest::TestSuite
{
public:
    void test_TS_ASSERT_THROWS_EQUALS()
    {
        TS_ASSERT_THROWS_EQUALS( { throw 1; }, int i, i, 2 );
        TS_ASSERT_THROWS_EQUALS( { throw Thing( 1 ); }, const Thing &thing, thing.i(), 2 );
    }

    void test_TS_ASSERT_THROWS_DIFFERS()
    {
        TS_ASSERT_THROWS_DIFFERS( { throw 1; }, int i, i, 1 );
        TS_ASSERT_THROWS_DIFFERS( { throw Thing( 1 ); }, const Thing &thing, thing.i(), 1 );
    }

    void test_TS_ASSERT_THROWS_SAME_DATA()
    {
        TS_ASSERT_THROWS_SAME_DATA( { throw "123"; }, const char *s, s, "456", 3 );
    }

    void test_TS_ASSERT_THROWS_LESS_THAN()
    {
        TS_ASSERT_THROWS_LESS_THAN( { throw 1; }, int i, i, 1 );
        TS_ASSERT_THROWS_LESS_THAN( { throw Thing( 1 ); }, const Thing &thing, thing.i(), 1 );
    }

    void test_TS_ASSERT_THROWS_LESS_THAN_EQUALS()
    {
        TS_ASSERT_THROWS_LESS_THAN_EQUALS( { throw 1; }, int i, i, 0 );
        TS_ASSERT_THROWS_LESS_THAN_EQUALS( { throw Thing( 1 ); }, const Thing &thing, thing.i(), 0 );
    }

    void test_TS_ASSERT_THROWS_PREDICATE()
    {
        TS_ASSERT_THROWS_PREDICATE( { throw 1; }, int i, Fail, i );
        TS_ASSERT_THROWS_PREDICATE( { throw Thing( 1 ); }, const Thing &thing, Fail, thing.i() );
    }

    void test_TS_ASSERT_THROWS_RELATION()
    {
        TS_ASSERT_THROWS_RELATION( { throw 1; }, int i, Fail, i, 1 );
        TS_ASSERT_THROWS_RELATION( { throw Thing( 1 ); }, const Thing &thing, Fail, thing.i(), 1 );
    }

    void test_TS_ASSERT_THROWS_DELTA()
    {
        TS_ASSERT_THROWS_DELTA( { throw 1; }, int i, i, 3, 1 );
        TS_ASSERT_THROWS_DELTA( { throw Thing( 1 ); }, const Thing &thing, thing.i(), 3, 1 );
    }

    void test_TS_ASSERT_THROWS_ASSERT()
    {
        TS_ASSERT_THROWS_ASSERT( { throw 1; }, int i,
                                 TS_ASSERT_EQUALS( i, 2 ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_EQUALS( thing.i(), 2 ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_FAIL( thing.i() ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT( thing.i() - 1 ) );

        char zero = 0, one = 1;
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &,
                                 TS_ASSERT_SAME_DATA( &zero, &one, sizeof(char) ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_DELTA( thing.i(), 5, 2 ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_DIFFERS( thing.i(), 1 ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_LESS_THAN( thing.i(), 1 ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_PREDICATE( Fail, thing.i() ) );
        
        TS_ASSERT_THROWS_ASSERT( { throw Thing( 1 ); }, const Thing &thing,
                                 TS_ASSERT_RELATION( Fail, thing.i(), 33 ) );
    }
};
Something went wrong with that request. Please try again.