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/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_TESTING
6#define SC_EXPORT_LIBRARY_TESTING 0
7#endif
8#define SC_TESTING_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_TESTING)
9
10#include "../Foundation/Result.h"
11#include "../Foundation/StringPath.h"
12#include "../Foundation/StringSpan.h"
13
14namespace SC
15{
16struct TestCase;
19
22
28{
29 struct IOutput
30 {
31 virtual ~IOutput() = default;
32
33 virtual void flush() = 0;
34 virtual void printLine(StringSpan text) = 0;
35 virtual void print(StringSpan text) = 0;
36 virtual void print(StringSpan text, StringSpan p0) = 0;
37 virtual void print(StringSpan text, StringSpan p0, StringSpan p1) = 0;
38 virtual void print(StringSpan text, size_t p0) = 0;
39 virtual void print(StringSpan text, size_t p0, size_t p1) = 0;
40 };
41
42 template <typename ConsoleType>
43 struct Output final : public TestReport::IOutput
44 {
45 ConsoleType& console;
46 Output(ConsoleType& console) : console(console) {}
47
48 virtual void flush() override { console.flush(); }
49 virtual void printLine(StringSpan text) override { console.printLine(text); }
50 virtual void print(StringSpan text) override { console.print(text); }
51 virtual void print(StringSpan text, StringSpan p0) override { console.print(text, p0); }
52 virtual void print(StringSpan text, StringSpan p0, StringSpan p1) override { console.print(text, p0, p1); }
53 virtual void print(StringSpan text, size_t p0) override { console.print(text, p0); }
54 virtual void print(StringSpan text, size_t p0, size_t p1) override { console.print(text, p0, p1); }
55 };
56 IOutput& console;
57
61
62 // Options
65 bool quietMode = false;
66 bool runAllTests = false;
67
72 TestReport(IOutput& console, int argc, const char** argv);
74
77 [[nodiscard]] int getTestReturnCode() const;
78
80 [[nodiscard]] bool hasStartupFailure() const { return startupFailure; }
81
85 [[nodiscard]] uint16_t mapPort(uint16_t basePort) const;
86
89 [[nodiscard]] bool isTestExplicitlySelected(StringSpan testName) const;
90
91 template <typename Statistics>
92 void runGlobalMemoryReport(Statistics stats, bool reportFailure = true)
93 {
94 TestReport::MemoryStatistics memStats;
95 memStats.numAllocate = stats.numAllocate;
96 memStats.numReallocate = stats.numReallocate;
97 memStats.numRelease = stats.numRelease;
98 internalRunGlobalMemoryReport(memStats, reportFailure);
99 }
100
101 private:
102 struct MemoryStatistics
103 {
104 size_t numAllocate = 0;
105 size_t numReallocate = 0;
106 size_t numRelease = 0;
107 };
108
110 void internalRunGlobalMemoryReport(MemoryStatistics stats, bool reportFailure);
111
112 [[nodiscard]] bool isTestEnabled(StringSpan testName) const;
113 [[nodiscard]] bool isSectionEnabled(StringSpan sectionName) const;
114
115 void testCaseFinished(TestCase& testCase);
116 void printSectionResult(TestCase& testCase);
117
118 friend struct TestCase;
119 uint32_t numTestsSucceeded = 0;
120 uint32_t numTestsFailed = 0;
121 uint16_t portOffset = 0;
122 StringSpan currentSection;
123 StringSpan firstFailedTest;
124 StringSpan testToRun;
125 StringSpan sectionToRun;
126 bool startupFailure = false;
127};
128
135{
140 ~TestCase();
141
147 bool recordExpectation(StringSpan expression, bool status, StringSpan detailedError = StringSpan());
148
153 bool recordExpectation(StringSpan expression, Result status);
154
155 enum class Execute
156 {
157 Default,
159 };
160
165 [[nodiscard]] bool test_section(StringSpan sectionName, Execute execution = Execute::Default);
166
168 private:
169 friend struct TestReport;
170 const StringSpan testName;
171
172 uint32_t numTestsSucceeded;
173 uint32_t numTestsFailed;
174 uint32_t numSectionTestsFailed;
175 bool printedSection;
176};
177
178// clang-format off
180#define SC_TEST_EXPECT(e) recordExpectation(StringSpan(#e), (e)) ? (void)0 : (TestCase::report.debugBreakOnFailedTest ? SC_COMPILER_DEBUG_BREAK : (void)0)
181// clang-format on
182
184} // namespace SC
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:28
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
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:135
bool recordExpectation(StringSpan expression, Result status)
Records an expectation for a given expression.
Execute
Definition Testing.h:156
@ 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:167
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:30
Definition Testing.h:44
Collects multiple TestCase and reports their results.
Definition Testing.h:28
StringPath applicationRootDirectory
Path to application (on macOS is different from executable path)
Definition Testing.h:60
bool hasStartupFailure() const
Returns true when startup path resolution failed before tests ran.
Definition Testing.h:80
StringPath executableFile
Path to current executable.
Definition Testing.h:59
uint16_t mapPort(uint16_t basePort) const
Applies an optional port offset configured through command line or environment.
int getTestReturnCode() const
Gets return code for this process.
bool isTestExplicitlySelected(StringSpan testName) const
Returns true only when --test explicitly matches testName.
bool quietMode
If true will not print recaps at start or end of the test.
Definition Testing.h:65
bool abortOnFirstFailedTest
If true will abort after first failed test.
Definition Testing.h:63
StringPath libraryRootDirectory
Path to sources directory for library.
Definition Testing.h:58
bool debugBreakOnFailedTest
If true will issue a debugger halt when a test fails.
Definition Testing.h:64
TestReport(IOutput &console, int argc, const char **argv)
Build from a console and executable arguments.
bool runAllTests
If true will run optional/slow tests that are skipped by default.
Definition Testing.h:66