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 2022/02/28 15:25:05 UTC
[groovy] branch master updated: GROOVY-10516: add test case
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new d16b22cf GROOVY-10516: add test case
d16b22cf is described below
commit d16b22cf364917d9c8b7c91fbc7040a89f562cea
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Feb 28 09:24:53 2022 -0600
GROOVY-10516: add test case
---
.../groovy/transform/FieldTransformTest.groovy | 172 +++++++++++----------
1 file changed, 90 insertions(+), 82 deletions(-)
diff --git a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
index 9146c6f..05f80ab 100644
--- a/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/FieldTransformTest.groovy
@@ -18,155 +18,161 @@
*/
package org.codehaus.groovy.transform
-import gls.CompilableTestSupport
+import org.codehaus.groovy.control.CompilerConfiguration
+import org.codehaus.groovy.control.customizers.ImportCustomizer
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
/**
- * Tests for the {@code @Field} transformation
+ * Tests for the {@code @Field} AST transform.
*/
-class FieldTransformTest extends CompilableTestSupport {
+final class FieldTransformTest {
+
+ private final GroovyShell shell = new GroovyShell(new CompilerConfiguration().
+ addCompilationCustomizers(new ImportCustomizer().tap { addImports('groovy.transform.Field') })
+ )
+ @Test
void testInstanceField() {
- assertScript """
- @groovy.transform.Field List awe = [1, 2, 3]
+ assertScript shell, '''
+ @Field List awe = [1, 2, 3]
def awesum() { awe.sum() }
assert awesum() == 6
assert this.awe instanceof List
assert this.class.getDeclaredField('awe').type.name == 'java.util.List'
- """
+ '''
}
+ @Test
void testStaticFieldFromScript() {
- assertScript """
- import groovy.transform.*
+ assertScript shell, '''
@Field static List awe = [1, 2, 3]
def awesum() { awe.sum() + this.class.awe.sum() }
assert awesum() == 12
assert this.class.awe instanceof List
- """
+ '''
}
+ @Test
void testStaticFieldFromMethod() {
- assertScript """
- import groovy.transform.*
+ assertScript shell, '''
@Field static String exer = 'exercise'
static exersize() { exer.size() }
assert exersize() == 8
- """
+ '''
}
+ @Test
void testFieldInitialization() {
- assertScript """
- def scriptText = '''
- import groovy.transform.*
- @Field public pepsi = [1, 2, 3]
- '''
- def gcs = new GroovyCodeSource(scriptText, 'foo', 'bar')
- def klass = new GroovyShell().parseClass(gcs)
- assert klass.newInstance().pepsi.max() == 3
- """
+ def src = '''
+ @Field public pepsi = [1, 2, 3]
+ '''
+ def gcs = new GroovyCodeSource(src, 'foo', 'bar')
+ def cls = shell.parseClass(gcs)
+
+ assert cls.getDeclaredConstructor().newInstance().pepsi.max() == 3
}
+ @Test
void testStaticFieldInitialization() {
- assertScript """
- def scriptText = '''
- import groovy.transform.*
- @Field public static ad = [1, 2, 3]
- assert ad.min() == 1
- '''
- def gcs = new GroovyCodeSource(scriptText, 'foo', 'bar')
- def klass = new GroovyShell().parseClass(gcs)
- assert klass.ad.min() == 1
- """
+ def src = '''
+ @Field public static ad = [1, 2, 3]
+ assert ad.min() == 1
+ '''
+ def gcs = new GroovyCodeSource(src, 'foo', 'bar')
+ def cls = shell.parseClass(gcs)
+ assert cls.ad.min() == 1
}
+ @Test
void testFieldTypes() {
- assertScript """
- import groovy.transform.*
+ assertScript shell, '''
@Field int one
@Field int two = 2
@Field Integer three = 3
this.one = 1
assert this.one + this.two + this.three == 6
- """
+ '''
}
+ @Test
void testNotAllowedInScriptMethods() {
- shouldNotCompile """
- import groovy.transform.*
+ shouldFail shell, '''
def method() {
@Field int one
}
- """
+ '''
}
+ @Test
void testNotAllowedForClassFields() {
- shouldNotCompile """
- import groovy.transform.*
+ shouldFail shell, '''
class Inner {
@Field int one
}
- """
+ '''
}
+ @Test
void testNotAllowedForScriptInnerClassFields() {
- shouldNotCompile """
- import groovy.transform.*
+ shouldFail shell, '''
class Inner {
@Field int one
}
println Inner.class.name
- """
+ '''
}
+ @Test
void testNotAllowedInClassMethods() {
// currently two error messages!
- shouldNotCompile """
- import groovy.transform.*
+ shouldFail shell, '''
class Inner {
def bar() {
@Field int one
}
}
- """
+ '''
}
+ @Test
void testNotAllowedInScriptInnerClassMethods() {
// currently two error messages!
- shouldNotCompile """
- import groovy.transform.*
+ shouldFail shell, '''
class Inner {
def bar() {
@Field int one
}
}
println Inner.class.name
- """
+ '''
}
+ @Test
void testFieldShouldBeAccessibleFromClosure() {
- assertScript """
- import groovy.transform.Field
+ assertScript shell, '''
@Field int x
def closure = { x = 1; x }
assert closure() == 1
- """
+ '''
}
+ @Test // GROOVY-4700
void testFieldShouldBeAccessibleFromClosureWithoutAssignment() {
- // GROOVY-4700
- assertScript """import groovy.transform.Field
+ assertScript shell, '''
@Field xxx = 3
foo = {
xxx + 1
}
assert foo() == 4
- """
+ '''
}
+ @Test // GROOVY-5207
void testFieldShouldBeAccessibleFromClosureForExternalClosures() {
- // GROOVY-5207
- assertScript """
- import groovy.transform.Field
+ assertScript shell, '''
@Field xxx = [:]
@Field static yyy = [:]
[1, 2, 3].each {
@@ -175,22 +181,22 @@ class FieldTransformTest extends CompilableTestSupport {
}
assert xxx == [(1):1, (2):2, (3):3]
assert yyy == xxx
- """
+ '''
}
+ @Test
void testStaticFieldShouldBeAccessibleFromClosure() {
- assertScript """
- import groovy.transform.Field
+ assertScript shell, '''
@Field static int x
x = 10
def closure = { x * 2 }
assert closure() == 20
- """
+ '''
}
+ @Test
void testAnnotationsOnFieldShouldBeSet() {
- assertScript """
- import groovy.transform.Field
+ assertScript shell, '''
import java.lang.annotation.*
@Retention(RetentionPolicy.RUNTIME)
@@ -206,15 +212,12 @@ class FieldTransformTest extends CompilableTestSupport {
assert Doh.getDeclaredField('test').getAnnotations().size() == 1
assert this.class.getDeclaredField('test').getAnnotations().size() == 1
-
- """
+ '''
}
+ @Test // GROOVY-6112
void testGroovyTransformsShouldTransferToFields() {
- // GROOVY-6112
- assertScript '''
- import groovy.transform.Field
-
+ assertScript shell, '''
@Lazy @Field foo = 'foo'
@Field @Lazy bar = 'bar'
@Field baz = 'baz'
@@ -226,10 +229,10 @@ class FieldTransformTest extends CompilableTestSupport {
'''
}
+ @Test // GROOVY-8112
void testAnonymousInnerClassReferencesToField() {
- // GROOVY-8112
- assertScript '''
- @groovy.transform.Field
+ assertScript shell, '''
+ @Field
StringBuilder logger = new StringBuilder()
logger.append('a')
['b'].each {
@@ -260,10 +263,10 @@ class FieldTransformTest extends CompilableTestSupport {
'''
}
+ @Test // GROOVY-9554
void testClosureReferencesToField() {
- // GROOVY-9554
- assertScript '''
- @groovy.transform.Field String abc
+ assertScript shell, '''
+ @Field String abc
binding.variables.clear()
abc = 'abc'
assert !binding.hasVariable('abc')
@@ -276,11 +279,9 @@ class FieldTransformTest extends CompilableTestSupport {
'''
}
+ @Test // GROOVY-8430
void testFieldTransformWithFinalField() {
- // GROOVY-8430
- assertScript '''
- import groovy.transform.Field
-
+ assertScript shell, '''
@Field final foo = 14
@Field final bar = foo * 2
@Field baz = foo + bar
@@ -292,11 +293,18 @@ class FieldTransformTest extends CompilableTestSupport {
'''
}
+ @Test // GROOVY-8430 in conjunction with @Option
void testFieldTransformWithFinalFieldAndOption() {
- // GROOVY-8430 in conjunction with @Option
- shouldNotCompile '''
+ shouldFail '''
import groovy.cli.OptionField
@OptionField final String first
'''
}
+
+ @Test // GROOVY-10516
+ void testFieldOnFullyQualifiedType() {
+ assertScript shell, '''
+ @Field java.util.List list
+ '''
+ }
}