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

Groovy example source code file (MultipleAssignmentDeclarationTest.groovy)

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

compilabletestsupport, integer, integer, missingpropertyexception, missingpropertyexception, multipleassignmentdeclarationtest, multipleassignmentdeclarationtest, string, string

The Groovy MultipleAssignmentDeclarationTest.groovy source code

package gls.statements

import gls.CompilableTestSupport

class MultipleAssignmentDeclarationTest extends CompilableTestSupport {

  void testDef() {
    assertScript """
      def (a,b) = [1,2]
      assert a==1
      assert b==2
    """
  }
  
  void testDefWithoutLiteral() {
    def list = [1, 2]
    def (c, d) = list
    assert c==1
    assert d==2
  }
  
  void testMixedTypes() {
    assertScript """
      def x = "foo"
      def (int i, String j) = [1,x]
      assert x == "foo"
      assert i == 1
      assert i instanceof Integer
      assert j == "foo"
      assert j instanceof String
    """
  }
  
  void testMixedTypesWithConversion() {
    assertScript '''
      def x = "foo"
      def (int i, String j) = [1,"$x $x"]
      assert x == "foo"
      assert i == 1
      assert i instanceof Integer
      assert j == "foo foo"
      assert j instanceof String
    '''
  }
  
  void testDeclarationOrder() {
    assertScript """
      try {
        def (i,j) = [1,i]
        assert false
      } catch (MissingPropertyException mpe) {
        assert true
      }
    """
  }
  
  void testNestedScope() {
    assertScript """
       def c = {
         def (i,j) = [1,2]
         assert i==1
         assert j==2
       }
       c()
       
       try {
         println i
         assert false
       } catch (MissingPropertyException mpe) {
         assert true
       }
       
       try {
         println j
         assert false
       } catch (MissingPropertyException mpe) {
         assert true
       }
              
       def (i,j) = [2,3]
       assert i==2
       assert j==3
       c()
       
       assert i==2
       assert j==3
    """   
  }
}

Other Groovy examples (source code examples)

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