Spock Web Console

subscribe to the feed Subscribe
Flow (via #spockwebconsole)
tweet this script Tweet

Flow

Published 2 months ago by Eric
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
package intro

import spock.lang.*

@Narrative("""
Now that we can store data and compare it, we can start making decisions on what to do in our code
""")
class FlowSpec extends Specification {
    def "The `if` Statement"() {
        given: "a true condition and a variable set to 0"
        def shouldWeSetIt = true
        def bar = 0

        when:
        if(shouldWeSetIt == true) {
            bar = 1
        }

        then:"What will bar be?"
        bar == null
    }

    def "if, else"() {
        given:
        def shouldWeSetIt = true
        def bar = 0

        when:"We can have multiple branches in our decision making"
        if(shouldWeSetIt) {
            bar = 1
        } else {
            bar = 2
        }

        then:
        bar == null
    }

    def "if, else if, else"() {
        given:
        def foo = "Eric"
        def bar = 0

        when: "Check to see the name we have and set bar"
        if(foo == "Bob") {
            bar = 1
        } else if(foo == "Sue") {
            bar = 2
        } else {
            bar = 3
        }

        then:
        bar == null
    }

    @See("If you have multiple conditions you can use a switch statement.")
    def "switch"() {
        given:
        def foo = "Eric"
        def bar = 0

        when: "The same as above, just a shorter way to write it."
        switch(foo) {
            case "Bob":
                bar = 1
            case "Sue":
                bar = 2
            default:
                bar = 3
        }

        then:
        bar == null
    }

    def "A bit more complex "() {
        given:
        def foo = "Eric"
        def bar = 0
        def student = "Barry"

        when: "Check to see the name we have and set bar"
        if(foo == "Eric" || bar < 1) {
            if(student == "Barry") {
                bar = 1
            }
        } else if(foo == "Eric" && bar == 0) {
            bar = 2
        } else {
            bar = 3
        }

        then:
        bar == null
    }
}