Created
February 26, 2018 16:39
-
-
Save danielkocot/377c6b9ec3036b59601c04d8ed5519a6 to your computer and use it in GitHub Desktop.
task create SpockSpecs
This file contains hidden or 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
task createSpockSpecs { | |
group = "Generating Spock Specs from gherkin" | |
doLast { | |
def gherkinFiles = fileTree("src/test/resources/Features").files | |
def specBlock | |
gherkinFiles.each { | |
File transformFile = new File("$projectDir/src/test/groovy/$it.name".replaceFirst(~/\.[^\.]+$/,'Spec.groovy')) | |
if (!transformFile.exists()) { | |
transformFile.createNewFile() | |
def completeScenarioString | |
def completeGivenString | |
def fileScenarioString | |
def completeWhenString | |
def completeThenString | |
println "Generate Spock Spec" | |
def multiline = it.text | |
def list = multiline.readLines() | |
list.removeAll { it.startsWith("Feature:") } | |
list.find { | |
if (it.contains("Scenario")) { | |
def scenarioString = it.toString().replace("Scenario: ", "def ") | |
fileScenarioString = scenarioString.substring(4) | |
completeScenarioString = scenarioString + "() {" | |
} | |
if (it.contains("Given")) { | |
String givenString = it.toString().replace("Given", "given: \"") | |
completeGivenString = givenString + "\"" | |
} | |
if (it.contains("When")) { | |
String whenString = it.toString().replace("When", "when: \"") | |
completeWhenString = whenString + "\"" | |
} | |
if (it.contains("Then")) { | |
def thenString = it.toString().replace("Then", "then: \"") | |
completeThenString = thenString + "\"\n}" | |
} | |
} | |
def minusContent = new StringBuilder(it.text) | |
def fileContentSb = new StringBuilder(it.text) | |
specBlock = fileContentSb.insert(0, "import spock.lang.Specification\n" + | |
"\n" + | |
"\n" + | |
"class " + it.name.take(it.name.lastIndexOf('.')) + "Spec extends Specification {\n" | |
+ completeScenarioString + "\n" | |
+ completeGivenString + "\n" | |
+ completeWhenString + "\n" | |
+ completeThenString + "\n}").minus(minusContent) | |
def spockFiles = fileTree("src/test/groovy").files | |
spockFiles.each { | |
println it.name | |
println it.name.take(it.name.lastIndexOf('.')) | |
if (specBlock.contains(it.name.take(it.name.lastIndexOf('.')))) { | |
it.setText(specBlock) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment