1

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?

Steffen
  • 8,033
  • 1
  • 28
  • 34

1 Answers1

2

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
              }
      }
   }
Eric
  • 15,494
  • 38
  • 61
  • Thanks for this hint I had to tweak your code a bit to get it run. I guess the first ^ in the upper code part should be a ! otherwise I get a "overloaded method value ^ with alternatives" exception. Moreover, I had to add "
    "-Tags to the include source method and for the linkToSource method I had to configure sbt to copy the test files into the specs2-reports folder.
    – Steffen Jun 20 '11 at 13:44