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

Cleanup tests using Java records #3261

Merged
merged 4 commits into from
Dec 4, 2024
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
3 changes: 1 addition & 2 deletions org.eclipse.xtend.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ Require-Bundle: org.eclipse.xtend.core;bundle-version="2.38.0",
org.objectweb.asm;bundle-version="[9.7.1,9.8.0)",
org.eclipse.jdt.core;bundle-version="3.37.0",
org.eclipse.xtext.builder.standalone;bundle-version="2.38.0",
org.eclipse.xtext.java;bundle-version="2.38.0",
org.eclipse.draw2d
org.eclipse.xtext.java;bundle-version="2.38.0"
Import-Package: jakarta.inject;version="2.0.1",
org.apache.log4j;version="1.2.24",
org.apache.log4j.spi;version="1.2.24",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import org.eclipse.xtend.core.xtend.XtendClass;
import org.eclipse.xtend.core.xtend.XtendInterface;
import org.eclipse.xtext.testing.validation.ValidationTestHelper;
import org.eclipse.xtext.util.JavaRuntimeVersion;
import org.eclipse.xtext.xbase.XbasePackage;
import org.junit.Assume;
import org.junit.Test;

import com.google.inject.Inject;
Expand Down Expand Up @@ -1068,18 +1066,11 @@ public void testClassMustBeAbstract_06() throws Exception {
helper.assertNoWarnings(xtendClass, XTEND_FUNCTION, MISSING_SYNCHRONIZED);
}

/**
* 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 testOverrideJavaRecord() throws Exception {
Assume.assumeTrue("Active only on Java 21 and later", JavaRuntimeVersion.isJava21OrLater());
var javaRecord = "org.eclipse.draw2d.geometry.Interval";
var source = "class Foo extends " + javaRecord + " { }";
var source = "class Foo extends test.SomeRecord { }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_CLASS, OVERRIDDEN_FINAL,
source.indexOf(javaRecord), javaRecord.length(),
source.indexOf("test.SomeRecord"), "test.SomeRecord".length(),
"override", "final");
}

Expand Down
16 changes: 16 additions & 0 deletions org.eclipse.xtend.core.tests/testdata/test/SomeRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* 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 test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public record SomeRecord() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test;

public record JavaRecord(String name) {
public XtendA foo(XtendA test) {
return test;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package test

class XtendA extends JavaB {

def JavaB test2(XtendC s) {
return s.foo.newJavaB
}

def JavaB newJavaB() {
return new JavaB()
}


def JavaRecord newJavaRecord() {
return new JavaRecord("a test")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

import java.io.File;

import org.eclipse.xtext.util.JavaRuntimeVersion;
import org.eclipse.xtext.util.JavaVersion;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -36,8 +34,7 @@ public void setUp() {
}

@Test
public void testCleanBuildAccessingJava21Record() {
Assume.assumeTrue("Active only on Java 21 and later", JavaRuntimeVersion.isJava21OrLater());
public void testCleanBuildAccessingJavaRecord() {
initBuilder(new ContentAssistFragmentTestLangConfiguration());
assertTrue(testBuilder.launch());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.testing.util.InMemoryURIHandler;
import org.eclipse.xtext.util.JavaRuntimeVersion;
import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -186,8 +184,7 @@ public void testClassShadowing() {
}

@Test
public void testJava21Record() {
Assume.assumeTrue("Active only on Java 21 and later", JavaRuntimeVersion.isJava21OrLater());
public void testJavaRecord() {
ImmutableMap<String, String> files = ImmutableMap.<String, String>builder()
.put("example/MyRecord.java",
"package example;\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public void javaLangBiRef() throws Exception {
verifier.verifyFilePresent(verifier.getBasedir() + "/src-gen/XbaseReferToJava.java");
verifier.verifyFilePresent(verifier.getBasedir() + "/target/xtext-temp/stub-classes/XbaseReferToJava.class");
verifier.verifyFilePresent(verifier.getBasedir() + "/target/xtext-temp/stub-classes/JavaClazz.class");
verifier.verifyFilePresent(verifier.getBasedir() + "/target/xtext-temp/stub-classes/JavaRecord.class");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public record JavaRecord(String name) {
public void useXbaseType(XbaseReferToJava xbaseType) throws Throwable {
xbaseType.myMethod();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
val JavaClazz jc = null
val JavaRecord jr = new JavaRecord("test")
val name = jr.name()
jc.useXbaseType(this)
3 changes: 1 addition & 2 deletions org.eclipse.xtext.xbase.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Require-Bundle: org.eclipse.xtext.xbase;bundle-version="2.38.0",
org.eclipse.core.runtime;bundle-version="3.31.0",
org.eclipse.xtext.xbase.testdata;bundle-version="2.38.0",
org.eclipse.xtext.xbase.ide;bundle-version="2.38.0",
org.junit;bundle-version="4.13.2",
org.eclipse.draw2d
org.junit;bundle-version="4.13.2"
Bundle-Vendor: Eclipse Xtext
Export-Package: org.eclipse.xtext.xbase.tests;version="2.38.0",
org.eclipse.xtext.xbase.tests.interpreter;version="2.38.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
package org.eclipse.xtext.xbase.tests.compiler;

import org.eclipse.xtext.testing.TemporaryFolder;
import org.eclipse.xtext.util.JavaRuntimeVersion;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper;
import org.eclipse.xtext.xbase.tests.jvmmodel.AbstractJvmModelTest;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -30,30 +28,24 @@ public class Java21RecordCompilerTest extends AbstractJvmModelTest {
@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 interval = new test.RecordWithFields(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"
"import test.RecordWithFields;\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"
+ " RecordWithFields interval = new RecordWithFields(0, 10);\n"
+ " int begin = interval.begin();\n"
+ " int end = interval.end();\n"
+ " _xblockexpression = interval.toString();\n"
Expand Down
16 changes: 16 additions & 0 deletions org.eclipse.xtext.xbase.tests/testdata/test/RecordWithFields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* 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 test;

/**
* @author Lorenzo Bettini - Initial contribution and API
*/
public record RecordWithFields(int begin, int end) {

}
Loading