Skip to content

Commit ffc911e

Browse files
committed
Add a way to test for lints, and use that to bring back FenceStepTest.broken
1 parent 700114f commit ffc911e

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

lib/src/main/java/com/diffplug/spotless/LintState.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ public Map<FormatterStep, List<Lint>> getLints(Formatter formatter) {
5858
return result;
5959
}
6060

61+
public String asString(File file, Formatter formatter) {
62+
if (!isHasLints()) {
63+
return "(none)";
64+
} else {
65+
StringBuilder result = new StringBuilder();
66+
for (int i = 0; i < lintsPerStep.size(); i++) {
67+
List<Lint> lints = lintsPerStep.get(i);
68+
if (lints != null) {
69+
FormatterStep step = formatter.getSteps().get(i);
70+
for (Lint lint : lints) {
71+
result.append(file.getName()).append(":").append(lint.getLineStart());
72+
if (lint.getLineEnd() != lint.getLineStart()) {
73+
result.append("-").append(lint.getLineEnd());
74+
}
75+
result.append(" ");
76+
result.append(step.getName()).append("(").append(lint.getCode()).append(") ");
77+
result.append(lint.getMsg());
78+
result.append("\n");
79+
}
80+
}
81+
}
82+
result.setLength(result.length() - 1);
83+
return result.toString();
84+
}
85+
}
86+
6187
public static LintState of(Formatter formatter, File file) throws IOException {
6288
return of(formatter, file, Files.readAllBytes(file.toPath()));
6389
}

testlib/src/main/java/com/diffplug/spotless/StepHarness.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.assertj.core.api.AbstractStringAssert;
2525
import org.assertj.core.api.Assertions;
2626

27+
import com.diffplug.selfie.Selfie;
28+
import com.diffplug.selfie.StringSelfie;
29+
2730
/** 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. */
2831
public class StepHarness extends StepHarnessBase {
2932
private StepHarness(Formatter formatter, RoundTrip roundTrip) {
@@ -111,4 +114,11 @@ public AbstractStringAssert<?> testExceptionMsg(String before) {
111114
}
112115
}
113116
}
117+
118+
public StringSelfie expectLintsOf(String before) {
119+
LintState state = LintState.of(formatter(), Formatter.NO_FILE_SENTINEL, before.getBytes(formatter().getEncoding()));
120+
String assertAgainst = state.asString(Formatter.NO_FILE_SENTINEL, formatter());
121+
String cleaned = assertAgainst.replace("NO_FILE_SENTINEL:", "");
122+
return Selfie.expectSelfie(cleaned);
123+
}
114124
}

testlib/src/test/java/com/diffplug/spotless/generic/FenceStepTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
import java.io.File;
1919
import java.util.Arrays;
2020

21-
import org.junit.jupiter.api.Disabled;
2221
import org.junit.jupiter.api.Test;
2322

2423
import com.diffplug.common.base.StringPrinter;
2524
import com.diffplug.spotless.FormatterStep;
2625
import com.diffplug.spotless.ResourceHarness;
2726
import com.diffplug.spotless.StepHarness;
28-
import com.diffplug.spotless.tag.ForLintRefactor;
2927

3028
class FenceStepTest extends ResourceHarness {
3129
@Test
@@ -82,18 +80,16 @@ void multiple() {
8280
"1 2 3"));
8381
}
8482

85-
@Disabled
86-
@ForLintRefactor
8783
@Test
8884
void broken() {
8985
FormatterStep fence = FenceStep.named("fence").openClose("spotless:off", "spotless:on")
90-
.preserveWithin(Arrays.asList(ToCaseStep.upper()));
86+
.preserveWithin(Arrays.asList(ReplaceStep.create("replace", "spotless:on", "REMOVED")));
9187
// this fails because uppercase turns spotless:off into SPOTLESS:OFF, etc
92-
StepHarness.forStepNoRoundtrip(fence).testExceptionMsg(StringPrinter.buildStringFromLines("A B C",
88+
StepHarness.forStep(fence).expectLintsOf(StringPrinter.buildStringFromLines("A B C",
9389
"spotless:off",
9490
"D E F",
9591
"spotless:on",
96-
"G H I")).isEqualTo("An intermediate step removed a match of spotless:off spotless:on");
92+
"G H I")).toBe("1-6 fence(fenceRemoved) An intermediate step removed a match of spotless:off spotless:on");
9793
}
9894

9995
@Test

0 commit comments

Comments
 (0)