1

Specs2を使用して、測定ライブラリの仕様を記述しています。計算された測定値を検証するために、標準的なケースだけでなく、多くのコーナーケースをカバーする多数のソースファイルがあります。正確な測定値を知るために手動で分析しましたが、すべてを文書化して自動化するには、これをSpecs2仕様の一部にする必要があります。

これまでのところ、いくつかのソースファイルを仕様にコピーし、それを文字列として検証メソッドに渡しました。ただし、これには、インライン化されたコードがチェックされなくなるという欠点があります。外部ファイルは標準コンパイラによって検証されるため、有効なコードであると確信しています。ファイル名を渡すだけでも問題ありませんが、私の仕様では、結果のHTMLレポートにソースコードを含める必要があり、手動で調べて確認する必要のあるファイルを指すだけではありません。ここであなたにいくつかのアイデアを与えるために、私が今使っているコードがあります


class CountVisitorSpec extends Specification { def is =

    "Given the ${com/example/Test1.java} source, the visitor should deliver a count of ${16}" ! new GivenThen {
        def extract(text: String) = {
            val (filename, count) = extract2(text)
            val file = classOf[CountVisitorSpec].getClassLoader.getResource(filename).getFile
            val src = Path(file).slurpString
            val visitor = new CountVisitor
            AstAnalyzer.runWith(src, visitor)
            visitor.count must_== count.toLong
        }
    }
}

誰かが外部ファイルをポイントして、結果のHTMLレポートに初期入力として含まれるようにする方法を知っていますか?

4

1 に答える 1

2

それはあなたが望むものをカプセル化することの問題であるはずです:

 def withFile(name: String, description: String)(ex: String => Result) = {
   ("Given the ${"+file+"},"+description) ^ new GivenThen {
     def extract(text: String) = ex(text)
   } ^
   linkToSource(file)^ // if you want to create a Markdown link to the source file
   includeSource(file) // if you want to include the source code    
 } 

 def linkToSource(fileName: String)  = "[source]("+fileName+")"
 def includeSource(fileName: String) = "<code class=\"prettyprint\">"+Path(file).slurpString+"</code>"  

その後:

  class CountVisitorSpec extends Specification { def is =

     withFile("com/example/Test1.java", "the visitor should deliver a count of ${16}", 
              (text: String) => {
                val (filename, count) = extract2(text)
                val file = classOf[CountVisitorSpec].getClassLoader.getResource(filename).getFile
                val src = Path(file).slurpString
                val visitor = new CountVisitor
                AstAnalyzer.runWith(src, visitor)
                visitor.count must_== count.toLong
              }
      }
   }
于 2011-06-19T23:40:21.680 に答える