Skip to content

Commit 39ced53

Browse files
committed
add trait tests
1 parent 50375ec commit 39ced53

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

src/test/groovy/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,33 +393,91 @@ final class TraitASTTransformationTest {
393393
@Test
394394
void testOverridePropertyDefinedInTrait() {
395395
assertScript shell, '''
396-
trait Id {
397-
Long id = 123L
396+
trait Foo {
397+
long id = 123L
398398
}
399-
400-
class Foo implements Id {
401-
Long id = 456L
399+
class Bar implements Foo {
400+
long id = 456L
402401
}
403-
def f = new Foo()
404-
assert f.id == 456L
402+
403+
def pogo = new Bar()
404+
assert pogo.id == 456L
405+
assert pogo.getId() == 456L
405406
'''
406407
}
407408

408409
@Test
409410
void testOverridePropertyGetterDefinedInTrait() {
410411
assertScript shell, '''
411-
trait Id {
412-
Long id = 123L
412+
trait Foo {
413+
long id = 123L
413414
}
415+
class Bar implements Foo {
416+
long getId() { 456L }
417+
}
418+
419+
def pogo = new Bar()
420+
assert pogo.id == 456L
421+
assert pogo.getId() == 456L
422+
'''
423+
}
414424

415-
class Foo implements Id {
416-
Long getId() { 456L }
425+
@Test
426+
void testShadowingPropertyGetterDefinedInTrait() {
427+
assertScript shell, '''
428+
trait Foo {
429+
private long getId() { 123L }
417430
}
418-
def f = new Foo()
419-
assert f.id == 456L
431+
trait Bar extends Foo {
432+
long id = 456L
433+
}
434+
class Baz implements Bar {
435+
}
436+
437+
def pogo = new Baz()
438+
assert pogo.id == 456L
439+
assert pogo.getId() == 456L
420440
'''
421441
}
422442

443+
@ParameterizedTest
444+
@ValueSource(strings=['public',/*TODO:'protected',*/'private'])
445+
void testFinalPropertyGetterDefinedInSuperClass(String modifier) {
446+
assertScript shell, """
447+
trait Foo {
448+
final long id = 123L
449+
}
450+
class Bar {
451+
$modifier final long getId() { 456L }
452+
}
453+
class Baz extends Bar implements Foo {
454+
}
455+
456+
def pogo = new Baz()
457+
assert pogo.id == ${modifier == 'private' ? '123L' : '456L'}
458+
assert pogo.getId() == ${modifier == 'private' ? '123L' : '456L'}
459+
"""
460+
}
461+
462+
@ParameterizedTest
463+
@ValueSource(strings=['private','@PackageScope','protected','public','static'])
464+
void testNonFinalPropertyGetterDefinedInSuperClass(String modifier) {
465+
assertScript shell, """
466+
trait Foo {
467+
long id = 123L
468+
}
469+
class Bar {
470+
$modifier long getId() { 456L }
471+
}
472+
class Baz extends Bar implements Foo {
473+
}
474+
475+
def pogo = new Baz()
476+
assert pogo.id == 123L
477+
assert pogo.getId() == 123L
478+
"""
479+
}
480+
423481
@Test
424482
void testSimpleTraitInheritance() {
425483
assertScript shell, '''

0 commit comments

Comments
 (0)