Skip to content

Commit

Permalink
adding a test to prove that we can support prettier-plugins now
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jun 11, 2020
1 parent 2bc0464 commit 6f8c550
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.io.IOException;

import org.assertj.core.api.Assertions;
import org.gradle.testkit.runner.BuildResult;
import org.junit.Test;
import org.junit.experimental.categories.Category;

Expand All @@ -41,7 +43,8 @@ public void useInlineConfig() throws IOException {
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
}

Expand All @@ -60,8 +63,33 @@ public void useFileConfig() throws IOException {
" }",
"}");
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
}

@Test
public void useJavaCommunityPlugin() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"def prettierConfig = [:]",
"prettierConfig['tabWidth'] = 4",
"prettierConfig['parser'] = 'java'",
"def prettierPackages = [:]",
"prettierPackages['prettier'] = '2.0.5'",
"prettierPackages['prettier-plugin-java'] = '0.8.0'",
"spotless {",
" format 'java', {",
" target 'JavaTest.java'",
" prettier(prettierPackages).config(prettierConfig)",
" }",
"}");
setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty");
final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean");
}
}
37 changes: 37 additions & 0 deletions testlib/src/main/resources/npm/prettier/plugins/java-test.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class JavaTest {
private static final String NAME = "JavaTest";

private List<String> strings = new ArrayList<>();

public JavaTest(String... strings) {
this.strings.addAll(Arrays.asList(strings));
}

/**
* Join using char.
* @param joiner the char to use for joining.
* @return the joined string.
*/
public String join(char joiner) {
return String.join(joiner, strings);
}

public void operateOn(Consumer<List<String>> consumer) {
// test comment
consumer.accept(strings);
}

public static void main(String[] args) {
JavaTest javaTest = new JavaTest("1", "2", "3");
System.out.println("joined: " + javaTest.join(','));
StringBuilder builder = new StringBuilder();
javaTest.operateOn(
strings -> builder.append(String.join("---", strings))
);
}
}
30 changes: 30 additions & 0 deletions testlib/src/main/resources/npm/prettier/plugins/java-test.dirty
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;


import java.util.Arrays;import java.util.List;
import java.util.function.Consumer;

public class JavaTest { private static final String NAME = "JavaTest";

private List<String> strings = new ArrayList<>();

public JavaTest( String ... strings) {
this.strings .addAll(Arrays.asList(strings));;
}

/**
* Join using char.
* @param joiner the char to use for joining.
* @return the joined string.
*/
public String
join(char joiner ) {
return String.join(joiner, strings);
}

public void operateOn( Consumer<List<String>> consumer) {
// test comment
consumer.accept( strings);
}

public static void main(String[] args) {JavaTest javaTest = new JavaTest("1", "2", "3");System.out.println("joined: " + javaTest.join(',' ));StringBuilder builder = new StringBuilder();javaTest.operateOn((strings) -> builder.append(String.join("---", strings)));}}

0 comments on commit 6f8c550

Please sign in to comment.