Last active
December 14, 2015 22:49
-
-
Save kyonmm/5161650 to your computer and use it in GitHub Desktop.
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
//Pict.groovyがClassPathにある前提で次のスクリプトを実行する | |
//pict.exeはPict.pictPathに設定する | |
//エラーが起きたときについて検知していない。 | |
//水準に「,」が入っているときは@escapeComma@でエスケープしている。@escapeComma@が水準にあるときは、escapeComma の値を変えてください。 | |
//水準に「\n」が入っているときは@escapeLineSeparator@でエスケープしている。@escapeLineSeparator@が水準にあるときは、escapeLineSeparator の値を変えてください。 | |
class Pict{ | |
def static pictPath = $/D:\Apps\PICT\pict.exe/$ | |
def static escapeLineSeparator = "@escapeLineSeparator@" | |
def static escapeComma = "@escapeComma@" | |
static { | |
String.metaClass.escape = { | |
delegate.replace("\n", escapeLineSeparator).replace(",", escapeComma) | |
} | |
String.metaClass.inEscape = {target -> | |
delegate.replace(escapeLineSeparator, "\n").replace(escapeComma, ",") | |
} | |
} | |
static TestCases generate(Map map){ | |
def cmd = $/${pictPath} ${createFile(map).absolutePath}/$ | |
println cmd | |
def p = cmd.execute([], new File(".")) | |
def text = p.text | |
new TestCases(text) | |
} | |
static File createFile(Map map){ | |
def file = new File("sampleInput.txt") | |
file.createNewFile() | |
def a = map.collectEntries {k, v -> ["${k.escape()}":v.collect {it.escape()}]}.toMapString().replace("], ", "]\n") | |
file.text = a.replace("[", "").replace("]", "") | |
file | |
} | |
/** | |
* toValues : [[item1Value1, item2Value1, item3Value1], [item1Value2, item2Value2, item3Value2]] | |
* toKeyValues : [[item1:item1Value1, item2:item2Value1, item3:item3Value1], [item1:item1Value2, item2:item2Value2, item3:item3Value2]] | |
*/ | |
static class TestCases{ | |
def header = [] | |
def values = [] | |
TestCases(String text){ | |
println text | |
text.readLines().eachWithIndex {line, i -> | |
if(i == 0){ | |
header = line.split("\t") | |
} | |
else{ | |
values += [line.split("\t").collect {it.inEscape()}] | |
} | |
} | |
} | |
def toValues = {values} | |
def toKeyValues = { | |
def result = [] | |
values.each{ | |
def testcase = [:] | |
it.eachWithIndex{ value, i -> | |
testcase << ["${header[i]}":value] | |
} | |
result << testcase | |
} | |
result | |
} | |
} | |
} | |
// 次のような条件の時にAll-Pairでテストケースを作成する。 | |
// 因子a : 1,2,3 | |
// 因子b : 1,2 | |
// 因子c : 4.5 | |
def testCases = Pict.generate([a: ["1","2","3"],b: ["1","2"],c: ["4","5"]]) | |
// テストケースは次の2つの形で取得できる | |
// toKeyValues : Mapで各要素が1テストケースに該当する。Keyに因子、Valueに値がはいっている。 | |
// toValues : Listで各要素が1テストケースに該当する。1要素にgenerateに渡した因子の順で各値が入っている。 | |
println "toValues" | |
testCases.toValues().eachWithIndex{it, i -> | |
println "No.${i} ==== ${it}" | |
} | |
println "toKeyValues" | |
testCases.toKeyValues().eachWithIndex{it, i -> | |
println "No.${i} ==== ${it}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment