Specs2 を使用して次の仕様を記述しようとしていますが、動作しないようです。コンパイラは常に「Unit => org.specs2.execute.Result から暗黙的なビューを利用できません」と不平を言います。
テストソースは次のとおりです。
"generate the correct output when merging an extraction" in {
val source = new File("src/test/resources/sample-docs/text-and-image-on-page-2.pdf")
val output = this.extractor.extract(source)
val pdfGenerator = new ITextPdfGenerator()
val processor = new ExecutableProcessor()
val ocrInput = IOUtils.createTempFile("ocr_input", "pdf")
val ocrOutput = IOUtils.createTempFile("ocr_output", "pdf")
deleteWhenDone[MatchResult[Any]](output.getFullTextFile, ocrInput, ocrOutput) ( {
pdfGenerator.generatePdf(source, ocrInput, output.getPagesWithImages)
processor.process(ocrInput, ocrOutput)
this.extractor.mergeExtraction(output, ocrOutput)
IOUtils.readFile(output.getFullTextFile) === """sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 """
})
}
完了時の削除機能は次のとおりです。
def deleteWhenDone[T]( files : File* ) ( fn : => T ) {
try {
fn
} finally {
IOUtils.deleteFiles( files )
}
}
この行が仕様の最後にある場合、機能します。
IOUtils.readFile(output.getFullTextFile) === "sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 "
なぜこのように失敗するのでしょうか? また、クロージャを引き続き使用してテストをコンパイルするにはどうすればよいでしょうか?
編集
deleteWhenDone 呼び出しを次のように変更しました。
deleteWhenDone[Result](output.getFullTextFile, ocrInput, ocrOutput) ( {
pdfGenerator.generatePdf(source, ocrInput, output.getPagesWithImages)
processor.process(ocrInput, ocrOutput)
this.extractor.mergeExtraction(output, ocrOutput)
IOUtils.readFile(output.getFullTextFile) === "sample text on line 1 page 1 sample text on line 2 page 1 sample text on line 1 page 3 sample text on line 2 page 3 "
})
しかし、まだ機能しません。
編集2
ラファエルの答えのおかげで、それを機能させる最終的なコードは次のとおりです。
def deleteWhenDone[T]( files : File* ) ( fn : => T ) : T = {
try {
fn
} finally {
IOUtils.deleteFiles( files )
}
}
メソッドの戻り値の型がありませんでした。ありがとう!