@@ -393,33 +393,91 @@ final class TraitASTTransformationTest {
393
393
@Test
394
394
void testOverridePropertyDefinedInTrait () {
395
395
assertScript shell, '''
396
- trait Id {
397
- Long id = 123L
396
+ trait Foo {
397
+ long id = 123L
398
398
}
399
-
400
- class Foo implements Id {
401
- Long id = 456L
399
+ class Bar implements Foo {
400
+ long id = 456L
402
401
}
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
405
406
'''
406
407
}
407
408
408
409
@Test
409
410
void testOverridePropertyGetterDefinedInTrait () {
410
411
assertScript shell, '''
411
- trait Id {
412
- Long id = 123L
412
+ trait Foo {
413
+ long id = 123L
413
414
}
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
+ }
414
424
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 }
417
430
}
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
420
440
'''
421
441
}
422
442
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
+
423
481
@Test
424
482
void testSimpleTraitInheritance () {
425
483
assertScript shell, '''
0 commit comments