开发者

How to include external source files in a spec to specify measures?

I'm using Specs2 to write a specification for a measurement library. To verify the calculated measures I have numerous source files covering standard cases as well as a lot of corner cases. I did analyze them开发者_运维问答 manually to I know the exact measures, but to document everything and automate it, this should be part of a Specs2 specification.

So far I copied some of the source files into my specification and passed it to the verifying methods as string. However, this has the downside, that the inlined code isn't checked anymore - the external files are verified by the standard compiler so I'm sure it is valid code. It's no problem to just pass the filename, but my specification should include the source code in the resulting HTML report and not only point to a file one has to dig out and look at manually. To give you some idea here is the code I'm using right now


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
        }
    }
}

Does someone have an idea, how it is possible to point to the external files so that they are included as initial input in the resulting HTML report?


That should be just a matter of encapsulating what you want:

 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>"  

And then:

  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
              }
      }
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜