Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 9

[ 243 ]

The following code is a simple Spock specification that tests some of the features of
the java.util.Stack class:


class ChapterNineSpec extends SpockScriptSpecification {
def "initial size is zero"() {
given:
def stack = new Stack()
expect: stack.size() == 0
}

def "pop from empty stack throws exception"() {
given:
def stack = new Stack()
when: stack.pop()
then: thrown(EmptyStackException)
}

def "peek from empty stack throws exception"() {
given:
def stack = new Stack()
when: stack.peek()
then: thrown(EmptyStackException)
}

def "after push size is one and we can peek"() {
given:
def stack = new Stack()

when: stack.push("elem")

then:
stack.size() == 1
stack.peek() == "elem"
}
}
Free download pdf