Skip to content

Commit 63414a7

Browse files
committed
Make roundtripping optional-ish.
1 parent cee7cad commit 63414a7

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

Diff for: testlib/src/main/java/com/diffplug/spotless/StepHarness.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
/** An api for testing a {@code FormatterStep} that doesn't depend on the File path. DO NOT ADD FILE SUPPORT TO THIS, use {@link StepHarnessWithFile} if you need that. */
2929
public class StepHarness extends StepHarnessBase {
30-
private StepHarness(Formatter formatter) {
31-
super(formatter);
30+
private StepHarness(Formatter formatter, RoundTrip roundTrip) {
31+
super(formatter, roundTrip);
3232
}
3333

3434
/** Creates a harness for testing steps which don't depend on the file. */
@@ -49,7 +49,7 @@ public static StepHarness forSteps(FormatterStep... steps) {
4949

5050
/** Creates a harness for testing a formatter whose steps don't depend on the file. */
5151
public static StepHarness forFormatter(Formatter formatter) {
52-
return new StepHarness(formatter);
52+
return new StepHarness(formatter, RoundTrip.ASSERT_EQUAL);
5353
}
5454

5555
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */

Diff for: testlib/src/main/java/com/diffplug/spotless/StepHarnessBase.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@
2020
import org.assertj.core.api.Assertions;
2121

2222
class StepHarnessBase implements AutoCloseable {
23+
enum RoundTrip {
24+
ASSERT_EQUAL, DONT_ASSERT_EQUAL, DONT_ROUNDTRIP
25+
}
26+
2327
private final Formatter formatter;
2428

25-
protected StepHarnessBase(Formatter formatter) {
29+
protected StepHarnessBase(Formatter formatter, RoundTrip roundTrip) {
2630
this.formatter = Objects.requireNonNull(formatter);
31+
if (roundTrip == RoundTrip.DONT_ROUNDTRIP) {
32+
return;
33+
}
2734
Formatter roundTripped = SerializableEqualityTester.reserialize(formatter);
28-
Assertions.assertThat(roundTripped).isEqualTo(formatter);
35+
if (roundTrip == RoundTrip.ASSERT_EQUAL) {
36+
Assertions.assertThat(roundTripped).isEqualTo(formatter);
37+
}
2938
}
3039

3140
protected Formatter formatter() {

Diff for: testlib/src/main/java/com/diffplug/spotless/StepHarnessWithFile.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
public class StepHarnessWithFile extends StepHarnessBase {
3030
private final ResourceHarness harness;
3131

32-
private StepHarnessWithFile(ResourceHarness harness, Formatter formatter) {
33-
super(formatter);
32+
private StepHarnessWithFile(ResourceHarness harness, Formatter formatter, RoundTrip roundTrip) {
33+
super(formatter, roundTrip);
3434
this.harness = Objects.requireNonNull(harness);
3535
}
3636

3737
/** Creates a harness for testing steps which do depend on the file. */
3838
public static StepHarnessWithFile forStep(ResourceHarness harness, FormatterStep step) {
39-
return new StepHarnessWithFile(harness, Formatter.builder()
39+
return forFormatter(harness, Formatter.builder()
4040
.name(step.getName())
4141
.encoding(StandardCharsets.UTF_8)
4242
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
@@ -48,7 +48,7 @@ public static StepHarnessWithFile forStep(ResourceHarness harness, FormatterStep
4848

4949
/** Creates a harness for testing a formatter whose steps do depend on the file. */
5050
public static StepHarnessWithFile forFormatter(ResourceHarness harness, Formatter formatter) {
51-
return new StepHarnessWithFile(harness, formatter);
51+
return new StepHarnessWithFile(harness, formatter, RoundTrip.ASSERT_EQUAL);
5252
}
5353

5454
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */

0 commit comments

Comments
 (0)