Skip to content

Commit d6caf7b

Browse files
committed
Automatic captures in local lambdas
1 parent 2d845a8 commit d6caf7b

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ Have a taste of some RSpec in your C++.
88
#include "cppspec.hpp"
99

1010

11-
describe fabs_spec("fabs", _ {
12-
int n = 0;
13-
11+
describe fabs_spec("fabs", $ {
1412
// you can use the `explain` keyword to
1513
// group behavior and nest descriptions
1614
explain("argument is zero", _ {
1715
it("return zero", _ {
18-
expect(fabs(n)).to_equal(n);
16+
expect(fabs(0)).to_equal(0);
1917
});
2018
});
2119

22-
before("each", _ {
23-
n = rand();
24-
});
20+
int n = 0;
21+
before_each([&]{ n = rand(); });
2522

2623
// you can also use `context` instead of
2724
// `explain`, just like in RSpec
@@ -38,7 +35,7 @@ describe fabs_spec("fabs", _ {
3835
});
3936
});
4037

41-
describe_a <std::list<int>> int_list_spec({1,2,3}, _ {
38+
describe_a <std::list<int>> int_list_spec({1,2,3}, $ {
4239
it(_{ is_expected().to_include(3); });
4340
});
4441

@@ -101,10 +98,18 @@ to be cleaned up.
10198
## It's Beautiful!
10299
Why, thank you! I certainly tried.
103100

104-
## What the heck is `_`!?
105-
It's literally `[](auto &self) -> auto`.
101+
## What the heck are `$` and `_`!?
102+
TLDR: They're syntax sugar for `[](auto &self)` and `[=](auto &self)` respectively.
103+
104+
They're defined to avoid repetition and improve readability. (`it([=](auto &self){ is_expected.to_equal(5); });` isn't exactly what I was aiming for with syntax)
105+
106+
We need them to pass a reference to the containing "thing". For example, `describe`
107+
blocks get a `Description` object, while `it` blocks get an `It` object.
106108

107-
We need this to pass a reference to the containing "thing". For example, `describe` blocks get a `Description` object, while `it` blocks get an `It` object.
109+
Annoyingly, you can't have an automatic capture-list on a non-local lambda
110+
(which is what a top-level `describe`'s body is). This means that there needs
111+
to be a separate one-character shortcut. If they really bother you, or you just
112+
want to be verbose, you can stick `#undef $` and/or `#undef _` at the top of your file.
108113

109114
## It's awesome and I totally want to use it, but how?
110115

examples/sample/example_spec.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
#include <list>
22
#include "cppspec.hpp"
33

4-
describe bool_spec("Some Tests", _ {
4+
5+
describe bool_spec("Some Tests", $ {
56
// context("true is", _ {
67
// it("true", _ {
78
// expect(true).to_be;
89
// });
910
// });
1011

11-
explain("4 is", _ {
12+
int i = 4;
13+
explain("4", _ {
1214
it("equals 4", _ {
13-
expect(4).to_equal(4);
15+
expect(i).to_equal(4);
1416
});
1517
it("does not equal 5", _ {
16-
expect(4).not_().to_equal(5);
18+
expect(i).not_().to_equal(5);
19+
});
20+
21+
it("plus 1 equals 5", _ {
22+
expect(i+1).to_equal(5);
1723
});
1824
});
1925

@@ -121,7 +127,7 @@ describe bool_spec("Some Tests", _ {
121127
// });
122128
// });
123129

124-
describe_a <std::vector<int>> vector_spec({1,2,3,4}, _ {
130+
describe_a <std::vector<int>> vector_spec({1,2,3,4}, $ {
125131
it("should contain 2", _ {
126132
expect({1,2,3,4}).to_include(6);
127133
});
@@ -136,13 +142,13 @@ describe_a <std::vector<int>> vector_spec({1,2,3,4}, _ {
136142
// });
137143
// });
138144

139-
describe_a <std::vector<int>> another_vector_spec({1,2,3,4}, _ {
145+
describe_a <std::vector<int>> another_vector_spec({1,2,3,4}, $ {
140146
it( _ { is_expected().to_include(6); });
141147
});
142148

143149

144-
describe list_spec("A list spec", _ {
145-
context<std::list<int>>({1,2,3,4}, _ {
150+
describe list_spec("A list spec", $ {
151+
explain <std::list<int>> ({1,2,3,4}, _ {
146152
it( _ { is_expected().to_include(6); });
147153
});
148154
});

include/cppspec.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "it.hpp"
44
#include "expectations/expectation.hpp"
55

6-
#define _ [](auto &self)
6+
#define _ [=](auto &self)
7+
#define $ [](auto &self)
78
#define it self.it
89
#define context self.template context
910
#define explain context

0 commit comments

Comments
 (0)