aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/testing.cpp
blob: 936424e0fe40fca970374b5f8e7c0c2742cb7a4e (plain) (blame)
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
101
102
103
104
105
106
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zencore/testing.h"
#include "zencore/logging.h"

#if ZEN_WITH_TESTS

#	include <doctest/doctest.h>

namespace zen::testing {

using namespace std::literals;

struct TestListener : public doctest::IReporter
{
	const std::string_view ColorYellow = "\033[0;33m"sv;
	const std::string_view ColorNone   = "\033[0m"sv;

	// constructor has to accept the ContextOptions by ref as a single argument
	TestListener(const doctest::ContextOptions&) {}

	void report_query(const doctest::QueryData& /*in*/) override {}

	void test_run_start() override {}

	void test_run_end(const doctest::TestRunStats& /*in*/) override {}

	void test_case_start(const doctest::TestCaseData& in) override
	{
		Current = &in;
		ZEN_CONSOLE("{}======== TEST_CASE: {:<50} ========{}", ColorYellow, Current->m_name, ColorNone);
	}

	// called when a test case is reentered because of unfinished subcases
	void test_case_reenter(const doctest::TestCaseData& /*in*/) override
	{
		ZEN_CONSOLE("{}-------------------------------------------------------------------------------{}", ColorYellow, ColorNone);
	}

	void test_case_end(const doctest::CurrentTestCaseStats& /*in*/) override { Current = nullptr; }

	void test_case_exception(const doctest::TestCaseException& /*in*/) override {}

	void subcase_start(const doctest::SubcaseSignature& in) override
	{
		ZEN_CONSOLE("{}-------- SUBCASE:   {:<50} --------{}",
					ColorYellow,
					fmt::format("{}/{}", Current->m_name, in.m_name.c_str()),
					ColorNone);
	}

	void subcase_end() override {}

	void log_assert(const doctest::AssertData& /*in*/) override {}

	void log_message(const doctest::MessageData& /*in*/) override {}

	void test_case_skipped(const doctest::TestCaseData& /*in*/) override {}

	const doctest::TestCaseData* Current = nullptr;
};

struct TestRunner::Impl
{
	Impl() { REGISTER_LISTENER("ZenTestListener", 1, TestListener); }
	doctest::Context Session;
};

TestRunner::TestRunner()
{
	m_Impl = std::make_unique<Impl>();
}

TestRunner::~TestRunner()
{
}

int
TestRunner::ApplyCommandLine(int argc, char const* const* argv)
{
	m_Impl->Session.applyCommandLine(argc, argv);

	for (int i = 1; i < argc; ++i)
	{
		if (argv[i] == "--debug"sv)
		{
			zen::logging::SetLogLevel(zen::logging::level::Debug);
		}
		else if (argv[i] == "--verbose"sv)
		{
			zen::logging::SetLogLevel(zen::logging::level::Trace);
		}
	}

	return 0;
}

int
TestRunner::Run()
{
	return m_Impl->Session.run();
}

}  // namespace zen::testing

#endif	// ZEN_WITH_TESTS