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/01/31 22:18:03 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-7160: fix test case
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new 5b0ce95 GROOVY-7160: fix test case
5b0ce95 is described below
commit 5b0ce955fa13fe65513e8254780155747896235c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jan 31 16:16:50 2022 -0600
GROOVY-7160: fix test case
---
.../classgen/asm/sc/BugsStaticCompileTest.groovy | 952 ++++++++++-----------
1 file changed, 475 insertions(+), 477 deletions(-)
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
index ed10ddd..21c67ac 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
@@ -19,21 +19,36 @@
package org.codehaus.groovy.classgen.asm.sc
import groovy.transform.stc.BugsSTCTest
+import org.codehaus.groovy.control.customizers.*
/**
* Unit tests for static type checking : bugs.
*/
final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilationTestSupport {
+ @Override
+ protected void configure() {
+ config.addCompilationCustomizers(new ImportCustomizer().tap {
+ addStarImports 'groovy.transform'
+ addStaticStars 'org.junit.Assert','groovy.transform.TypeCheckingMode'
+ })
+ }
+
+ protected void removeAutoCS() {
+ config.compilationCustomizers.removeAll {
+ it instanceof ASTTransformationCustomizer
+ }
+ }
+
+ //--------------------------------------------------------------------------
+
// GROOVY-5498
void testPropertyAccess() {
assertScript '''
- class Test {
-
+ class C {
List getListVar() {
new ArrayList()
}
-
void someMethod() {
def t = new Object()
t = this
@@ -43,17 +58,18 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
assert t.listVar == t.getListVar()
}
}
- new Test().someMethod()
+
+ new C().someMethod()
'''
}
// GROOVY-5512
void testCreateRangeInInnerClass() {
- def shell = new GroovyShell()
- shell.evaluate '''
+ removeAutoCS()
+ assertScript '''
class Outer {
static class Inner {
- @groovy.transform.CompileStatic
+ @CompileStatic
int m() {
int x = 0
for (int i in 1..10) {x++}
@@ -68,61 +84,53 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5526
void testAssertEqualsShouldNotThrowVerifyError() {
assertScript '''
- import static org.junit.Assert.*;
- import groovy.transform.CompileStatic;
-
class CompilerBugs {
-
public static void main(String[] args) {
int expected = 0
assertEquals(expected, args.length)
}
-
}
'''
}
// GROOVY-5529
void testStaticCompilationOfClosureWhenSingleMethodAnnotated() {
- new GroovyShell().evaluate '''import groovy.transform.ASTTest
- import static org.codehaus.groovy.control.CompilePhase.*
-
- interface Row {
- int getKey()
- }
+ removeAutoCS()
+ assertScript '''
+ interface Row {
+ int getKey()
+ }
- class RowImpl implements Row {
- int getKey() { 1 }
- }
+ class RowImpl implements Row {
+ int getKey() { 1 }
+ }
- @groovy.transform.CompileStatic
- def test() {
- def rows = [new RowImpl(), new RowImpl(), new RowImpl()]
+ @CompileStatic
+ def test() {
+ def rows = [new RowImpl(), new RowImpl(), new RowImpl()]
- rows.each { Row row ->
- println row.key
+ rows.each { Row row ->
+ println row.key
+ }
}
- }
- test()
+ test()
'''
}
// GROOVY-5536
void testShouldNotThrowVerifyErrorWithNullDereferenceInIf() {
assertScript '''
- boolean getDescriptorForPlugin(File pluginDir) {
- if (pluginDir?.exists()) { true } else { false }
- }
- assert getDescriptorForPlugin(null) == false
+ boolean getDescriptorForPlugin(File pluginDir) {
+ if (pluginDir?.exists()) { true } else { false }
+ }
+ assert getDescriptorForPlugin(null) == false
'''
}
// GROOVY-9863
void testPlusShouldNotThrowGroovyBugError() {
assertScript '''
- import static org.junit.Assert.assertEquals
-
class C {
double getSomeValue() {
0.0d
@@ -138,7 +146,8 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-
void testPowerShouldNotThrowVerifyError() {
- assertScript '''int squarePlusOne(int num) {
+ assertScript '''
+ int squarePlusOne(int num) {
num ** num + 1
}
assert squarePlusOne(2) == 5
@@ -148,17 +157,17 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5570
void testShouldNotThrowVerifyErrorRegisterContainsWrongType() {
assertScript '''
- void foo() {
- boolean idx = false
- def cl = { idx }
- }
- '''
+ void foo() {
+ boolean idx = false
+ def cl = { idx }
+ }
+ '''
assertScript '''
- void foo() {
- int idx = 0
- def cl = { idx }
- }
- '''
+ void foo() {
+ int idx = 0
+ def cl = { idx }
+ }
+ '''
}
// GROOVY-5572
@@ -170,9 +179,8 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5564
void testSkipStaticCompile() {
- new GroovyShell().evaluate '''import groovy.transform.CompileStatic
- import static groovy.transform.TypeCheckingMode.SKIP
-
+ removeAutoCS()
+ assertScript '''
@CompileStatic
class A {
@CompileStatic(SKIP)
@@ -182,18 +190,16 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
def a = new A()
assert a.toString('foo')=='foo'
assert a.toString(1) == '1'
- '''
+ '''
}
// GROOVY-5586
void testCanonicalInInnerClass() {
- new GroovyShell().evaluate '''import groovy.transform.*
- @CompileStatic
+ assertScript '''
class CanonicalStaticTest extends GroovyTestCase {
@Canonical class Thing {
String stuff
}
-
Thing testCanonical() {
new Thing()
}
@@ -212,7 +218,6 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
}
}
- @groovy.transform.CompileStatic
class Main {
void test() {
@ASTTest(phase=INSTRUCTION_SELECTION, value= {
@@ -227,105 +232,108 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
}
void testCompileStaticTwiceShouldNotBeAProblem() {
- new GroovyShell().evaluate '''import groovy.transform.CompileStatic
- @CompileStatic
- class Tool {
- @CompileStatic // annotated too, even if class is already annotated
- String relativePath(File relbase, File file) {
- def pathParts = []
- def currentFile = file
- while (currentFile != null && currentFile != relbase) {
- pathParts += currentFile.name
- currentFile = currentFile.parentFile
+ removeAutoCS()
+ assertScript '''
+ @CompileStatic
+ class C {
+ @CompileStatic // annotated too, even if class is already annotated
+ String relativePath(File relbase, File file) {
+ def pathParts = []
+ def currentFile = file
+ while (currentFile != null && currentFile != relbase) {
+ pathParts += currentFile.name
+ currentFile = currentFile.parentFile
+ }
+ pathParts.reverse().join('/')
}
- pathParts.reverse().join('/')
}
- }
- File a = new File('foo')
- File b = new File(new File(a, 'bar'), 'baz')
- assert new Tool().relativePath(a,b) == 'bar/baz'
+ File a = new File('foo')
+ File b = new File(new File(a, 'bar'), 'baz')
+ assert new C().relativePath(a,b) == 'bar/baz'
'''
}
void testCompileStaticTwiceShouldNotBeAProblemUsingCustomizer() {
- assertScript '''import groovy.transform.CompileStatic
- @CompileStatic
- class Tool {
- @CompileStatic // annotated too, even if class is already annotated
- String relativePath(File relbase, File file) {
- def pathParts = []
- def currentFile = file
- while (currentFile != null && currentFile != relbase) {
- pathParts += currentFile.name
- currentFile = currentFile.parentFile
+ assertScript '''
+ @CompileStatic
+ class C {
+ @CompileStatic // annotated too, even if class is already annotated
+ String relativePath(File relbase, File file) {
+ def pathParts = []
+ def currentFile = file
+ while (currentFile != null && currentFile != relbase) {
+ pathParts += currentFile.name
+ currentFile = currentFile.parentFile
+ }
+ pathParts.reverse().join('/')
}
- pathParts.reverse().join('/')
}
- }
- File a = new File('foo')
- File b = new File(new File(a, 'bar'), 'baz')
- assert new Tool().relativePath(a,b) == 'bar/baz'
+ File a = new File('foo')
+ File b = new File(new File(a, 'bar'), 'baz')
+ assert new C().relativePath(a,b) == 'bar/baz'
'''
}
// GROOVY-5613
void testNullSafeAssignment() {
assertScript '''
- class A {
- int x = -1
- }
- A a = new A()
- @ASTTest(phase=INSTRUCTION_SELECTION, value={
- assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
- })
- def x = a?.x
+ class A {
+ int x = -1
+ }
+ A a = new A()
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
+ assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
+ })
+ def x = a?.x
'''
}
void testNullSafeAssignmentWithLong() {
assertScript '''
- class A {
- long x = -1
- }
- A a = new A()
- @ASTTest(phase=INSTRUCTION_SELECTION, value={
- assert node.getNodeMetaData(INFERRED_TYPE) == Long_TYPE
- })
- def x = a?.x
+ class A {
+ long x = -1
+ }
+ A a = new A()
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
+ assert node.getNodeMetaData(INFERRED_TYPE) == Long_TYPE
+ })
+ def x = a?.x
'''
}
void testNullSafeAssignmentWithChar() {
assertScript '''
- class A {
- char x = 'a'
- }
- A a = new A()
- @ASTTest(phase=INSTRUCTION_SELECTION, value={
- assert node.getNodeMetaData(INFERRED_TYPE) == Character_TYPE
- })
- def x = a?.x
- assert x == 'a'
+ class A {
+ char x = 'a'
+ }
+ A a = new A()
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
+ assert node.getNodeMetaData(INFERRED_TYPE) == Character_TYPE
+ })
+ def x = a?.x
+ assert x == 'a'
'''
}
void testCallStaticallyImportedMethodWithNullSafeArgument() {
- assertScript '''import static java.lang.Math.abs
- class A {
- int x = -1
- }
- def a = new A()
- def x = a?.x
- assert abs(a?.x) == 1
+ assertScript '''
+ import static java.lang.Math.abs
+
+ class A {
+ int x = -1
+ }
+ def a = new A()
+ def x = a?.x
+ assert abs(a?.x) == 1
'''
}
void testClosureAsInterfaceArgument() {
assertScript '''
- Closure c = { Integer x, Integer y -> x <=> y }
- def list = [ 3,1,5,2,4 ]
- assert ((Collection)list).sort(c) == [1,2,3,4,5]
- '''
+ Closure c = { Integer x, Integer y -> x <=> y }
+ def list = [ 3,1,5,2,4 ]
+ assert ((Collection)list).sort(c) == [1,2,3,4,5]
+ '''
}
void testInferredTypeForInteger() {
@@ -351,35 +359,35 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
void testPostfixIncInteger() {
assertScript '''
- Integer x = 0
- x++
- x++
- assert x == 2
- assert x++ == 2
- assert x == 3
- '''
+ Integer x = 0
+ x++
+ x++
+ assert x == 2
+ assert x++ == 2
+ assert x == 3
+ '''
}
void testPostfixDecInt() {
assertScript '''
- int x = 0
- x--
- x--
- assert x == -2
- assert x-- == -2
- assert x == -3
- '''
+ int x = 0
+ x--
+ x--
+ assert x == -2
+ assert x-- == -2
+ assert x == -3
+ '''
}
void testPostfixDecInteger() {
assertScript '''
- Integer x = 0
- x--
- x--
- assert x == -2
- assert x-- == -2
- assert x == -3
- '''
+ Integer x = 0
+ x--
+ x--
+ assert x == -2
+ assert x-- == -2
+ assert x == -3
+ '''
}
void testPrefixIncPrimitiveInteger() {
@@ -395,42 +403,41 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
void testPrefixIncInteger() {
assertScript '''
- Integer x = 0
- ++x
- ++x
- assert x == 2
- assert ++x == 3
- assert x == 3
- '''
+ Integer x = 0
+ ++x
+ ++x
+ assert x == 2
+ assert ++x == 3
+ assert x == 3
+ '''
}
void testPrefixDecInt() {
assertScript '''
- int x = 0
- --x
- --x
- assert --x == -3
- assert x == -3
- '''
+ int x = 0
+ --x
+ --x
+ assert --x == -3
+ assert x == -3
+ '''
}
void testPrefixDecInteger() {
assertScript '''
- Integer x = 0
- --x
- --x
- assert --x == -3
- assert x == -3
- '''
+ Integer x = 0
+ --x
+ --x
+ assert --x == -3
+ assert x == -3
+ '''
}
void testShouldSkipSpreadOperator() {
- new GroovyShell().evaluate '''import groovy.transform.TypeCheckingMode
- import groovy.transform.CompileStatic
-
+ removeAutoCS()
+ assertScript '''
@CompileStatic // top level must be @CS
class Foo {
- @CompileStatic(TypeCheckingMode.SKIP)
+ @CompileStatic(SKIP)
static void foo(fun, args) {
new Runnable() { // create an anonymous class which should *not* be visited
void run() {
@@ -445,16 +452,15 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5672
void testTypeCheckedPlusCompileStatic() {
- new GroovyShell().evaluate '''import groovy.transform.CompileStatic
- import groovy.transform.TypeChecked
-
- @TypeChecked
- @CompileStatic
- class SampleClass {
- def a = "some string"
- def b = a.toString()
- }
- new SampleClass()
+ removeAutoCS()
+ assertScript '''
+ @TypeChecked
+ @CompileStatic
+ class SampleClass {
+ def a = "some string"
+ def b = a.toString()
+ }
+ new SampleClass()
'''
}
@@ -479,79 +485,86 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
void testIncrementOperatorOnInt() {
assertScript '''
- int incInt(int n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt(5) == 7'''
+ int incInt(int n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt(5) == 7
+ '''
}
void testIncrementOperatorOnShort() {
assertScript '''
- short incInt(short n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt((short)5) == 7'''
+ short incInt(short n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt((short)5) == 7
+ '''
}
void testIncrementOperatorOnByte() {
assertScript '''
- byte incInt(byte n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt((byte)5) == 7'''
+ byte incInt(byte n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt((byte)5) == 7
+ '''
}
void testIncrementOperatorOnLong() {
assertScript '''
- long incInt(long n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt(5) == 7'''
+ long incInt(long n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt(5) == 7
+ '''
}
void testIncrementOperatorOnFloat() {
assertScript '''
- float incInt(float n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt(5) == 7'''
+ float incInt(float n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt(5) == 7
+ '''
}
void testIncrementOperatorOnDouble() {
assertScript '''
- double incInt(double n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt(5) == 7'''
+ double incInt(double n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt(5) == 7
+ '''
}
void testIncrementOperatorOnChar() {
assertScript '''
- char incInt(char n) {
- def result = n
- ++result
- result++
- return result
- }
- assert incInt((char)'a') == (char)('c')'''
+ char incInt(char n) {
+ def result = n
+ ++result
+ result++
+ return result
+ }
+ assert incInt((char)'a') == (char)('c')
+ '''
}
void testIncrementField() {
@@ -594,8 +607,6 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5814
void testCompileStaticImmutable() {
assertScript '''
- import groovy.transform.*
-
@Immutable
class Test {
int a
@@ -608,26 +619,27 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5738
void testAccessFieldFromGStringAfterInstanceOf() {
- new GroovyShell().evaluate '''
+ removeAutoCS()
+ assertScript '''
class Greeting { String who }
@groovy.transform.CompileStatic
class GreetingActor {
-
def receive = {
if(it instanceof Greeting) {
println "Hello ${it.who}"
}
}
-
}
+
new GreetingActor().receive(new Greeting(who:'cedric'))
'''
}
// GROOVY-5738
void testAccessMethodFromGStringAfterInstanceOf() {
- new GroovyShell().evaluate '''
+ removeAutoCS()
+ assertScript '''
class Greeting {
String who
String whoAmI() { who }
@@ -635,14 +647,13 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
@groovy.transform.CompileStatic
class GreetingActor {
-
def receive = {
- if(it instanceof Greeting) {
+ if (it instanceof Greeting) {
println "Hello ${it.whoAmI()}"
}
}
-
}
+
new GreetingActor().receive(new Greeting(who:'cedric'))
'''
}
@@ -650,16 +661,16 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
// GROOVY-5804
void testNegateSharedBooleanInClosure() {
assertScript '''
- boolean x = false
- def cl = {
- if (!x) {
- assert true
- } else {
- assert false
- }
+ boolean x = false
+ def cl = {
+ if (!x) {
+ assert true
+ } else {
+ assert false
}
- cl()
- '''
+ }
+ cl()
+ '''
}
void testCallClosureInInnerClass() {
@@ -711,13 +722,12 @@ final class BugsStaticCompileTest extends BugsSTCTest implements StaticCompilati
}
void testSuperMethodCallInSkippedSection() {
- assertScript '''import groovy.transform.CompileStatic
-import groovy.transform.TypeCheckingMode
+ assertScript '''
class Top {
public int foo() { 123 }
}
class Bottom extends Top {
- @CompileStatic(TypeCheckingMode.SKIP)
+ @CompileStatic(SKIP)
public int bar() {
foo()
}
@@ -732,7 +742,8 @@ import groovy.transform.TypeCheckingMode
def foo(Object o) {
o[0]
}
- ''', 'Cannot find matching method java.lang.Object#getAt(int)'
+ ''',
+ 'Cannot find matching method java.lang.Object#getAt(int)'
}
void testStaticCompileWithPattern() {
@@ -745,25 +756,26 @@ import groovy.transform.TypeCheckingMode
def value = fieldMatcher[0]
// should not pass
def str = value[0]
- ''', 'Cannot find matching method java.lang.Object#getAt(int)'
+ ''',
+ 'Cannot find matching method java.lang.Object#getAt(int)'
}
void testChainedNullSafePropertyOnMap() {
assertScript '''
- Map<String, Map<String,Map<String,Integer>>> m=[:]
- // this is ok
- assert m?.a == null
- assert m?.a?.b == null
- assert m?.a?.b?.c == null
- assert m?.a?.b?.c?.intValue() == null
+ Map<String, Map<String,Map<String,Integer>>> m=[:]
+ // this is ok
+ assert m?.a == null
+ assert m?.a?.b == null
+ assert m?.a?.b?.c == null
+ assert m?.a?.b?.c?.intValue() == null
'''
}
void testNullSafePropertyOnList() {
assertScript '''
- List<Class> classes = null
- // this is ok
- assert classes?.name == null
+ List<Class> classes = null
+ // this is ok
+ assert classes?.name == null
'''
}
@@ -784,21 +796,21 @@ import groovy.transform.TypeCheckingMode
// GROOVY-6101
void testShouldNotGenerateInvalidClassWithNullSafeInvocationOnMethodReturningPrimitiveType() {
assertScript '''
- class Piece {
- int x() { 333 }
- }
+ class Piece {
+ int x() { 333 }
+ }
- void foo() {
- Piece[] pieces = [new Piece(), null] as Piece[]
- int sum = 0
- for (int i=0;i<pieces.length;i++) {
- if (pieces[i]?.x()) {
- sum += pieces[i].x()
+ void foo() {
+ Piece[] pieces = [new Piece(), null] as Piece[]
+ int sum = 0
+ for (int i=0;i<pieces.length;i++) {
+ if (pieces[i]?.x()) {
+ sum += pieces[i].x()
+ }
}
+ assert sum == 333
}
- assert sum == 333
- }
- foo()
+ foo()
'''
}
@@ -841,7 +853,6 @@ import groovy.transform.TypeCheckingMode
def b = new Groovy5921()
b.doSomething()
-
'''
}
@@ -857,7 +868,6 @@ import groovy.transform.TypeCheckingMode
def b = new Groovy5921()
b.doSomething()
-
'''
}
@@ -873,7 +883,6 @@ import groovy.transform.TypeCheckingMode
def b = new Groovy5921()
b.doSomething()
-
'''
}
@@ -889,46 +898,35 @@ import groovy.transform.TypeCheckingMode
def b = new Groovy5921()
b.doSomething()
-
'''
}
// GROOVY-6113
void testCallObjectVargsMethodWithPrimitiveIntConstant() {
- try {
- assertScript '''
- int sum(Object... elems) {
- (Integer)elems.toList().sum()
- }
- int x = sum(Closure.DELEGATE_FIRST)
- assert x == Closure.DELEGATE_FIRST
- '''
- } finally {
-// println astTrees
- }
+ assertScript '''
+ int sum(Object... elems) {
+ (Integer)elems.toList().sum()
+ }
+ int x = sum(Closure.DELEGATE_FIRST)
+ assert x == Closure.DELEGATE_FIRST
+ '''
}
// GROOVY-6095
void testServletError() {
- def shell = new GroovyShell()
- shell.evaluate '''
+ assertScript '''
@Grab('javax.servlet:javax.servlet-api:3.0.1')
- import groovy.transform.CompileStatic
-
import javax.servlet.ServletContext
import javax.servlet.ServletRegistration
/**
* author: Richard Vowles - http://gplus.to/RichardVowles
*/
- @CompileStatic
class ServletExample {
-
public void myMethod(ServletContext ctx) {
ctx.getServletRegistrations().each { String name, ServletRegistration sr ->
println name
}
-
}
}
new ServletExample()
@@ -938,67 +936,67 @@ import groovy.transform.TypeCheckingMode
// GROOVY-6137
void testIsCaseShouldBeNullSafe() {
assertScript '''
- def isCaseNullCS(a, b) {
- a in b
- }
- assert isCaseNullCS(1, null) == false
- assert isCaseNullCS(null, 1) == false
- assert isCaseNullCS(1,1) == true
- assert isCaseNullCS(1,2) == false
- assert isCaseNullCS(2,1) == false
+ def isCaseNullCS(a, b) {
+ a in b
+ }
+ assert isCaseNullCS(1, null) == false
+ assert isCaseNullCS(null, 1) == false
+ assert isCaseNullCS(1,1) == true
+ assert isCaseNullCS(1,2) == false
+ assert isCaseNullCS(2,1) == false
'''
}
// GROOVY-6242
void testGetAtBigInt() {
assertScript '''
-class Sequence {
- public Sequence() {}
- static BigInteger getAt(final int index) { 1G }
- static BigInteger getAt(final BigInteger index) { getAt(index as int) }
-}
+ class Sequence {
+ public Sequence() {}
+ static BigInteger getAt(final int index) { 1G }
+ static BigInteger getAt(final BigInteger index) { getAt(index as int) }
+ }
-class Iterator implements java.util.Iterator {
- private BigInteger currentIndex = 0G
- private final Sequence sequence
- Iterator(final Sequence s) { sequence = s }
- boolean hasNext() { return true }
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
- def expr = node.code.statements[0].expression
- def indexType = expr.rightExpression.getNodeMetaData(INFERRED_TYPE)
- assert indexType == make(BigInteger)
- })
- BigInteger next() { sequence[currentIndex++] }
- void remove() { throw new UnsupportedOperationException() }
-}
+ class Iterator implements java.util.Iterator {
+ private BigInteger currentIndex = 0G
+ private final Sequence sequence
+ Iterator(final Sequence s) { sequence = s }
+ boolean hasNext() { return true }
+ @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ def expr = node.code.statements[0].expression
+ def indexType = expr.rightExpression.getNodeMetaData(INFERRED_TYPE)
+ assert indexType == make(BigInteger)
+ })
+ BigInteger next() { sequence[currentIndex++] }
+ void remove() { throw new UnsupportedOperationException() }
+ }
-def it = new Iterator(new Sequence())
-assert it.next() == 1G
-'''
+ def it = new Iterator(new Sequence())
+ assert it.next() == 1G
+ '''
}
// GROOVY-6243
void testSwapUsingMultipleAssignment() {
assertScript '''
- def swap(result,next) {
- print "($result,$next) -> "
- (result, next) = [next, result]
- println "($result,$next)"
- [result, next]
- }
+ def swap(result,next) {
+ print "($result,$next) -> "
+ (result, next) = [next, result]
+ println "($result,$next)"
+ [result, next]
+ }
- assert swap(0,1) == [1,0]
- assert swap('a','b') == ['b','a']
- assert swap('a', 1) == [1, 'a']
- def o1 = new Object()
- def o2 = new Date()
- assert swap(o1,o2) == [o2, o1]
+ assert swap(0,1) == [1,0]
+ assert swap('a','b') == ['b','a']
+ assert swap('a', 1) == [1, 'a']
+ def o1 = new Object()
+ def o2 = new Date()
+ assert swap(o1,o2) == [o2, o1]
'''
}
// GROOVY-5882
void testMod() {
- assertScript """
+ assertScript '''
int foo(Map<Integer, Object> markers, int i) {
int res = 0
for (e in markers.entrySet()) {
@@ -1007,9 +1005,8 @@ assert it.next() == 1G
return res
}
assert foo([(1):null,(2):null,(3):null],2)==2
- """
-
- assertScript """
+ '''
+ assertScript '''
int foo(Map<Integer, Object> markers, int i) {
int res = 0
for (e in markers.entrySet()) {
@@ -1019,8 +1016,8 @@ assert it.next() == 1G
return res
}
assert foo([(1):null,(2):null,(3):null],2)==2
- """
- assertScript """
+ '''
+ assertScript '''
int foo(Map<Integer, Object> markers, int i) {
int res = 0
for (e in markers.entrySet()) {
@@ -1029,7 +1026,7 @@ assert it.next() == 1G
return res
}
assert foo([(1):null,(2):null,(3):null],2)==2
- """
+ '''
}
void testSuperCallShouldBeDirect() {
@@ -1068,7 +1065,7 @@ assert it.next() == 1G
println seq.next?.longValue()
}
assert seq.next == 5
-'''
+ '''
}
void testNullSafeOperatorShouldNotCallMethodTwiceWithPrimitive() {
@@ -1088,7 +1085,7 @@ assert it.next() == 1G
println seq.next?.longValue()
}
assert seq.next == 5
-'''
+ '''
}
void testNullSafeOperatorShouldNotCallMethodTwice1Arg() {
@@ -1108,7 +1105,7 @@ assert it.next() == 1G
println seq.getNext(2)?.longValue()
}
assert seq.getNext(2) == 10
-'''
+ '''
}
void testNullSafeOperatorShouldNotCallMethodTwiceWithPrimitive1Arg() {
@@ -1128,7 +1125,7 @@ assert it.next() == 1G
println seq.getNext(2)?.longValue()
}
assert seq.getNext(2) == 10
-'''
+ '''
}
void testShouldAllowSubscriptOperatorOnSet() {
@@ -1160,159 +1157,161 @@ assert it.next() == 1G
// GROOVY-6552
void testShouldNotThrowClassCastException() {
- assertScript '''import java.util.concurrent.Callable
+ assertScript '''
+ import java.util.concurrent.Callable
- String text(Class clazz) {
- new Callable<String>() {
- String call() throws Exception {
+ String text(Class clazz) {
new Callable<String>() {
String call() throws Exception {
- clazz.getName()
+ new Callable<String>() {
+ String call() throws Exception {
+ clazz.getName()
+ }
+ }.call()
}
}.call()
}
- }.call()
- }
- assert text(String) == 'java.lang.String'
- '''
+ assert text(String) == 'java.lang.String'
+ '''
}
// GROOVY-6851
void testShouldNotThrowNPEIfElvisOperatorIsUsedInDefaultArgumentValue() {
- assertScript '''import org.codehaus.groovy.ast.expr.MethodCallExpression
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.MethodCallExpression
-class GrailsHomeWorkspaceReader {
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
- def defaultValue = node.parameters[0].initialExpression
- assert defaultValue instanceof MethodCallExpression
- def target = defaultValue.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
- assert target != null
- })
- GrailsHomeWorkspaceReader(String grailsHome = System.getProperty('grails.home')) {
- }
-}
-new GrailsHomeWorkspaceReader()
-'''
+ class GrailsHomeWorkspaceReader {
+ @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ def defaultValue = node.parameters[0].initialExpression
+ assert defaultValue instanceof MethodCallExpression
+ def target = defaultValue.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
+ assert target != null
+ })
+ GrailsHomeWorkspaceReader(String grailsHome = System.getProperty('grails.home')) {
+ }
+ }
+ new GrailsHomeWorkspaceReader()
+ '''
assertScript '''
-class GrailsHomeWorkspaceReader {
- GrailsHomeWorkspaceReader(String grailsHome = System.getProperty('grails.home') ?: System.getenv('GRAILS_HOME')) {
- }
-}
-new GrailsHomeWorkspaceReader()
-'''
+ class GrailsHomeWorkspaceReader {
+ GrailsHomeWorkspaceReader(String grailsHome = System.getProperty('grails.home') ?: System.getenv('GRAILS_HOME')) {
+ }
+ }
+ new GrailsHomeWorkspaceReader()
+ '''
}
// GROOVY-6342
void testShouldNotThrowNPEIfElvisOperatorIsUsedInsideTernary() {
- assertScript '''class Inner {
- int somestuff
-}
-Inner inner = null
-int someInt = inner?.somestuff ?: 0
-println someInt
-
-'''
+ assertScript '''
+ class Inner {
+ int somestuff
+ }
+ Inner inner = null
+ int someInt = inner?.somestuff ?: 0
+ println someInt
+ '''
}
void testAccessOuterClassMethodFromInnerClassConstructor() {
assertScript '''
- class Parent {
- String str
- Parent(String s) { str = s }
- }
- class Outer {
- String a
+ class Parent {
+ String str
+ Parent(String s) { str = s }
+ }
+ class Outer {
+ String a
- private class Inner extends Parent {
- Inner() { super(getA()) }
- }
+ private class Inner extends Parent {
+ Inner() { super(getA()) }
+ }
- String test() { new Inner().str }
- }
- def o = new Outer(a:'ok')
- assert o.test() == 'ok'
- '''
+ String test() { new Inner().str }
+ }
+ def o = new Outer(a:'ok')
+ assert o.test() == 'ok'
+ '''
}
void testAccessOuterClassMethodFromInnerClassConstructorUsingExplicitOuterThis() {
assertScript '''
- class Parent {
- String str
- Parent(String s) { str = s }
- }
- class Outer {
- String a
+ class Parent {
+ String str
+ Parent(String s) { str = s }
+ }
+ class Outer {
+ String a
- private class Inner extends Parent {
- Inner() { super(Outer.this.getA()) }
- }
+ private class Inner extends Parent {
+ Inner() { super(Outer.this.getA()) }
+ }
- String test() { new Inner().str }
- }
- def o = new Outer(a:'ok')
- assert o.test() == 'ok'
- '''
+ String test() { new Inner().str }
+ }
+ def o = new Outer(a:'ok')
+ assert o.test() == 'ok'
+ '''
}
void testAccessOuterClassMethodFromInnerClassConstructorUsingExplicitOuterThisAndProperty() {
assertScript '''
- class Parent {
- String str
- Parent(String s) { str = s }
- }
- class Outer {
- String a
+ class Parent {
+ String str
+ Parent(String s) { str = s }
+ }
+ class Outer {
+ String a
- private class Inner extends Parent {
- Inner() { super(Outer.this.a) }
- }
+ private class Inner extends Parent {
+ Inner() { super(Outer.this.a) }
+ }
- String test() { new Inner().str }
- }
- def o = new Outer(a:'ok')
- assert o.test() == 'ok'
- '''
+ String test() { new Inner().str }
+ }
+ def o = new Outer(a:'ok')
+ assert o.test() == 'ok'
+ '''
}
void testAccessOuterClassStaticMethodFromInnerClassConstructor() {
assertScript '''
- class Parent {
- String str
- Parent(String s) { str = s }
- }
- class Outer {
- static String a
+ class Parent {
+ String str
+ Parent(String s) { str = s }
+ }
+ class Outer {
+ static String a
- private class Inner extends Parent {
- Inner() { super(getA()) }
- }
+ private class Inner extends Parent {
+ Inner() { super(getA()) }
+ }
- String test() { new Inner().str }
- }
- def o = new Outer()
- Outer.a = 'ok'
- assert o.test() == 'ok'
- '''
+ String test() { new Inner().str }
+ }
+ def o = new Outer()
+ Outer.a = 'ok'
+ assert o.test() == 'ok'
+ '''
}
void testStaticMethodFromInnerClassConstructor() {
assertScript '''
- class Parent {
- String str
- Parent(String s) { str = s }
- }
- class Outer {
- private class Inner extends Parent {
- static String a = 'ok'
- Inner() { super(getA()) }
- }
+ class Parent {
+ String str
+ Parent(String s) { str = s }
+ }
+ class Outer {
+ private class Inner extends Parent {
+ static String a = 'ok'
+ Inner() { super(getA()) }
+ }
- String test() { new Inner().str }
- }
- def o = new Outer()
- assert o.test() == 'ok'
- '''
+ String test() { new Inner().str }
+ }
+ def o = new Outer()
+ assert o.test() == 'ok'
+ '''
}
// GROOVY-6876
@@ -1326,8 +1325,7 @@ println someInt
}
}
assert new Foo().method() == -1L
- '''
-
+ '''
assertScript '''
class Foo {
long bar
@@ -1338,7 +1336,6 @@ println someInt
}
assert new Foo().method(-1L) == -1L
'''
-
assertScript '''
class Foo {
long rankOrderingOrId
@@ -1368,20 +1365,22 @@ println someInt
// GROOVY-6924
void testShouldNotThrowIncompatibleClassChangeError() {
try {
- assertScript '''import org.codehaus.groovy.classgen.asm.sc.Groovy6924Support
- class Test {
- static void foo() {
- Groovy6924Support bean = new Groovy6924Support()
- bean.with {
- foo = 'foo'
- bar = 'bar'
+ assertScript '''
+ import org.codehaus.groovy.classgen.asm.sc.Groovy6924Support
+
+ class Test {
+ static void foo() {
+ Groovy6924Support bean = new Groovy6924Support()
+ bean.with {
+ foo = 'foo'
+ bar = 'bar'
+ }
+ String val = "$bean.foo and $bean.bar"
+ assert val == 'foo and bar'
}
- String val = "$bean.foo and $bean.bar"
- assert val == 'foo and bar'
}
- }
- Test.foo()
- '''
+ Test.foo()
+ '''
} finally {
assert astTrees['Test$_foo_closure1'][1].contains('INVOKEVIRTUAL org/codehaus/groovy/classgen/asm/sc/Groovy6924Support.setFoo (Ljava/lang/String;)V')
}
@@ -1468,36 +1467,35 @@ println someInt
// GROOVY-7784
void testWithSamAndVarArgs() {
assertScript '''
- class Foo {
+ class C {
static foo(Integer x, Iterable y, String... z) { [*y.toList(), *z].join('-') }
}
- class Groovy7784 {
- static emptyVarArgs() { Foo.foo(42, { ['foo', 'bar'].iterator() }) }
- static singleVarArgs() { Foo.foo(42, { ['foo', 'bar'].iterator() }, 'baz') }
- static multiVarArgs() { Foo.foo(42, { ['foo', 'bar'].iterator() }, 'baz1', 'baz2') }
+ class D {
+ static emptyVarArgs() { C.foo(42, { ['foo', 'bar'].iterator() }) }
+ static singleVarArgs() { C.foo(42, { ['foo', 'bar'].iterator() }, 'baz') }
+ static multiVarArgs() { C.foo(42, { ['foo', 'bar'].iterator() }, 'baz1', 'baz2') }
}
- assert Groovy7784.emptyVarArgs() == 'foo-bar'
- assert Groovy7784.singleVarArgs() == 'foo-bar-baz'
- assert Groovy7784.multiVarArgs() == 'foo-bar-baz1-baz2'
+ assert D.emptyVarArgs() == 'foo-bar'
+ assert D.singleVarArgs() == 'foo-bar-baz'
+ assert D.multiVarArgs() == 'foo-bar-baz1-baz2'
'''
}
// GROOVY-7160
void testGenericsArrayPlaceholder() {
assertScript '''
- import static java.nio.file.AccessMode.*
+ import static java.lang.Thread.State.*
- @groovy.transform.CompileStatic
- class Dummy {
- static main() {
- // more than 5 to match `of(E first, E[] rest)` variant
- EnumSet.of(READ, WRITE, EXECUTE, READ, WRITE, EXECUTE)
+ class C {
+ static m() {
+ // more than 5 to match `of(E first, E... rest)` variant
+ EnumSet.of(NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED)
}
}
- assert Dummy.main() == [READ, WRITE, EXECUTE].toSet()
+ assert C.m() == [NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED].toSet()
'''
}
}