Skip to content

Commit

Permalink
Adds test for switch with arrow (#183)
Browse files Browse the repository at this point in the history
* Adds test for switch with arrow

* Run switch test only if java >= 14

---------

Co-authored-by: Caesar Ralf <[email protected]>
  • Loading branch information
caesar-ralf and caesar-ralf authored Oct 9, 2023
1 parent 01671e2 commit dd03018
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/test/java/com/spotify/fmt/FMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/test/resources/switchwitharrows/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:format
invoker.java.version=14+
29 changes: 29 additions & 0 deletions src/test/resources/switchwitharrows/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>









Original file line number Diff line number Diff line change
@@ -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;
};
}
}

0 comments on commit dd03018

Please sign in to comment.