scala - Matching map with specs2 -



scala - Matching map with specs2 -

suppose have function foo: () -> map[string, a], a trait.

trait { def x: int, def y: int }

now need write specs2 spec create sure foo returns map 2 expected pairs. note don't know actual type of values.

unfortunately didn't figure out how utilize havepairs matcher in case, writing:

"test foo" in { val m = foo() def test(a: a, x: int, y: int) = (a.x must_== 0) , (a.y must_== 1) test(m("key1"), 0, 1) test(m("key2"), 3, 5) }

this code ugly. how suggest alter it?

i guess should write custom matcher a , new havepairs matcher, utilize a-matcher match pairs values. create sense ?

normally utilize havepairs because of utilize of traits can not utilize normal contains method based on hashcode.

scala supports no structural equality out of box class comparission. if utilize case classes feature , utilize havepairs.

if need back upwards traits fear stuck having code beingness equal means each individual case want back upwards , test.

class examplespec extends specification{override def is: fragments = s2""" generated map contains info $caseclasstest works trait $traittest """ trait { def x: int ; def y: int } // case class, same trait case class casea(x: int, y:int) extends // generates map traits on calling apply on function def generatetraitmap : () => map[string,a] = () => map( "k1" -> new a{ def x = 1 ; def y = -1 }, "k2" -> new a{ def x = 0 ; def y = 42 } ) // generates map caseclasses on calling apply on function def generatecaseclassmap : () => map[string,a] = () => map( "k1" -> casea(1, -1), "k2" -> casea(0,42) ) // test case classes works havepairs out of box def caseclasstest = generatecaseclassmap() must havepairs( "k1" -> casea(1, -1), "k2" -> casea(0,42) ) // testing traits needs plumbing, , give poor error feedback def traittest = { val info = generatetraitmap() def teststructuralequality(a: a, b: a) = list[a => int]( _.x, _.y ).forall(f => f(a) == f(b) ) val testdata = map( "k1" -> new a{ def x = 1 ; def y = -1 }, "k2" -> new a{ def x = 0 ; def y = 42 } ) testdata.forall{ case (k,v) => testdata.contains(k) && teststructuralequality(v, testdata(k)) } must betrue }

}

scala specs2

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -