Testing map.each while map is empty, MultipleCompilationErrorsException?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Testing map.each while map is empty, MultipleCompilationErrorsException?

BlackTie
This post has NOT been accepted by the mailing list yet.
Using the following test, I'm encountering a MultipleCompilationErrorsException.

  def "using each on empty map"() {
    expect:
    [:].each { entry ->
        def v = 'abc'
        assert entry.value == v
    }
  }

This is resulting in the following exception:
   org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
   Spec expression: 1: expecting '}', found 'assert' @ line 1, column 35.
      [:].each { entry -> def v = 'abc' assert entry.value == v }
                                                   ^

Adding a semicolon after the "def v = 'abc'" statement will stop the MultipleCompilationErrorsException, and instead, verification will fail:

Condition not satisfied:
   
   [:].each { entry -> def v = 'abc'; assert entry.value == v }
       |
       [:]

I'm not quite understanding why, on an empty map, the verification is failing.  Do empty maps get automatically represented as false?  I can wrap the map evaluation with an if statement to check the map's size first, but that seems extra verbose to me.  In my real test case, the assert statements in the each closure should only be evaluated if the map has data in it.

Is there a more terse way to achieve this, rather than using an if statement to check the size of the map first before using an each closure on the map?
Loading...