Skip to content

Commit

Permalink
Issue #212 reproduced.
Browse files Browse the repository at this point in the history
  • Loading branch information
aunkrig committed Jan 9, 2024
1 parent 53f9e69 commit 397fa1e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1938,4 +1938,25 @@ interface Nukable {
);
this.assertCompilationUnitMainReturnsTrue(body, "MyClass");
}

@Test public void
testIssue212AssignmentConversionNotPossibleFromTypeInterfaceClassToTypeAbstractClass() throws Exception {

String body = (
""
+ "import org.codehaus.commons.compiler.tests.issue212.base.IBase;\n"
+ "import org.codehaus.commons.compiler.tests.issue212.base.BaseClass;\n"
+ "import org.codehaus.commons.compiler.tests.issue212.base.DerivedClass;\n"
+ "import org.codehaus.commons.compiler.tests.issue212.base.MapperClass;\n"
+ "\n"
+ "public class MyClass {\n"
+ " public static String main() {\n"
+ " IBase o1 = new MapperClass().mapper().path();\n"
+ " BaseClass o2 = new MapperClass().mapper().path();\n"
+ " return o2.toString();\n"
+ " }\n"
+ "}\n"
);
this.assertCompilationUnitMainEquals("BaseClass [baseId=0]DerivedClass [name=default]", body, "MyClass");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

package org.codehaus.commons.compiler.tests.issue212.base;

public abstract class BaseClass implements IBase {

private String baseId;

public
BaseClass() {}

public
BaseClass(String baseId) {
super();
this.baseId = baseId;
}

@Override public abstract BaseClass
path();

@Override public String
toString() {
return "BaseClass [baseId=" + this.baseId + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

package org.codehaus.commons.compiler.tests.issue212.base;

public
class DerivedClass extends BaseClass {

private String name;

public
DerivedClass() {
super();
}

public
DerivedClass(String baseId, String name) {
super(baseId);
this.name = name;
}

@Override public BaseClass
path() {
return new DerivedClass("0", "default");
}

@Override public String
toString() {
return super.toString() + "DerivedClass [name=" + this.name + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

package org.codehaus.commons.compiler.tests.issue212.base;

public
interface IBase {
IBase path();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

package org.codehaus.commons.compiler.tests.issue212.base;

public
class MapperClass {

public BaseClass
mapper() {
return new DerivedClass();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,23 @@ class ClassBodyTest extends CompileAndExecuteTest {

/**
* Asserts that the given <var>compilationUnit</var> can be cooked by the {@link ISimpleCompiler} and its {@code
* public static boolean }<em>className</em>{@code .main()} method returns TRUE, <em>or</em> issues an error that matches
* public static boolean }<var>className</var>{@code .main()} method returns TRUE, <em>or</em> issues an error that matches
* the <var>messageRegex</var>.
*/
protected void
assertCompilationUnitMainReturnsTrue(String compilationUnit, String className, String messageRegex) throws Exception {
new SimpleCompilerTest(compilationUnit, className).assertResultTrue(messageRegex);
}

/**
* Asserts that the given <var>compilationUnit</var> can be cooked by the {@link ISimpleCompiler}, and its {@code
* public static any-type }<var>className</var>{@code .main()} method returns a value that equals <var>expected</var>.
*/
protected void
assertCompilationUnitMainEquals(Object expected, String compilationUnit, String className) throws Exception {
new SimpleCompilerTest(compilationUnit, className).assertResultEquals(expected);
}

public
class SimpleCompilerTest extends CompileAndExecuteTest {

Expand Down Expand Up @@ -732,6 +741,16 @@ class CompileAndExecuteTest {
Assert.assertTrue("Test result is FALSE", (Boolean) result);
}

/**
* Asserts that cooking completes normally and executing returns a value that equals <var>expected</var>.
*/
public void
assertResultEquals(@Nullable Object expected) throws Exception {
this.cook();
Object result = this.execute();
Assert.assertEquals(expected, result);
}

/**
* Asserts that cooking completes normally and executing returns {@code null}.
*/
Expand Down

0 comments on commit 397fa1e

Please sign in to comment.