Skip to content

Introduce SpringTestRules Refaster rule collection #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative to write it to the overload that does this by default:

Suggested change
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
return spec.json(expectedJson, JsonCompareMode.LENIENT);
}
BodyContentSpec after(BodyContentSpec spec, String expectedJson) {
return spec.json(expectedJson);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's up for debate, indeed. As the Javadoc indicates, personally I feel this is a case where it's better to be explicit, because the default behavior is IMHO surprising / less strict than I'd have assumed.

}

/**
* 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ final class RefasterRulesTest {
PrimitiveRules.class,
ReactorRules.class,
RxJava2AdapterRules.class,
SpringTestRules.class,
StreamRules.class,
StringRules.class,
SuggestedFixRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading