From dd03018f79c74edf0ab6628553387330bd7e5f3e Mon Sep 17 00:00:00 2001 From: Caesar Ralf Franz Hoppen Date: Mon, 9 Oct 2023 11:01:51 +0200 Subject: [PATCH] Adds test for switch with arrow (#183) * Adds test for switch with arrow * Run switch test only if java >= 14 --------- Co-authored-by: Caesar Ralf --- src/test/java/com/spotify/fmt/FMTTest.java | 22 ++++++++++---- .../switchwitharrows/invoker.properties | 2 ++ src/test/resources/switchwitharrows/pom.xml | 29 +++++++++++++++++++ .../main/java/com.spotify.fmt.test/Main.java | 13 +++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/switchwitharrows/invoker.properties create mode 100644 src/test/resources/switchwitharrows/pom.xml create mode 100644 src/test/resources/switchwitharrows/src/main/java/com.spotify.fmt.test/Main.java diff --git a/src/test/java/com/spotify/fmt/FMTTest.java b/src/test/java/com/spotify/fmt/FMTTest.java index 79c69ab..2f0c92e 100644 --- a/src/test/java/com/spotify/fmt/FMTTest.java +++ b/src/test/java/com/spotify/fmt/FMTTest.java @@ -43,8 +43,8 @@ import org.mockito.Mockito; public class FMTTest { - private static String FORMAT = "format"; - private static String CHECK = "check"; + private static final String FORMAT = "format"; + private static final String CHECK = "check"; @Rule public MojoRule mojoRule = new MojoRule(); @@ -177,7 +177,7 @@ public void forkAlways() throws Exception { @Test public void forkNeverBeforeJDK16() throws Exception { - assumeFalse(javaRuntimeStronglyEncapsulatesByDefault()); // Skip if forking is needed. + assumeFalse(isJavaVersionEqualOrHigherThan("16")); // Skip if forking is needed. FMT fmt = loadMojo("fork_never_beforejdk16", FORMAT); assertThat(fmt.shouldFork()).isFalse(); fmt.execute(); @@ -189,7 +189,7 @@ public void forkNeverBeforeJDK16() throws Exception { expected = IllegalAccessError.class) // Could stop throwing this if google-java-format is fixed. public void forkNeverAfterJDK16() throws Exception { - assumeTrue(javaRuntimeStronglyEncapsulatesByDefault()); // Skip if forking is not needed. + assumeTrue(isJavaVersionEqualOrHigherThan("16")); FMT fmt = loadMojo("fork_never_afterjdk16", FORMAT); assertThat(fmt.shouldFork()).isFalse(); fmt.execute(); @@ -205,6 +205,16 @@ public void unsupportedForkMode() throws Exception { assertThat(fmt.getResult().processedFiles()).hasSize(1); } + @Test + public void switchWithArrows() throws Exception { + assumeTrue(isJavaVersionEqualOrHigherThan("14")); + FMT fmt = loadMojo("switchwitharrows", FORMAT); + + fmt.execute(); + + assertThat(fmt.getResult().processedFiles()).hasSize(1); + } + @Test(expected = MojoFailureException.class) public void validateOnlyFailsWhenNotFormatted() throws Exception { Check check = loadMojo("validateonly_notformatted", CHECK); @@ -322,7 +332,7 @@ private Log setupLogSpy(Mojo mojo) { return spy; } - private static boolean javaRuntimeStronglyEncapsulatesByDefault() { - return Runtime.version().compareTo(Runtime.Version.parse("16")) >= 0; + private static boolean isJavaVersionEqualOrHigherThan(final String javaVersion) { + return Runtime.version().compareTo(Runtime.Version.parse(javaVersion)) >= 0; } } diff --git a/src/test/resources/switchwitharrows/invoker.properties b/src/test/resources/switchwitharrows/invoker.properties new file mode 100644 index 0000000..273517c --- /dev/null +++ b/src/test/resources/switchwitharrows/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format +invoker.java.version=14+ diff --git a/src/test/resources/switchwitharrows/pom.xml b/src/test/resources/switchwitharrows/pom.xml new file mode 100644 index 0000000..1e445a2 --- /dev/null +++ b/src/test/resources/switchwitharrows/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + org.apache.maven.plugin.my.unit + project-to-test + 1.0.0 + jar + Test MyMojo + + + + junit + junit + 4.13.1 + test + + + + + + + + + + + + diff --git a/src/test/resources/switchwitharrows/src/main/java/com.spotify.fmt.test/Main.java b/src/test/resources/switchwitharrows/src/main/java/com.spotify.fmt.test/Main.java new file mode 100644 index 0000000..12fa4af --- /dev/null +++ b/src/test/resources/switchwitharrows/src/main/java/com.spotify.fmt.test/Main.java @@ -0,0 +1,13 @@ +package com.spotify.fmt.test; + +public class Main { + public static void main(String[] args) { + var test = "a"; + var result = + switch (test) { + case "videos" -> null; + case "images" -> null; + default -> null; + }; + } +}