代码之家  ›  专栏  ›  技术社区  ›  Serhii

Groovy阵列。[[],[]之间的区别。toArray()和[[],[]]*。toArray()

  •  2
  • Serhii  · 技术社区  · 7 年前

    我已经为中的参数化单元测试准备了数据 groovy

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

    它工作正常。我检查了这段代码,并尝试简化替换 ]*.toArray() 具有 ].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)
    

    我把找零的钱还给了他,但我不明白为什么 ].toArray() ].toArray() ].toArray() ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   aristotll    7 年前

    从…起 doc 这个 *.

    扩展运算符( *. )用于对聚合对象的所有项调用操作。这相当于对每个项目调用操作并将结果收集到列表中:

    * , toArray


    具有 您当前的代码执行以下操作:

      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
        }