Skip to content

Commit 54c9bd3

Browse files
committed
Remove macros in it,xit and pre-post conditions
1 parent b68cb76 commit 54c9bd3

File tree

1 file changed

+70
-38
lines changed

1 file changed

+70
-38
lines changed

src/test-builder.hpp

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
#pragma once
2+
#include <source_location>
23
#include "types.hpp"
34
#include "globals.hpp"
45

56
#define PP(x,y) x##y
67
#define P(x,y) PP(x,y)
78

89
#define describe(...) __attribute__((unused)) static int P(dummy, __LINE__) = cest::describeFn(__VA_ARGS__)
9-
#define it(...) cest::itFn(__FILE__, __LINE__, __VA_ARGS__)
10-
#define xit(...) cest::xitFn(__FILE__, __LINE__, __VA_ARGS__)
11-
#define fit(...) cest::fitFn(__FILE__, __LINE__, __VA_ARGS__)
12-
#define beforeEach(x) cest::beforeEachFn(__FILE__, __LINE__, x)
13-
#define afterEach(x) cest::afterEachFn(__FILE__, __LINE__, x)
14-
#define beforeAll(x) cest::beforeAllFn(__FILE__, __LINE__, x)
15-
#define afterAll(x) cest::afterAllFn(__FILE__, __LINE__, x)
1610

1711
namespace cest
1812
{
@@ -70,42 +64,80 @@ namespace cest
7064

7165
return 0;
7266
}
67+
}
7368

74-
void itFn(std::string file, int line, std::string name, std::function<void()> fn)
75-
{
76-
TestCase *test = TestCaseBuilder(file, line, name, fn).build();
77-
__cest_globals.current_test_suite->test_cases.push_back(test);
78-
}
69+
void it(
70+
const std::string& name,
71+
const std::function<void()>& fn,
72+
const std::source_location location = std::source_location::current())
73+
{
74+
const auto file = location.file_name();
75+
const auto line = location.line();
7976

80-
void fitFn(std::string file, int line, std::string name, std::function<void()> fn)
81-
{
82-
TestCase *test = TestCaseBuilder(file, line, name, fn).Focused()->build();
83-
__cest_globals.current_test_suite->test_cases.push_back(test);
84-
}
77+
cest::TestCase *test = cest::TestCaseBuilder(file, line, name, fn).build();
78+
__cest_globals.current_test_suite->test_cases.push_back(test);
79+
}
8580

86-
void xitFn(std::string file, int line, std::string name, std::function<void()> fn)
87-
{
88-
TestCase *test = TestCaseBuilder(file, line, name, fn).skipped()->build();
89-
__cest_globals.current_test_suite->test_cases.push_back(test);
90-
}
81+
void fit(
82+
const std::string& name,
83+
const std::function<void()>& fn,
84+
const std::source_location location = std::source_location::current())
85+
{
86+
const auto file = location.file_name();
87+
const auto line = location.line();
9188

92-
void beforeEachFn(std::string file, int line, std::function<void()> fn)
93-
{
94-
__cest_globals.current_test_suite->before_each = { file, line, fn};
95-
}
89+
cest::TestCase *test = cest::TestCaseBuilder(file, line, name, fn).Focused()->build();
90+
__cest_globals.current_test_suite->test_cases.push_back(test);
91+
}
9692

97-
void afterEachFn(std::string file, int line, std::function<void()> fn)
98-
{
99-
__cest_globals.current_test_suite->after_each = { file, line, fn};
100-
}
93+
void xit(
94+
const std::string& name,
95+
const std::function<void()>& fn,
96+
const std::source_location location = std::source_location::current())
97+
{
98+
const auto file = location.file_name();
99+
const auto line = location.line();
101100

102-
void beforeAllFn(std::string file, int line, std::function<void()> fn)
103-
{
104-
__cest_globals.current_test_suite->before_all = { file, line, fn};
105-
}
101+
cest::TestCase *test = cest::TestCaseBuilder(file, line, name, fn).skipped()->build();
102+
__cest_globals.current_test_suite->test_cases.push_back(test);
103+
}
106104

107-
void afterAllFn(std::string file, int line, std::function<void()> fn)
108-
{
109-
__cest_globals.current_test_suite->after_all = { file, line, fn};
110-
}
105+
void beforeEach(
106+
const std::function<void()>& fn,
107+
const std::source_location location = std::source_location::current())
108+
{
109+
const auto file = std::string(location.file_name());
110+
int line = location.line();
111+
112+
__cest_globals.current_test_suite->before_each = { file, line, fn};
113+
}
114+
115+
void afterEach(
116+
const std::function<void()>& fn,
117+
const std::source_location location = std::source_location::current())
118+
{
119+
const auto file = location.file_name();
120+
int line = location.line();
121+
122+
__cest_globals.current_test_suite->after_each = { file, line, fn};
123+
}
124+
125+
void beforeAll(
126+
const std::function<void()>& fn,
127+
const std::source_location location = std::source_location::current())
128+
{
129+
const auto file = location.file_name();
130+
int line = location.line();
131+
132+
__cest_globals.current_test_suite->before_all = { file, line, fn};
133+
}
134+
135+
void afterAll(
136+
const std::function<void()>& fn,
137+
const std::source_location location = std::source_location::current())
138+
{
139+
const auto file = location.file_name();
140+
int line = location.line();
141+
142+
__cest_globals.current_test_suite->after_all = { file, line, fn};
111143
}

0 commit comments

Comments
 (0)