Skip to content

Commit bb1a26f

Browse files
committed
Features/lazy-expectation
commit d7e8604f014b0837a09406b12b342374d7b51fbe Author: Katherine Whitlock <[email protected]> Date: Fri Apr 29 01:08:35 2016 -0400 More matchers and refactoring commit ec9e7653cac7c29ba4c19ec874c8214f9c709691 Author: Katherine Whitlock <[email protected]> Date: Thu Apr 28 22:30:13 2016 -0400 Refactor names to be consistent commit 60385f5891f5e1817d45cb625ef07d53c7a89248 Author: Katherine Whitlock <[email protected]> Date: Thu Apr 28 22:19:56 2016 -0400 More matchers and a Jasmine-intro port commit 5eee0910752177f95aa461c27a1dbcbb28538343 Author: Katherine Whitlock <[email protected]> Date: Thu Apr 28 19:12:06 2016 -0400 Working exception test commit f12b428b99aa5169e4262da07f5862a16cd27141 Author: Katherine Whitlock <[email protected]> Date: Thu Apr 28 18:29:26 2016 -0400 Initial support for lazy evaluation
1 parent 85a2bee commit bb1a26f

31 files changed

+1018
-306
lines changed

examples/sample/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ add_executable(cppspec_sample
33
)
44

55
target_link_libraries(cppspec_sample c++spec)
6+
7+
add_executable(jasmine_intro jasmine_intro.cpp)
8+
target_link_libraries(jasmine_intro c++spec)

examples/sample/example_spec.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,34 @@ describe bool_spec("Some Tests", $ {
4949

5050

5151
explain("{1,2,3}", _ {
52-
it("includes 1", _ {
53-
expect({1,2,3}).to_include({1});
52+
it("contains 1", _ {
53+
expect({1,2,3}).to_contain({1});
5454
});
5555
it("includes [1,2,3]", _ {
56-
expect({1,2,3}).to_include({1,2,8});
57-
expect({1,2,3}).to_include({1,2,7});
56+
expect({1,2,3}).to_contain({1,2,8});
57+
expect({1,2,3}).to_contain({1,2,7});
5858
});
5959
it("does not include 4", _ {
60-
expect({1,2,3}).not_().to_include({4});
60+
expect({1,2,3}).not_().to_contain({4});
6161
});
6262

6363
it("does not include [4,5,6]", _ {
64-
expect({1,2,3}).not_().to_include({4,5,6});
64+
expect({1,2,3}).not_().to_contain({4,5,6});
6565
});
6666
});
6767

6868

6969
explain <std::list<int>> ({1,2,3}, _ {
70-
it(_ { is_expected().to_include(1); });
70+
it(_ { is_expected().to_contain(1); });
7171

7272
it("includes [1,2,3]", _ {
73-
expect<std::list<int>>({1,2,3}).to_include({1,2,3});
73+
expect<std::list<int>>({1,2,3}).to_contain({1,2,3});
7474
});
7575

76-
it( _ { is_expected().not_().to_include(4); });
76+
it( _ { is_expected().not_().to_contain(4); });
7777

7878
it("does not include [4,5,6]", _ {
79-
is_expected().not_().to_include({4,5,6});
79+
is_expected().not_().to_contain({4,5,6});
8080
});
8181
});
8282

@@ -140,22 +140,22 @@ describe strcmp_spec("int strcmp ( const char * str1, const char * str2 )", $ {
140140
});
141141

142142
it("returns a negative integer when str1 is less than str2", _ {
143-
expect(strcmp("hello", "world")).to_be(less_than_zero);
144-
expect(strcmp("0123", "1321431")).to_be(less_than_zero);
143+
expect(strcmp("hello", "world")).to_satisfy(less_than_zero);
144+
expect(strcmp("0123", "1321431")).to_satisfy(less_than_zero);
145145
});
146146

147147
it("returns a positive integer if str1 is greater than str2", _ {
148-
expect(strcmp("yellow", "world")).to_be(greater_than_zero);
149-
expect(strcmp("9", "789")).to_be(greater_than_zero);
148+
expect(strcmp("yellow", "world")).to_satisfy(greater_than_zero);
149+
expect(strcmp("9", "789")).to_satisfy(greater_than_zero);
150150
});
151151
});
152152

153153
describe_a <std::vector<int>> vector_spec({1,2,3,4}, $ {
154154
it("should contain 2", _ {
155-
expect({1,2,3,4}).to_include(2);
155+
expect({1,2,3,4}).to_contain(2);
156156
});
157157

158-
it( _ { is_expected().to_include(2); });
158+
it( _ { is_expected().to_contain(2); });
159159
});
160160

161161
int _count = 0;
@@ -184,7 +184,8 @@ describe let_spec("let", $ {
184184

185185
describe list_spec("A list spec", $ {
186186
explain <std::list<int>> ({1,2,3,4}, _ {
187-
it( _ { is_expected().to_include(8); });
187+
it( _ { is_expected().to_contain(8); });
188+
it( _ { is_expected().to_start_with({1,2,3}); });
188189
});
189190
});
190191

examples/sample/jasmine_intro.cpp

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
// Copyright 2016 Katherine Whitlock
2+
3+
#include "cppspec.hpp"
4+
5+
describe a_suite("A suite", $ {
6+
it("contains a spec with an expectation", _ {
7+
expect(true).to_be_true();
8+
});
9+
});
10+
11+
describe suite_object("A suite is just an object wrapping a lambda", $ {
12+
bool a = false;
13+
14+
it("and so is a spec", _ {
15+
a = true;
16+
expect(a).to_be_true();
17+
});
18+
});
19+
20+
describe to_be_compare("The 'to_satisfy' matcher compares equality via evaluation",
21+
$ {
22+
it("and has a positive case", _ {
23+
expect(true).to_satisfy([](const bool &b) { return b == true; });
24+
});
25+
26+
it("and can have a negative case", _ {
27+
expect(false).not_().to_satisfy([](const bool &b) { return b == true; });
28+
});
29+
});
30+
31+
32+
describe to_equal_compare("The 'to_equal' matcher compares with '=='", $ {
33+
it("and has a positive case", _ {
34+
expect(true).to_equal(true);
35+
});
36+
37+
it("and can have a negative case", _ {
38+
expect(false).not_().to_equal(true);
39+
});
40+
});
41+
42+
struct AB { int a; int b; };
43+
inline bool operator==(const AB& lhs, const AB&rhs) {
44+
return lhs.a == rhs.a && lhs.b == rhs.b;
45+
}
46+
47+
48+
describe included_matchers("Included matchers:", $ {
49+
it("The 'to_be' matcher compares equality via lambda evaluation", _ {
50+
int a = 12;
51+
int b = a;
52+
53+
expect(a).to_satisfy([b](const int &value) -> bool { return value == b;});
54+
// to_equal is shorter for things that can be compared
55+
// with operator==
56+
expect(a).to_equal(b);
57+
});
58+
59+
context("The 'to_equal' matcher", _ {
60+
it("works for simple literals and variables", _ {
61+
auto a = 12;
62+
expect(a).to_equal(12);
63+
});
64+
65+
it("should work for objects with operator== defined", _ {
66+
AB foo;
67+
foo.a = 12;
68+
foo.b = 34;
69+
70+
AB bar;
71+
bar.a = 12;
72+
bar.b = 34;
73+
74+
expect(foo).to_equal(bar);
75+
});
76+
});
77+
// Not yet implemented
78+
// it("The 'to_match' matcher is for regular expressions", _ {
79+
// std::string message = "foo bar baz";
80+
//
81+
// expect(message).to_match(std::regex("bar"));
82+
// expect(message).to_match("bar");
83+
// expect(message).not_().to_match(std::regex("quux"));
84+
// });
85+
86+
it("The 'to_be_truthy' matcher is for boolean casting testing", _ {
87+
void* a = nullptr;
88+
//auto foo = "foo";
89+
90+
// expect(foo).to_be_truthy();
91+
expect(a).not_().to_be_truthy();
92+
});
93+
94+
it("The 'to_be_falsy' matcher is for boolean casting testing", _ {
95+
void* a = nullptr;
96+
//auto foo = "foo";
97+
98+
expect(a).to_be_falsy();
99+
// expect(foo).not_().to_be_falsy();
100+
});
101+
102+
it("The 'to_include' matcher is for finding an item in a container", _ {
103+
auto a = {"foo", "bar", "baz"};
104+
105+
expect(a).to_contain("bar");
106+
expect(a).not_().to_contain("quux");
107+
});
108+
109+
it("The 'to_be_less_than' matcher is for mathematical comparisons", _ {
110+
auto pi = 3.1415926,
111+
e = 2.78;
112+
113+
expect(e).to_be_less_than(pi);
114+
expect(pi).not_().to_be_less_than(e);
115+
});
116+
117+
it("The 'to_be_greater_than' matcher is for mathematical comparisons", _ {
118+
auto pi = 3.1415926,
119+
e = 2.78;
120+
121+
expect(pi).to_be_greater_than(e);
122+
expect(e).not_().to_be_greater_than(pi);
123+
});
124+
125+
// TODO: take another look at Jasmine's toBeCloseTo
126+
// it("The 'to_be_within' matcher is for precision match comparison", _ {
127+
// auto pi = 3.1415926,
128+
// e = 2.78;
129+
//
130+
// expect(pi).not_().to_be_within(0.5).of(e);
131+
// });
132+
133+
it("The 'to_throw' matcher is for testing if a function throws an exception", _ {
134+
std::function<int()> foo = [] {
135+
return 1 + 2;
136+
};
137+
std::function<void*()> bar = [] {
138+
throw(std::exception());
139+
return nullptr;
140+
};
141+
142+
expect(foo).not_().to_throw();
143+
expect(bar).to_throw();
144+
});
145+
});
146+
147+
148+
//describe fail_function_spec("A spec using the fail function", $ {
149+
// auto foo = [](auto &x, auto &callback) {
150+
// if (x) {
151+
// callback();
152+
// }
153+
// };
154+
//
155+
// it("should not call the callback", _ {
156+
// foo(false, [] -> {
157+
// fail("Callback has been called");
158+
// });
159+
// });
160+
//});
161+
162+
describe a_spec("A spec", $ {
163+
it("is just a function, so it can contain any code", _ {
164+
auto foo = 0;
165+
foo += 1;
166+
expect(foo).to_equal(1);
167+
});
168+
169+
it("can have more than one expectation", _ {
170+
auto foo = 0;
171+
foo += 1;
172+
173+
expect(foo).to_equal(1);
174+
expect(true).to_equal(true);
175+
});
176+
});
177+
178+
describe a_spec_using_be_ae("A spec using before_each and after_each", $ {
179+
auto foo = 0;
180+
181+
before_all([&foo]{ foo = 1; });
182+
after_all([&foo]{ foo = 0; });
183+
184+
it("sets the initial value of foo before specs run", [&](auto &self) {
185+
expect(foo).to_equal(1);
186+
foo += 1;
187+
});
188+
189+
it("does not reset foo between specs", _ {
190+
expect(foo).to_equal(2);
191+
});
192+
});
193+
194+
describe a_spec_before_each("A spec", $ {
195+
int foo;
196+
before_each([&foo]{ foo = 0; });
197+
198+
it("can use a variable to share state", _ {
199+
expect(foo).to_equal(0);
200+
foo += 1;
201+
});
202+
203+
it("prevents test pollution by starting with a clean test environment", _ {
204+
expect(foo).to_equal(0);
205+
});
206+
});
207+
208+
describe a_spec_nesting("A spec", $ {
209+
int foo;
210+
211+
before_each([&foo] {
212+
foo = 0;
213+
foo += 1;
214+
});
215+
216+
after_each([&foo] {
217+
foo = 0;
218+
});
219+
220+
it("is just a function, so it can contain any code", _ {
221+
expect(foo).to_equal(1);
222+
});
223+
224+
it("can have more than one expectation", _ {
225+
expect(foo).to_equal(1);
226+
expect(true).to_equal(true);
227+
});
228+
229+
explain("nested contexts", _ {
230+
int bar;
231+
232+
before_each([&bar] {
233+
bar = 1;
234+
});
235+
236+
it("can reference both scopes as needed", _ {
237+
expect(foo).to_equal(bar);
238+
});
239+
});
240+
});
241+
242+
// xdescribes are unnecessary, as test are simply not run or
243+
// added to the Runner object.
244+
245+
// TODO: pending specs
246+
247+
describe to_include_matcher("to contain", $ {
248+
let(foo, ([]() -> std::vector<int> { return {1,2,3,4}; }));
249+
250+
it("matches arrays with some of the values", _ {
251+
expect(foo).to_contain({3,1});
252+
expect(foo).not_().to_contain(6);
253+
});
254+
});
255+
256+
int main() {
257+
return CppSpec::Runner(CppSpec::Formatters::verbose)
258+
.add_spec(a_suite)
259+
.add_spec(suite_object)
260+
.add_spec(to_be_compare)
261+
.add_spec(to_equal_compare)
262+
.add_spec(included_matchers)
263+
.add_spec(a_spec)
264+
.add_spec(a_spec_using_be_ae)
265+
.add_spec(a_spec_before_each)
266+
.add_spec(a_spec_nesting)
267+
.add_spec(to_include_matcher)
268+
.exec() ? EXIT_SUCCESS : EXIT_FAILURE;
269+
}

include/class_description.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ Result ClassDescription<T>::run(Formatters::BaseFormatter &printer) {
262262
}
263263

264264
template <class T>
265-
Expectations::Expectation<T> ItCd<T>::is_expected() {
265+
Expectations::ExpectationValue <T> ItCd<T>::is_expected() {
266266
auto cd = static_cast<ClassDescription<T> *>(this->get_parent());
267-
Expectations::Expectation<T> expectation(*this, cd->subject);
267+
Expectations::ExpectationValue<T> expectation(*this, cd->subject);
268268
return expectation;
269269
}
270270

0 commit comments

Comments
 (0)