1
1
package specs ;
2
2
3
3
import static com .greghaskins .spectrum .dsl .specification .Specification .afterEach ;
4
+ import static com .greghaskins .spectrum .dsl .specification .Specification .beforeEach ;
5
+ import static com .greghaskins .spectrum .dsl .specification .Specification .context ;
4
6
import static com .greghaskins .spectrum .dsl .specification .Specification .describe ;
5
7
import static com .greghaskins .spectrum .dsl .specification .Specification .it ;
6
8
import static com .greghaskins .spectrum .dsl .specification .Specification .let ;
15
17
16
18
import com .greghaskins .spectrum .Spectrum ;
17
19
import com .greghaskins .spectrum .SpectrumHelper ;
20
+ import com .greghaskins .spectrum .Variable ;
18
21
19
22
import org .junit .runner .Result ;
20
23
import org .junit .runner .RunWith ;
29
32
public class LetSpecs {
30
33
{
31
34
describe ("The `let` helper function" , () -> {
32
-
33
35
final Supplier <List <String >> items = let (() -> new ArrayList <>(asList ("foo" , "bar" )));
34
36
35
37
it ("is a way to supply a value for specs" , () -> {
@@ -41,18 +43,36 @@ public class LetSpecs {
41
43
42
44
items .get ().add ("baz" );
43
45
items .get ().add ("blah" );
46
+
44
47
assertThat (items .get (), contains ("foo" , "bar" , "baz" , "blah" ));
45
48
});
46
49
50
+ context ("when the value returned by the supplier is `null`" , () -> {
51
+ final AtomicInteger callCounter = new AtomicInteger ();
52
+
53
+ final Supplier <String > stringLet = let (() -> {
54
+ if (callCounter .getAndIncrement () == 0 ) {
55
+ return null ;
56
+ } else {
57
+ return "fail" ;
58
+ }
59
+ });
60
+
61
+ it ("does not call the supplier multiple times" , () -> {
62
+ assertThat (stringLet .get (), is (nullValue ()));
63
+ assertThat (stringLet .get (), is (nullValue ()));
64
+ });
65
+ });
66
+
47
67
it ("creates a fresh value for every spec" , () -> {
48
68
assertThat (items .get (), contains ("foo" , "bar" ));
49
69
});
50
70
51
71
describe ("in complex test hierarchies" , () -> {
52
72
describe ("a new let object is created for each spec" , () -> {
53
73
AtomicInteger integer = new AtomicInteger ();
54
- describe ("a thing" , () -> {
55
74
75
+ describe ("a thing" , () -> {
56
76
final Supplier <Integer > intLet = let (integer ::getAndIncrement );
57
77
58
78
it ("starts with one value" , () -> {
@@ -85,14 +105,26 @@ public class LetSpecs {
85
105
});
86
106
});
87
107
});
108
+ });
109
+ });
110
+ });
111
+
112
+ describe ("lazy initialization" , () -> {
113
+ context ("when setup has to be done before a let is evaluated" , () -> {
114
+ final Variable <Integer > theVariable = new Variable <>();
115
+ final Supplier <Integer > result = let (() -> theVariable .get ());
88
116
117
+ beforeEach (() -> {
118
+ theVariable .set (123 );
89
119
});
90
120
121
+ it ("does not cache a value until the let is referenced" , () -> {
122
+ assertThat (result .get (), is (123 ));
123
+ });
91
124
});
92
125
});
93
126
94
127
describe ("when trying to use a value outside a spec" , () -> {
95
-
96
128
final Supplier <Result > result =
97
129
let (() -> SpectrumHelper .run (getSuiteThatUsesLetValueOutsideSpec ()));
98
130
0 commit comments