iwein
iwein

Reputation: 26161

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.

Upvotes: 2

Views: 926

Answers (2)

Eric
Eric

Reputation: 15557

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

Upvotes: 6

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297205

Doesn't look like it. The tell-tale sign for that capability is a method accepting a partial function, and all methods on JsonMatchers accept Any.

Upvotes: 2

Related Questions