-
Notifications
You must be signed in to change notification settings - Fork 36
/
AssertJOptionalRules.java
131 lines (112 loc) · 4.39 KB
/
AssertJOptionalRules.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package tech.picnic.errorprone.refasterrules;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Optional;
import java.util.function.Predicate;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.AbstractOptionalAssert;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.api.OptionalAssert;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
@OnlineDocumentation
final class AssertJOptionalRules {
private AssertJOptionalRules() {}
static final class AssertThatOptional<T> {
@BeforeTemplate
ObjectAssert<T> before(Optional<T> optional) {
return assertThat(optional.orElseThrow());
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, T> after(Optional<T> optional) {
return assertThat(optional).get();
}
}
static final class AbstractOptionalAssertIsPresent<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(AbstractOptionalAssert<?, T> optionalAssert) {
return Refaster.anyOf(
optionalAssert.isNotEmpty(), optionalAssert.isNotEqualTo(Optional.empty()));
}
@AfterTemplate
AbstractOptionalAssert<?, T> after(AbstractOptionalAssert<?, T> optionalAssert) {
return optionalAssert.isPresent();
}
}
static final class AssertThatOptionalIsPresent<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(Optional<T> optional) {
return Refaster.anyOf(
assertThat(optional.isPresent()).isTrue(), assertThat(optional.isEmpty()).isFalse());
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalAssert<T> after(Optional<T> optional) {
return assertThat(optional).isPresent();
}
}
static final class AbstractOptionalAssertIsEmpty<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(AbstractOptionalAssert<?, T> optionalAssert) {
return Refaster.anyOf(
optionalAssert.isNotPresent(), optionalAssert.isEqualTo(Optional.empty()));
}
@AfterTemplate
AbstractOptionalAssert<?, T> after(AbstractOptionalAssert<?, T> optionalAssert) {
return optionalAssert.isEmpty();
}
}
static final class AssertThatOptionalIsEmpty<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(Optional<T> optional) {
return Refaster.anyOf(
assertThat(optional.isEmpty()).isTrue(), assertThat(optional.isPresent()).isFalse());
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalAssert<T> after(Optional<T> optional) {
return assertThat(optional).isEmpty();
}
}
static final class AbstractOptionalAssertHasValue<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(AbstractOptionalAssert<?, T> optionalAssert, T value) {
return Refaster.anyOf(
optionalAssert.get().isEqualTo(value),
optionalAssert.isEqualTo(Optional.of(value)),
optionalAssert.contains(value),
optionalAssert.isPresent().hasValue(value));
}
@AfterTemplate
AbstractOptionalAssert<?, T> after(AbstractOptionalAssert<?, T> optionalAssert, T value) {
return optionalAssert.hasValue(value);
}
}
static final class AbstractOptionalAssertContainsSame<T> {
@BeforeTemplate
AbstractAssert<?, ?> before(AbstractOptionalAssert<?, T> optionalAssert, T value) {
return Refaster.anyOf(
optionalAssert.get().isSameAs(value), optionalAssert.isPresent().isSameAs(value));
}
@AfterTemplate
AbstractOptionalAssert<?, T> after(AbstractOptionalAssert<?, T> optionalAssert, T value) {
return optionalAssert.containsSame(value);
}
}
static final class AssertThatOptionalHasValueMatching<T> {
@BeforeTemplate
AbstractOptionalAssert<?, T> before(Optional<T> optional, Predicate<? super T> predicate) {
return assertThat(optional.filter(predicate)).isPresent();
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, T> after(Optional<T> optional, Predicate<? super T> predicate) {
return assertThat(optional).get().matches(predicate);
}
}
}