alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (CustomBooleanCoercionTest.groovy)

This example Groovy source code file (CustomBooleanCoercionTest.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

boolcategory, boolcategory, custombooleancoerciontest, groovy, groovy, groovytestcase, guillaume, laforge, object, object, predicate, predicate, truth

The Groovy CustomBooleanCoercionTest.groovy source code

package org.codehaus.groovy.runtime

/**
 * Test the customization of the Groovy truth, aka. boolean coercion.
 * It is possible to customize how instances of a type are coerced into boolean
 * by implementing a method with the following signature:
 * <code>boolean asBoolean()
 * <p>
 * The test also checks it's possible to override the behaviour of pre-existing boolean coercion.
 *
 * @author Guillaume Laforge
 */
class CustomBooleanCoercionTest extends GroovyTestCase {

    void testPredefinedGroovyTruth() {
        // null is false
        def nullVar = null
        assert !nullVar

        // non-null object is true
        def obj = new Object()
        assert obj

        // empty strings are false, and >1 length strings are true
        assert !""
        assert "  \t \n "
        assert "Groovy Truth"

        // characters are true unless if it's the zeroth one
        char c0 = '\0'
        char c1 = 'a'
        assert !c0
        assert c1

        // empty lists are true, and list with 1+ elements are true
        assert ![]
        assert [1, 2, 3]
        assert 1..10

        // empty maps are false, and non-empty maps are true
        assert ![:]
        assert [firstname: 'Guillaume', lastname: 'Laforge']

        // number equal to 0 is false, and true otherwise
        assert !0
        assert 1
        assert !0.0
        assert 1.1

        // an iterator is true if there are other elements to iterate over, false otherwise
        assert ![].iterator()
        assert [1, 2, 3].iterator()

        assert Boolean.TRUE
        assert !Boolean.FALSE

        // empty arrays are false, non-empty are true
        assert !([] as Object[])
        assert [1, 2, 3] as int[]
    }

    /**
     * A Predicate instance should coerce to the same boolean as the one in the value property of the instance
     */
    void testCustomAsBooleanMethod() {
        assert new Predicate(value: true)
        assert !new Predicate(value: false)
    }

    void testOverrideAsBooleanMethodWithACategory() {
        use (BoolCategory) {
            assert !new Predicate(value: true)
            assert new Predicate(value: false)
        }
    }

    void testOverrideAsBooleanMethodWithEMC() {
        try {
            Predicate.metaClass.asBoolean = { -> true }
            assert new Predicate(value: true)
            assert new Predicate(value: false)
        } finally {
            Predicate.metaClass = null
        }
    }

    void testOverideStringAsBooleanThroughEMC() {
        try {
            String.metaClass.asBoolean = { -> true }

            assert ""
            assert " \t \n "
            assert "ok"
            assert "true"
            assert "false"
            assert "Groovy rocks!"
        } finally {
            String.metaClass = null
        }
    }
}

/** A Predicate classe coercible to a boolea expression */
class Predicate {
    boolean value
    boolean asBoolean() { value }
}

/** A Boolean Category which coerces the boolean value to its opposite */
class BoolCategory {
    static boolean asBoolean(Predicate self) {
        !self.value
    }
}

Other Groovy examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.