-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Javier Salinas <jsalinaspolo@gmail.com>
- Loading branch information
1 parent
fa3be0c
commit 6856c6e
Showing
16 changed files
with
223 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ class Given extends Stage<Given> { | |
def some_state_$(String param) { | ||
self() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ class Then extends Stage<Then> { | |
assert true | ||
self() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ class When extends Stage<When> { | |
When some_action() { | ||
self() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
apply plugin: 'groovy' | ||
|
||
description = "Module for writing JGiven tests with Spock 2" | ||
|
||
ext { | ||
spock2_version = '2.0-groovy-3.0' | ||
groovy_version = '3.0.9' | ||
} | ||
|
||
dependencies { | ||
api project(':jgiven-junit') | ||
|
||
implementation "org.codehaus.groovy:groovy:$groovy_version" | ||
implementation "org.spockframework:spock-core:$spock2_version" | ||
implementation "org.spockframework:spock-junit4:$spock2_version" | ||
|
||
testImplementation project(':jgiven-html5-report') | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
finalizedBy(jgivenHtml5Report) | ||
} |
58 changes: 58 additions & 0 deletions
58
jgiven-spock2/src/main/groovy/com/tngtech/jgiven/spock/ScenarioSpec.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.tngtech.jgiven.spock | ||
|
||
import com.google.common.reflect.TypeToken | ||
import com.tngtech.jgiven.annotation.DoNotIntercept | ||
import com.tngtech.jgiven.impl.Scenario | ||
import com.tngtech.jgiven.junit.JGivenClassRule | ||
import com.tngtech.jgiven.spock.junit.JGivenSpockMethodRule | ||
import net.bytebuddy.ByteBuddy | ||
import net.bytebuddy.description.annotation.AnnotationDescription | ||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy | ||
import net.bytebuddy.implementation.SuperMethodCall | ||
import net.bytebuddy.matcher.ElementMatchers | ||
import org.junit.ClassRule | ||
import org.junit.Rule | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class ScenarioSpec<GIVEN, WHEN, THEN> extends Specification { | ||
@ClassRule @Shared JGivenClassRule writerRule = new JGivenClassRule() | ||
@Rule JGivenSpockMethodRule scenarioRule = new JGivenSpockMethodRule(createScenario()) | ||
|
||
GIVEN given() { | ||
getScenario().given() | ||
} | ||
|
||
WHEN when() { | ||
getScenario().when() | ||
} | ||
|
||
THEN then() { | ||
getScenario().then() | ||
} | ||
|
||
Scenario<GIVEN, WHEN, THEN> getScenario() { | ||
(Scenario<GIVEN, WHEN, THEN>) scenarioRule.getScenario() | ||
} | ||
|
||
Scenario<GIVEN, WHEN, THEN> createScenario() { | ||
Class<GIVEN> givenClass = addDoNotInterceptToMetaClass(new TypeToken<GIVEN>(getClass()) {}.getRawType()) | ||
Class<WHEN> whenClass = addDoNotInterceptToMetaClass(new TypeToken<WHEN>(getClass()) {}.getRawType()) | ||
Class<THEN> thenClass = addDoNotInterceptToMetaClass(new TypeToken<THEN>(getClass()) {}.getRawType()) | ||
|
||
new Scenario<GIVEN, WHEN, THEN>(givenClass, whenClass, thenClass) | ||
} | ||
|
||
private <T> Class<T> addDoNotInterceptToMetaClass(Class<T> clazz) { | ||
AnnotationDescription doNotIntercept = AnnotationDescription.Builder.ofType(DoNotIntercept.class).build() | ||
def clazzA = clazz.metaClass.getTheClass() | ||
new ByteBuddy() | ||
.subclass(clazzA) | ||
.method(ElementMatchers.named("getMetaClass")) | ||
.intercept(SuperMethodCall.INSTANCE) | ||
.annotateMethod(doNotIntercept) | ||
.make() | ||
.load(clazzA.getClassLoader(), ClassLoadingStrategy.Default.INJECTION) | ||
.getLoaded() as Class<T> | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
jgiven-spock2/src/main/groovy/com/tngtech/jgiven/spock/junit/JGivenSpockMethodRule.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.tngtech.jgiven.spock.junit; | ||
|
||
import com.tngtech.jgiven.impl.ScenarioBase; | ||
import com.tngtech.jgiven.junit.JGivenMethodRule; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
import spock.lang.Specification; | ||
|
||
/** | ||
* JUnit Rule to enable JGiven with Spock2 | ||
* | ||
* @since 1.2.0 | ||
*/ | ||
public class JGivenSpockMethodRule extends JGivenMethodRule { | ||
|
||
/** | ||
* @since 1.2.0 | ||
*/ | ||
public JGivenSpockMethodRule(ScenarioBase scenario) { | ||
super(scenario); | ||
} | ||
|
||
@Override | ||
protected void starting(Statement base, FrameworkMethod testMethod, Object target) { | ||
super.starting(base, testMethod, target); | ||
scenario.getScenarioModel().setDescription(methodNameFromSpec(target)); | ||
} | ||
|
||
private String methodNameFromSpec(Object target) { | ||
return ((Specification) target).getSpecificationContext().getCurrentIteration().getParent().getDisplayName(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
jgiven-spock2/src/test/groovy/com/tngtech/jgiven/spock/ShinySpockJGivenShould.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.tngtech.jgiven.spock | ||
|
||
import com.tngtech.jgiven.annotation.ExtendedDescription | ||
import com.tngtech.jgiven.spock.stages.Given | ||
import com.tngtech.jgiven.spock.stages.Then | ||
import com.tngtech.jgiven.spock.stages.When | ||
import spock.lang.Unroll | ||
|
||
class ShinySpockJGivenShould extends ScenarioSpec<Given, When, Then> { | ||
|
||
def "something should happen"() { | ||
expect: | ||
|
||
given().some_state() | ||
when().some_action() | ||
then().some_outcome() | ||
|
||
assert scenario.scenarioModel.description == "something should happen" | ||
} | ||
|
||
def "be some able to use params"() { | ||
expect: | ||
|
||
given().some_state_$("param") | ||
when().some_action() | ||
then().some_outcome() | ||
|
||
assert scenario.scenarioModel.scenarioCases.first().getStep(0).words.join(" ") == "Given some state param" | ||
} | ||
|
||
@ExtendedDescription("more details") | ||
def "be able to have extended descriptions"() { | ||
expect: | ||
|
||
given().some_state() | ||
when().some_action() | ||
then().some_outcome() | ||
|
||
assert scenario.scenarioModel.extendedDescription == "more details" | ||
} | ||
|
||
@Unroll | ||
def "be able to use tables #param"() { | ||
expect: | ||
given().some_state_$(param) | ||
when().some_action() | ||
then().some_outcome() | ||
|
||
assert scenario.scenarioModel.getDescription() == "be able to use tables #param" | ||
|
||
where: | ||
param | _ | ||
"param" | _ | ||
"word param" | _ | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
jgiven-spock2/src/test/groovy/com/tngtech/jgiven/spock/stages/Given.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.tngtech.jgiven.spock.stages | ||
|
||
import com.tngtech.jgiven.Stage | ||
import com.tngtech.jgiven.annotation.As | ||
|
||
class Given extends Stage<Given> { | ||
|
||
Given some_state() { | ||
self() | ||
} | ||
|
||
def some_state_$(String param) { | ||
self() | ||
} | ||
} | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
jgiven-spock2/src/test/groovy/com/tngtech/jgiven/spock/stages/Then.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tngtech.jgiven.spock.stages | ||
|
||
import com.tngtech.jgiven.Stage | ||
|
||
class Then extends Stage<Then> { | ||
|
||
Then some_outcome() { | ||
assert true | ||
self() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
jgiven-spock2/src/test/groovy/com/tngtech/jgiven/spock/stages/When.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tngtech.jgiven.spock.stages | ||
|
||
import com.tngtech.jgiven.Stage | ||
|
||
class When extends Stage<When> { | ||
|
||
When some_action() { | ||
self() | ||
} | ||
} |
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
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