OK, so one of the first items things I looked at was the unit testing framework, SUnit. It comes bundled in the standard library in the scala.testing.SUnit package and is dead simple. I hope the following snippet is self-explanatory enough:
object StackTest {It really is pretty simple; the whole testing framework sits on a single 200-line file. But is also is a bit verbose, isn't it? All those inner classes, cluttering the code... I tried to simplify things a bit. Here is what I came up with:
import scala.testing.SUnit._
def main(args:Array[String]): Unit = {
val tr = new TestResult
new TestSuite(
new Test01,
new Test02,
).run(tr)
for(val f <- tr.failures())
Console println f
}
class Test01 extends TestCase("pushing an element onto an empty stack") {
override def runTest() = {
val stack = new Stack()
val element = "asdf"
stack push element
assertEquals(stack.peek(), element)
}
}
class Test02 extends TestCase("popping an element from a stack") {
override def runTest() = {
val stack = new Stack()
val element = "asdf"
stack push element
stack.pop()
assertEquals(stack.isEmpty, true)
}
}
}
import sorg.testing._;I think it looks better. Sort of like those DSLs that are so fashionable these days... But the really cool thing is that it only took a couple dozen lines of code and a couple of hours to extend SUnit. Mind you that someone really proficient in Scala could probably do it much more quickly. See the whole unit testing domain specific language: (the name is almost larger than the code itself :)
object StackTests extends Tests with ConsoleDriver {
test ("pushing an element onto an empty stack") {
val stack = new Stack()
val element = "asdf"
stack push element
assertEquals(stack.peek(), element)
}
test ("popping an element from a stack") {
val stack = new Stack()
val element = "asdf"
stack push element
stack.pop()
assertEquals(stack.isEmpty, true)
}
}
package sorg.testing;
import scala.testing.SUnit._;
abstract class Tests extends Test with Assert {
type TestExp = () => Unit;
var tests = List[Pair[String,TestExp]]();
def test(desc: String)(t: => Unit) : Unit = {tests = Pair(desc,()=>t) :: tests};
override def run(tr: TestResult) = {
for (val Pair(desc, expression) <- tests) new TestCase(desc) {
override def runTest() = {Console println "running (" + desc + ")"; expression()}
}.run(tr)
}
}
trait ConsoleDriver extends Test {
def main(args:Array[String]): Unit = {
val results = new TestResult
Console println "running tests..."
this.run( results )
if (!results.failures.hasNext)
Console println "Success!";
else {
Console println "The following tests failed:";
for(val each:TestFailure <- results.failures)
Console println (each.toString + ":\n" + each.trace);
}
}
}
Expressiveness and power, what more can one ask of a programming language?
8 comments:
thanx for sharing this code. I've been looking for/thinking of something similar too but i was using an array of functions since i'm not that cool with currying yet. Yours is definitely a nice approach!
@mishelangelo:
Thanks. You should check out the newer scala libraries for testing:
http://rehersal.sourceforge.net/ (looks good but seems to be stalled at the moment)
http://code.google.com/p/specs/ (used for behavior driven development, integrates with ScalaCheck)
http://code.google.com/p/scalacheck/ (has some sophisticated data generation and invariant verification functionality).
Hi! I’d have to check with you here. Which is not something I usually do!
I enjoy reading a post that will make people think. Also, thanks for allowing me to comment!
Try to check my webpage - 휴게텔
(freaky)
It is one of the exceptional beauty through out the time and can get all the great moments out here that have been coming.
Delhi High Profile Girls
You never need to be upset, I just have a wonderful assistant this site www.freepaperwriter.com/, which helps me in writing works of any complexity for a small price and competently, I recommend you
I am grateful for all your data, Site is an exceptionally quite instructive substance
Spa Girls in Bangalore
Bangalore Massage Service
Massage Service in Bangalore
Bangalore Spa Girls
Night Out Girls in Bangalore
I thought late that it was time to start studying well, so now I am a frequent user of the subject service, because any written work will be written, and paper writers will help you earn a good grade
wow it was good New Friends Colony Model Girls
Post a Comment