blob: ca0e78298f6e798fb5f6735ad7f6d3ab52fb9e83 [file] [log] [blame]
/*
* Copyright 2003-2008 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 groovy.lang
/**
* Tests the .with method
*
*/
class WithMethodTest extends GroovyTestCase {
void testDelegateGetsFirstOpportunity() {
def sb = new StringBuffer()
sb.with {
// this should call append() on the
// delegate not, the owner
append 'some text'
}
assertEquals 'delegate had wrong value', 'some text', sb.toString()
}
void testOwnerGetsOpportunityIfDelegateCannotRespond() {
def sb = new StringBuffer()
def returnValue
sb.with {
// this should call ownerMethod() on the owner
returnValue = ownerMethod()
}
assertEquals 'owner should have responded to method call',
42,
returnValue
}
void testCallingNonExistentMethod() {
def sb = new StringBuffer()
shouldFail(MissingMethodException) {
sb.with {
someNoneExistentMethod()
}
}
}
void testClosureWithResolveStrategyExplicitlySet() {
def closure = {
append 'some text'
}
closure.resolveStrategy = Closure.OWNER_ONLY
def sb = new StringBuffer()
// .with should use DELEGATE_FIRST, even though
// the closure has another strategy set
sb.with closure
assertEquals 'delegate had wrong value', 'some text', sb.toString()
}
def ownerMethod() {
42
}
void append(String s) {
fail 'this should never have been called'
}
}