Skip to content

Commit 1ee1748

Browse files
committed
Revert to using enum for BeBetween
1 parent d9f6820 commit 1ee1748

File tree

7 files changed

+211
-147
lines changed

7 files changed

+211
-147
lines changed

examples/sample/example_spec.cpp

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,102 +5,102 @@
55

66

77
describe bool_spec("Some Tests", $ {
8-
context("true is", _ {
9-
it("true", _ {
10-
expect(static_cast<bool>(1)).to_equal(true);
11-
});
12-
13-
it("true", _ {
14-
expect(true).to_equal(true);
15-
});
16-
17-
});
18-
19-
int i = 4;
20-
explain("4", _ {
21-
it("equals 4", _ {
22-
expect(i).to_equal(4);
23-
});
24-
it("does not equal 5", _ {
25-
expect(i).not_().to_equal(5);
26-
});
27-
28-
it("plus 1 equals 5", _ {
29-
expect(i+1).to_equal(5);
30-
});
31-
});
32-
33-
explain("0 is", _ {
34-
it("between -1 and 1 (exclusive)", _ {
35-
expect(0).to_be_between(-1, 1).exclusive();
36-
});
37-
it("between -0 and 0 (inclusive)", _ {
38-
expect(0).to_be_between(-0,0).inclusive();
39-
});
40-
it("not between -0 and 0 (exclusive)", _ {
41-
expect(0).not_().to_be_between(-0,0).exclusive();
42-
});
43-
it("not between 1 and -1 (inclusive)", _ {
44-
expect(0).not_().to_be_between(1,-1).inclusive();
45-
});
46-
it("between -1.0 and 1.0 (exclusive)", _ {
47-
expect(0).to_be_between(-1.0, 1.0).exclusive();
48-
});
49-
});
50-
51-
52-
explain("{1,2,3}", _ {
53-
it("includes 1", _ {
54-
expect({1,2,3}).to_include({1});
55-
});
56-
it("includes [1,2,3]", _ {
57-
expect({1,2,3}).to_include({1,2,3});
58-
});
59-
it("does not include 4", _ {
60-
expect({1,2,3}).not_().to_include({4});
61-
});
62-
63-
it("does not include [4,5,6]", _ {
64-
expect({1,2,3}).not_().to_include({4,5,6});
65-
});
66-
});
67-
68-
69-
explain <std::list<int>> ({1,2,3}, _ {
70-
it(_ { is_expected().to_include(1); });
71-
72-
it("includes [1,2,3]", _ {
73-
expect<std::list<int>>({1,2,3}).to_include({1,2,3});
74-
});
75-
76-
it( _ { is_expected().not_().to_include(4); });
77-
78-
it("does not include [4,5,6]", _ {
79-
is_expected().not_().to_include({4,5,6});
80-
});
81-
});
82-
83-
84-
// context("std::map {{1,2},{3,4},{5,6}}", _ {
85-
// it("includes 1", _ {
86-
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
87-
// expect(m).to_include({1});
88-
// });
89-
// it("includes [1,2,3]", _ {
90-
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
91-
// expect(m).to_include({1,2,3});
92-
// });
93-
// it("does not include 4", _ {
94-
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
95-
// expect(m).not_().to_include({4});
96-
// });
97-
// it("does not include 4,5,6", _ {
98-
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
99-
// expect({1,2,3}).not_().to_include({4,5,6});
100-
// });
101-
// });
8+
context("true is", _ {
9+
it("true", _ {
10+
expect(static_cast<bool>(1)).to_equal(true);
11+
});
12+
13+
it("true", _ {
14+
expect(true).to_equal(true);
15+
});
16+
17+
});
18+
19+
int i = 4;
20+
explain("4", _ {
21+
it("equals 4", _ {
22+
expect(i).to_equal(4);
23+
});
24+
it("does not equal 5", _ {
25+
expect(i).not_().to_equal(5);
26+
});
27+
28+
it("plus 1 equals 5", _ {
29+
expect(i+1).to_equal(5);
30+
});
31+
});
32+
33+
explain("0 is", _ {
34+
using namespace Matchers;
35+
it("between -1 and 1 (exclusive)", _ {
36+
expect(0).to_be_between(-1, 1, RangeMode::exclusive);
37+
});
38+
it("between -0 and 0 (inclusive)", _ {
39+
expect(0).to_be_between(-0,0, RangeMode::inclusive);
40+
});
41+
it("not between -0 and 0 (exclusive)", _ {
42+
expect(0).not_().to_be_between(-0,0, RangeMode::exclusive);
43+
});
44+
it("not between 1 and -1", _ {
45+
expect(0).not_().to_be_between(1,-1);
46+
});
47+
it("between -1.0 and 1.0 (exclusive)", _ {
48+
expect(0).to_be_between(-1.0, 1.0, RangeMode::exclusive);
49+
});
50+
});
51+
52+
53+
explain("{1,2,3}", _ {
54+
it("includes 1", _ {
55+
expect({1,2,3}).to_include({1});
56+
});
57+
it("includes [1,2,3]", _ {
58+
expect({1,2,3}).to_include({1,2,3});
59+
});
60+
it("does not include 4", _ {
61+
expect({1,2,3}).not_().to_include({4});
62+
});
63+
64+
it("does not include [4,5,6]", _ {
65+
expect({1,2,3}).not_().to_include({4,5,6});
66+
});
67+
});
68+
69+
70+
explain <std::list<int>> ({1,2,3}, _ {
71+
it(_ { is_expected().to_include(1); });
72+
73+
it("includes [1,2,3]", _ {
74+
expect<std::list<int>>({1,2,3}).to_include({1,2,3});
75+
});
76+
77+
it( _ { is_expected().not_().to_include(4); });
78+
79+
it("does not include [4,5,6]", _ {
80+
is_expected().not_().to_include({4,5,6});
81+
});
10282
});
10383

84+
// context("std::map {{1,2},{3,4},{5,6}}", _ {
85+
// it("includes 1", _ {
86+
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
87+
// expect(m).to_include({1});
88+
// });
89+
// it("includes [1,2,3]", _ {
90+
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
91+
// expect(m).to_include({1,2,3});
92+
// });
93+
// it("does not include 4", _ {
94+
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
95+
// expect(m).not_().to_include({4});
96+
// });
97+
// it("does not include 4,5,6", _ {
98+
// std::map<int,int> m = {{1,2},{3,4},{5,6}};
99+
// expect({1,2,3}).not_().to_include({4,5,6});
100+
// });
101+
// });
102+
});
103+
104104
describe abs_spec("abs", $ {
105105
// you can use the `explain` keyword to
106106
// group behavior and nest descriptions
@@ -184,10 +184,10 @@ describe let_spec("let", $ {
184184
// });
185185

186186
describe list_spec("A list spec", $ {
187-
explain <std::list<int>> ({1,2,3,4}, _ {
188-
it( _ { is_expected().to_include(6); });
189-
});
187+
explain <std::list<int>> ({1,2,3,4}, _ {
188+
it( _ { is_expected().to_include(6); });
190189
});
190+
});
191191

192192
/* Here is the declaration of fabs description defined in an other file (fabs_spec.c in this sample)*/
193193
int main(){

include/expectations/expectation.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class Expectation : public Child {
103103
bool to_include(E expected, std::string msg = "");
104104

105105
template <typename E>
106-
Matchers::BeBetween<A, E> to_be_between(E min, E max, std::string msg = "");
106+
bool to_be_between(E min, E max,
107+
Matchers::RangeMode mode = Matchers::RangeMode::inclusive,
108+
std::string msg = "");
107109

108110
template <typename E>
109111
bool to_equal(E expected, std::string msg = "");
@@ -186,11 +188,11 @@ bool Expectation<A>::to_be_false(std::string msg) {
186188
*/
187189
template <typename A>
188190
template <typename E>
189-
Matchers::BeBetween<A, E> Expectation<A>::to_be_between(E min, E max,
190-
std::string msg) {
191-
Matchers::BeBetween<A, E> matcher(*this, min, max);
192-
matcher.set_message(msg);
193-
return matcher;
191+
bool Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode,
192+
std::string msg) {
193+
return Matchers::BeBetween<A, E>(*this, min, max, mode)
194+
.set_message(msg)
195+
.run();
194196
}
195197

196198
/**

include/matchers/be_between.hpp

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,39 @@
44
#include "basematcher.hpp"
55

66
namespace Matchers {
7+
enum class RangeMode { exclusive, inclusive };
8+
79
template <typename A, typename E>
810
class BeBetween : public BaseMatcher<A, E> {
9-
enum class RangeMode { exclusive, inclusive };
1011
E min;
1112
E max;
12-
RangeMode mode = RangeMode::inclusive;
13+
RangeMode mode;
1314
enum class LtOp { lt, lt_eq } lt_op;
1415
enum class GtOp { gt, gt_eq } gt_op;
1516

1617
public:
17-
BeBetween(Expectations::Expectation<A> &expectation, E min, E max)
18-
: BaseMatcher<A, E>(expectation), min(min), max(max) {}
18+
// BeBetween(Expectations::Expectation<A> &expectation, E min, E max)
19+
// : BaseMatcher<A, E>(expectation), min(min), max(max) {}
1920

20-
bool inclusive();
21-
bool exclusive();
21+
BeBetween(Expectations::Expectation<A> &expectation, E min, E max,
22+
RangeMode mode = RangeMode::inclusive)
23+
: BaseMatcher<A, E>(expectation), min(min), max(max), mode(mode) {
24+
switch (mode) {
25+
case RangeMode::inclusive:
26+
lt_op = LtOp::lt_eq;
27+
gt_op = GtOp::gt_eq;
28+
break;
29+
case RangeMode::exclusive:
30+
lt_op = LtOp::lt;
31+
gt_op = GtOp::gt;
32+
break;
33+
}
34+
}
2235

2336
bool match() override;
2437
std::string description() override;
2538
};
2639

27-
/**
28-
* Makes the between comparison inclusive
29-
*
30-
* @example
31-
* expect(4).to(be_between(2,3).inclusive())
32-
*
33-
* @note The matcher is inclusive by default; this simply
34-
* provides a way to be more explicit about it.
35-
*/
36-
template <typename A, typename E>
37-
bool BeBetween<A, E>::inclusive() {
38-
lt_op = LtOp::lt_eq;
39-
gt_op = GtOp::gt_eq;
40-
mode = RangeMode::inclusive;
41-
return this->run();
42-
}
43-
44-
/**
45-
* Makes the between comparison exclusive
46-
*
47-
* @example
48-
* expect(4).to(be_between(2,3).exclusive())
49-
*/
50-
template <typename A, typename E>
51-
bool BeBetween<A, E>::exclusive() {
52-
lt_op = LtOp::lt;
53-
gt_op = GtOp::gt;
54-
mode = RangeMode::exclusive;
55-
return this->run();
56-
}
57-
5840
template <typename A, typename E>
5941
bool BeBetween<A, E>::match() {
6042
auto actual = this->get_actual();

spec/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ add_executable( expectation_spec expectations/expectation_spec.cpp )
1010
target_link_libraries( expectation_spec c++spec ${CMAKE_THREAD_LIBS_INIT} )
1111
add_test ( expectation_spec expectation_spec )
1212

13+
add_executable( be_between_spec matchers/be_between_spec.cpp )
14+
target_link_libraries( be_between_spec c++spec ${CMAKE_THREAD_LIBS_INIT} )
15+
add_test ( be_between_spec be_between_spec )
1316

1417
add_executable( be_within_spec matchers/be_within_spec.cpp )
1518
target_link_libraries( be_within_spec c++spec ${CMAKE_THREAD_LIBS_INIT} )

spec/matchers/be_between_spec.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "cppspec.hpp"
2+
3+
//describe be_between_spec("BeBetween", $ {
4+
// class SizeMatters : public Matchers::BaseMatcher<std::string, std::string> {
5+
// SizeMatters(Expectations::Expectation<std::string> &expectation, std::string expected)
6+
// : Matchers::BaseMatcher<std::string,std::string>(expectation, expected){};
7+
// bool match() { return this->get_actual() == this->get_expected(); }
8+
// };
9+
//
10+
// describe("expect(...).to_be_between(min, max) (inclusive)", _ {
11+
// it_behaves_like("be_between", &Matchers::BeBetween::inclusive, _ {
12+
// Matchers::BeBetween matcher(auto min, auto max) {
13+
// return Matchers::BeBetween(self, min, max);
14+
// }
15+
// });
16+
//
17+
// it("is inclusive", _ {
18+
// expect(1).to_be_between(1, 10).run();
19+
// expect(10).to_be_between(1, 10).run();
20+
// });
21+
//
22+
// it("indicates it was not comparable if it does not respond to <= and >=", _ {
23+
// expect([=]{
24+
// expect(nullptr).to_be_between(0,10).run();
25+
// }).to_fail_with("expected nullptr to be between 0 and 10")
26+
// });
27+
// });
28+
//});
29+
30+
int main() { return EXIT_SUCCESS; }

spec/matchers/be_between_spec.hpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)