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 91e96b9 GROOVY-4349: add test case 91e96b9 is described below commit 91e96b904be7c2148eec0fdb9be51778d6e9774a Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Mon May 17 10:07:54 2021 -0500 GROOVY-4349: add test case --- src/test/groovy/StaticImportTest.groovy | 59 ++++++++++++------------ src/test/groovy/bugs/Groovy4349.groovy | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 29 deletions(-) diff --git a/src/test/groovy/StaticImportTest.groovy b/src/test/groovy/StaticImportTest.groovy index 4ef9adc..d44b616 100644 --- a/src/test/groovy/StaticImportTest.groovy +++ b/src/test/groovy/StaticImportTest.groovy @@ -18,7 +18,6 @@ */ package groovy -import gls.CompilableTestSupport import static java.lang.Boolean.FALSE as F import static java.text.DateFormat.MEDIUM as M import static java.util.regex.Pattern.* @@ -44,7 +43,8 @@ import static java.util.jar.Attributes.Name as AttrName import static groovy.Container5087.* import org.codehaus.groovy.runtime.DefaultGroovyMethods as DGM -class StaticImportTest extends CompilableTestSupport { +final class StaticImportTest extends groovy.test.GroovyTestCase { + void testFieldWithAliasInExpression() { assert !F } @@ -120,25 +120,25 @@ class StaticImportTest extends CompilableTestSupport { assert cfield == 21 assert pfield == 42 } - + void testStaticImportAndDefaultValue() { - assertScript """ - import static Foo.* - import static Bar.* - - class Bar { - static void bar() { - assert foo(10,1000) == 1010 - assert foo(10) == 110 - } - } - - class Foo { - static int foo(int x, int y = 100) {x+y} - } - - Bar.bar() - """ + assertScript ''' + import static Foo.* + import static Bar.* + + class Bar { + static void bar() { + assert foo(10,1000) == 1010 + assert foo(10) == 110 + } + } + + class Foo { + static int foo(int x, int y = 100) {x+y} + } + + Bar.bar() + ''' } void testStaticImportProperty() { @@ -232,14 +232,14 @@ class StaticImportTest extends CompilableTestSupport { void testConstructorArgsAliasing() { // not recommended style to use statics in constructors but supported - assertScript """ - class Foo { - static x - } - import static Foo.x as z - new Foo(z:'hi') - assert z == 'hi' - """ + assertScript ''' + class Foo { + static x + } + import static Foo.x as z + new Foo(z:'hi') + assert z == 'hi' + ''' } void testMethodCallWithThisTargetIsNotResolvedToStaticallyImportedMethod() { @@ -269,7 +269,7 @@ class StaticImportTest extends CompilableTestSupport { } void testMethodCallExpressionInStaticContextWithInstanceVariableShouldFail() { //GROOVY-4228 - shouldNotCompile ''' + def err = shouldFail ''' class B { def c = new Object() static main(args) { @@ -277,6 +277,7 @@ class StaticImportTest extends CompilableTestSupport { } } ''' + assert err =~ /Apparent variable 'c' was found in a static scope but doesn't refer to a local variable, static field or class/ } void testStaticStarImportOfStaticInnerClass() { diff --git a/src/test/groovy/bugs/Groovy4349.groovy b/src/test/groovy/bugs/Groovy4349.groovy new file mode 100644 index 0000000..84b1b5b --- /dev/null +++ b/src/test/groovy/bugs/Groovy4349.groovy @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.bugs + +import org.codehaus.groovy.control.CompilerConfiguration +import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit +import org.junit.Test + +final class Groovy4349 { + @Test + void testStaticImport() { + def config = new CompilerConfiguration( + targetDirectory: File.createTempDir(), + jointCompilationOptions: [memStub: true] + ) + def parentDir = File.createTempDir() + try { + new File(parentDir, 'p').mkdir() + + def a = new File(parentDir, 'p/Types.groovy') + a.write ''' + package p + class C { + static final List<String> calls = [] + def prop + } + class Utility { + static C method1(int i) { + C.calls << 'Utility.method1' + return new C() + } + static C method2(C c) { + C.calls << 'Utility.method2' + return c + } + } + ''' + def b = new File(parentDir, 'p/Main.groovy') + b.write ''' + package p + import static Utility.* + class Main { + def method1() { + C.calls << 'Harness.method1' + new C() + } + void test() { + method2(method1()).prop = 8 + assert C.calls == ['Harness.method1', 'Utility.method2'] + } + } + ''' + + def loader = new GroovyClassLoader(this.class.classLoader) + def cu = new JavaAwareCompilationUnit(config, loader) + cu.addSources(a, b) + cu.compile() + + loader.loadClass('p.Main').newInstance().test() + } finally { + config.targetDirectory.deleteDir() + parentDir.deleteDir() + } + } +}