-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.5.6 -- withDelegate added to Closure
- Loading branch information
Tim Yates
committed
Nov 25, 2013
1 parent
278b3c4
commit f692cbc
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
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
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
56 changes: 56 additions & 0 deletions
56
src/main/groovy/com/bloidonia/groovy/extensions/ClosureExtensionMethods.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2012-2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.bloidonia.groovy.extensions | ||
|
||
/** | ||
* @author Tim Yates | ||
*/ | ||
class ClosureExtensionMethods { | ||
|
||
/** | ||
* Set a Closure's delegate | ||
* <pre class="groovyTestCase"> | ||
* def testClosure = { num -> | ||
* num + val | ||
* } | ||
* | ||
* def value = testClosure.withDelegate( [ val: 4 ] )( 3 ) | ||
* assert value == 7 | ||
* </pre> | ||
* | ||
* @param self the Closure | ||
* @param delegate the object to delegate to | ||
* @return the closure but with its delegate set | ||
*/ | ||
static <T> Closure<T> withDelegate( Closure<T> self, Object delegate ) { | ||
self.delegate = delegate | ||
self | ||
} | ||
|
||
/** | ||
* Set a Closure's delegate and resolveStrategy | ||
* @param self the Closure | ||
* @param delegate the object to delegate to | ||
* @param strategy the resolveStrategy to use | ||
* @return the closure but with its delegate and strategy set | ||
*/ | ||
static <T> Closure<T> withDelegate( Closure<T> self, Object delegate, int strategy ) { | ||
self.delegate = delegate | ||
self.resolveStrategy = strategy | ||
self | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2013 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package tests | ||
|
||
import spock.lang.Specification | ||
|
||
public class ClosureTests extends Specification { | ||
|
||
def val = 99 | ||
|
||
def testClosure = { num -> | ||
num + val + val2 | ||
} | ||
|
||
def 'quick test'() { | ||
given: | ||
def val = testClosure.withDelegate( [ val: 4, val2: 8 ] )( 3 ) | ||
expect: | ||
// Should use val from the class, but val2 from the delegate | ||
val == 110 | ||
} | ||
|
||
def 'strategy test'() { | ||
given: | ||
def val = testClosure.withDelegate( [ val: 4, val2: 8 ], Closure.DELEGATE_FIRST )( 3 ) | ||
expect: | ||
// val and val2 from the delegate | ||
val == 15 | ||
} | ||
} |