Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "specs" BDD framework for Scala work?

Tags:

scala

bdd

specs

I'm just getting started with Scala, and I'm wondering which language feature allows you to do this:

"PersistentQueue" should {
  "add and remove one item" in {
    withTempFolder {
      val q = new PersistentQueue(folderName, "work", Config.fromMap(Map.empty))
      q.setup

      q.length mustEqual 0
      q.totalItems mustEqual 0
      q.bytes mustEqual 0
      q.journalSize mustEqual 0

      q.add("hello kitty".getBytes)

      q.length mustEqual 1
      q.totalItems mustEqual 1
      q.bytes mustEqual 11
      q.journalSize mustEqual 32

      new String(q.remove.get.data) mustEqual "hello kitty"

      q.length mustEqual 0
      q.totalItems mustEqual 1
      q.bytes mustEqual 0
      q.journalSize mustEqual 33

      q.close
      dumpJournal("work") mustEqual "add(11:0:hello kitty), remove"
    }
  }
}

That's from the unit tests for Kestrel.

What's going on here? Does "PersistentQueue" should mean that the Scala string class has been extended with a "should" method, or is something else happening here? I had a quick look through the Scala documentation but couldn't see which language features are being used for this code sample.

like image 524
Simon Willison Avatar asked Feb 26 '09 10:02

Simon Willison


1 Answers

It looks like implicit methods being added to the String class to me. This post has a demonstration.

like image 181
Dominic Mitchell Avatar answered Nov 13 '22 01:11

Dominic Mitchell