Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: diffplug/spotless
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ffbc5dca767eb7de4241589965b9a777f343c61a
Choose a base ref
..
head repository: diffplug/spotless
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ec624c0347e2eecb0bfcfea894ab4fb166771066
Choose a head ref
Original file line number Diff line number Diff line change
@@ -30,19 +30,20 @@
public final class JsonFormatterStep {
private static final String MAVEN_COORDINATE = "org.json:json:";
private static final String DEFAULT_VERSION = "20210307";
private static final int INDENT = 4;

public static FormatterStep create(Provisioner provisioner) {
public static FormatterStep create(Integer indent, Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner cannot be null");
return FormatterStep.createLazy("json", () -> new State(provisioner), State::toFormatter);
return FormatterStep.createLazy("json", () -> new State(indent, provisioner), State::toFormatter);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

private final int indentSpaces;
private final JarState jarState;

private State(Provisioner provisioner) throws IOException {
private State(Integer indent, Provisioner provisioner) throws IOException {
this.indentSpaces = indent;
this.jarState = JarState.from(MAVEN_COORDINATE + DEFAULT_VERSION, provisioner);
}

@@ -61,8 +62,7 @@ FormatterFunc toFormatter() {
Class<?> jsonArray = classLoader.loadClass("org.json.JSONArray");
arrayConstructor = jsonArray.getConstructor(constructorArguments);
arrayToString = jsonArray.getMethod("toString", int.class);

} catch (Exception e) {
} catch (ClassNotFoundException | NoSuchMethodException e) {
throw new IllegalStateException("There was a problem preparing org.json dependencies", e);
}

@@ -74,15 +74,15 @@ FormatterFunc toFormatter() {
if (s.startsWith("{")) {
try {
Object parsed = objectConstructor.newInstance(s);
prettyPrinted = objectToString.invoke(parsed, INDENT) + "\n";
prettyPrinted = objectToString.invoke(parsed, indentSpaces) + "\n";
} catch (InvocationTargetException ignored) {
// ignore if we cannot convert to JSON string
}
}
if (s.startsWith("[")) {
try {
Object parsed = arrayConstructor.newInstance(s);
prettyPrinted = arrayToString.invoke(parsed, INDENT) + "\n";
prettyPrinted = arrayToString.invoke(parsed, indentSpaces) + "\n";
} catch (InvocationTargetException ignored) {
// ignore if we cannot convert to JSON string
}
Original file line number Diff line number Diff line change
@@ -20,12 +20,13 @@
import com.diffplug.spotless.json.JsonFormatterStep;

public class JsonExtension extends FormatExtension {
private static final Integer DEFAULT_INDENTATION = 4;
static final String NAME = "json";

@Inject
public JsonExtension(SpotlessExtension spotless) {
super(spotless);
addStep(JsonFormatterStep.create(provisioner()));
addStep(JsonFormatterStep.create(DEFAULT_INDENTATION, provisioner()));
}

@Override
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@

public class JsonExtensionTest extends GradleIntegrationHarness {
@Test
public void formatsWhenTargetsAreSpecified() throws IOException {
public void defaultFormatting() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
@@ -39,4 +39,42 @@ public void formatsWhenTargetsAreSpecified() throws IOException {
assertFile("src/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json");
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectAfter.json");
}

@Test
public void formattingWithCustomNumberOfSpaces() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" json {" +
" indentWithSpaces(6)" +
" target '**/*.json'" +
"}",
"}");
setFile("src/main/resources/example.json").toResource("json/singletonArrayBefore.json");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/resources/example.json").sameAsResource("json/singletonArrayAfter6Spaces.json");
}

@Test
public void formattingWithTabs() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" json {" +
" indentWithTabs()" +
" target '**/*.json'" +
"}",
"}");
setFile("src/main/resources/example.json").toResource("json/singletonArrayBefore.json");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/resources/example.json").sameAsResource("json/singletonArrayAfterTabs.json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,2,3,4]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
1,
2,
3,
4
]
6 changes: 6 additions & 0 deletions testlib/src/main/resources/json/singletonArrayAfterTabs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
1,
2,
3,
4
]
Original file line number Diff line number Diff line change
@@ -26,12 +26,14 @@

public class JsonFormatterStepTest {

private final FormatterStep step = JsonFormatterStep.create(TestProvisioner.mavenCentral());
private static final int INDENT = 4;

private final FormatterStep step = JsonFormatterStep.create(INDENT, TestProvisioner.mavenCentral());
private final StepHarness stepHarness = StepHarness.forStep(step);

@Test
public void cannotProvidedNullProvisioner() {
assertThatThrownBy(() -> JsonFormatterStep.create(null)).isInstanceOf(NullPointerException.class).hasMessage("provisioner cannot be null");
assertThatThrownBy(() -> JsonFormatterStep.create(INDENT, null)).isInstanceOf(NullPointerException.class).hasMessage("provisioner cannot be null");
}

@Test
@@ -79,15 +81,54 @@ public void handlesNotJson() {
assertThatThrownBy(() -> doWithResource(stepHarness, "notJson")).isInstanceOf(AssertionError.class).hasMessage("Invalid JSON file provided");
}

@Test
public void canSetCustomIndentationLevel() throws Exception {
FormatterStep step = JsonFormatterStep.create(6, TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStep(step);

String before = "json/singletonArrayBefore.json";
String after = "json/singletonArrayAfter6Spaces.json";
stepHarness.testResource(before, after);
}

@Test
public void canSetIndentationLevelTo0() throws Exception {
FormatterStep step = JsonFormatterStep.create(0, TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStep(step);

String before = "json/singletonArrayBefore.json";
String after = "json/singletonArrayAfter0Spaces.json";
stepHarness.testResource(before, after);
}

@Test
public void indentAsNullUsesTabs() throws Exception {
FormatterStep step = JsonFormatterStep.create(null, TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStep(step);

String before = "json/singletonArrayBefore.json";
String after = "json/singletonArrayAfterTabs.json";
stepHarness.testResource(before, after);
}

@Test
public void equality() {
new SerializableEqualityTester() {
int spaces = 0;

@Override
protected void setupTest(API api) {}
protected void setupTest(API api) {
// no changes, are the same
api.areDifferentThan();

// with different spacing
spaces = 1;
api.areDifferentThan();
}

@Override
protected FormatterStep create() {
return JsonFormatterStep.create(TestProvisioner.mavenCentral());
return JsonFormatterStep.create(spaces, TestProvisioner.mavenCentral());
}
}.testEquals();
}