Skip to content

Commit c05f205

Browse files
author
Matthias Balke
committed
refactor unit tests
1 parent 17b3cd7 commit c05f205

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Antlr4TaskTest.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,59 @@
3030
import com.diffplug.spotless.TestProvisioner;
3131
import com.diffplug.spotless.antlr4.Antlr4FormatterStep;
3232

33-
public class Antlr4TaskTest extends ResourceHarness {
33+
public class Antlr4TaskTest extends GradleIntegrationHarness {
3434

3535
private Provisioner provisioner = TestProvisioner.mavenCentral();
3636

37-
private SpotlessTask checkTask;
38-
private SpotlessTask applyTask;
39-
40-
@Before
41-
public void createTask() {
42-
Project project = ProjectBuilder.builder().build();
43-
checkTask = project.getTasks().create("checkTaskUnderTest", SpotlessTask.class);
44-
checkTask.setCheck();
45-
applyTask = project.getTasks().create("applyTaskUnderTest", SpotlessTask.class);
46-
applyTask.setApply();
47-
}
48-
4937
@Test(expected = GradleException.class)
5038
public void testFormatCheckFail() throws Exception {
51-
String unformatted = getTestResource("antlr4/Hello.unformatted.g4");
39+
setFile("build.gradle").toLines(
40+
"buildscript { repositories { mavenCentral() } }",
41+
"plugins {",
42+
" id 'com.diffplug.gradle.spotless'",
43+
"}",
44+
"spotless {",
45+
" antlr4 {",
46+
" antlr4Formatter()",
47+
" }",
48+
"}");
5249

53-
checkTask.addStep(Antlr4FormatterStep.create(provisioner));
54-
checkTask.setTarget(Collections.singleton(setFile("testFile.g4").toContent(unformatted)));
55-
execute(checkTask);
50+
setFile("src/main/antlr/Hello.g4").toResource("antlr4/Hello.unformatted.g4");
51+
gradleRunner().withArguments("spotlessCheck").build();
5652
}
5753

5854
@Test
5955
public void testFormatedCheckPass() throws Exception {
60-
String formatted = getTestResource("antlr4/Hello.formatted.g4");
56+
setFile("build.gradle").toLines(
57+
"buildscript { repositories { mavenCentral() } }",
58+
"plugins {",
59+
" id 'com.diffplug.gradle.spotless'",
60+
"}",
61+
"spotless {",
62+
" antlr4 {",
63+
" antlr4Formatter()",
64+
" }",
65+
"}");
6166

62-
checkTask.addStep(Antlr4FormatterStep.create(provisioner));
63-
checkTask.setTarget(Collections.singleton(setFile("testFile.g4").toContent(formatted)));
64-
execute(checkTask);
67+
setFile("src/main/antlr/Hello.g4").toResource("antlr4/Hello.formatted.g4");
68+
gradleRunner().withArguments("spotlessCheck").build();
6569
}
6670

6771
@Test
6872
public void testFormatApplyPass() throws Exception {
69-
String unformatted = getTestResource("antlr4/Hello.unformatted.g4");
70-
String formatted = getTestResource("antlr4/Hello.formatted.g4");
71-
String testFile = "testFile.g4";
72-
73-
applyTask.addStep(Antlr4FormatterStep.create(provisioner));
74-
applyTask.setTarget(Collections.singleton(setFile(testFile).toContent(unformatted)));
75-
execute(applyTask);
73+
setFile("build.gradle").toLines(
74+
"buildscript { repositories { mavenCentral() } }",
75+
"plugins {",
76+
" id 'com.diffplug.gradle.spotless'",
77+
"}",
78+
"spotless {",
79+
" antlr4 {",
80+
" antlr4Formatter()",
81+
" }",
82+
"}");
7683

77-
assertFile(testFile).hasContent(formatted);
84+
setFile("src/main/antlr/Hello.g4").toResource("antlr4/Hello.unformatted.g4");
85+
gradleRunner().withArguments("spotlessApply").build();
86+
assertFile("src/main/antlr/Hello.g4").sameAsResource("antlr4/Hello.formatted.g4");
7887
}
7988
}

0 commit comments

Comments
 (0)