/casbah-commons/src/test/scala/test/ScalaTestHelpers.scala

http://github.com/mongodb/casbah · Scala · 126 lines · 76 code · 14 blank · 36 comment · 9 complexity · 44a20ce68f61f4c0f6a10339d4949b72 MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package com.mongodb.casbah.commons
  18. package scalatest
  19. import com.mongodb.casbah.commons.Imports._
  20. import org.scalatest.matchers.Matcher
  21. import org.scalatest.matchers.MatchResult
  22. trait ScalaTestDBObjectMatchers {
  23. protected def someField(map: Option[DBObject], k: String) = if (k.indexOf('.') < 0) {
  24. map.getOrElse(MongoDBObject.empty).getAs[AnyRef](k)
  25. } else {
  26. map.getOrElse(MongoDBObject.empty).expand[AnyRef](k)
  27. }
  28. protected def field(map: DBObject, k: String) = if (k.indexOf('.') < 0) {
  29. map.getAs[AnyRef](k)
  30. } else {
  31. map.expand[AnyRef](k)
  32. }
  33. protected def listField(map: DBObject, k: String) = if (k.indexOf('.') < 0) {
  34. map.getAs[Seq[Any]](k)
  35. } else {
  36. map.expand[Seq[Any]](k)
  37. }
  38. val beDBObject = Matcher {
  39. left: AnyRef =>
  40. MatchResult(left.isInstanceOf[DBObject], left + " is a DBObject", left + " is not a DBObject")
  41. }
  42. def containSomeField(k: String) = Matcher {
  43. map: Option[DBObject] =>
  44. MatchResult(someField(map, k).isDefined, map.toString + " contains the key " + k, map.toString + " doesn't contain the key " + k)
  45. }
  46. /** matches if dbObject.contains(k) */
  47. def containField(k: String) = Matcher {
  48. map: DBObject =>
  49. MatchResult(field(map, k).isDefined, map.toString + " contains the key " + k, map.toString + " doesn't contain the key " + k)
  50. }
  51. /**
  52. * matches if a Some(map) contains a pair (key, value) == (k, v)
  53. */
  54. def containSomeEntry[V](p: (String, V)) = Matcher {
  55. map: Option[DBObject] =>
  56. MatchResult(
  57. someField(map, p._1).exists(_ == p._2), // match only the value
  58. map.toString + " contain the pair " + p,
  59. map.toString + " doesn't contain the pair " + p
  60. )
  61. }
  62. /**
  63. * Special version of "containEntry" that expects a list and then uses
  64. * "hasSameElements" on it.
  65. */
  66. def containListEntry(k: String, l: => scala.Traversable[Any]) = Matcher {
  67. map: DBObject =>
  68. val objL = listField(map, k).getOrElse(Seq.empty[Any]).toSeq
  69. val _l = l.toSeq
  70. MatchResult(
  71. objL.sameElements(_l), // match only the value
  72. map.toString + " has the pair " + k,
  73. map.toString + " doesn't have the pair " + k
  74. )
  75. }
  76. /**
  77. * matches if map contains a pair (key, value) == (k, v)
  78. */
  79. // If a DBObject is a Map, can use contain already
  80. def containEntry[V](p: (String, V)) = Matcher {
  81. map: DBObject =>
  82. MatchResult(
  83. field(map, p._1).exists(_.equals(p._2)), // match only the value
  84. map.toString + " has the pair " + p,
  85. map.toString + "[" + field(map, p._1) + "] doesn't have the pair " + p + "[" + p._2 + "]"
  86. )
  87. }
  88. /**
  89. * matches if Some(map) contains all the specified pairs
  90. * can expand dot notation to match specific sub-keys
  91. */
  92. def containSomeEntries[V](pairs: (String, V)*) = Matcher {
  93. map: Option[DBObject] =>
  94. MatchResult(
  95. pairs.forall(pair => someField(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  96. map.toString + " has the pairs " + pairs.mkString(", "),
  97. map.toString + " doesn't have the pairs " + pairs.mkString(", ")
  98. )
  99. }
  100. /**
  101. * matches if map contains all the specified pairs
  102. * can expand dot notation to match specific sub-keys
  103. */
  104. def containEntries[V](pairs: (String, V)*) = Matcher {
  105. map: DBObject =>
  106. MatchResult(
  107. pairs.forall(pair => field(map, pair._1).exists(_ == pair._2) /* match only the value */ ),
  108. map.toString + " has the pairs " + pairs.mkString(", "),
  109. map.toString + " doesn't have the pairs " + pairs.mkString(", ")
  110. )
  111. }
  112. }