Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test for switch with arrow #183

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
}
}
Loading