Chapter 7
[ 175 ]
}
}
given: "A class with an Instance method"
def clazz = new Clazz()
when: "In a class instance method 'this' is the "
def methodThis = clazz.method()
then:
methodThis == clazz
when: "we try to access delegate from a method"
def methodDelegate = clazz.methodDelegate()
then:
thrown(MissingPropertyException)
methodDelegate == null
when: "we try to access owner from a method"
def methodOwner = clazz.methodOwner()
then:
thrown(MissingPropertyException)
methodOwner == null
when: "we get this, delegate and owner for a closure"
def (closureThis, closureDelegate, closureOwner) = clazz.closure()
then: "this, delegate and owner are the class instance"
closureThis == clazz
closureDelegate == clazz
closureOwner == clazz
when: "get this, delegate and owner for a closure in method"
def (closureInMethodThis, closureInMethodDelegate,
closureInMethodOwner) = clazz.closureWithinMethod()
then: """this, delegate and owner are the class instance not the
enclosing closure"""
closureInMethodThis == clazz
closureInMethodDelegate == clazz
closureInMethodOwner == clazz
closureInMethodThis != clazz.closure
closureInMethodDelegate != clazz.closure
closureInMethodOwner != clazz.closure