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

Added additional Xtend validator tests #2873

Merged
merged 20 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a5be13b
Added an Xtend validator test for cycles in internal types
LorenzoBettini Dec 18, 2023
e90a83c
also verify the error's position for hierarchy cycles
LorenzoBettini Dec 20, 2023
7620075
also verify error's position for INTERFACE_EXPECTED
LorenzoBettini Dec 20, 2023
69b04d3
also verify error's position for OVERRIDDEN_FINAL
LorenzoBettini Dec 20, 2023
bd3c61a
also verify error's position for CLASS_EXPECTED
LorenzoBettini Dec 20, 2023
3af4f66
also verify error's position for field INVALID_USE_OF_TYPE
LorenzoBettini Dec 20, 2023
b68fdb0
also verify error's position for parameter INVALID_USE_OF_TYPE
LorenzoBettini Dec 20, 2023
798947e
fixed verify error's position for parameter INVALID_USE_OF_TYPE
LorenzoBettini Dec 20, 2023
9ee8300
also verify error's position for DUPLICATE_PARAMETER_NAME
LorenzoBettini Dec 20, 2023
0a107d6
also verify error's position for duplicates
LorenzoBettini Dec 20, 2023
2492401
also verify error's position for duplicate methods
LorenzoBettini Dec 20, 2023
0216c6b
also verify error's message parts for duplicate methods
LorenzoBettini Dec 20, 2023
1713723
added missing tests for doCheckOverriddenMethods
LorenzoBettini Dec 29, 2023
31e82ab
also verify error position for CONFLICTING_DEFAULT_METHODS
LorenzoBettini Dec 29, 2023
82b03fb
also verify error position for CLASS_MUST_BE_ABSTRACT
LorenzoBettini Dec 29, 2023
ad69b30
also verify error position for ANONYMOUS_CLASS_MISSING_MEMBERS
LorenzoBettini Dec 29, 2023
fd8c3bc
also verify error position for anonymous class override final class
LorenzoBettini Dec 29, 2023
365f1fa
added test with anonymous class not implementing abstract methods
LorenzoBettini Dec 30, 2023
9b52cd7
also verify error position for constructors' errors
LorenzoBettini Jan 4, 2024
6140007
also verify error position for anonymous class' members
LorenzoBettini Jan 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,19 @@ public abstract class AbstractXtendTestCase extends Assert {

@Rule
@Inject public TemporaryFolder temporaryFolder;

@Inject
private Provider<XtextResourceSet> resourceSetProvider;

/**
* To build a function or constructor for tests
*/
protected static final String TEMPLATE_CLASS = "class Foo { %s }";
/**
* Use it to add it to the index position of a source element of a function or constructor
*/
protected static final int TEMPLATE_CLASS_PREFIX_SIZE = TEMPLATE_CLASS.indexOf("%s");

protected XtendClass clazz(String string) throws Exception {
return (XtendClass) file(string).getXtendTypes().get(0);
}
Expand Down Expand Up @@ -160,7 +169,7 @@ protected XtendEnum enumeration(String string) throws Exception {
}

protected XtendFunction function(String string) throws Exception {
XtendClass clazz = clazz("class Foo { " + string + "}");
XtendClass clazz = clazz(String.format(TEMPLATE_CLASS, string));
return (XtendFunction) clazz.getMembers().get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,57 +69,65 @@ class Java8ValidationTest extends AbstractXtendTestCase {

@Test
def void testConflictingDefaultMethods01() {
file('''
val source = '''
interface A {
def void foo() { }
}
interface B {
def void foo() { }
}
class C implements A, B { }
''').assertError(XTEND_CLASS, CONFLICTING_DEFAULT_METHODS,
'''
file(source).assertError(XTEND_CLASS, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The type C inherits multiple implementations of the method foo() from A and B.")
}

@Test
def void testConflictingDefaultMethods02() {
file('''
val source = '''
interface A {
def void foo() { }
}
interface B {
def void foo() { }
}
interface C extends A, B { }
''').assertError(XTEND_INTERFACE, CONFLICTING_DEFAULT_METHODS,
'''
file(source).assertError(XTEND_INTERFACE, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The type C inherits multiple implementations of the method foo() from A and B.")
}

@Test
def void testConflictingDefaultMethods03() {
file('''
val source = '''
interface A {
def void foo() { }
}
interface B {
def void foo()
}
class C implements A, B { }
''').assertError(XTEND_CLASS, CONFLICTING_DEFAULT_METHODS,
'''
file(source).assertError(XTEND_CLASS, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The non-abstract method foo() inherited from A conflicts with the method foo() inherited from B.")
}

@Test
def void testConflictingDefaultMethods04() {
file('''
val source = '''
interface A {
def void foo() { }
}
interface B {
def void foo()
}
interface C extends A, B { }
''').assertError(XTEND_INTERFACE, CONFLICTING_DEFAULT_METHODS,
'''
file(source).assertError(XTEND_INTERFACE, CONFLICTING_DEFAULT_METHODS,
source.indexOf("C"), 1,
"The non-abstract method foo() inherited from A conflicts with the method foo() inherited from B.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ public class OverrideValidationTest extends AbstractXtendTestCase {
private ValidationTestHelper helper;

@Test public void testDuplicateMethod_0() throws Exception {
XtendClass xtendClass = clazz("class Foo { def bar(int x) {true} def bar(int x) {false} }");
helper.assertError(xtendClass.getMembers().get(0), XTEND_FUNCTION, DUPLICATE_METHOD, "duplicate");
var source = "class Foo { def bar(int x) {true} def bar(int x) {false} }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass.getMembers().get(0), XTEND_FUNCTION, DUPLICATE_METHOD,
source.indexOf("bar"), "bar".length(),
"duplicate", "method");
helper.assertError(xtendClass.getMembers().get(0), XTEND_FUNCTION, DUPLICATE_METHOD,
source.lastIndexOf("bar"), "bar".length(),
"duplicate", "method");
}

@Test public void testDuplicateMethod_1() throws Exception {
Expand All @@ -50,9 +56,11 @@ public class OverrideValidationTest extends AbstractXtendTestCase {

@Test public void testDuplicateMethod_3() throws Exception {
XtendClass xtendClass = clazz("class Foo { def bar(java.util.List<String> x) {true} def bar(java.util.List<Integer> x) {false} }");
helper.assertError(xtendClass.getMembers().get(0), XTEND_FUNCTION, DUPLICATE_METHOD, "erasure", "List)",
helper.assertError(xtendClass.getMembers().get(0), XTEND_FUNCTION, DUPLICATE_METHOD,
"erasure", "List)", "method",
"List<String");
helper.assertError(xtendClass.getMembers().get(1), XTEND_FUNCTION, DUPLICATE_METHOD, "erasure", "List)",
helper.assertError(xtendClass.getMembers().get(1), XTEND_FUNCTION, DUPLICATE_METHOD,
"erasure", "List)", "method",
"List<Integer");
}

Expand Down Expand Up @@ -148,10 +156,14 @@ public class OverrideValidationTest extends AbstractXtendTestCase {
}

@Test public void testOverrideGenericMethod_10() throws Exception {
XtendClass xtendClass = clazz(" abstract class Foo<T> extends test.GenericSuperTypeClass<T> { " +
var source = " abstract class Foo<T> extends test.GenericSuperTypeClass<T> { " +
"override <T extends String> foo1() {}"+
"}");
helper.assertError(xtendClass, XTEND_FUNCTION, DUPLICATE_METHOD);
"}";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_FUNCTION, DUPLICATE_METHOD,
source.indexOf("foo1"), "foo1".length(),
"Name clash", "same erasure", "GenericSuperTypeClass",
"does not override it");
}

@Test public void testOverrideGenericMethod_11() throws Exception {
Expand Down Expand Up @@ -189,6 +201,28 @@ public class OverrideValidationTest extends AbstractXtendTestCase {
helper.assertNoErrors(xtendClass);
}

@Test public void testStaticMethodHidesInstanceMethod() throws Exception {
var source = " abstract class Foo<T> extends test.GenericSuperTypeClass<T> { " +
"static def foo1() {}"+
"}";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_FUNCTION, DUPLICATE_METHOD,
source.indexOf("foo1"), "foo1".length(),
"The static method", "GenericSuperTypeClass",
"cannot hide the instance method");
}

@Test public void testOverrideStaticMethod() throws Exception {
var source = " abstract class Foo extends testdata.Methods { " +
"override staticMethod() {}"+
"}";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_FUNCTION, DUPLICATE_METHOD,
source.indexOf("staticMethod"), "staticMethod".length(),
"The instance method", "Methods",
"cannot override the static method");
}

@Test public void testOverrideReturnType() throws Exception {
XtendClass xtendClass = clazz("abstract class Foo<T> extends test.GenericSuperTypeClass<T> { " +
"override <T> T getSomething(T t){} "+
Expand Down Expand Up @@ -355,20 +389,29 @@ public class OverrideValidationTest extends AbstractXtendTestCase {
}

@Test public void testClassMustBeAbstract_01() throws Exception {
XtendClass xtendClass = clazz("class Foo<S> implements Comparable<S> { }");
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT, "abstract", "not", "implement",
var source = "class Foo<S> implements Comparable<S> { }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT,
source.indexOf("Foo"), "Foo".length(),
"abstract", "not", "implement",
"compareTo(S)");
}

@Test public void testClassMustBeAbstract_02() throws Exception {
XtendClass xtendClass = clazz("class Foo<S> implements Comparable { }");
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT, "abstract", "not", "implement",
var source = "class Foo<S> implements Comparable { }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT,
source.indexOf("Foo"), "Foo".length(),
"abstract", "not", "implement",
"compareTo(Object)");
}

@Test public void testClassMustBeAbstract_03() throws Exception {
XtendClass xtendClass = clazz("class Foo implements Comparable<String> { }");
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT, "abstract", "not", "implement",
var source = "class Foo implements Comparable<String> { }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_CLASS, CLASS_MUST_BE_ABSTRACT,
source.indexOf("Foo"), "Foo".length(),
"abstract", "not", "implement",
"compareTo(String)");
}

Expand All @@ -391,8 +434,11 @@ public void testClassMustBeAbstract_06() throws Exception {
}

@Test public void testOverrideFinalClass() throws Exception {
XtendClass xtendClass = clazz("class Foo extends String { }");
helper.assertError(xtendClass, XTEND_CLASS, OVERRIDDEN_FINAL, "override", "final");
var source = "class Foo extends String { }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, XTEND_CLASS, OVERRIDDEN_FINAL,
source.indexOf("String"), "String".length(),
"override", "final");
}

@Test public void testOverrideFinalMethod() throws Exception {
Expand Down Expand Up @@ -913,8 +959,19 @@ public void testClassMustBeAbstract_06() throws Exception {
}

@Test public void testAnonymousClassMustBeAbstract() throws Exception {
XtendClass xtendClass = clazz("class Foo { val foo = new Runnable() {} }");
helper.assertError(xtendClass, ANONYMOUS_CLASS, ANONYMOUS_CLASS_MISSING_MEMBERS, "The anonymous subclass of Runnable does not implement run()");
var source = "class Foo { val foo = new Runnable() {} }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, ANONYMOUS_CLASS, ANONYMOUS_CLASS_MISSING_MEMBERS,
source.indexOf("new Runnable()"), "new Runnable()".length(),
"The anonymous subclass of Runnable does not implement run()");
}

@Test public void testAnonymousClassMustBeAbstract_1() throws Exception {
var source = "class Foo { val foo = new Runnable() { int i; } }";
XtendClass xtendClass = clazz(source);
helper.assertError(xtendClass, ANONYMOUS_CLASS, ANONYMOUS_CLASS_MISSING_MEMBERS,
source.indexOf("new Runnable()"), "new Runnable()".length(),
"The anonymous subclass of Runnable does not implement run()");
}

@Test public void testAnonymousClassIncompatibleSignature_0() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,41 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
}

@Test def void test_02() {
val c = parser.parse('''
val source = '''
class Foo {
def void foo() {
new Object {
static var x = 1
}
}
}
''')
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD)
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("static"), "static".length,
"A static field of an anonymous class must be final"
)
}

@Test def void test_03() {
val c = parser.parse('''
val source = '''
class Foo {
def void foo() {
new Object {
static val x = Math.max(1, 2)
}
}
}
''')
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD)
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("Math.max(1, 2)"), "Math.max(1, 2)".length,
"must be initialized with a constant expression"
)
}

@Test def void test_04() {
val c = parser.parse('''
val source = '''
class Foo {
def void foo() {
new Object {
Expand All @@ -77,8 +85,12 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
}
}
}
''')
c.assertError(XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD)
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD,
source.indexOf("static"), "static".length,
"A method of an anonymous class cannot be static"
)
}

}
Loading
Loading