Introduce SpringTestRules Refaster rule collection

This commit is contained in:
Stephan Schroevers
2025-03-29 21:10:21 +01:00
parent 5a7aaf12d6
commit 6c8f11fbdc
4 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
package tech.picnic.errorprone.refasterrules;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.springframework.test.json.JsonCompareMode;
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
/** Refaster rules related to Spring Test expressions and statements. */
@OnlineDocumentation
final class SpringTestRules {
private SpringTestRules() {}
/**
* Prefer {@link BodyContentSpec#json(String, JsonCompareMode)} over alternatives that implicitly
* perform a {@link JsonCompareMode#LENIENT lenient} comparison or are deprecated.
*/
static final class BodyContentSpecJsonLenient {
@BeforeTemplate
@SuppressWarnings("deprecation" /* This deprecated method invocation will be rewritten. */)
BodyContentSpec before(BodyContentSpec spec, String expectedJson) {
return Refaster.anyOf(spec.json(expectedJson), spec.json(expectedJson, /* strict= */ false));
}
@AfterTemplate
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
return spec.json(expectedJson, JsonCompareMode.LENIENT);
}
}
/**
* Prefer {@link BodyContentSpec#json(String, JsonCompareMode)} over the deprecated alternative.
*/
static final class BodyContentSpecJsonStrict {
@BeforeTemplate
@SuppressWarnings("deprecation" /* This deprecated method invocation will be rewritten. */)
BodyContentSpec before(BodyContentSpec spec, String expectedJson) {
return spec.json(expectedJson, /* strict= */ true);
}
@AfterTemplate
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
return spec.json(expectedJson, JsonCompareMode.STRICT);
}
}
}

View File

@@ -73,6 +73,7 @@ final class RefasterRulesTest {
PrimitiveRules.class, PrimitiveRules.class,
ReactorRules.class, ReactorRules.class,
RxJava2AdapterRules.class, RxJava2AdapterRules.class,
SpringTestRules.class,
StreamRules.class, StreamRules.class,
StringRules.class, StringRules.class,
SuggestedFixRules.class, SuggestedFixRules.class,

View File

@@ -0,0 +1,18 @@
package tech.picnic.errorprone.refasterrules;
import com.google.common.collect.ImmutableSet;
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
final class SpringTestRulesTest implements RefasterRuleCollectionTestCase {
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
ImmutableSet<BodyContentSpec> testBodyContentSpecJsonLenient() {
return ImmutableSet.of(
((BodyContentSpec) null).json("foo"), ((BodyContentSpec) null).json("bar", false));
}
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
BodyContentSpec testBodyContentSpecJsonStrict() {
return ((BodyContentSpec) null).json("foo", true);
}
}

View File

@@ -0,0 +1,20 @@
package tech.picnic.errorprone.refasterrules;
import com.google.common.collect.ImmutableSet;
import org.springframework.test.json.JsonCompareMode;
import org.springframework.test.web.reactive.server.WebTestClient.BodyContentSpec;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
final class SpringTestRulesTest implements RefasterRuleCollectionTestCase {
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
ImmutableSet<BodyContentSpec> testBodyContentSpecJsonLenient() {
return ImmutableSet.of(
((BodyContentSpec) null).json("foo", JsonCompareMode.LENIENT),
((BodyContentSpec) null).json("bar", JsonCompareMode.LENIENT));
}
@SuppressWarnings("deprecation" /* Rule rewrites deprecated method invocation. */)
BodyContentSpec testBodyContentSpecJsonStrict() {
return ((BodyContentSpec) null).json("foo", JsonCompareMode.STRICT);
}
}