Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Testing.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Foundation/StringPath.h"
6#include "../Foundation/StringSpan.h"
7
8namespace SC
9{
10struct TestCase;
13
16
22{
23 struct IOutput
24 {
25 virtual ~IOutput() = default;
26
27 virtual void printLine(StringSpan text) = 0;
28 virtual void print(StringSpan text) = 0;
29 virtual void print(StringSpan text, StringSpan p0) = 0;
30 virtual void print(StringSpan text, StringSpan p0, StringSpan p1) = 0;
31 virtual void print(StringSpan text, size_t p0) = 0;
32 virtual void print(StringSpan text, size_t p0, size_t p1) = 0;
33 };
34
35 template <typename ConsoleType>
36 struct Output final : public TestReport::IOutput
37 {
38 ConsoleType& console;
39 Output(ConsoleType& console) : console(console) {}
40
41 virtual void printLine(StringSpan text) override { console.printLine(text); }
42 virtual void print(StringSpan text) override { console.print(text); }
43 virtual void print(StringSpan text, StringSpan p0) override { console.print(text, p0); }
44 virtual void print(StringSpan text, StringSpan p0, StringSpan p1) override { console.print(text, p0, p1); }
45 virtual void print(StringSpan text, size_t p0) override { console.print(text, p0); }
46 virtual void print(StringSpan text, size_t p0, size_t p1) override { console.print(text, p0, p1); }
47 };
48 IOutput& console;
49
53
54 // Options
57 bool quietMode = false;
58
63 TestReport(IOutput& console, int argc, const char** argv);
65
68 [[nodiscard]] int getTestReturnCode() const;
69
70 template <typename Statistics>
71 void runGlobalMemoryReport(Statistics stats, bool reportFailure = true)
72 {
73 TestReport::MemoryStatistics memStats;
74 memStats.numAllocate = stats.numAllocate;
75 memStats.numReallocate = stats.numReallocate;
76 memStats.numRelease = stats.numRelease;
77 internalRunGlobalMemoryReport(memStats, reportFailure);
78 }
79
80 private:
81 struct MemoryStatistics
82 {
83 size_t numAllocate = 0;
84 size_t numReallocate = 0;
85 size_t numRelease = 0;
86 };
87
89 void internalRunGlobalMemoryReport(MemoryStatistics stats, bool reportFailure);
90
91 [[nodiscard]] bool isTestEnabled(StringSpan testName) const;
92 [[nodiscard]] bool isSectionEnabled(StringSpan sectionName) const;
93
94 void testCaseFinished(TestCase& testCase);
95 void printSectionResult(TestCase& testCase);
96
97 friend struct TestCase;
98 uint32_t numTestsSucceeded = 0;
99 uint32_t numTestsFailed = 0;
100 StringSpan currentSection;
101 StringSpan firstFailedTest;
102 StringSpan testToRun;
103 StringSpan sectionToRun;
104};
105
112{
117 ~TestCase();
118
124 bool recordExpectation(StringSpan expression, bool status, StringSpan detailedError = StringSpan());
125
130 bool recordExpectation(StringSpan expression, Result status);
131
132 enum class Execute
133 {
134 Default,
136 };
137
142 [[nodiscard]] bool test_section(StringSpan sectionName, Execute execution = Execute::Default);
143
145 private:
146 friend struct TestReport;
147 const StringSpan testName;
148
149 uint32_t numTestsSucceeded;
150 uint32_t numTestsFailed;
151 uint32_t numSectionTestsFailed;
152 bool printedSection;
153};
154
155// clang-format off
157#define SC_TEST_EXPECT(e) recordExpectation(StringSpan(#e), (e)) ? (void)0 : (TestCase::report.debugBreakOnFailedTest ? SC_COMPILER_DEBUG_BREAK : (void)0)
158// clang-format on
159
161} // namespace SC
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:42
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
A test case that can be split into multiple sections.
Definition Testing.h:112
bool recordExpectation(StringSpan expression, Result status)
Records an expectation for a given expression.
Execute
Definition Testing.h:133
@ OnlyExplicit
Test is executed only if explicitly requested with –test-section.
@ Default
Test is executed if all tests are enabled or if this specific one matches –test-section.
TestCase(TestReport &report, StringSpan testName)
Adds this TestCase to a TestReport with a name.
TestReport & report
The TestReport object passed in the constructor.
Definition Testing.h:144
bool recordExpectation(StringSpan expression, bool status, StringSpan detailedError=StringSpan())
Records an expectation for a given expression.
bool test_section(StringSpan sectionName, Execute execution=Execute::Default)
Starts a new test section.
Definition Testing.h:24
Definition Testing.h:37
Collects multiple TestCase and reports their results.
Definition Testing.h:22
StringPath applicationRootDirectory
Path to application (on macOS is different from executable path)
Definition Testing.h:52
StringPath executableFile
Path to current executable.
Definition Testing.h:51
int getTestReturnCode() const
Gets return code for this process.
bool quietMode
If true will not print recaps at start or end of the test.
Definition Testing.h:57
bool abortOnFirstFailedTest
If true will abort after first failed test.
Definition Testing.h:55
StringPath libraryRootDirectory
Path to sources directory for library.
Definition Testing.h:50
bool debugBreakOnFailedTest
If true will issue a debugger halt when a test fails.
Definition Testing.h:56
TestReport(IOutput &console, int argc, const char **argv)
Build from a console and executable arguments.