简体   繁体   中英

How to make a List of Strings in Groovy

I'm a bit puzzled by what's happening here. I'm trying to make a test which tests that addLDocument() is passed a List of String s where there are 10 String s in the List . There are multiple calls to addLDocument ... they should usually have 10 String s in the List passed as the parameter.

Clearly the test should currently be failing (it is, but in the wrong way), as I am currently only passing one String at a time. The aim is to change the code to make the test pass by packaging the succeeding String s up into batches of 10.

for( String textLine : allTextLines ) {
            List<String> textLines = []
            textLines.add( textLine )
            addLDocument( textLines )
}

def addLDocument( List<String> textLines ) {
        ...
}

In my test I have this:

    given:
    List stringVal

    ...

    then:
    1 * spyCH.addLDocument( _ ) >> { 
        arguments -> stringVal = arguments[ 0 ] 

    }
    println "stringVal $stringVal size ${stringVal.size()}"
    stringVal.size() == 10

    then:
    ( 1 .. _ ) * spyCH.addLDocument( _ ) >> null

... unfortunately I find, whatever I do, that stringVal.size() is counting the total number of characters in whatever String s are contained in textLines .

Why might that be? How can I force textLines to contain a set of differentiated String s, and for size() to give the number of such elements in the List ?

later

Oh dear, puzzling inconsistency now: after adding the lines suggested by daggett I then tried "rolling back": what did I have to do to get back to the "wrong" character-count, instead of element-count. Replacing List<String> xxx.... by List xxx... and then by def xxx... is now giving size() == 1 . [Weeps]

请在这里查看我的答案https://stackoverflow.com/a/46801418/2145769您遇到了同样的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM