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

Groovy example source code file (AsBoolBug.groovy)

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

a, asboolbug, asboolbug, boolean, boolean, false, false, groovytestcase, it, it

The Groovy AsBoolBug.groovy source code

package groovy.bugs

/**
 * Test to fix the Jira issues GROOVY-810 and GROOVY-811.
 * Test of "string as Boolean" against the issue GROOVY-812.
 *
 * @author Pilho Kim
 * @version $Revision: 2207 $
 */

public class AsBoolBug extends GroovyTestCase {

    void testMapAsBool() {
        def a = ["A":123]
        println ("$a : ${a as Boolean}")
        assert a as Boolean == true
        a = [:]
        println ("$a : ${a as Boolean}")
        assert a as Boolean == false
    }

    void testListAsBool() {
        def b = [123]
        println ("$b : ${b as Boolean}")
        assert b as Boolean == true
        b = []
        println ("$b : ${b as Boolean}")
        assert b as Boolean == false
    }

    /**
     * void testStringAsBool().
     *
     * <code>string as Boolean is equivalent to
     *     <code>string != null && string.length() > 0.
     */
    // Unfortunately, it contradicts several other test cases, and
    // it has already been decided to handle string-to-boolean conversions
    // differently. Commented out temporarily on 10 May 2005.
    // This is a test case against GROOVY-812
    void testStringAsBool() {
        def c = "false"
        println ("$c : ${c as Boolean}")
        assert c as Boolean == true
        assert c as Boolean == (c != null && c.length() > 0)
        boolean z = c
        println ("$z")
        assert z == true
        if (c)
           println "It is true!!"
        else
           println "It is false!!"

        c = "123"
        println ("$c : ${c as Boolean}")
        assert c as Boolean == true
        assert c as Boolean == (c != null && c.length() > 0)

        c = "False"
        println ("$c : ${c as Boolean}")
        assert c as Boolean == true
        assert c as Boolean == (c != null && c.length() > 0)
        if (c)
           println "It is true!!"
        else
           println "It is false!!"
        z = c
        println ("$z")
        assert z
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy AsBoolBug.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.