Search code examples
scalaspecs2

Is it possible to use string matchers in combination with specs2 Json matchers?


Inside a specs2 test I'm validating json strings using JSON matchers. I know that it's possible to use regexes to match values like so

someJson must */("key")/("(one|other)".r)

Is it possible in a neat way to use other string matchers (e.g. contains)?

Given this messy example:

val someJson = """{"blob": "multiline string
                  |with various line endings"}"""

This matcher

someJson must */("blob")/contains("various")

Looks much cleaner than

someJson must */("blob")/"[^v]various.*".r

Which isn't the proper expression to use but it might work in this case.


Solution

  • It is now possible (in specs2-1.12.4-SNAPSHOT and specs2-1.13.1-SNAPSHOT) to use specs2 matchers in addition to simple strings and regular expressions to match values and keys:

    person must /("p.*".r) */(".*on".r) /("age" -> "33")
    person must /("p.*".r) */(".*on".r) /("age" -> "\d+\.\d".r)
    person must /("p.*".r) */(".*on".r) /("age" -> startWith("3"))
    person must /("p.*".r) */(".*on".r) /("age" -> (be_>(30) ^^ ((_:String).toInt)))