Sergii Sergii - 2 years ago 219
Groovy Question

Groovy array. Which difference between [[],[]].toArray() and [[],[]]*.toArray()

I have prepared data for parametrized unit test in

groovy
:

@Parameterized.Parameters
static List data() {
[
['20101113', 'DATE',
'20101114', 'DATE',
'some statement',
'20101101T000000/20110101T000000',
null,
['some', 'expected', 'data', 'results']]
// ...and other
]*.toArray()
}


it works correctly. I checked this code, and tried simplify replacing
]*.toArray()
with
].toArray()
.

java.lang.Exception: FirstParametrizedTest.data() must return a Collection of arrays.

at org.junit.runners.Parameterized$TestClassRunnerForParameters.computeParams(Parameterized.java:94)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:86)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


I reverted my change back and it works, but I can't understand why instruction
].toArray()
was unexpected? How does
].toArray()
work and which difference between
].toArray()
and
]*.toArray()
?

Answer Source

From doc, The *. is called spread operator and returns a list.

The Spread Operator (*.) is used to invoke an action on all items of an aggregate object. It is equivalent to calling the action on each item and collecting the result into a list:

Without *, toArray just return an object array, of course array cannot be cast to List at runtime.


With * your current code does something like this:

  static List data() {
        def result = []
        List input = [
                ['20101113', 'DATE',
                 '20101114', 'DATE',
                 'some statement',
                 '20101101T000000/20110101T000000',
                 null,
                 ['some', 'expected', 'data', 'results']]
                // ...and other
        ]

        for (def item : input) {
            result.add(item != null ? ((List) item).toArray() : null)
        }

        result
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download