Skip to content

Commit e78f5bc

Browse files
authored
Introduce SpringTestRules Refaster rule collection (#1621)
1 parent af98871 commit e78f5bc

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tech.picnic.errorprone.refasterrules;
2+
3+
import com.google.errorprone.refaster.Refaster;
4+
import com.google.errorprone.refaster.annotation.AfterTemplate;
5+
import com.google.errorprone.refaster.annotation.BeforeTemplate;
6+
import org.springframework.test.json.JsonCompareMode;
7+
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
8+
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
9+
10+
/** Refaster rules related to Spring Test expressions and statements. */
11+
@OnlineDocumentation
12+
final class SpringTestRules {
13+
private SpringTestRules() {}
14+
15+
/**
16+
* Prefer {@link BodyContentSpec#json(String, JsonCompareMode)} over alternatives that implicitly
17+
* perform a {@link JsonCompareMode#LENIENT lenient} comparison or are deprecated.
18+
*/
19+
static final class BodyContentSpecJsonLenient {
20+
@BeforeTemplate
21+
@SuppressWarnings("deprecation" /* This deprecated method invocation will be rewritten. */)
22+
BodyContentSpec before(BodyContentSpec spec, String expectedJson) {
23+
return Refaster.anyOf(spec.json(expectedJson), spec.json(expectedJson, /* strict= */ false));
24+
}
25+
26+
@AfterTemplate
27+
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
28+
return spec.json(expectedJson, JsonCompareMode.LENIENT);
29+
}
30+
}
31+
32+
/**
33+
* Prefer {@link BodyContentSpec#json(String, JsonCompareMode)} over the deprecated alternative.
34+
*/
35+
static final class BodyContentSpecJsonStrict {
36+
@BeforeTemplate
37+
@SuppressWarnings("deprecation" /* This deprecated method invocation will be rewritten. */)
38+
BodyContentSpec before(BodyContentSpec spec, String expectedJson) {
39+
return spec.json(expectedJson, /* strict= */ true);
40+
}
41+
42+
@AfterTemplate
43+
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
44+
return spec.json(expectedJson, JsonCompareMode.STRICT);
45+
}
46+
}
47+
}

error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ final class RefasterRulesTest {
8181
RandomGeneratorRules.class,
8282
ReactorRules.class,
8383
RxJava2AdapterRules.class,
84+
SpringTestRules.class,
8485
StreamRules.class,
8586
StringRules.class,
8687
SuggestedFixRules.class,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tech.picnic.errorprone.refasterrules;
2+
3+
import com.google.common.collect.ImmutableSet;
4+
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
5+
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
6+
7+
final class SpringTestRulesTest implements RefasterRuleCollectionTestCase {
8+
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
9+
ImmutableSet<BodyContentSpec> testBodyContentSpecJsonLenient() {
10+
return ImmutableSet.of(
11+
((BodyContentSpec) null).json("foo"), ((BodyContentSpec) null).json("bar", false));
12+
}
13+
14+
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
15+
BodyContentSpec testBodyContentSpecJsonStrict() {
16+
return ((BodyContentSpec) null).json("foo", true);
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tech.picnic.errorprone.refasterrules;
2+
3+
import com.google.common.collect.ImmutableSet;
4+
import org.springframework.test.json.JsonCompareMode;
5+
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
6+
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
7+
8+
final class SpringTestRulesTest implements RefasterRuleCollectionTestCase {
9+
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
10+
ImmutableSet<BodyContentSpec> testBodyContentSpecJsonLenient() {
11+
return ImmutableSet.of(
12+
((BodyContentSpec) null).json("foo", JsonCompareMode.LENIENT),
13+
((BodyContentSpec) null).json("bar", JsonCompareMode.LENIENT));
14+
}
15+
16+
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
17+
BodyContentSpec testBodyContentSpecJsonStrict() {
18+
return ((BodyContentSpec) null).json("foo", JsonCompareMode.STRICT);
19+
}
20+
}

0 commit comments

Comments
 (0)