CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Test.hpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Test.hpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/18 20:31:22 by kamitsui #+# #+# */
9/* Updated: 2025/04/23 01:43:52 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#ifndef TEST_HPP
19#define TEST_HPP
20
21#include <iostream>
22#include <stdexcept>
23#include <string>
24
25// Define colors for OK and KO messages
26#define GREEN "\033[0;32m";
27#define RED "\033[0;31m";
28#define NC "\033[0m";
29#define MSG_ASS_FAILED " Assertion \033[0;31mFAILED\033[0m: "
30#define MSG_FAILED "\033[0;31mFAILED\033[0m"
31#define MSG_PASSED "\033[0;32mPASSED\033[0m"
32
36class Test {
37 private:
38 int testCount;
39 int passCount;
40
41 public:
47 Test();
48
52 ~Test();
53
59 void runTest(const std::string &testName, void (*testFunction)(Test &));
60
67 template <typename T> void expectTrue(const T &condition, const std::string &message) {
68 ++testCount;
69 if (condition) {
70 ++passCount;
71 } else {
72 std::cerr << MSG_ASS_FAILED << message << std::endl;
73 }
74 }
75
82 template <typename T> void expectEq(const T &val1, const T &val2, const std::string &message) {
83 ++testCount;
84 if (val1 == val2) {
85 ++passCount;
86 } else {
87 std::cerr << MSG_ASS_FAILED << message << " (Expected: " << val2 << ", Actual: " << val1 << ")"
88 << std::endl;
89 }
90 }
91
98 template <typename ExceptionType, typename Callable>
99 void expectThrow(Callable statement, const std::string &message) {
100 ++testCount;
101 try {
102 statement(); // Execute the statement (which should throw)
103 std::cerr << MSG_ASS_FAILED << message << " (No exception thrown)" << std::endl;
104 } catch (const ExceptionType &e) {
105 ++passCount;
106 } catch (...) {
107 std::cerr << MSG_ASS_FAILED << message << " (Wrong exception type)" << std::endl;
108 }
109 };
110};
111
112#endif
#define MSG_ASS_FAILED
Definition Test.hpp:29
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cerr
A custom class for performing unit tests and reporting results.
Definition Test.hpp:36
void expectEq(const T &val1, const T &val2, const std::string &message)
Asserts that two values are equal.
Definition Test.hpp:82
void expectTrue(const T &condition, const std::string &message)
Asserts that a condition is true.
Definition Test.hpp:67
~Test()
Destructor for the Test class.
Definition Test.cpp:30
Test()
Default constructor for the Test class.
Definition Test.cpp:25
void runTest(const std::string &testName, void(*testFunction)(Test &))
Runs a single test case.
Definition Test.cpp:40
void expectThrow(Callable statement, const std::string &message)
Asserts that a statement throws an exception of the specified type.
Definition Test.hpp:99