Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Composing spock specs with mixins
import grails.plugin.spock.UnitSpec;
import org.codehaus.groovy.runtime.ArrayUtil;
import org.codehaus.groovy.runtime.callsite.CallSite;
import org.spockframework.runtime.model.FeatureMetadata;
import org.spockframework.runtime.model.FieldMetadata;
import org.spockframework.runtime.model.SpecMetadata;
@SpecMetadata(filename="PrincipalSpec.groovy")
public class PrincipalSpec extends UnitSpec
{
@FieldMetadata(name="principalField", ordinal=0, line=14)
private Object principalField;
public PrincipalSpec()
{
CallSite[] arrayOfCallSite = $getCallSiteArray();
}
public void My principal test()
{
CallSite[] arrayOfCallSite = $getCallSiteArray();
arrayOfCallSite[0].call($get$$class$org$spockframework$runtime$SpockRuntime());
return;
}
public Object My secondary test()
{
CallSite[] arrayOfCallSite = $getCallSiteArray(); return arrayOfCallSite[1].callCurrent(this);
}
@FeatureMetadata(name="My principal test", ordinal=0, parameterNames={}, blocks={@org.spockframework.runtime.model.BlockMetadata(kind=org.spockframework.runtime.model.BlockKind.SETUP, texts={}), @org.spockframework.runtime.model.BlockMetadata(kind=org.spockframework.runtime.model.BlockKind.EXPECT, texts={})})
public void $spock_feature_1_0()
{
CallSite[] arrayOfCallSite = $getCallSiteArray(); Object $spock_valueRecorder = arrayOfCallSite[2].callConstructor($get$$class$org$spockframework$runtime$ValueRecorder()); arrayOfCallSite[3].callCurrent(this, "I am the only one");
arrayOfCallSite[4].callCurrent(this, arrayOfCallSite[5].callCurrent(this));
arrayOfCallSite[6].call($get$$class$org$spockframework$runtime$SpockRuntime(), ArrayUtil.createArray(arrayOfCallSite[7].call($spock_valueRecorder), "true", Integer.valueOf(18), Integer.valueOf(11), null, arrayOfCallSite[8].call($spock_valueRecorder, $const$1, Boolean.TRUE))); arrayOfCallSite[9].call(arrayOfCallSite[10].callGroovyObjectGetProperty(this)); return;
}
public Object getPrincipalField()
{
return this.principalField;
}
public void setPrincipalField(Object paramObject)
{
this.principalField = paramObject;
}
}
import grails.plugin.spock.UnitSpec;
@Mixin(ToMixWithPrincipalSpec)
class PrincipalSpec extends UnitSpec {
def principalField="principalField"
def "My principal test"() {
given: println "I am the only one"
and: println secondaryMethod()
expect: true
}
}
/*
grails testApp unit: PrincipalSpec
....
Tests passed: 1
Tests failed: 0
--Output from My principal test--
I am the only one
Hi from helper method of ToMix:principalField
I guess the spock ast trans dont see the feature method of mixed spec (see reverse PrincipalSpec.class)
or i am missing something basic thing
Too much magic at same time ;-)
It seems a spock extension would be neccesary to make it possible/user friendly
*/
import grails.plugin.spock.UnitSpec;
class ToMixWithPrincipalSpec extends UnitSpec{
def "My mixed test" () {
given: println "hey, i am too"
expect: true
}
def secondaryMethod() {
"Hi from helper method of ToMix:"+principalField
}
}
@mgryszko
Copy link

mgryszko commented Jan 18, 2013

The only way to reuse code with Spock specification is to extract Test Helpers and use them in the spec:

def 'spec with shared code' {
   given: givenHelper()

   when: whenHelper()

   then: thenHelper()
}

Groovy mixins delegate from the mixee to the mixin at runtime (by modifying the metaclass). I suppose you could mix in a feature method with an AST transformation (at compile time).

@jneira
Copy link

jneira commented Jan 18, 2013

Mmm it can work and it's usable but requires some boilerplate code (and imo composition is no clean)
Yep, the spock ast transformation is done at compile time too and you could use grails.util.Mixin in same phase but... can you controlle the order of transformations or will you need another AST transform (a spock extension? a new one?) to compose them in correct order? I've never did an ast trans and i dont know its details
Anyway thanks for remind me the @mixin feature, i already have used it in my domain classes to add common filters to compose criteria as in http://javierneirasanchez.blogspot.com.es/2011/09/abracadabra-componiendo-criterios-de.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment