check.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#pragma once


#include <type_traits>

#include <felspar/test/report.hpp>


namespace felspar::test {


    template<typename T>
    concept testable_value = std::is_move_constructible_v<T>;


    struct injected;

Testing checks

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    template<typename V>
    struct checks {
        injected const &check;
        std::remove_cv_t<V> value;
        source_location source;

        template<typename X>
        checks(injected const &c, X &&v, source_location const &l)
        : check{c}, value{std::forward<X>(v)}, source{l} {}

        template<typename C>
        auto report(bool passed, std::string_view op, C const &c) const {
            if (not passed) {
                throw_failure(source, op, value_string(value), value_string(c));
            }
        }

All supported comparisons

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        template<typename C>
        auto operator==(C const &c) const {
            return report(value == c, "==", c);
        }
        template<typename C>
        auto operator!=(C const &c) const {
            return report(value != c, "!=", c);
        }
        template<typename C>
        auto operator<(C const &c) const {
            return report(value < c, "<", c);
        }
        template<typename C>
        auto operator<=(C const &c) const {
            return report(value <= c, "<=", c);
        }
        template<typename C>
        auto operator>(C const &c) const {
            return report(value > c, ">", c);
        }
        template<typename C>
        auto operator>=(C const &c) const {
            return report(value >= c, ">=", c);
        }

Other supported checks

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        template<typename E>
        auto throws(
                E const &v,
                source_location const &loc = source_location::current()) const;
        template<typename E>
        auto throws_type() const {
            bool passed{false};
            try {
                value();
            } catch (E const &) { passed = true; }
            if (not passed) { throw_failure(source, "throws"); }
        }

        auto is_truthy() const {
            if (not value) {
                throw_failure(source, "is_truthy", value_string(value));
            }
        }
        auto is_falsey() const {
            if (value) {
                throw_failure(source, "is_falsey", value_string(value));
            }
        }
    };

The type that is injected into the test

 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    struct injected {
        template<testable_value V>
        auto operator()(
                V &&v,
                source_location const &loc = source_location::current()) const {
            return checks<V>{*this, std::forward<V>(v), loc};
        }
    };


    template<typename V>
    template<typename E>
    inline auto checks<V>::throws(E const &v, source_location const &loc) const {
        try {
            value();
        } catch (E &e) {
            std::string_view e_what = e.what();
            std::string_view v_what = v.what();
            check(e_what.substr(0, e_what.find('\n')), loc)
                    == v_what.substr(0, v_what.find('\n'));
            return std::current_exception();
        }
        throw_failure(source, "throws");
    }


}