home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (ClosureMethodTest.groovy)

This example Groovy source code file (ClosureMethodTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

closuremethodtest, collection, dumping, dumping, expected, expected, found, found, groovytestcase, i, i, inspecting, iterator

The Groovy ClosureMethodTest.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

/** 
 * Tests the various Closure methods in Groovy
 * 
 * @author <a href="mailto:james@coredevelopers.net">James Strachan
 * @version $Revision: 20756 $
 */
class ClosureMethodTest extends GroovyTestCase {

    void testListCollect() {
        def list = [1, 2, 3, 4]
        def answer = list.collect{item -> return item * 2 }
        assert answer.size() == 4
        def expected = [2, 4, 6, 8]
        assert answer == expected
    }

    void testMapCollect() {
        def map = [1:2, 2:4, 3:6, 4:8]
        def answer = map.collect{e-> return e.key + e.value }
        // lest sort the results since maps are in hash code order
        answer = answer.sort()
        assert answer.size() == 4
        assert answer == [3, 6, 9, 12]
        assert answer.get(0) == 3
        assert answer.get(1) == 6
        assert answer.get(2) == 9
        assert answer.get(3) == 12
    }

    void testObjectFindResult() {
        def oneToTenObjectIterator = {
            def i = 1
            [ hasNext: { i <= 10 }, next: { i++ } ] as Iterator
        }

        assert "Found 3" == oneToTenObjectIterator().findResult {
            if (it > 2) return "Found $it"
        }

        assert false == oneToTenObjectIterator().findResult {
            if (it > 2) return false
        }

        assert null == oneToTenObjectIterator().findResult {
            if (false) return "won't get here, should return null"
        }

        assert "Found 3" == oneToTenObjectIterator().findResult("default") {
            if (it > 2) return "Found $it"
        }

        assert "default" == oneToTenObjectIterator().findResult("default") {
            if (false) return "won't get here, should return null"
        }
    }
    
    void testListFind() {
        def list = ["a", "b", "c"]
        def answer = list.find{item-> item == "b" }
        assert answer == "b"
        answer = list.find{item-> item == "z" }
        assert answer == null
    }

    void testListFindResult() {
        Collection<Integer> oneThroughFive = [1, 2, 3, 4, 5]
        
        assert "I found 4" == oneThroughFive.findResult { 
            if (it > 3) return "I found $it"
        }

        assert null == oneThroughFive.findResult { 
            if (it > 8) return "I found $it"
        }

        assert null == [].findResult {
            if (it) return "I found $it"
        }

        assert false == oneThroughFive.findResult {
            if (it > 3) return false  // a result of false is a valid result
        }       

        assert "I found 3" == oneThroughFive.findResult("default") { 
            if (it > 2) return "I found $it"
        }

        assert "default" == oneThroughFive.findResult("default") { 
            if (it > 8) return "I found $it"
        }
    }

    void testMapFind() {
        def map = [1:2, 2:4, 3:6, 4:8]
        def answer = map.find{entry -> entry.value == 6 }
        assert answer != null
        assert answer.key == 3
        assert answer.value == 6
        answer = map.find{entry -> entry.value == 0 }
        assert answer == null
        answer = map.find{ k, v -> v > 5 }
        assert answer instanceof Map.Entry
        assert answer.key == 3
        assert answer.value == 6

        answer = map.find{ k, v -> k == 2 }
        assert answer instanceof Map.Entry
        assert answer.key == 2
        assert answer.value == 4
    }

    void testMapFindResult() {
        def oneThroughFourMap = [a: 1, b: 2, c: 3, d: 4] 

        assert "I found c:3" == oneThroughFourMap.findResult { entry ->
            if (entry.value > 2) return "I found ${entry.key}:${entry.value}"
        }

        assert "I found c:3" == oneThroughFourMap.findResult { key, value ->
            if (value > 2) return "I found ${key}:${value}"
        }

        assert false == oneThroughFourMap.findResult { 
            if (it.value > 2) return false // a result of false is a valid result
        }

        assert "I found c:3" == oneThroughFourMap.findResult("default") { key, value ->
            if (value > 2) return "I found ${key}:${value}"
        }

        assert "default" == oneThroughFourMap.findResult("default") { key, value ->
            if (value > 10) return "I found ${key}:${value}"
        }
    }

    void testListFindAll() {
        def list = [20, 5, 40, 2]
        def answer = list.findAll{item -> item < 10 }
        assert answer.size() == 2
        assert answer == [5, 2]
    }

    void testMapFindAll() {
        def map = [1:2, 2:4, 3:6, 4:8]
        def answer = map.findAll{ entry -> entry.value > 5 }
        assert answer.size() == 2
        def keys = answer.collect { entry -> entry.key }
        def values = answer.collect { entry -> entry.value }
        // maps are in hash order so lets sort the results
        keys.sort()
        values.sort()
        assert keys == [3, 4], "Expected [3, 4] but was $keys"
        assert values == [6, 8], "Expected [6, 8] but was $values"
    }

    void testMapEach() {
        def count = 0
        def map = [1:2, 2:4, 3:6, 4:8]
        map.each{e-> count = count + e.value }
        assert count == 20
        map.each{e-> count = count + e.value + e.key }
        assert count == 50
    }

    void testMapEachWith2Params() {
        def count = 0
        def map = [1:2, 2:4, 3:6, 4:8]
        map.each {key, value -> count = count + value }
        assert count == 20
        map.each {key, value -> count = count + value + key }
        assert count == 50
    }

    void testListEach() {
        def count = 0
        def list = [1, 2, 3, 4]
        list.each({item-> count = count + item })
        assert count == 10
        list.each{item-> count = count + item }
        assert count == 20
    }

    void testListEvery() {
        assert [1, 2, 3, 4].every {i-> return i < 5 }
        assert [1, 2, 7, 4].every {i-> i < 5 } == false
        assert [a:1, b:2, c:3].every {k,v -> k < 'd' && v < 4 }
        assert ![a:1, b:2, c:3].every {k,v -> k < 'd' && v < 3 }
    }

    void testListAny() {
        assert [1, 2, 3, 4].any {i-> return i < 5 }
        assert [1, 2, 3, 4].any {i-> i > 3 }
        assert [1, 2, 3, 4].any {i-> i > 5 } == false
        assert [a:1, b:2, c:3].any { k,v -> k == 'c' }
        def isThereAFourValue = [a:1, b:2, c:3].any{ k,v -> v == 4 }
        assert !isThereAFourValue
    }

    void testJoin() {
        def value = [1, 2, 3].join('-')
        assert value == "1-2-3"
    }

    void testListReverse() {
        def value = [1, 2, 3, 4].reverse()
        assert value == [4, 3, 2, 1]
    }

    void testListInject() {
        def value = [1, 2, 3].inject('counting: ') { str, item -> str + item }
        assert value == "counting: 123"
        value = [1, 2, 3].inject(0) { c, item -> c + item }
        assert value == 6
        value = ([1, 2, 3, 4] as Object[]).inject(0) { c, item -> c + item }
        assert value == 10
    }

    void testObjectInject() {
        def value = [1:1, 2:2, 3:3].inject('counting: ') { str, item -> str + item.value }
        assert value == "counting: 123"
        value = [1:1, 2:2, 3:3].inject(0) { c, item -> c + item.value }
        assert value == 6
    }

    void testIteratorInject() {
        def value = [1:1, 2:2, 3:3].iterator().inject('counting: ') { str, item -> str + item.value }
        assert value == "counting: 123"
        value = [1:1, 2:2, 3:3].iterator().inject(0) { c, item -> c + item.value }
        assert value == 6
    }

    void testDump() {
        def text = dump()
        println("Dumping object ${text}")
        assert text != null && text.startsWith("<")
    }

    void testInspect() {
        def text = [1, 2, 'three'].inspect()
        println("Inspecting ${text}")
        assert text == '[1, 2, "three"]'
    }

    void testTokenize() {
        def text = "hello-there-how-are-you"
        def answer = []
        for (i in text.tokenize('-')) {
            answer.add(i)
        }
        assert answer == ['hello', 'there', 'how', 'are', 'you']
    }
    
    void testUpto() {
        def answer = []
        1.upto(5) { answer.add(it) }
        assert answer == [1, 2, 3, 4, 5]
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureMethodTest.groovy source code file:

new blog posts

 

Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.