forked from greghaskins/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LetSpecs.java
91 lines (71 loc) · 2.65 KB
/
LetSpecs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package specs;
import static com.greghaskins.spectrum.Spectrum.describe;
import static com.greghaskins.spectrum.Spectrum.it;
import static com.greghaskins.spectrum.Spectrum.let;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import com.greghaskins.spectrum.Spectrum;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runner.notification.Failure;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
@RunWith(Spectrum.class)
public class LetSpecs {
{
describe("The `let` helper function", () -> {
final Supplier<List<String>> items = let(() -> new ArrayList<>(asList("foo", "bar")));
it("is a way to supply a value for specs", () -> {
assertThat(items.get(), contains("foo", "bar"));
});
it("caches the value so it doesn't get created multiple times for the same spec", () -> {
assertThat(items.get(), is(sameInstance(items.get())));
items.get().add("baz");
items.get().add("blah");
assertThat(items.get(), contains("foo", "bar", "baz", "blah"));
});
it("creates a fresh value for every spec", () -> {
assertThat(items.get(), contains("foo", "bar"));
});
describe("when trying to use a value outside a spec", () -> {
final Supplier<Result> result = let(() -> {
try {
return helpers.SpectrumRunner.run(getSuiteThatUsesLetValueOutsideSpec());
} catch (final Exception exception) {
throw new RuntimeException(exception);
}
});
it("causes a failure", () -> {
assertThat(result.get().getFailureCount(), is(1));
});
it("describes the error", () -> {
final Failure failure = result.get().getFailures().get(0);
assertThat(failure.getException(), instanceOf(IllegalStateException.class));
assertThat(failure.getMessage(),
is("Cannot use the value from let() in a suite declaration. "
+ "It may only be used in the context of a running spec."));
});
});
});
}
private static Class<?> getSuiteThatUsesLetValueOutsideSpec() {
class Suite {
{
describe("a thing", () -> {
final Supplier<Integer> value = let(() -> 1);
value.get();
it("does stuff", () -> {
});
it("does more stuff", () -> {
});
});
}
}
return Suite.class;
}
}