forked from eclipse/xtext
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java21RecordCompilerTest in xbase.tests
- Loading branch information
1 parent
c266e18
commit c2befaf
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...text.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/Java21RecordCompilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Lorenzo Bettini and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.xtext.xbase.tests.compiler; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.xtext.diagnostics.Severity; | ||
import org.eclipse.xtext.util.JavaRuntimeVersion; | ||
import org.eclipse.xtext.util.Strings; | ||
import org.eclipse.xtext.xbase.testing.CompilationTestHelper; | ||
import org.eclipse.xtext.xbase.testing.TemporaryFolder; | ||
import org.eclipse.xtext.xbase.tests.jvmmodel.AbstractJvmModelTest; | ||
import org.junit.Assume; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import com.google.inject.Inject; | ||
|
||
/** | ||
* @author Lorenzo Bettini - Initial contribution and API | ||
*/ | ||
public class Java21RecordCompilerTest extends AbstractJvmModelTest { | ||
@Rule | ||
@Inject | ||
public TemporaryFolder temporaryFolder; | ||
|
||
@Inject | ||
private CompilationTestHelper compilationTestHelper; | ||
|
||
/** | ||
* Since there's no record in the JDK and adding a Java source with a record in this | ||
* project would require Java 21 compilation level, I'm using a known record from | ||
* draw2d, which is in our target platform. | ||
*/ | ||
@Test public void testUseJavaRecord() throws Exception { | ||
Assume.assumeTrue("Active only on Java 21 and later", JavaRuntimeVersion.isJava21OrLater()); | ||
String source = | ||
"{\n" | ||
+ " var interval = new org.eclipse.draw2d.geometry.Interval(0, 10);\n" | ||
+ " var int begin = interval.begin\n" | ||
+ " var end = interval.end()\n" | ||
+ " interval.toString" | ||
+ "}"; | ||
compilationTestHelper.compile(source, it -> { | ||
String expectation = | ||
"import org.eclipse.draw2d.geometry.Interval;\n" | ||
+ "\n" | ||
+ "@SuppressWarnings(\"all\")\n" | ||
+ "public class Test {\n" | ||
+ " public String doStuff(final String s) {\n" | ||
+ " String _xblockexpression = null;\n" | ||
+ " {\n" | ||
+ " Interval interval = new Interval(0, 10);\n" | ||
+ " int begin = interval.begin();\n" | ||
+ " int end = interval.end();\n" | ||
+ " _xblockexpression = interval.toString();\n" | ||
+ " }\n" | ||
+ " return _xblockexpression;\n" | ||
+ " }\n" | ||
+ "}\n" | ||
+ ""; | ||
var errors = it.getErrorsAndWarnings().stream() | ||
.filter(issue -> issue.getSeverity() == Severity.ERROR) | ||
.collect(Collectors.toList()); | ||
if (!errors.isEmpty()) { | ||
throw new IllegalStateException("One or more resources contained issues: " + errors); | ||
} | ||
assertEquals(expectation, Strings.toUnixLineSeparator(it.getSingleGeneratedCode())); | ||
it.getCompiledClass(); | ||
}); | ||
} | ||
} |