Skip to content

Commit

Permalink
Java21RecordCompilerTest in xbase.tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed May 16, 2024
1 parent d4cd0f1 commit c08cf7b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Require-Bundle: org.eclipse.xtext.xbase;bundle-version="2.35.0",
org.eclipse.core.runtime;bundle-version="3.24.100",
org.eclipse.xtext.xbase.testdata;bundle-version="2.35.0",
org.eclipse.xtext.xbase.ide;bundle-version="2.35.0",
org.junit;bundle-version="4.13.2"
org.junit;bundle-version="4.13.2",
org.eclipse.draw2d;bundle-version="3.16.0"
Bundle-Vendor: Eclipse Xtext
Export-Package: org.eclipse.xtext.xbase.tests;version="2.35.0",
org.eclipse.xtext.xbase.tests.interpreter;version="2.35.0"
Expand Down
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();
});
}
}

0 comments on commit c08cf7b

Please sign in to comment.