suite.hpp

  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
#pragma once


#include <functional>
#include <span>

#include <felspar/test/check.hpp>


namespace felspar::test {


    using test_function_ptr =
            std::function<void(std::ostream &, test::injected)>;
    using test_check_log_function = void (*)(test::injected, std::ostream &);
    using test_check_function = void (*)(test::injected);
    using test_nullary_function = void (*)(void);


    void register_test(
            std::string_view suite, std::string test, test_function_ptr);
    inline void register_test(
            std::string_view suite,
            std::string test,
            test_check_log_function f) {
        register_test(
                suite, std::move(test),
                [f](std::ostream &l, test::injected c) { f(c, l); });
    }
    inline void register_test(
            std::string_view suite, std::string test, test_check_function f) {
        register_test(
                suite, std::move(test),
                [f](std::ostream &, test::injected c) { f(c); });
    }
    inline void register_test(
            std::string_view suite, std::string test, test_nullary_function f) {
        register_test(
                suite, std::move(test),
                [f](std::ostream &, test::injected) { f(); });
    }


    template<typename F>
    concept test_function = requires(F f, std::string_view sv, std::string s) {
        register_test(sv, s, f);
    };


    struct registration {
        std::string_view const suite;

        template<test_function TF>
        auto test(char const *name, TF t) const {
            register_test(suite, name, t);
            return *this;
        }
        template<test_function TF>
        auto test(TF t) const {
            register_test(suite, {}, t);
            return *this;
        }
        template<test_function TF, test_function... TFs>
        auto test(char const *name, TF t, TFs... ts) const {
            rt(name, std::tuple{t, ts...},
               std::make_index_sequence<sizeof...(TFs) + 1>{});
            return *this;
        }
        template<test_function TF, test_function... TFs>
        auto test(TF t, TFs... ts) const {
            register_test(suite, {}, t);
            (register_test(suite, {}, ts), ...);
            return *this;
        }

      private:
        template<test_function... TFs, std::size_t... I>
        void rt(const char *n,
                std::tuple<TFs...> ts,
                std::index_sequence<I...>) const {
            (register_test(
                     suite, std::string{n} + "/" + std::to_string(I + 1),
                     std::get<I>(ts)),
             ...);
        }
    };


    template<std::size_t N>
    inline auto testsuite(char const (&n)[N]) {
        return registration{n};
    }
    template<std::size_t N, test_function... Ts>
    inline auto testsuite(char const (&n)[N], Ts &&...tests) {
        registration suite{n};
        (suite.test(tests), ...);
        return suite;
    }

}