|
Groovy example source code file (WithMethodTest.groovy)
The Groovy WithMethodTest.groovy source code/* * 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' } } Other Groovy examples (source code examples)Here is a short list of links related to this Groovy WithMethodTest.groovy source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.