如何在规范中包含外部源文件以指定措施?

发布于 2024-11-15 16:09:04 字数 922 浏览 4 评论 0原文

我正在使用 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 报告中吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎夕旧梦 2024-11-22 16:09:04

这应该只是封装你想要的内容的问题:

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

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
              }
      }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文