Sane C++ Libraries
C++ Platform Abstraction Libraries
Testing.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Strings/Console.h" // SC_COMPILER_DEBUG_BREAK
6#include "../Strings/StringView.h"
7
8namespace SC
9{
10struct TestCase;
11struct TestReport;
12} // namespace SC
13
16
19
25{
30
31 // Options
34 bool quietMode = false;
35
40 TestReport(Console& console, int argc, const char** argv);
42
45 [[nodiscard]] int getTestReturnCode() const;
46
47 private:
48 [[nodiscard]] bool isTestEnabled(StringView testName) const;
49 [[nodiscard]] bool isSectionEnabled(StringView sectionName) const;
50
51 void testCaseFinished(TestCase& testCase);
52 void printSectionResult(TestCase& testCase);
53
54 friend struct TestCase;
55 uint32_t numTestsSucceeded = 0;
56 uint32_t numTestsFailed = 0;
57 StringView currentSection;
58 StringView firstFailedTest;
59 StringView testToRun;
60 StringView sectionToRun;
61};
62
69{
74 ~TestCase();
75
81 bool recordExpectation(StringView expression, bool status, StringView detailedError = StringView());
82
87 bool recordExpectation(StringView expression, Result status);
88
89 enum class Execute
90 {
91 Default,
93 };
94
99 [[nodiscard]] bool test_section(StringView sectionName, Execute execution = Execute::Default);
100
102 private:
103 friend struct TestReport;
104 const StringView testName;
105
106 uint32_t numTestsSucceeded;
107 uint32_t numTestsFailed;
108 uint32_t numSectionTestsFailed;
109 bool printedSection;
110};
111
113#define SC_TEST_EXPECT(e) \
114 recordExpectation(StringView(#e##_a8), (e)) \
115 ? (void)0 \
116 : (TestCase::report.debugBreakOnFailedTest ? SC_COMPILER_DEBUG_BREAK : (void)0)
117
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
Writes to console using SC::StringFormat.
Definition: Console.h:27
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A test case that can be split into multiple sections.
Definition: Testing.h:69
bool recordExpectation(StringView expression, bool status, StringView detailedError=StringView())
Records an expectation for a given expression.
Execute
Definition: Testing.h:90
@ 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.
TestReport & report
The TestReport object passed in the constructor.
Definition: Testing.h:101
TestCase(TestReport &report, StringView testName)
Adds this TestCase to a TestReport with a name.
bool test_section(StringView sectionName, Execute execution=Execute::Default)
Starts a new test section.
bool recordExpectation(StringView expression, Result status)
Records an expectation for a given expression.
Collects multiple TestCase and reports their results.
Definition: Testing.h:25
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:34
bool abortOnFirstFailedTest
If true will abort after first failed test.
Definition: Testing.h:32
TestReport(Console &console, int argc, const char **argv)
Build from a console and executable arguments.
StringView libraryRootDirectory
Path to sources directory for library.
Definition: Testing.h:27
bool debugBreakOnFailedTest
If true will issue a debugger halt when a test fails.
Definition: Testing.h:33
StringView executableFile
Path to current executable.
Definition: Testing.h:28
Console & console
The passed in console object where to print results.
Definition: Testing.h:26
StringView applicationRootDirectory
Path to application (on macOS is different from executable path)
Definition: Testing.h:29