You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2024/02/22 18:36:24 UTC
(groovy) branch GROOVY_3_0_X updated: fix `instantiateAggregateFromInterface` call
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new b01a33569f fix `instantiateAggregateFromInterface` call
b01a33569f is described below
commit b01a33569f943a34e66018ecaa16ab5f8a379f50
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Feb 22 12:36:28 2024 -0600
fix `instantiateAggregateFromInterface` call
---
src/test/groovy/util/ProxyGeneratorTest.groovy | 116 +++++++++++++------------
1 file changed, 59 insertions(+), 57 deletions(-)
diff --git a/src/test/groovy/util/ProxyGeneratorTest.groovy b/src/test/groovy/util/ProxyGeneratorTest.groovy
index c8db381fa4..357f086989 100644
--- a/src/test/groovy/util/ProxyGeneratorTest.groovy
+++ b/src/test/groovy/util/ProxyGeneratorTest.groovy
@@ -20,58 +20,75 @@ package groovy.util
import groovy.test.GroovyTestCase
-class ProxyGeneratorTest extends GroovyTestCase {
+import static groovy.util.ProxyGenerator.INSTANCE as generator
- ProxyGenerator generator = ProxyGenerator.INSTANCE
+final class ProxyGeneratorTest extends GroovyTestCase {
void testAggregateFromBaseClass() {
- Map map = [myMethodB: {"the new B"}, myMethodX: {"the injected X"}]
+ Map map = [myMethodB: {'the new B'}, myMethodX: {'the injected X'}]
def testClass = generator.instantiateAggregateFromBaseClass(map, TestClass)
assert testClass instanceof TestClass
- assert testClass.myMethodA() == "the original A"
- assert testClass.myMethodB() == "the new B"
- assert testClass.myMethodX() == "the injected X"
+ assert testClass.myMethodA() == 'the original A'
+ assert testClass.myMethodB() == 'the new B'
+ assert testClass.myMethodX() == 'the injected X'
}
void testAggregateFromAbstractBaseClass() {
- Map map = [myMethodG: {"the concrete G"}, myMethodX: {"the injected X"}]
+ Map map = [myMethodG: {'the concrete G'}, myMethodX: {'the injected X'}]
def testClass = generator.instantiateAggregateFromBaseClass(map, AbstractClass)
assert testClass instanceof AbstractClass
- assert testClass.myMethodA() == "the original A"
- assert testClass.myMethodG() == "the concrete G"
- assert testClass.myMethodX() == "the injected X"
+ assert testClass.myMethodA() == 'the original A'
+ assert testClass.myMethodG() == 'the concrete G'
+ assert testClass.myMethodX() == 'the injected X'
}
void testAggregateFromInterface() {
- Map map = [myMethodC: {"the injected C"}]
+ Map map = [myMethodC: {'the injected C'}]
def testClass = generator.instantiateAggregateFromInterface(map, TestInterface)
assert testClass instanceof TestInterface
assert testClass instanceof GroovyObject
- assert testClass.myMethodC() == "the injected C"
+ assert testClass.myMethodC() == 'the injected C'
}
void testAggregate() {
- Map map = [myMethodE: {"the injected E"}, myMethodB: {"the new B"}, myMethodX: {"the injected X"}]
+ Map map = [myMethodE: {'the injected E'}, myMethodB: {'the new B'}, myMethodX: {'the injected X'}]
def testClass = generator.instantiateAggregate(map, [TestInterface, TestOtherInterface], TestClass)
assert testClass instanceof TestInterface
assert testClass instanceof TestOtherInterface
assert testClass instanceof TestClass
- assert testClass.myMethodA() == "the original A"
- assert testClass.myMethodB() == "the new B"
- assert testClass.myMethodX() == "the injected X"
- assert testClass.myMethodE() == "the injected E"
+ assert testClass.myMethodA() == 'the original A'
+ assert testClass.myMethodB() == 'the new B'
+ assert testClass.myMethodX() == 'the injected X'
+ assert testClass.myMethodE() == 'the injected E'
}
void testDelegate() {
def delegate = new TestClass()
- Map map = [myMethodE: {"the injected E"}, myMethodB: {"the new B"}, myMethodX: {"the injected X"}]
+ Map map = [myMethodE: {'the injected E'}, myMethodB: {'the new B'}, myMethodX: {'the injected X'}]
def testClass = generator.instantiateDelegate(map, [TestInterface, TestOtherInterface], delegate)
assert testClass instanceof TestInterface
assert testClass instanceof TestOtherInterface
- assert testClass.myMethodA() == "the original A"
- assert testClass.myMethodB() == "the new B"
- assert testClass.myMethodX() == "the injected X"
- assert testClass.myMethodE() == "the injected E"
+ assert testClass.myMethodA() == 'the original A'
+ assert testClass.myMethodB() == 'the new B'
+ assert testClass.myMethodX() == 'the injected X'
+ assert testClass.myMethodE() == 'the injected E'
+ }
+
+ // GROOVY-2705
+ void testDelegateOfList() {
+ def delegate = [1, 2, 3, 4, 5]
+ def testClass = generator.instantiateDelegate([List], delegate)
+ assert testClass instanceof List
+ assert testClass.size() == 5
+ assert testClass.iterator().collect() == [1, 2, 3, 4, 5]
+ assert testClass[2] == 3
+
+ testClass[3] = 99
+ assert testClass[3] == 99
+
+ testClass.removeAt(2)
+ testClass.removeAt(1)
+ assert testClass == [1, 99, 5]
}
// GROOVY-10201
@@ -82,33 +99,19 @@ class ProxyGeneratorTest extends GroovyTestCase {
void testDelegateWithBaseClass() {
def delegate = new TestClass()
- Map map = [myMethodE: {"the injected E"}, myMethodB: {"the new B"}, myMethodX: {"the injected X"}]
+ Map map = [myMethodE: {'the injected E'}, myMethodB: {'the new B'}, myMethodX: {'the injected X'}]
TestClass testClass = generator.instantiateDelegateWithBaseClass(map, [TestInterface, TestOtherInterface], delegate)
assert testClass instanceof TestInterface
assert testClass instanceof TestOtherInterface
- assert testClass.myMethodA() == "the original A"
- assert testClass.myMethodB() == "the new B"
- assert testClass.myMethodX() == "the injected X"
- assert testClass.myMethodE() == "the injected E"
- }
-
- void testDelegateForGROOVY_2705() {
- def delegate = [1, 2, 3, 4, 5]
- def testClass = generator.instantiateDelegate([List], delegate)
- assert testClass instanceof List
- assert 5 == testClass.size()
- assert [1, 2, 3, 4, 5] == testClass.iterator().collect { it }
- assert 3 == testClass[2]
- testClass[3] = 99
- assert 99 == testClass[3]
- testClass.removeAt(2)
- testClass.removeAt(1)
- assert [1, 99, 5] == testClass
+ assert testClass.myMethodA() == 'the original A'
+ assert testClass.myMethodB() == 'the new B'
+ assert testClass.myMethodX() == 'the injected X'
+ assert testClass.myMethodE() == 'the injected E'
}
void testUnknownMethodThrowsUnsupportedOperationException() {
- def map = [ myMethodA: { 'some string' } ]
- def proxy = ProxyGenerator.instantiateAggregateFromInterface(map, TestInterface)
+ def map = [myMethodA: { -> 'some string' }]
+ def proxy = generator.instantiateAggregateFromInterface(map, TestInterface)
assert proxy instanceof TestInterface
assertEquals 'some string', proxy.myMethodA()
shouldFail(UnsupportedOperationException) {
@@ -117,47 +120,46 @@ class ProxyGeneratorTest extends GroovyTestCase {
}
void testUnknownMethodWithBlankBody() {
- def map = [ myMethodA: { 'some string' } ]
- def gen = new ProxyGenerator()
- gen.emptyMethods = true
+ def map = [myMethodA: { -> 'some string' }]
+ def gen = new ProxyGenerator(emptyMethods: true)
def proxy = gen.instantiateAggregate(map, [TestInterface])
assert proxy instanceof TestInterface
- assertEquals 'some string', proxy.myMethodA()
+ assert proxy.myMethodA() == 'some string'
proxy.myMethodC()
}
void testProxyWithToString() {
- def map = [ toString: {'hello'} ]
+ def map = [toString: { -> 'hello' }]
def gen = new ProxyGenerator()
def proxy = gen.instantiateAggregateFromBaseClass(map, Object)
assert proxy.toString() == 'hello'
}
-
+
void testProxyWithClosureChangedAfterCreation() {
- def map = [ toString: { 'hello'} ]
+ def map = [toString: { -> 'hello' }]
def gen = new ProxyGenerator()
def proxy = gen.instantiateAggregateFromBaseClass(map, Object)
assert proxy.toString() == 'hello'
map.toString = { 'world' }
assert proxy.toString() == 'world'
}
-
+
void testProxyMethodUsingLongAsParameter() {
- def map = [ foo: { a,b -> a*b }]
+ def map = [foo: { a,b -> a*b }]
def gen = new ProxyGenerator()
def proxy = gen.instantiateAggregateFromBaseClass(map, TestInterfaceWithLong)
assert proxy.foo(3,3) == 9
}
void testProxyMethodUsingDoubleAsParameter() {
- def map = [ foo: { a,b -> a*b }]
+ def map = [foo: { a,b -> a*b }]
def gen = new ProxyGenerator()
def proxy = gen.instantiateAggregateFromBaseClass(map, TestInterfaceWithDouble)
assert proxy.foo(3d,3d) == 9d
}
void testProxyMethodUsingVariousTypesAsParameters() {
- def map = [ foo: { a,b,c,d -> a*b+c-d }]
+ def map = [foo: { a,b,c,d -> a*b+c-d }]
def gen = new ProxyGenerator()
def proxy = gen.instantiateAggregateFromBaseClass(map, TestInterfaceWithVariousTypes)
assert proxy.foo(3, 3d, 4, 1f) == 12d
@@ -165,8 +167,8 @@ class ProxyGeneratorTest extends GroovyTestCase {
}
class TestClass {
- def myMethodA() { return "the original A" }
- def myMethodB() { return "the original B" }
+ def myMethodA() { return 'the original A' }
+ def myMethodB() { return 'the original B' }
}
interface TestInterface {
@@ -194,6 +196,6 @@ interface TestInterfaceWithVariousTypes {
}
abstract class AbstractClass {
- def myMethodA() { return "the original A" }
+ def myMethodA() { return 'the original A' }
abstract myMethodG()
}