如何在 Scala REPL 中使用 Specs 匹配器?

发布于 2024-08-18 06:50:07 字数 538 浏览 3 评论 0原文

在调试或探索规范功能时,在 REPL(Scala 解释器)中输入它们而不是在规范文件中输入并使用 Maven 之类的东西运行它会更有利。在 REPL 中创建与规范对象中相同的“环境”的最佳方法是什么?

更新: 看起来在 REPL 中试验规范匹配器的最简单方法是定义一些辅助子类并在其主体内使用表达式:

scala> class S extends Specification { override def toString = { reportSpecs; "" } }
defined class S

scala> new S { 1 mustEqual 2 }
Specification "anon"

  x example 1
    '1' is not equal to '2' (<console>:10)

Total for specification "anon":
Finished in 0 second, 4 ms
1 example, 1 expectation, 1 failure, 0 error

While debugging or exploring spec features it would be more advantageous to type them in REPL (Scala interpreter) rather then in file with spec and run it with something like maven. What is the optimal way to create in REPL the same "environment" as in Specification object?

Update:
It looks like the simplest way to experiment with specs' matchers in REPL is to define some helper subclass and use expressions inside its body:

scala> class S extends Specification { override def toString = { reportSpecs; "" } }
defined class S

scala> new S { 1 mustEqual 2 }
Specification "anon"

  x example 1
    '1' is not equal to '2' (<console>:10)

Total for specification "anon":
Finished in 0 second, 4 ms
1 example, 1 expectation, 1 failure, 0 error

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

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

发布评论

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

评论(2

围归者 2024-08-25 06:50:07

您可以使用 scala -classpath 启动 Scala 控制台,并为规范和您在规范中使用的其他库(例如 JUnit、Scalacheck)提供必要的 jar。或者,您可以使用 SBT 的控制台功能以正确的类路径启动控制台。

进入控制台后,您可以定义规范并执行它,如下所示。

Welcome to Scala version 2.8.0.Beta1-RC5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object Foo extends org.specs.Specification {
     |    "1 + 1" in { (1 + 1) must_== 2 }         
     | }                                           
defined module Foo

scala> Foo.reportSpecs
Specification "Foo"

  + 1 + 1

Total for specification "Foo":
Finished in 0 second, 184 ms
1 example, 1 expectation, 0 failure, 0 error

res0: Foo.type = Foo

您可能还想尝试 SBT 中的连续测试运行程序,它会在您每次保存 .scala 文件后自动重新编译并运行测试。从 SBT 控制台运行 > 〜测试

You can start the Scala console with scala -classpath and provide the neccesary jars for specs and other libraries you use from within specs (e.g. JUnit, Scalacheck). Alternatively, you could use the console feature from SBT to start the console with the correct classpath.

Once in the console, you can define a spec and execute it, as below.

Welcome to Scala version 2.8.0.Beta1-RC5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object Foo extends org.specs.Specification {
     |    "1 + 1" in { (1 + 1) must_== 2 }         
     | }                                           
defined module Foo

scala> Foo.reportSpecs
Specification "Foo"

  + 1 + 1

Total for specification "Foo":
Finished in 0 second, 184 ms
1 example, 1 expectation, 0 failure, 0 error

res0: Foo.type = Foo

You may also like to try the continuous test runner in SBT, which automatically recompiles and runs tests after every time you save a .scala file. From the SBT console, run > ~test

纸伞微斜 2024-08-25 06:50:07

我不了解 Specs,但我已经使用 ScalaCheck 做到了这一点,它真正需要的只是将其 JAR 放在类路径中。

I don't know about Specs, but I have done so with ScalaCheck, and all it really needs is having its JAR in the classpath.

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