Spock Web Console

subscribe to the feed Subscribe
sisg - 06 Spock Basics: Parameterized Tests - Lists and Tables (via #spockwebconsole)
tweet this script Tweet

sisg - 06 Spock Basics: Parameterized Tests - Lists and Tables

Published 5 months ago by Matthias Schulz with tags sisg Spock Basics Parameter
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
package parameter

import spock.lang.Specification
import spock.lang.Unroll

// This tag lists each parameterized test separately:
@Unroll
class ParameterizedDifferingSpec extends Specification{

    static int[] globalValues = [1, 2, 3, 4, 5]
    static int[] globalResults = [1, 4, 9, 16, 25]
    static int[][] globalData = [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36]]


    def "Local simple Lists: #value * #value = #result"() {
        expect:
        result == value * value

        where:
        value  << [1, 2, 3]
        result << [1, 4, 9]
    }


    def "Local nested List: #data{0} * #data{0} = #data{1}"() {
        expect:
        data[1] == data[0] * data[0]

        where:
        data  << [[1, 1], [2, 4], [3, 9], [4, 16]]
    }


    def "Global simple Lists: #value * #value = #result"() {
        expect:
        result == value * value

        where:
        value  << globalValues
        result << globalResults
    }


    def "Global nested List: #data{0} * #data{0} = #data{1}"() {
        expect:
        data[1] == data[0] * data[0]

        where:
        data << globalData
    }


    def "Local Table - #testname: #a * #b = #result"() {
        expect:
        result == a  * b

        where:
        testname            | a  | b   | result
        "positive numbers"  |  1 |   1 |      1
        "negative numbers"  | -1 |  -1 |      1
        "mixed numbers"     |  1 |  -1 |     -1
        "positive numbers"  |  2 |   2 |      4
        "negative numbers"  | -2 |  -2 |      4
        "mixed numbers"     |  2 |  -2 |     -4
    }
}