the Compartmented Robust Posix C++ Unit Test system

VERIFY_THROW(expr, exc_type, matcher?)

Verify that an expression throws the desired exception.

Used in: A test function body, the constructor or destructor of a fixture, or a function called from them. See TEST(name, ...).

Requirement: expr; can be used as a complete statement.

Requirement: exc_type is either a type, or ....

Requirement: matcher? is an optional parameter. If present it must be one of

The check succeeds if, and only if, the statement expr; throws an exception that can be caught using catch(exc_type&).

On success the test function continues without side effects.

On failure the test is marked as failed, but continues execution with an error report. The report includes the expression text, the expression type and, if the expression caught inherits from std::exception the output string from the virtual function std::exception::what(), or a generic message otherwise.

See also: ASSERT_THROW(expr, exc_type, matcher?)

Example: The test program

     
     #include <crpcut.hpp>
     #include <vector>
     
     class int_matcher
     {
       int n_, memory_;
      public:
       int_matcher(int n) : n_(n), memory_(0) {}
       bool operator()(int val) { memory_ = val; return val == n_; }
       friend std::ostream& operator<<(std::ostream &os, const int_matcher &m)
       {
         return os << m.memory_ << " does not match the expected " << m.n_;
       }
     };
     
     TEST(verify_throw_succeeds)
     {
       std::vector<int> v;
       VERIFY_THROW(v.at(3), std::out_of_range);
       VERIFY_THROW(throw 3, int, int_matcher(3));
     }
     
     TEST(verify_throw_fails)
     {
       std::vector<int> v;
       VERIFY_THROW(v.at(3), std::domain_error);
       VERIFY_THROW(v.at(3), std::bad_alloc);
       VERIFY_THROW(throw 3, int, int_matcher(5));
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

      

reports one failed test with three violated VERIFY_THROW() checks.


     FAILED!: verify_throw_fails
     fail---------------------------------------------------------------------------
     samples/verify_throw_fails.cpp:53
     VERIFY_THROW(v.at(3), std::domain_error)
       caught std::exception
     	what()=vector::_M_range_check: __n (which is 3) >= this->size() (which is 0)
     -------------------------------------------------------------------------------
     fail---------------------------------------------------------------------------
     samples/verify_throw_fails.cpp:54
     VERIFY_THROW(v.at(3), std::bad_alloc)
       caught std::exception
     	what()=vector::_M_range_check: __n (which is 3) >= this->size() (which is 0)
     -------------------------------------------------------------------------------
     fail---------------------------------------------------------------------------
     samples/verify_throw_fails.cpp:55
     VERIFY_THROW(throw 3, int, int_matcher(5))
     3 does not match the expected 5 does not match 3
     -------------------------------------------------------------------------------
     phase="running"  --------------------------------------------------------------
     samples/verify_throw_fails.cpp:50
     Earlier VERIFY failed
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0