Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ case BreakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined) -> {
// levels that are meant to look good even if partially inlined, e.g. method
// chains, which will catch things like builders, but not other kinds of levels like
// constant expressions.
// See the palantir-expression-lambdas cases in test-cases/testdata for an example of
// See the palantir-expression-lambdas cases in test-cases-java21/testdata for an example of
// what this is trying to avoid.

// For this, need to actually check the last inner level of `lastLevel` (2 levels down).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@

@Execution(ExecutionMode.CONCURRENT)
public final class FileBasedTests {
// Test files that are only used when run with a minimum Java version
private static final ImmutableMultimap<Integer, String> VERSIONED_TESTS =
ImmutableMultimap.<Integer, String>builder()
.putAll(23, "ModuleImport")
.build();

private final Class<?> testClass;
/** The path prefix for all tests if loaded as resources. */
private final Path resourcePrefix;
Expand All @@ -66,13 +60,6 @@ public FileBasedTests(Class<?> testClass, String testDirName) {
this.fullTestPath = Paths.get("src/test/resources").resolve(resourcePrefix);
}

public static void assumeJavaVersionForTest(String testName) {
Optional<Integer> maybeJavaVersion =
VERSIONED_TESTS.inverse().get(testName).stream().collect(toOptional());
maybeJavaVersion.ifPresent(version -> Assumptions.assumeTrue(
Formatter.getRuntimeVersion() >= version, String.format("Not running on jdk %d or later", version)));
}

/** The names of the resources under {@link #resourcePrefix}, in every directory of the class path that has it. */
private List<String> resourceNames(ClassLoader classLoader) throws IOException {
var names = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.java;

import java.util.List;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/** The cases on JDK 21 and later: those for the syntax of JDK 21. */
@EnabledForJreRange(min = JRE.JAVA_21)
@Execution(ExecutionMode.CONCURRENT)
final class FormatterCasesJava21Test {
@TestFactory
List<DynamicTest> cases() throws Exception {
return FormatterCasesTest.casesIn("test-cases-java21");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.java;

import java.util.List;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/** The cases on JDK 25 and later: those of JDK 21, and those for the syntax of JDK 25. */
@EnabledForJreRange(min = JRE.JAVA_25)
@Execution(ExecutionMode.CONCURRENT)
final class FormatterCasesJava25Test {
@TestFactory
List<DynamicTest> cases() throws Exception {
return FormatterCasesTest.casesIn("test-cases-java25", "test-cases-java21");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.java;

import java.util.List;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/** The cases on JDK 28 and later: those of JDK 21 and 25, and those for the syntax of JDK 28. */
@EnabledForJreRange(min = JRE.JAVA_28)
@Execution(ExecutionMode.CONCURRENT)
final class FormatterCasesJava28Test {
@TestFactory
List<DynamicTest> cases() throws Exception {
return FormatterCasesTest.casesIn("test-cases-java28", "test-cases-java25", "test-cases-java21");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,49 @@
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/**
* One test for each {@link CaseFile} under {@code test-cases} on the test classpath: formatting the before block gives
* the after block, and formatting that again changes nothing. Adding a case is adding a file.
* One test for each {@link CaseFile} in the given folders on the test classpath: formatting the before block gives the
* after block, and formatting that again changes nothing. Adding a case is adding a file. The cases are in a folder of
* the JDK they need, and each JDK has a class that runs the folders it can parse, such as
* {@link FormatterCasesJava21Test}.
*/
@Execution(ExecutionMode.CONCURRENT)
final class FormatterCasesTest {
private static final String CASES = "test-cases";

private static final Formatter FORMATTER = Formatter.createFormatter(
JavaFormatterOptions.builder().style(Style.OJF).build());

@TestFactory
List<DynamicTest> cases() throws Exception {
var folder = requireNonNull(
FormatterCasesTest.class.getClassLoader().getResource(CASES),
"no " + CASES + " folder on the test classpath");
var root = Path.of(folder.toURI());
var paths = CaseFile.pathsIn(root);
/** The tests of the case files under {@code folders} on the test classpath. */
static List<DynamicTest> casesIn(String... folders) throws Exception {
var tests = new ArrayList<DynamicTest>();

// A wrong folder or a resource filter would otherwise pass with no tests at all.
assertThat(paths).as("the case files under %s", root).isNotEmpty();
for (var folder : folders) {
var url = requireNonNull(
FormatterCasesTest.class.getClassLoader().getResource(folder),
"no " + folder + " folder on the test classpath");
var root = Path.of(url.toURI());
var paths = CaseFile.pathsIn(root);

var tests = new ArrayList<DynamicTest>();
// A wrong folder or a resource filter would otherwise pass with no tests at all.
assertThat(paths).as("the case files under %s", root).isNotEmpty();

for (var path : paths) {
var name = path.substring(0, path.length() - CaseFile.EXTENSION.length());
for (var path : paths) {
var name = path.substring(0, path.length() - CaseFile.EXTENSION.length());

tests.add(DynamicTest.dynamicTest(name, () -> {
var testCase = CaseFile.parse(path, Files.readString(root.resolve(path)));
var once = FORMATTER.formatSourceAndFixImports(testCase.before());
tests.add(DynamicTest.dynamicTest(name, () -> {
var testCase = CaseFile.parse(path, Files.readString(root.resolve(path)));
var once = FORMATTER.formatSourceAndFixImports(testCase.before());

assertThat(once)
.as(() -> CASES + "/" + path + ", first run, " + testCase.firstDifference(once))
.isEqualTo(testCase.after());
assertThat(once)
.as(() -> folder + "/" + path + ", first run, " + testCase.firstDifference(once))
.isEqualTo(testCase.after());

var twice = FORMATTER.formatSourceAndFixImports(once);
var twice = FORMATTER.formatSourceAndFixImports(once);

assertThat(twice)
.as(() -> CASES + "/" + path + ", second run, " + testCase.firstDifference(twice))
.isEqualTo(once);
}));
assertThat(twice)
.as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice))
.isEqualTo(once);
}));
}
}

return tests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package com.palantir.javaformat.java;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.palantir.javaformat.java.FileBasedTests.assumeJavaVersionForTest;
import static com.palantir.javaformat.java.FileBasedTests.isRecreate;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -74,7 +73,6 @@ private static boolean isDebugMode() {

@TestTemplate
public void format() {
assumeJavaVersionForTest(name);
try {
Formatter formatter = createFormatter();
String output = formatter.formatSource(input);
Expand All @@ -99,7 +97,6 @@ private static Formatter createFormatter() {

@TestTemplate
public void idempotentLF() {
assumeJavaVersionForTest(name);
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
try {
String mangled = expected.replace(separator(), "\n");
Expand All @@ -112,7 +109,6 @@ public void idempotentLF() {

@TestTemplate
public void idempotentCR() {
assumeJavaVersionForTest(name);
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
try {
String mangled = expected.replace(separator(), "\r");
Expand All @@ -125,7 +121,6 @@ public void idempotentCR() {

@TestTemplate
public void idempotentCRLF() {
assumeJavaVersionForTest(name);
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
try {
String mangled = expected.replace(separator(), "\r\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import javax.lang.model.element.Modifier;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
Expand Down Expand Up @@ -352,44 +349,6 @@ void indentsCommentsThatShareALineLinearly() throws FormatterException {
assertThat(formatter.formatSource(input.toString()).length()).isLessThan(10_000);
}

@Test
void valueClassAndRecord() throws FormatterException {
Assumptions.assumeTrue(
Arrays.stream(Modifier.values())
.anyMatch(modifier -> modifier.name().equals("VALUE")),
"`value` is a modifier only on a JDK with Project Valhalla's value classes");
String input = """
value final public class PointValueClass {
final int x;
final int y;

PointValueClass(int x, int y) {
this.x = x;
this.y = y;
}
}

value public record PointValueRecord(int x, int y) {}
""";
String expected = """
public final value class PointValueClass {
final int x;
final int y;

PointValueClass(int x, int y) {
this.x = x;
this.y = y;
}
}

public value record PointValueRecord(int x, int y) {}
""";
Formatter formatter = Formatter.createFormatter(
JavaFormatterOptions.builder().style(Style.OJF).build());

assertThat(formatter.formatSource(input)).isEqualTo(expected);
}

@Test
@SuppressWarnings("deprecation")
void palantirStyleAliasMatchesOjfStyle() throws FormatterException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.java;

import static com.google.common.truth.Truth.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/** {@link ModifierOrderer} on the {@code value} modifier of value classes, a preview of JDK 28. */
@EnabledForJreRange(min = JRE.JAVA_28)
@Execution(ExecutionMode.CONCURRENT)
final class ModifierOrdererJava28Test {
@Test
void valueClass() throws FormatterException {
assertThat(ModifierOrderer.reorderModifiers("value final public").getText())
.isEqualTo("public final value");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Range;
import java.util.Arrays;
import javax.lang.model.element.Modifier;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
Expand Down Expand Up @@ -115,16 +113,6 @@ void nonSealedBeforeAccessModifier() throws FormatterException {
.isEqualTo("private non-sealed interface B extends I {}");
}

@Test
void valueClass() throws FormatterException {
Assumptions.assumeTrue(
Arrays.stream(Modifier.values())
.anyMatch(modifier -> modifier.name().equals("VALUE")),
"`value` is a modifier only on a JDK with Project Valhalla's value classes");
assertThat(ModifierOrderer.reorderModifiers("value final public").getText())
.isEqualTo("public final value");
}

@Test
void nonMinusValueIsAnExpression() throws FormatterException {
// `non - value` lexes like `non-sealed`, and there is no NON_VALUE modifier to look up for it
Expand Down
Loading
Loading