summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/assertion_frameworks/matchers/ThrowException.h
blob: cd8bfc34fab42eb5ea396c414501dce13d17122d (plain)
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
#ifndef BANDIT_THROWEXCEPTION_H
#define BANDIT_THROWEXCEPTION_H

#include "Matcher.h"

namespace bandit { namespace Matchers {

    template <typename T>
    class ThrowException : public Matcher
    {
    public:
        ThrowException() : Matcher(), _allow_subclasses(false) {}
        explicit ThrowException(bool allow_subclasses) : Matcher(), _allow_subclasses(allow_subclasses) {}

	template <typename U = std::exception>
        ThrowException<U> operator()() const
	{
	    return ThrowException<U>();
	}

        ThrowException& or_subclass()
	{
	    _allow_subclasses = true;
	    return *this;
	}

	template <typename U>
        bool matches(const U& block) const
	{
	    try
	    {
		block();
	    }
	    catch(const T& e)
	    {
		return true;
	    }
	    catch(...)	// Wrong exception
	    {
		_exception = std::current_exception();
	    }

	    return false;
	}

    protected:
        std::string failure_message_end() const
	{
	    return std::string("throw an exception");
	}

    private:
        bool	_allow_subclasses;
	mutable std::exception_ptr   _exception;
    };

    static const ThrowException<std::exception> throw_exception;
}}

#endif	// BANDIT_THROWEXCEPTION_H