Skip to content

Commit 4dc47c0

Browse files
committed
Support BDD style macros
1 parent 15ab116 commit 4dc47c0

5 files changed

Lines changed: 67 additions & 3 deletions

File tree

include/zeroerr/internal/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
// If you wish to disable AND, OR macro
3030
// #define ZEROERR_DISABLE_COMPLEX_AND_OR
3131

32+
// If you wish ot disable BDD style macros
33+
// #define ZEROERR_DISABLE_BDD
3234

3335
// Detect C++ standard with a cross-platform way
3436

include/zeroerr/unittest.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
1818

1919
#define SUB_CASE(name) \
2020
zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT) \
21-
<< [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT))
21+
<< [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
2222

2323
#define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name) \
2424
class classname : public fixture { \
@@ -40,6 +40,12 @@ ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
4040

4141
#define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
4242

43+
#ifndef ZEROERR_DISABLE_BDD
44+
#define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
45+
#define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
46+
#define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
47+
#define THEN(...) SUB_CASE("then: " __VA_ARGS__)
48+
#endif
4349

4450
namespace zeroerr {
4551

src/unittest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void TestContext::reset() {
6969
static inline std::string getFileName(std::string file) {
7070
std::string fileName(file);
7171
auto p = fileName.find_last_of('/');
72+
if (p == std::string::npos) p = fileName.find_last_of('\\');
7273
if (p != std::string::npos) fileName = fileName.substr(p + 1);
7374
return fileName;
7475
}

test/unit_test.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,50 @@ TEST_CASE("combinational test args") {
175175

176176
TEST_CASE("check 0") {
177177
CHECK(0 == 1);
178-
}
178+
}
179+
180+
181+
SCENARIO("vectors can be sized and resized") {
182+
GIVEN("A vector with some items") {
183+
std::vector<int> v(5);
184+
185+
REQUIRE(v.size() == 5);
186+
REQUIRE(v.capacity() >= 5);
187+
188+
WHEN("the size is increased") {
189+
v.resize(10);
190+
191+
THEN("the size and capacity change") {
192+
CHECK(v.size() == 20);
193+
CHECK(v.capacity() >= 10);
194+
};
195+
};
196+
197+
WHEN("the size is reduced") {
198+
v.resize(0);
199+
200+
THEN("the size changes but not capacity") {
201+
CHECK(v.size() == 0);
202+
CHECK(v.capacity() >= 5);
203+
};
204+
};
205+
206+
WHEN("more capacity is reserved") {
207+
v.reserve(10);
208+
209+
THEN("the capacity changes but not the size") {
210+
CHECK(v.size() == 5);
211+
CHECK(v.capacity() >= 10);
212+
};
213+
};
214+
215+
WHEN("less capacity is reserved") {
216+
v.reserve(0);
217+
218+
THEN("neither size nor capacity are changed") {
219+
CHECK(v.size() == 10);
220+
CHECK(v.capacity() >= 5);
221+
};
222+
};
223+
};
224+
}

zeroerr.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
// If you wish to disable AND, OR macro
3333
// #define ZEROERR_DISABLE_COMPLEX_AND_OR
3434

35+
// If you wish ot disable BDD style macros
36+
// #define ZEROERR_DISABLE_BDD
3537

3638
// Detect C++ standard with a cross-platform way
3739

@@ -3625,7 +3627,7 @@ ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
36253627

36263628
#define SUB_CASE(name) \
36273629
zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT) \
3628-
<< [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT))
3630+
<< [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
36293631

36303632
#define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name) \
36313633
class classname : public fixture { \
@@ -3647,6 +3649,12 @@ ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
36473649

36483650
#define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
36493651

3652+
#ifndef ZEROERR_DISABLE_BDD
3653+
#define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
3654+
#define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
3655+
#define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
3656+
#define THEN(...) SUB_CASE("then: " __VA_ARGS__)
3657+
#endif
36503658

36513659
namespace zeroerr {
36523660

@@ -4929,6 +4937,7 @@ void TestContext::reset() {
49294937
static inline std::string getFileName(std::string file) {
49304938
std::string fileName(file);
49314939
auto p = fileName.find_last_of('/');
4940+
if (p == std::string::npos) p = fileName.find_last_of('\\');
49324941
if (p != std::string::npos) fileName = fileName.substr(p + 1);
49334942
return fileName;
49344943
}

0 commit comments

Comments
 (0)