Quantcast

.with closure inside a then: block does not execute

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

.with closure inside a then: block does not execute

Howard Lewis Ship
I've been writing some tests like this:

  def "analysis of a simple exception"() {
    when:
    def ea = analyzer.analyze(t)

    then:

    ea.exceptionInfos.size() == 1

    def ei = ea.exceptionInfos[0]

    ei.className == RuntimeException.name
    ei.message == message

    ei.propertyNames.empty == true
    ei.stackTrace.empty == false

    where:

    message = "Hey! We've Got No Tomatoes"
    t = new RuntimeException(message)
  }

But I thought I could rewrite it as:

  def "analysis of a simple exception"() {
    when:
    def ea = analyzer.analyze(t)

    then:

    ea.exceptionInfos.size() == 1

    ea.exceptionInfos[0].with {
      className == RuntimeException.name
     message == message

     propertyNames.empty == true
     stackTrace.empty == false
    }

    where:

    message = "Hey! We've Got No Tomatoes"
    t = new RuntimeException(message)
  }

However, I found that I had to directly add an assert to each line
inside the with {} closure.

Is there a magic way that Spock could automatically convert
expressions to inserts in such a block?  Alternately, should there be
a method on Specification to provide similar behavior, i.e.

  check ea.exceptionInfos[0], {
    className == RuntimeException.name
    ...
  }

--
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: .with closure inside a then: block does not execute

Howard Lewis Ship
I spoke too soon; looks like Spock really doesn't like what I'm trying to do:


  def "analysis of a simple exception"() {
    when:
    def ea = analyzer.analyze(t)

    then:

    ea.exceptionInfos.size() == 1

    ea.exceptionInfos[0].with {
      assert className == RuntimeException.name
      assert message == message

      assert propertyNames.empty
      assert !stackTrace.empty
    }

    where:

    message = "Hey! We've Got No Tomatoes"
    t = new RuntimeException(message)
  }


Results in a runtime error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Spec expression: 1: unexpected token: assert @ line 1, column 71.
   Name == RuntimeException.name assert mes
                                 ^
1 error
        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:106)
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:148)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:141)
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:107)
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
        at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

On Tue, May 1, 2012 at 1:20 PM, Howard Lewis Ship <[hidden email]> wrote:

> I've been writing some tests like this:
>
>  def "analysis of a simple exception"() {
>    when:
>    def ea = analyzer.analyze(t)
>
>    then:
>
>    ea.exceptionInfos.size() == 1
>
>    def ei = ea.exceptionInfos[0]
>
>    ei.className == RuntimeException.name
>    ei.message == message
>
>    ei.propertyNames.empty == true
>    ei.stackTrace.empty == false
>
>    where:
>
>    message = "Hey! We've Got No Tomatoes"
>    t = new RuntimeException(message)
>  }
>
> But I thought I could rewrite it as:
>
>  def "analysis of a simple exception"() {
>    when:
>    def ea = analyzer.analyze(t)
>
>    then:
>
>    ea.exceptionInfos.size() == 1
>
>    ea.exceptionInfos[0].with {
>      className == RuntimeException.name
>     message == message
>
>     propertyNames.empty == true
>     stackTrace.empty == false
>    }
>
>    where:
>
>    message = "Hey! We've Got No Tomatoes"
>    t = new RuntimeException(message)
>  }
>
> However, I found that I had to directly add an assert to each line
> inside the with {} closure.
>
> Is there a magic way that Spock could automatically convert
> expressions to inserts in such a block?  Alternately, should there be
> a method on Specification to provide similar behavior, i.e.
>
>  check ea.exceptionInfos[0], {
>    className == RuntimeException.name
>    ...
>  }
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210
> http://howardlewisship.com



--
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: .with closure inside a then: block does not execute

Peter Niederwieser
Administrator
You are running into a known limitation, namely that multi-line conditions don't always work. Also, `with` will return `null` in this case, failing the (outer) condition.

Support for `with` in conditions (without explicit assert) has been requested before. I think that giving a new meaning to Groovy's `with` is too invasive, but I'm thinking about adding `Specification.with` in 0.7.

Cheers,
Peter

On May 1, 2012, at 10:27 PM, Howard Lewis Ship wrote:

> I spoke too soon; looks like Spock really doesn't like what I'm trying to do:
>
>
>  def "analysis of a simple exception"() {
>    when:
>    def ea = analyzer.analyze(t)
>
>    then:
>
>    ea.exceptionInfos.size() == 1
>
>    ea.exceptionInfos[0].with {
>      assert className == RuntimeException.name
>      assert message == message
>
>      assert propertyNames.empty
>      assert !stackTrace.empty
>    }
>
>    where:
>
>    message = "Hey! We've Got No Tomatoes"
>    t = new RuntimeException(message)
>  }
>
>
> Results in a runtime error:
>
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Spec expression: 1: unexpected token: assert @ line 1, column 71.
>   Name == RuntimeException.name assert mes
>                                 ^
> 1 error
> at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
> at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:106)
> at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:148)
> at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119)
> at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131)
> at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359)
> at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:141)
> at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:107)
> at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
> at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
> at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
> at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:176)
> at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
>
> On Tue, May 1, 2012 at 1:20 PM, Howard Lewis Ship <[hidden email]> wrote:
>> I've been writing some tests like this:
>>
>>  def "analysis of a simple exception"() {
>>    when:
>>    def ea = analyzer.analyze(t)
>>
>>    then:
>>
>>    ea.exceptionInfos.size() == 1
>>
>>    def ei = ea.exceptionInfos[0]
>>
>>    ei.className == RuntimeException.name
>>    ei.message == message
>>
>>    ei.propertyNames.empty == true
>>    ei.stackTrace.empty == false
>>
>>    where:
>>
>>    message = "Hey! We've Got No Tomatoes"
>>    t = new RuntimeException(message)
>>  }
>>
>> But I thought I could rewrite it as:
>>
>>  def "analysis of a simple exception"() {
>>    when:
>>    def ea = analyzer.analyze(t)
>>
>>    then:
>>
>>    ea.exceptionInfos.size() == 1
>>
>>    ea.exceptionInfos[0].with {
>>      className == RuntimeException.name
>>     message == message
>>
>>     propertyNames.empty == true
>>     stackTrace.empty == false
>>    }
>>
>>    where:
>>
>>    message = "Hey! We've Got No Tomatoes"
>>    t = new RuntimeException(message)
>>  }
>>
>> However, I found that I had to directly add an assert to each line
>> inside the with {} closure.
>>
>> Is there a magic way that Spock could automatically convert
>> expressions to inserts in such a block?  Alternately, should there be
>> a method on Specification to provide similar behavior, i.e.
>>
>>  check ea.exceptionInfos[0], {
>>    className == RuntimeException.name
>>    ...
>>  }
>>
>> --
>> Howard M. Lewis Ship
>>
>> Creator of Apache Tapestry
>>
>> The source for Tapestry training, mentoring and support. Contact me to
>> learn how I can get you up and productive in Tapestry fast!
>>
>> (971) 678-5210
>> http://howardlewisship.com
>
>
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210
> http://howardlewisship.com
>
> --
> You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: .with closure inside a then: block does not execute

Peter Niederwieser
Administrator
In reply to this post by Howard Lewis Ship
By the way, the following should work but isn't pretty:

...

then:
ea.exceptionInfos[0].with {
  assert className == RuntimeException.name;
  assert message == message;

  assert propertyNames.empty;
  assert !stackTrace.empty;
  true
}

Spock should really throw a more explanatory exception when it hits a problem parsing a multi-line condition. I'll fix this for 0.7.

Cheers,
Peter

On May 1, 2012, at 10:27 PM, Howard Lewis Ship wrote:

> I spoke too soon; looks like Spock really doesn't like what I'm trying to do:
>
>
>  def "analysis of a simple exception"() {
>    when:
>    def ea = analyzer.analyze(t)
>
>    then:
>
>    ea.exceptionInfos.size() == 1
>
>    ea.exceptionInfos[0].with {
>      assert className == RuntimeException.name
>      assert message == message
>
>      assert propertyNames.empty
>      assert !stackTrace.empty
>    }
>
>    where:
>
>    message = "Hey! We've Got No Tomatoes"
>    t = new RuntimeException(message)
>  }
>
>
> Results in a runtime error:
>
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Spec expression: 1: unexpected token: assert @ line 1, column 71.
>   Name == RuntimeException.name assert mes
>                                 ^

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.

Loading...