English 中文(简体)
How to play with Specs matchers in Scala REPL?
原标题:

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
最佳回答

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

问题回答

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.





相关问题
Is there assembler REPL under linux?

Recently I ve started plaing with assembler under linux, there s good debuger, but comming from Ruby I m missing simple REPL that would let me enter a line of assembler code and see the result on ...

C# REPL tools; quick console-like compiling tool

Often times, I start a new instance of Visual Studio, just to create a console application that has some output and/or input. It s a temporary sandbox I use to test a method or something else and ...

How to play with Specs matchers in Scala REPL?

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

octave: load many functions from single file

How can I put multiple functions in one file and later get access to all of them in the octave interpreter ? I don t want to have a thousand files and want to group functions together. I d like ...

Clojure emacs slime + swank directory question

I m using emacs with clojure-swank and slime and trying to set my development environment. And I ran into a problem. When I start a repl I m stuck in an unknown directory preventing me to load my ...

scala: tracing implicits selection and other code magics

When trying to figure how a library works, implicit conversions are confusing. For example, looking at an expression like val foo: Foo = 1 , what converts 1 to Foo? Is it possible to instruct the ...

热门标签