Skip to content

Commit 195bc16

Browse files
committed
Remove testResourceExceptionMsg and replace with expectLintsOfResource.
1 parent d00a4ab commit 195bc16

File tree

2 files changed

+26
-50
lines changed

2 files changed

+26
-50
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.Arrays;
2323

24-
import org.assertj.core.api.AbstractStringAssert;
25-
import org.assertj.core.api.Assertions;
26-
2724
import com.diffplug.selfie.Selfie;
2825
import com.diffplug.selfie.StringSelfie;
2926

@@ -91,34 +88,26 @@ public StepHarness testResourceUnaffected(String resourceIdempotent) {
9188
return testUnaffected(idempotentElement);
9289
}
9390

94-
public AbstractStringAssert<?> testResourceExceptionMsg(String resourceBefore) {
95-
return testExceptionMsg(ResourceHarness.getTestResource(resourceBefore));
96-
}
97-
98-
public AbstractStringAssert<?> testExceptionMsg(String before) {
99-
try {
100-
formatter().compute(LineEnding.toUnix(before), Formatter.NO_FILE_SENTINEL);
101-
throw new SecurityException("Expected exception");
102-
} catch (Throwable e) {
103-
if (e instanceof SecurityException) {
104-
throw new AssertionError(e.getMessage());
105-
} else {
106-
Throwable rootCause = e;
107-
while (rootCause.getCause() != null) {
108-
if (rootCause instanceof IllegalStateException) {
109-
break;
110-
}
111-
rootCause = rootCause.getCause();
112-
}
113-
return Assertions.assertThat(rootCause.getMessage());
114-
}
115-
}
91+
public StringSelfie expectLintsOfResource(String before) {
92+
return expectLintsOf(ResourceHarness.getTestResource(before));
11693
}
11794

11895
public StringSelfie expectLintsOf(String before) {
11996
LintState state = LintState.of(formatter(), Formatter.NO_FILE_SENTINEL, before.getBytes(formatter().getEncoding()));
120-
String assertAgainst = state.asString(Formatter.NO_FILE_SENTINEL, formatter());
97+
return expectLintsOf(state, formatter());
98+
}
99+
100+
static StringSelfie expectLintsOf(LintState state, Formatter formatter) {
101+
String assertAgainst = state.asString(Formatter.NO_FILE_SENTINEL, formatter);
121102
String cleaned = assertAgainst.replace("NO_FILE_SENTINEL:", "");
122-
return Selfie.expectSelfie(cleaned);
103+
104+
int numLines = 1;
105+
int lineEnding = cleaned.indexOf('\n');
106+
while (lineEnding != -1 && numLines < 10) {
107+
++numLines;
108+
lineEnding = cleaned.indexOf('\n', lineEnding + 1);
109+
}
110+
String toSnapshot = lineEnding == -1 ? cleaned : (cleaned.substring(0, lineEnding) + "\n(... and more)");
111+
return Selfie.expectSelfie(toSnapshot);
123112
}
124113
}

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

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import static org.junit.jupiter.api.Assertions.*;
1919

2020
import java.io.File;
21+
import java.io.IOException;
2122
import java.nio.charset.StandardCharsets;
2223
import java.util.Collections;
2324
import java.util.Objects;
2425

25-
import org.assertj.core.api.AbstractStringAssert;
26-
import org.assertj.core.api.Assertions;
26+
import com.diffplug.selfie.StringSelfie;
2727

2828
/** An api for testing a {@code FormatterStep} that depends on the File path. */
2929
public class StepHarnessWithFile extends StepHarnessBase {
@@ -85,30 +85,17 @@ public StepHarnessWithFile testResourceUnaffected(String resourceIdempotent) {
8585
return testUnaffected(file, contentBefore);
8686
}
8787

88-
public AbstractStringAssert<?> testResourceExceptionMsg(String resourceBefore) {
89-
return testResourceExceptionMsg(resourceBefore, resourceBefore);
88+
public StringSelfie expectLintsOfResource(String resource) {
89+
return expectLintsOfResource(resource, resource);
9090
}
9191

92-
public AbstractStringAssert<?> testResourceExceptionMsg(String filename, String resourceBefore) {
93-
String contentBefore = ResourceHarness.getTestResource(resourceBefore);
94-
File file = harness.setFile(filename).toContent(contentBefore);
95-
return testExceptionMsg(file, contentBefore);
96-
}
97-
98-
public AbstractStringAssert<?> testExceptionMsg(File file, String before) {
92+
public StringSelfie expectLintsOfResource(String filename, String resource) {
9993
try {
100-
formatter().compute(LineEnding.toUnix(before), file);
101-
throw new SecurityException("Expected exception");
102-
} catch (Throwable e) {
103-
if (e instanceof SecurityException) {
104-
throw new AssertionError(e.getMessage());
105-
} else {
106-
Throwable rootCause = e;
107-
while (rootCause.getCause() != null) {
108-
rootCause = rootCause.getCause();
109-
}
110-
return Assertions.assertThat(rootCause.getMessage());
111-
}
94+
File file = harness.setFile(filename).toResource(resource);
95+
LintState state = LintState.of(formatter(), file);
96+
return StepHarness.expectLintsOf(state, formatter());
97+
} catch (IOException e) {
98+
throw new AssertionError(e);
11299
}
113100
}
114101
}

0 commit comments

Comments
 (0)