Skip to content

Commit

Permalink
Bump default version. Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Simkin committed Aug 20, 2021
1 parent b2e29e2 commit 719aa09
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

/** Wraps up [scalafmt](https://github.com/olafurpg/scalafmt) as a FormatterStep. */
/** Wraps up [scalafmt](https://github.com/scalameta/scalafmt) as a FormatterStep. */
public class ScalaFmtStep {
// prevent direct instantiation
private ScalaFmtStep() {}

private static final Pattern VERSION_PRE_2_0 = Pattern.compile("[10]\\.(\\d+)\\.\\d+");
private static final Pattern VERSION_PRE_3_0 = Pattern.compile("2\\.(\\d+)\\.\\d+");
private static final String DEFAULT_VERSION = "2.0.1";
private static final String DEFAULT_VERSION = "3.0.0";
static final String NAME = "scalafmt";
static final String MAVEN_COORDINATE_PRE_2_0 = "com.geirsson:scalafmt-core_2.11:";
static final String MAVEN_COORDINATE_PRE_3_0 = "org.scalameta:scalafmt-core_2.11:";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,6 @@ public void integration() throws IOException {
setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf");
setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty");
gradleRunner().withArguments("spotlessApply").build();
assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class IncludesExcludesTest extends MavenIntegrationHarness {
private static final String JAVA_FORMATTED = "java/eclipse/JavaCodeFormatted.test";
private static final String JAVA_UNFORMATTED = "java/eclipse/JavaCodeUnformatted.test";
private static final String SCALA_UNFORMATTED = "scala/scalafmt/basic.dirty";
private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_2.0.1";
private static final String SCALA_FORMATTED = "scala/scalafmt/basic.clean_3.0.0";

@Test
public void testDefaultIncludesJava() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testConfigurationDependency() throws Exception {
assertFile("two/src/main/java/test1.java").sameAsResource("java/eclipse/JavaCodeFormatted.test");
assertFile("two/src/test/java/test2.java").sameAsResource("java/eclipse/JavaCodeFormatted.test");

assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
assertFile("three/src/main/scala/test1.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
assertFile("three/src/test/scala/test2.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ScalafmtTest extends MavenIntegrationHarness {
public void testScalafmtWithDefaultConfig() throws Exception {
writePomWithScalaSteps("<scalafmt/>");

runTest("scala/scalafmt/basic.clean_2.0.1");
runTest("scala/scalafmt/basic.clean_3.0.0");
}

@Test
Expand All @@ -36,7 +36,7 @@ public void testScalafmtWithCustomConfig() throws Exception {
" <file>${project.basedir}/scalafmt.conf</file>",
"</scalafmt>");

runTest("scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
runTest("scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

private void runTest(String s) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a
extends b
with c {
def foo[
T: Int#Double#Triple,
R <% String
](
@annot1
x: Int @annot2 =
2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> =>
2
}
}
}
20 changes: 20 additions & 0 deletions testlib/src/main/resources/scala/scalafmt/basic.clean_3.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a extends b with c {
def foo[T: Int#Double#Triple, R <% String](
@annot1
x: Int @annot2 = 2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> => 2
}
}
}
20 changes: 20 additions & 0 deletions testlib/src/main/resources/scala/scalafmt/basicPost3.0.0.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a extends b with c {
def foo[T: Int#Double#Triple, R <% String](
@annot1
x: Int @annot2 = 2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> => 2
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@foobar(
"annot", {
val x = 2
val y = 2 // y=2
x + y
}
)
object a
extends b
with c {
def foo[
T: Int#Double#Triple,
R <% String
](
@annot1
x: Int @annot2 =
2,
y: Int = 3
): Int = {
"match" match {
case 1 | 2 =>
3
case <A>2</A> =>
2
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void behaviorDefaultConfig() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_1.1.0");
StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), null))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_2.0.1");
StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.clean_3.0.0");
}

@Test
Expand All @@ -47,6 +49,8 @@ public void behaviorCustomConfig() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_1.1.0");
StepHarness.forStep(ScalaFmtStep.create("2.0.1", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_2.0.1");
StepHarness.forStep(ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf")))
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basic.cleanWithCustomConf_3.0.0");
}

@Test
Expand All @@ -63,6 +67,20 @@ public void behaviorCustomConfigVersion_2_0_0() throws Exception {
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost2.0.0.cleanWithCustomConf");
}

@Test
public void behaviorDefaultConfigVersion_3_0_0() throws Exception {
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), null);
StepHarness.forStep(step)
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.clean");
}

@Test
public void behaviorCustomConfigVersion_3_0_0() throws Exception {
FormatterStep step = ScalaFmtStep.create("3.0.0", TestProvisioner.mavenCentral(), createTestFile("scala/scalafmt/scalafmt.conf"));
StepHarness.forStep(step)
.testResource("scala/scalafmt/basic.dirty", "scala/scalafmt/basicPost3.0.0.cleanWithCustomConf");
}

@Test
public void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down Expand Up @@ -106,5 +124,9 @@ public void invalidConfiguration() throws Exception {
exception = assertThrows(InvocationTargetException.class,
() -> StepHarness.forStep(ScalaFmtStep.create("2.0.1", provisioner, invalidConfFile)).test("", ""));
assertThat(exception.getTargetException().getMessage(), containsString("Invalid field: invalidScalaFmtConfigField"));

exception = assertThrows(InvocationTargetException.class,
() -> StepHarness.forStep(ScalaFmtStep.create("3.0.0", provisioner, invalidConfFile)).test("", ""));
assertThat(exception.getTargetException().getMessage(), containsString("found option 'invalidScalaFmtConfigField' which wasn't expected"));
}
}

0 comments on commit 719aa09

Please sign in to comment.