Crossed Fingers 🤞
C++ testing framework
Loading...
Searching...
No Matches
test.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025-Present Kevin Traini
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef TEST_H
25#define TEST_H
29
30#include "./assert.h"
31#include "internals/TestRun.h"
32
33#include <format>
34
35#define describe(name, ...) \
36 [[maybe_unused]] const auto t_##name = crossedfingers::internals::TestRun::instance().addSuite(#name, __VA_ARGS__)
37
38inline auto skip() -> void {
39 crossedfingers::internals::Assertion::_skip();
40}
41
42inline auto fail(const std::string &message) -> void {
43 crossedfingers::internals::Assertion::_fail(message);
44}
45
46inline auto it(const std::string &name, const std::function<void()> &callback) -> void {
47 crossedfingers::internals::TestRun::instance().addCase(name, callback);
48}
49
50template<typename Callback, typename... Args>
51auto it_each(
52 const std::vector<std::tuple<Args...>> &args_list, const std::string &template_name, const Callback *callback
53) -> void {
54 for (const auto &args : args_list) {
55 const auto name = std::apply(
56 [&template_name](auto &&...format_args) {
57 return std::vformat(template_name, std::make_format_args(format_args...));
58 },
59 args
60 );
61 crossedfingers::internals::TestRun::instance().addParameterizedCase(name, args, callback);
62 }
63}
64
65inline auto before(const std::function<void()> &callback) -> void {
66 crossedfingers::internals::TestRun::instance().addBefore(callback);
67}
68
69inline auto beforeEach(const std::function<void()> &callback) -> void {
70 crossedfingers::internals::TestRun::instance().addBeforeEach(callback);
71}
72
73inline auto after(const std::function<void()> &callback) -> void {
74 crossedfingers::internals::TestRun::instance().addAfter(callback);
75}
76
77inline auto afterEach(const std::function<void()> &callback) -> void {
78 crossedfingers::internals::TestRun::instance().addAfterEach(callback);
79}
80
81#endif // TEST_H
T string(T... args)
T vector