Created
June 1, 2011 08:30
-
-
Save EwanDawson/1001984 to your computer and use it in GitHub Desktop.
Groovy closure delegation test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
class ClassInstanceAccessFromWithinClosureSpec extends Specification { | |
def component = new ClassUnderTest() | |
def "class instance variable is accessible when called from within closure delegated to Object" (){ | |
setup: def delegate = new Object() | |
expect: component.execute(delegate) | |
} | |
def "class instance variable is accessible when called from within closure delegated to String" (){ | |
setup: def delegate = 'delegate' | |
expect: component.execute(delegate) | |
} | |
def "class instance variable is accessible when called from within closure delegated to Closure" (){ | |
setup: def delegate = {} | |
expect: component.execute(delegate) | |
} | |
def "class instance variable is accessible when called from within closure delegated to Integer" (){ | |
setup: def delegate = 1 | |
expect: component.execute(delegate) | |
} | |
def "class instance variable is accessible when called from within closure delegated to Map" (){ | |
setup: def delegate = new HashMap() | |
expect: component.execute(delegate) | |
// Throws NPE | |
} | |
def "class instance variable is accessible when called from within closure delegated to List" (){ | |
setup: def delegate = new ArrayList() | |
expect: component.execute(delegate) | |
// Throws groovy.lang.MissingMethodException | |
} | |
def "class instance variable is accessible when called from within closure delegated to Collection" (){ | |
setup: def delegate = Mock(Collection) | |
expect: component.execute(delegate) | |
// Throws NPE | |
} | |
} | |
class ClassUnderTest { | |
def speaker = [speak:{println "Hello"}] | |
def execute(delegate) { | |
speaker.speak() | |
delegate.with { | |
speaker.speak() | |
} | |
true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment