Created
January 18, 2013 08:09
-
-
Save anonymous/4563080 to your computer and use it in GitHub Desktop.
Composing spock specs with mixins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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