Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
testing.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
30
31#pragma once
32
33#include <string>
34
35using namespace std::literals;
36
38// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
39#define TEST_START() \
40 /* NOLINTNEXTLINE(bugprone-exception-escape) */ \
41 int main() \
42 { \
43 using namespace steppable::__internals::stringUtils; \
44 using namespace steppable::__internals::utils; \
45 using namespace steppable::testing; \
46 using namespace steppable::output; \
47 Utf8CodePage use_utf8; \
48 int errors = 0;
49
51// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
52#define SECTION(...) \
53 { \
54 const std::string& nameSection = #__VA_ARGS__; \
55 std::cout << colors::brightBlue << std::setw(80) << std::setfill('-') << reset << '\n'; \
56 std::cout << colors::brightBlue << "[Testing: " << nameSection << ']' << reset << '\n'; \
57 auto start = std::chrono::high_resolution_clock::now(); \
58 auto _ = TestCase(nameSection);
59
61// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
62#define SECTION_END() \
63 auto end = std::chrono::high_resolution_clock::now(); \
64 auto duration = \
65 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start) \
66 .count(); \
67 _.summarize(); \
68 std::cout << colors::brightBlue << '[' << nameSection << " took " << duration << "(microseconds) to finish]" \
69 << reset << '\n'; \
70 std::cout << reset << '\n'; \
71 errors += _.errorCount; \
72 }
73
75// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
76#define TEST_END() \
77 if (errors) \
78 error("TEST_END"s, "Not all tests passed. There are {0} errors."s, { std::to_string(errors) }); \
79 else \
80 info("TEST_END"s, "All tests passed."s); \
81 std::cout << colors::brightBlue << std::setw(80) << std::setfill('=') << reset << '\n'; \
82 if (errors) \
83 return 1; \
84 }
85
90namespace steppable::testing
91{
97 {
98 private:
104 void assert(bool condition, const std::string& conditionName);
105
106 std::string testCaseName;
107
108 public:
109 int errorCount = 0; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
110
115 explicit TestCase(std::string testCaseName);
116
122 void assertIsEqual(const std::string& a, const std::string& b);
123
129 void assertIsNotEqual(const std::string& a, const std::string& b);
130
136 void assertIsEqual(int a, int b);
137
143 void assertIsNotEqual(int a, int b);
144
149 void assertTrue(bool value);
150
155 void assertFalse(bool value);
156
160 void summarize() const;
161 };
162} // namespace steppable::testing
int errorCount
Definition testing.hpp:109
std::string testCaseName
Definition testing.hpp:106
TestCase(std::string testCaseName)
Constructs a new TestCase object with the given name.
Definition testing.cpp:36
void summarize() const
Prints a summary of the test case, including the number of errors encountered.
Definition testing.cpp:89
void assertFalse(bool value)
Asserts that a boolean value is false.
Definition testing.cpp:83
void assertTrue(bool value)
Asserts that a boolean value is true.
Definition testing.cpp:77
void assertIsEqual(const std::string &a, const std::string &b)
Asserts that two strings are equal.
Definition testing.cpp:51
void assertIsNotEqual(const std::string &a, const std::string &b)
Asserts that two strings are not equal.
Definition testing.cpp:57
void assert(bool condition, const std::string &conditionName)
Asserts a given condition and logs the condition name if it fails.
Definition testing.cpp:38
Includes testing utilities for Steppable.
Definition testing.cpp:35
Untitled