This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push: new 283c93ac4b GROOVY-5744, GROOVY-10666: add test cases 283c93ac4b is described below commit 283c93ac4bd773565adb78a5934236730d9876b9 Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Fri Jul 29 09:49:46 2022 -0500 GROOVY-5744, GROOVY-10666: add test cases --- .../MultipleAssignmentDeclarationTest.groovy | 258 +++++++++++++-------- .../gls/statements/MultipleAssignmentTest.groovy | 98 +++++--- 2 files changed, 227 insertions(+), 129 deletions(-) diff --git a/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy b/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy index c253b89c51..8ae6744b3e 100644 --- a/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy +++ b/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy @@ -18,97 +18,169 @@ */ package gls.statements -import gls.CompilableTestSupport - -class MultipleAssignmentDeclarationTest extends CompilableTestSupport { - - void testDef() { - assertScript """ - def (a,b) = [1,2] - assert a==1 - assert b==2 - """ - } - - void testDefWithoutLiteral() { - def list = [1, 2] - def (c, d) = list - assert c==1 - assert d==2 - } - - void testMixedTypes() { - assertScript """ - def x = "foo" - def (int i, String j) = [1,x] - assert x == "foo" - assert i == 1 - assert i instanceof Integer - assert j == "foo" - assert j instanceof String - """ - } - - void testMixedTypesWithConversion() { - assertScript ''' - def x = "foo" - def (int i, String j) = [1,"$x $x"] - assert x == "foo" - assert i == 1 - assert i instanceof Integer - assert j == "foo foo" - assert j instanceof String - ''' - } - - void testDeclarationOrder() { - assertScript """ - try { - def (i,j) = [1,i] - assert false - } catch (MissingPropertyException mpe) { - assert true - } - """ - } - - void testNestedScope() { - assertScript """ - def c = { - def (i,j) = [1,2] - assert i==1 - assert j==2 - } - c() - - try { - println i - assert false - } catch (MissingPropertyException mpe) { - assert true - } - - try { - println j - assert false - } catch (MissingPropertyException mpe) { - assert true - } - - def (i,j) = [2,3] - assert i==2 - assert j==3 - c() - - assert i==2 - assert j==3 - """ - } - - void testChainedMultiAssignmentDecl() { - def a, b - def (c, d) = (a, b) = [1, 2] - assert [a, b] == [1, 2] - assert [c, d] == [1, 2] - } +import groovy.test.NotYetImplemented +import org.junit.Test + +import static groovy.test.GroovyAssert.assertScript +import static groovy.test.GroovyAssert.shouldFail + +final class MultipleAssignmentDeclarationTest { + + @Test + void testDef1() { + assertScript ''' + def (a,b) = [1,2] + assert a == 1 + assert b == 2 + ''' + } + + @Test + void testDef2() { + assertScript ''' + def list = [1,2] + def (c,d) = list + assert c == 1 + assert d == 2 + ''' + } + + @Test + void testMixedTypes() { + assertScript ''' + def x = "foo" + def (int i, String j) = [1,x] + assert x == "foo" + assert i == 1 + assert i instanceof Integer + assert j == "foo" + assert j instanceof String + ''' + } + + @Test + void testMixedTypesWithConversion() { + assertScript ''' + def x = "foo" + def (int i, String j) = [1,"$x $x"] + assert x == "foo" + assert i == 1 + assert i instanceof Integer + assert j == "foo foo" + assert j instanceof String + ''' + } + + @Test + void testDeclarationOrder() { + shouldFail MissingPropertyException, ''' + def (i,j) = [1,i] + ''' + } + + @Test + void testNestedScope() { + assertScript '''import static groovy.test.GroovyAssert.shouldFail + + def c = { + def (i,j) = [1,2] + assert i == 1 + assert j == 2 + } + c() + + shouldFail(MissingPropertyException) { + println i + } + + shouldFail(MissingPropertyException) { + println j + } + + def (i,j) = [2,3] + assert i == 2 + assert j == 3 + c() + + assert i == 2 + assert j == 3 + ''' + } + + @Test + void testMultiAssignChain() { + assertScript ''' + def a,b + def (c,d) = (a,b) = [1,2] + assert [a,b] == [1,2] + assert [c,d] == [1,2] + ''' + } + + @NotYetImplemented @Test // GROOVY-5744 + void testMultiAssignFromIterator() { + assertScript ''' + def list = [1,2,3] + def iter = list.iterator() + + def (a,b,c) = list + def (d,e,f) = iter + assert "$a $b $c" == "$d $e $f" + ''' + } + + @NotYetImplemented @Test // GROOVY-10666 + void testMultiAssignFromIterable() { + assertScript ''' + class MySet { + List<String> ops = [] + @Delegate Set<String> strings = [] + + String getAt(int index) { + ops << "getAt($index)".toString() + org.codehaus.groovy.runtime.DefaultGroovyMethods.getAt(this, index) + } + + Iterator<String> iterator() { + ops << "iterator()" + def iterator = strings.iterator() + return new Iterator<String>() { + @Override + boolean hasNext() { + iterator.hasNext() + } + @Override + String next() { + ops << "next()" + iterator.next() + } + } + } + } + + Set<String> strings = new MySet() + strings << 'foo' + strings << 'bar' + strings << 'baz' + + def (foo,bar,baz) = strings + assert foo == 'foo' + assert bar == 'bar' + assert baz == 'baz' + + assert strings.ops == ['iterator()','next()','next()','next()'] + ''' + } + + @NotYetImplemented @Test // GROOVY-10666 + void testMultiAssignFromJavaStream() { + assertScript '''import java.util.stream.Stream + + Stream<String> strings = Stream.of('foo','bar','baz') + def (foo,bar,baz) = strings + assert foo == 'foo' + assert bar == 'bar' + assert baz == 'baz' + ''' + } } diff --git a/src/test/gls/statements/MultipleAssignmentTest.groovy b/src/test/gls/statements/MultipleAssignmentTest.groovy index 537967ffc1..71e58743c4 100644 --- a/src/test/gls/statements/MultipleAssignmentTest.groovy +++ b/src/test/gls/statements/MultipleAssignmentTest.groovy @@ -18,65 +18,91 @@ */ package gls.statements -import gls.CompilableTestSupport +import org.junit.Test -class MultipleAssignmentTest extends CompilableTestSupport { +import static groovy.test.GroovyAssert.assertScript +final class MultipleAssignmentTest { + + @Test void testList() { - def list = [1, 2] - def a, b + assertScript ''' + def list = [1, 2] + def a, b - (a, b) = list - assert a == 1 - assert b == 2 + (a, b) = list + assert a == 1 + assert b == 2 - (a, b) = [3, 4] - assert a == 3 - assert b == 4 + (a, b) = [3, 4] + assert a == 3 + assert b == 4 + ''' } + @Test void testArray() { - def array = [1, 2] as int[] - def a, b + assertScript ''' + def array = [1, 2] as int[] + def a, b - (a, b) = array - assert a == 1 - assert b == 2 + (a, b) = array + assert a == 1 + assert b == 2 + ''' } - def foo() {[1, 2]} - + @Test void testMethod() { - def a, b + assertScript ''' + def foo() {[1, 2]} - (a, b) = foo() - assert a == 1 - assert b == 2 + def a, b + + (a, b) = foo() + assert a == 1 + assert b == 2 + ''' } + @Test void testMethodOverflow() { - def a, b = 3 + assertScript ''' + def foo() {[1, 2]} + + def a, b = 3 - (a) = foo() - assert a == 1 - assert b == 3 + (a) = foo() + assert a == 1 + assert b == 3 + ''' } + @Test void testMethodUnderflow() { - def a, b, c = 4 + assertScript ''' + def foo() {[1, 2]} - (a, b, c) = foo() - assert a == 1 - assert b == 2 - assert c == null + def a, b, c = 4 + + (a, b, c) = foo() + assert a == 1 + assert b == 2 + assert c == null + ''' } + @Test void testChainedMultiAssignment() { - def a, b, c, d - (c, d) = (a, b) = [1, 2] - assert [a, b] == [1, 2] - assert [c, d] == [1, 2] - (c, d) = a = (a, b) = [3, 4] - assert [c, d] == [3, 4] + assertScript ''' + def a, b, c, d + + (c, d) = (a, b) = [1, 2] + assert [a, b] == [1, 2] + assert [c, d] == [1, 2] + + (c, d) = a = (a, b) = [3, 4] + assert [c, d] == [3, 4] + ''' } } \ No newline at end of file