Skip to content

Commit 72e9ef7

Browse files
committed
lintify GsonStep (tests only)
1 parent 99c3705 commit 72e9ef7

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232

3333
public class GsonFormatterFunc implements FormatterFunc {
3434

35-
private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to format JSON";
35+
private static final String FAILED_TO_PARSE_ERROR_MESSAGE = "Unable to parse JSON";
3636

3737
private final Gson gson;
3838
private final GsonConfig gsonConfig;
@@ -56,7 +56,7 @@ public String apply(String inputString) {
5656
} else {
5757
JsonElement jsonElement = gson.fromJson(inputString, JsonElement.class);
5858
if (jsonElement == null) {
59-
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE);
59+
throw new IllegalArgumentException(FAILED_TO_PARSE_ERROR_MESSAGE);
6060
}
6161
if (gsonConfig.isSortByKeys()) {
6262
jsonElement = sortByKeys(jsonElement);

testlib/src/test/java/com/diffplug/spotless/json/gson/GsonStepTest.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
import static com.diffplug.spotless.json.gson.GsonStep.DEFAULT_VERSION;
1919

20-
import java.io.File;
21-
22-
import org.assertj.core.api.Assertions;
2320
import org.junit.jupiter.api.Test;
2421

2522
import com.diffplug.spotless.FormatterStep;
@@ -42,12 +39,32 @@ void handlesObjectWithNull() {
4239

4340
@Test
4441
void handlesInvalidJson() {
45-
getStepHarness().testResourceExceptionMsg("json/invalidJsonBefore.json").isEqualTo("End of input at line 3 column 1 path $.a");
42+
getStepHarness().expectLintsOfResource("json/invalidJsonBefore.json").toBe("L3 gson(com.google.gson.JsonSyntaxException) java.io.EOFException: End of input at line 3 column 1 path $.a",
43+
"\tat com.google.gson.Gson.fromJson(Gson.java:1370)",
44+
"\tat com.google.gson.Gson.fromJson(Gson.java:1262)",
45+
"\tat com.google.gson.Gson.fromJson(Gson.java:1171)",
46+
"\tat com.google.gson.Gson.fromJson(Gson.java:1107)",
47+
"\tat com.diffplug.spotless.glue.gson.GsonFormatterFunc.apply(GsonFormatterFunc.java:57)",
48+
"\tat com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)",
49+
"\tat com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)",
50+
"\tat com.diffplug.spotless.LintState.of(LintState.java:141)",
51+
"\tat com.diffplug.spotless.StepHarness.expectLintsOf(StepHarness.java:96)",
52+
"(... and more)");
4653
}
4754

4855
@Test
4956
void handlesNotJson() {
50-
getStepHarness().testResourceExceptionMsg("json/notJsonBefore.json").isEqualTo("Unable to format JSON");
57+
getStepHarness().expectLintsOfResource("json/notJsonBefore.json").toBe("LINE_UNDEFINED gson(java.lang.IllegalArgumentException) Unable to parse JSON",
58+
"\tat com.diffplug.spotless.glue.gson.GsonFormatterFunc.apply(GsonFormatterFunc.java:59)",
59+
"\tat com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)",
60+
"\tat com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)",
61+
"\tat com.diffplug.spotless.LintState.of(LintState.java:141)",
62+
"\tat com.diffplug.spotless.StepHarness.expectLintsOf(StepHarness.java:96)",
63+
"\tat com.diffplug.spotless.StepHarness.expectLintsOfResource(StepHarness.java:92)",
64+
"\tat com.diffplug.spotless.json.gson.GsonStepTest.handlesNotJson(GsonStepTest.java:57)",
65+
"\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)",
66+
"\tat java.base/java.lang.reflect.Method.invoke(Method.java:580)",
67+
"(... and more)");
5168
}
5269

5370
@Test
@@ -79,10 +96,18 @@ void writesRawHtmlWhenHtmlEscapeDisabled() {
7996

8097
@Test
8198
void handlesVersionIncompatibility() {
82-
FormatterStep step = GsonStep.create(new GsonConfig(false, false, INDENT, "1.7"), TestProvisioner.mavenCentral());
83-
Assertions.assertThatThrownBy(() -> step.format("", new File("")))
84-
.isInstanceOf(IllegalStateException.class)
85-
.hasMessage("There was a problem interacting with Gson; maybe you set an incompatible version?");
99+
StepHarness.forStep(GsonStep.create(new GsonConfig(false, false, INDENT, "1.7"), TestProvisioner.mavenCentral()))
100+
.expectLintsOf("").toBe("LINE_UNDEFINED gson(java.lang.IllegalStateException) There was a problem interacting with Gson; maybe you set an incompatible version?",
101+
"\tat com.diffplug.spotless.json.gson.GsonStep$State.toFormatter(GsonStep.java:73)",
102+
"\tat com.diffplug.spotless.FormatterStepSerializationRoundtrip.stateToFormatter(FormatterStepSerializationRoundtrip.java:64)",
103+
"\tat com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:47)",
104+
"\tat com.diffplug.spotless.LintState.of(LintState.java:141)",
105+
"\tat com.diffplug.spotless.StepHarness.expectLintsOf(StepHarness.java:96)",
106+
"\tat com.diffplug.spotless.json.gson.GsonStepTest.handlesVersionIncompatibility(GsonStepTest.java:100)",
107+
"\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)",
108+
"\tat java.base/java.lang.reflect.Method.invoke(Method.java:580)",
109+
"\tat org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:766)",
110+
"(... and more)");
86111
}
87112

88113
@Override

0 commit comments

Comments
 (0)