1/* 2 * Copyright 2001-2008 Artima, Inc. 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 */ 16package org.scalatest.matchers 17 18import org.scalatest._ 19import org.scalatest.prop.Checkers 20import org.scalacheck._ 21import Arbitrary._ 22import Prop._ 23 24class ShouldContainValueSpec extends Spec with ShouldMatchers with Checkers with ReturnsNormallyThrowsAssertion { 25 26 // Checking for a specific size 27 describe("The 'contain value (Int)' syntax") { 28 29 describe("on scala.collection.immutable.Map") { 30 31 it("should do nothing if map contains specified value") { 32 Map("one" -> 1, "two" -> 2) should contain value (2) 33 Map("one" -> 1, "two" -> 2) should (contain value (2)) 34 Map(1 -> "one", 2 -> "two") should contain value ("two") 35 } 36 37 it("should do nothing if map does not contain the specified value and used with not") { 38 Map("one" -> 1, "two" -> 2) should not { contain value (3) } 39 Map("one" -> 1, "two" -> 2) should not contain value (3) 40 Map("one" -> 1, "two" -> 2) should (not contain value (3)) 41 } 42 43 it("should do nothing when map contains specified value and used in a logical-and expression") { 44 Map("one" -> 1, "two" -> 2) should { contain value (2) and (contain value (1)) } 45 Map("one" -> 1, "two" -> 2) should ((contain value (2)) and (contain value (1))) 46 Map("one" -> 1, "two" -> 2) should (contain value (2) and contain value (1)) 47 } 48 49 it("should do nothing when map contains specified value and used in a logical-or expression") { 50 Map("one" -> 1, "two" -> 2) should { contain value (7) or (contain value (1)) } 51 Map("one" -> 1, "two" -> 2) should ((contain value (7)) or (contain value (1))) 52 Map("one" -> 1, "two" -> 2) should (contain value (7) or contain value (1)) 53 } 54 55 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 56 Map("one" -> 1, "two" -> 2) should { not { contain value (5) } and not { contain value (3) }} 57 Map("one" -> 1, "two" -> 2) should ((not contain value (5)) and (not contain value (3))) 58 Map("one" -> 1, "two" -> 2) should (not contain value (5) and not contain value (3)) 59 } 60 61 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 62 Map("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (3) }} 63 Map("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (3))) 64 Map("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (3)) 65 } 66 67 it("should throw TestFailedException if map does not contain the specified value") { 68 val caught1 = intercept[TestFailedException] { 69 Map("one" -> 1, "two" -> 2) should contain value (3) 70 } 71 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3") 72 } 73 74 it("should throw TestFailedException if contains the specified value when used with not") { 75 76 val caught1 = intercept[TestFailedException] { 77 Map("one" -> 1, "two" -> 2) should (not contain value (2)) 78 } 79 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2") 80 81 val caught2 = intercept[TestFailedException] { 82 Map("one" -> 1, "two" -> 2) should not (contain value (2)) 83 } 84 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2") 85 86 val caught3 = intercept[TestFailedException] { 87 Map("one" -> 1, "two" -> 2) should not contain value (2) 88 } 89 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2") 90 } 91 92 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 93 94 val caught1 = intercept[TestFailedException] { 95 Map("one" -> 1, "two" -> 2) should { contain value (5) and (contain value (2)) } 96 } 97 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 98 99 val caught2 = intercept[TestFailedException] { 100 Map("one" -> 1, "two" -> 2) should ((contain value (5)) and (contain value (2))) 101 } 102 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 103 104 val caught3 = intercept[TestFailedException] { 105 Map("one" -> 1, "two" -> 2) should (contain value (5) and contain value (2)) 106 } 107 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 108 } 109 110 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 111 112 val caught1 = intercept[TestFailedException] { 113 Map("one" -> 1, "two" -> 2) should { contain value (55) or (contain value (22)) } 114 } 115 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 116 117 val caught2 = intercept[TestFailedException] { 118 Map("one" -> 1, "two" -> 2) should ((contain value (55)) or (contain value (22))) 119 } 120 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 121 122 val caught3 = intercept[TestFailedException] { 123 Map("one" -> 1, "two" -> 2) should (contain value (55) or contain value (22)) 124 } 125 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 126 } 127 128 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 129 130 val caught1 = intercept[TestFailedException] { 131 Map("one" -> 1, "two" -> 2) should { not { contain value (3) } and not { contain value (2) }} 132 } 133 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 134 135 val caught2 = intercept[TestFailedException] { 136 Map("one" -> 1, "two" -> 2) should ((not contain value (3)) and (not contain value (2))) 137 } 138 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 139 140 val caught3 = intercept[TestFailedException] { 141 Map("one" -> 1, "two" -> 2) should (not contain value (3) and not contain value (2)) 142 } 143 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 144 } 145 146 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 147 148 val caught1 = intercept[TestFailedException] { 149 Map("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (2) }} 150 } 151 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 152 153 val caught2 = intercept[TestFailedException] { 154 Map("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (2))) 155 } 156 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 157 158 val caught3 = intercept[TestFailedException] { 159 Map("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (2)) 160 } 161 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 162 } 163 } 164 165 describe("on scala.collection.mutable.Map") { 166 167 import scala.collection.mutable 168 169 it("should do nothing if map contains specified value") { 170 mutable.Map("one" -> 1, "two" -> 2) should contain value (2) 171 mutable.Map("one" -> 1, "two" -> 2) should (contain value (2)) 172 mutable.Map(1 -> "one", 2 -> "two") should contain value ("two") 173 } 174 175 it("should do nothing if map does not contain the specified value and used with not") { 176 mutable.Map("one" -> 1, "two" -> 2) should not { contain value (3) } 177 mutable.Map("one" -> 1, "two" -> 2) should not contain value (3) 178 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (3)) 179 } 180 181 it("should do nothing when map contains specified value and used in a logical-and expression") { 182 mutable.Map("one" -> 1, "two" -> 2) should { contain value (2) and (contain value (1)) } 183 mutable.Map("one" -> 1, "two" -> 2) should ((contain value (2)) and (contain value (1))) 184 mutable.Map("one" -> 1, "two" -> 2) should (contain value (2) and contain value (1)) 185 } 186 187 it("should do nothing when map contains specified value and used in a logical-or expression") { 188 mutable.Map("one" -> 1, "two" -> 2) should { contain value (7) or (contain value (1)) } 189 mutable.Map("one" -> 1, "two" -> 2) should ((contain value (7)) or (contain value (1))) 190 mutable.Map("one" -> 1, "two" -> 2) should (contain value (7) or contain value (1)) 191 } 192 193 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 194 mutable.Map("one" -> 1, "two" -> 2) should { not { contain value (5) } and not { contain value (3) }} 195 mutable.Map("one" -> 1, "two" -> 2) should ((not contain value (5)) and (not contain value (3))) 196 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (5) and not contain value (3)) 197 } 198 199 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 200 mutable.Map("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (3) }} 201 mutable.Map("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (3))) 202 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (3)) 203 } 204 205 it("should throw TestFailedException if map does not contain the specified value") { 206 val caught1 = intercept[TestFailedException] { 207 mutable.Map("one" -> 1, "two" -> 2) should contain value (3) 208 } 209 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3") 210 } 211 212 it("should throw TestFailedException if contains the specified value when used with not") { 213 214 val caught1 = intercept[TestFailedException] { 215 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (2)) 216 } 217 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2") 218 219 val caught2 = intercept[TestFailedException] { 220 mutable.Map("one" -> 1, "two" -> 2) should not (contain value (2)) 221 } 222 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2") 223 224 val caught3 = intercept[TestFailedException] { 225 mutable.Map("one" -> 1, "two" -> 2) should not contain value (2) 226 } 227 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2") 228 } 229 230 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 231 232 val caught1 = intercept[TestFailedException] { 233 mutable.Map("one" -> 1, "two" -> 2) should { contain value (5) and (contain value (2)) } 234 } 235 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 236 237 val caught2 = intercept[TestFailedException] { 238 mutable.Map("one" -> 1, "two" -> 2) should ((contain value (5)) and (contain value (2))) 239 } 240 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 241 242 val caught3 = intercept[TestFailedException] { 243 mutable.Map("one" -> 1, "two" -> 2) should (contain value (5) and contain value (2)) 244 } 245 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 246 } 247 248 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 249 250 val caught1 = intercept[TestFailedException] { 251 mutable.Map("one" -> 1, "two" -> 2) should { contain value (55) or (contain value (22)) } 252 } 253 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 254 255 val caught2 = intercept[TestFailedException] { 256 mutable.Map("one" -> 1, "two" -> 2) should ((contain value (55)) or (contain value (22))) 257 } 258 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 259 260 val caught3 = intercept[TestFailedException] { 261 mutable.Map("one" -> 1, "two" -> 2) should (contain value (55) or contain value (22)) 262 } 263 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 264 } 265 266 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 267 268 val caught1 = intercept[TestFailedException] { 269 mutable.Map("one" -> 1, "two" -> 2) should { not { contain value (3) } and not { contain value (2) }} 270 } 271 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 272 273 val caught2 = intercept[TestFailedException] { 274 mutable.Map("one" -> 1, "two" -> 2) should ((not contain value (3)) and (not contain value (2))) 275 } 276 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 277 278 val caught3 = intercept[TestFailedException] { 279 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (3) and not contain value (2)) 280 } 281 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 282 } 283 284 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 285 286 val caught1 = intercept[TestFailedException] { 287 mutable.Map("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (2) }} 288 } 289 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 290 291 val caught2 = intercept[TestFailedException] { 292 mutable.Map("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (2))) 293 } 294 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 295 296 val caught3 = intercept[TestFailedException] { 297 mutable.Map("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (2)) 298 } 299 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 300 } 301 } 302 303 describe("on scala.collection.Map") { 304 305 val map: scala.collection.Map[String, Int] = Map("one" -> 1, "two" -> 2) 306 307 it("should do nothing if map contains specified value") { 308 map should contain value (2) 309 map should (contain value (2)) 310 } 311 312 it("should do nothing if map does not contain the specified value and used with not") { 313 map should not { contain value (3) } 314 map should not contain value (3) 315 map should (not contain value (3)) 316 } 317 318 it("should do nothing when map contains specified value and used in a logical-and expression") { 319 map should { contain value (2) and (contain value (1)) } 320 map should ((contain value (2)) and (contain value (1))) 321 map should (contain value (2) and contain value (1)) 322 } 323 324 it("should do nothing when map contains specified value and used in a logical-or expression") { 325 map should { contain value (7) or (contain value (1)) } 326 map should ((contain value (7)) or (contain value (1))) 327 map should (contain value (7) or contain value (1)) 328 } 329 330 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 331 map should { not { contain value (5) } and not { contain value (3) }} 332 map should ((not contain value (5)) and (not contain value (3))) 333 map should (not contain value (5) and not contain value (3)) 334 } 335 336 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 337 map should { not { contain value (2) } or not { contain value (3) }} 338 map should ((not contain value (2)) or (not contain value (3))) 339 map should (not contain value (2) or not contain value (3)) 340 } 341 342 it("should throw TestFailedException if map does not contain the specified value") { 343 val caught1 = intercept[TestFailedException] { 344 map should contain value (3) 345 } 346 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3") 347 } 348 349 it("should throw TestFailedException if contains the specified value when used with not") { 350 351 val caught1 = intercept[TestFailedException] { 352 map should (not contain value (2)) 353 } 354 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2") 355 356 val caught2 = intercept[TestFailedException] { 357 map should not (contain value (2)) 358 } 359 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2") 360 361 val caught3 = intercept[TestFailedException] { 362 map should not contain value (2) 363 } 364 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2") 365 } 366 367 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 368 369 val caught1 = intercept[TestFailedException] { 370 map should { contain value (5) and (contain value (2)) } 371 } 372 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 373 374 val caught2 = intercept[TestFailedException] { 375 map should ((contain value (5)) and (contain value (2))) 376 } 377 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 378 379 val caught3 = intercept[TestFailedException] { 380 map should (contain value (5) and contain value (2)) 381 } 382 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 383 } 384 385 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 386 387 val caught1 = intercept[TestFailedException] { 388 map should { contain value (55) or (contain value (22)) } 389 } 390 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 391 392 val caught2 = intercept[TestFailedException] { 393 map should ((contain value (55)) or (contain value (22))) 394 } 395 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 396 397 val caught3 = intercept[TestFailedException] { 398 map should (contain value (55) or contain value (22)) 399 } 400 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 401 } 402 403 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 404 405 val caught1 = intercept[TestFailedException] { 406 map should { not { contain value (3) } and not { contain value (2) }} 407 } 408 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 409 410 val caught2 = intercept[TestFailedException] { 411 map should ((not contain value (3)) and (not contain value (2))) 412 } 413 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 414 415 val caught3 = intercept[TestFailedException] { 416 map should (not contain value (3) and not contain value (2)) 417 } 418 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 419 } 420 421 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 422 423 val caught1 = intercept[TestFailedException] { 424 map should { not { contain value (2) } or not { contain value (2) }} 425 } 426 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 427 428 val caught2 = intercept[TestFailedException] { 429 map should ((not contain value (2)) or (not contain value (2))) 430 } 431 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 432 433 val caught3 = intercept[TestFailedException] { 434 map should (not contain value (2) or not contain value (2)) 435 } 436 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 437 } 438 } 439 440 describe("on scala.collection.immutable.HashMap") { 441 442 import scala.collection.immutable.HashMap 443 444 it("should do nothing if map contains specified value") { 445 HashMap("one" -> 1, "two" -> 2) should contain value (2) 446 HashMap("one" -> 1, "two" -> 2) should (contain value (2)) 447 HashMap(1 -> "one", 2 -> "two") should contain value ("two") 448 } 449 450 it("should do nothing if map does not contain the specified value and used with not") { 451 HashMap("one" -> 1, "two" -> 2) should not { contain value (3) } 452 HashMap("one" -> 1, "two" -> 2) should not contain value (3) 453 HashMap("one" -> 1, "two" -> 2) should (not contain value (3)) 454 } 455 456 it("should do nothing when map contains specified value and used in a logical-and expression") { 457 HashMap("one" -> 1, "two" -> 2) should { contain value (2) and (contain value (1)) } 458 HashMap("one" -> 1, "two" -> 2) should ((contain value (2)) and (contain value (1))) 459 HashMap("one" -> 1, "two" -> 2) should (contain value (2) and contain value (1)) 460 } 461 462 it("should do nothing when map contains specified value and used in a logical-or expression") { 463 HashMap("one" -> 1, "two" -> 2) should { contain value (7) or (contain value (1)) } 464 HashMap("one" -> 1, "two" -> 2) should ((contain value (7)) or (contain value (1))) 465 HashMap("one" -> 1, "two" -> 2) should (contain value (7) or contain value (1)) 466 } 467 468 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 469 HashMap("one" -> 1, "two" -> 2) should { not { contain value (5) } and not { contain value (3) }} 470 HashMap("one" -> 1, "two" -> 2) should ((not contain value (5)) and (not contain value (3))) 471 HashMap("one" -> 1, "two" -> 2) should (not contain value (5) and not contain value (3)) 472 } 473 474 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 475 HashMap("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (3) }} 476 HashMap("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (3))) 477 HashMap("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (3)) 478 } 479 480 it("should throw TestFailedException if map does not contain the specified value") { 481 val caught1 = intercept[TestFailedException] { 482 HashMap("one" -> 1, "two" -> 2) should contain value (3) 483 } 484 //assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3") 485 assert(caught1.getMessage.startsWith("Map(")) 486 assert(caught1.getMessage.endsWith(") did not contain value 3")) 487 } 488 489 it("should throw TestFailedException if contains the specified value when used with not") { 490 491 val caught1 = intercept[TestFailedException] { 492 HashMap("one" -> 1, "two" -> 2) should (not contain value (2)) 493 } 494 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2") 495 496 val caught2 = intercept[TestFailedException] { 497 HashMap("one" -> 1, "two" -> 2) should not (contain value (2)) 498 } 499 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2") 500 501 val caught3 = intercept[TestFailedException] { 502 HashMap("one" -> 1, "two" -> 2) should not contain value (2) 503 } 504 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2") 505 } 506 507 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 508 509 val caught1 = intercept[TestFailedException] { 510 HashMap("one" -> 1, "two" -> 2) should { contain value (5) and (contain value (2)) } 511 } 512 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 5") 513 514 val caught2 = intercept[TestFailedException] { 515 HashMap("one" -> 1, "two" -> 2) should ((contain value (5)) and (contain value (2))) 516 } 517 //assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 518 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 5") 519 520 val caught3 = intercept[TestFailedException] { 521 HashMap("one" -> 1, "two" -> 2) should (contain value (5) and contain value (2)) 522 } 523 //assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 524 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 5") 525 } 526 527 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 528 529 val caught1 = intercept[TestFailedException] { 530 HashMap("one" -> 1, "two" -> 2) should { contain value (55) or (contain value (22)) } 531 } 532 //assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 533 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 55, and Map(.*) did not contain value 22") 534 535 val caught2 = intercept[TestFailedException] { 536 HashMap("one" -> 1, "two" -> 2) should ((contain value (55)) or (contain value (22))) 537 } 538 //assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 539 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 55, and Map(.*) did not contain value 22") 540 541 val caught3 = intercept[TestFailedException] { 542 HashMap("one" -> 1, "two" -> 2) should (contain value (55) or contain value (22)) 543 } 544 //assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 545 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 55, and Map(.*) did not contain value 22") 546 } 547 548 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 549 550 val caught1 = intercept[TestFailedException] { 551 HashMap("one" -> 1, "two" -> 2) should { not { contain value (3) } and not { contain value (2) }} 552 } 553 //assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 554 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 3, but Map(.*) contained value 2") 555 556 val caught2 = intercept[TestFailedException] { 557 HashMap("one" -> 1, "two" -> 2) should ((not contain value (3)) and (not contain value (2))) 558 } 559 //assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 560 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 3, but Map(.*) contained value 2") 561 562 val caught3 = intercept[TestFailedException] { 563 HashMap("one" -> 1, "two" -> 2) should (not contain value (3) and not contain value (2)) 564 } 565 //assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 566 caught1.getMessage should fullyMatch regex ("Map(.*) did not contain value 3, but Map(.*) contained value 2") 567 } 568 569 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 570 571 val caught1 = intercept[TestFailedException] { 572 HashMap("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (2) }} 573 } 574 //assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 575 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2, and Map(.*) contained value 2") 576 577 val caught2 = intercept[TestFailedException] { 578 HashMap("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (2))) 579 } 580 //assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 581 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2, and Map(.*) contained value 2") 582 583 val caught3 = intercept[TestFailedException] { 584 HashMap("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (2)) 585 } 586 //assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 587 caught1.getMessage should fullyMatch regex ("Map(.*) contained value 2, and Map(.*) contained value 2") 588 } 589 } 590 591 describe("on scala.collection.mutable.HashMap") { 592 593 import scala.collection.mutable 594 595 it("should do nothing if map contains specified value") { 596 mutable.HashMap("one" -> 1, "two" -> 2) should contain value (2) 597 mutable.HashMap("one" -> 1, "two" -> 2) should (contain value (2)) 598 mutable.HashMap(1 -> "one", 2 -> "two") should contain value ("two") 599 } 600 601 it("should do nothing if map does not contain the specified value and used with not") { 602 mutable.HashMap("one" -> 1, "two" -> 2) should not { contain value (3) } 603 mutable.HashMap("one" -> 1, "two" -> 2) should not contain value (3) 604 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (3)) 605 } 606 607 it("should do nothing when map contains specified value and used in a logical-and expression") { 608 mutable.HashMap("one" -> 1, "two" -> 2) should { contain value (2) and (contain value (1)) } 609 mutable.HashMap("one" -> 1, "two" -> 2) should ((contain value (2)) and (contain value (1))) 610 mutable.HashMap("one" -> 1, "two" -> 2) should (contain value (2) and contain value (1)) 611 } 612 613 it("should do nothing when map contains specified value and used in a logical-or expression") { 614 mutable.HashMap("one" -> 1, "two" -> 2) should { contain value (7) or (contain value (1)) } 615 mutable.HashMap("one" -> 1, "two" -> 2) should ((contain value (7)) or (contain value (1))) 616 mutable.HashMap("one" -> 1, "two" -> 2) should (contain value (7) or contain value (1)) 617 } 618 619 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 620 mutable.HashMap("one" -> 1, "two" -> 2) should { not { contain value (5) } and not { contain value (3) }} 621 mutable.HashMap("one" -> 1, "two" -> 2) should ((not contain value (5)) and (not contain value (3))) 622 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (5) and not contain value (3)) 623 } 624 625 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 626 mutable.HashMap("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (3) }} 627 mutable.HashMap("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (3))) 628 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (3)) 629 } 630 631 it("should throw TestFailedException if map does not contain the specified value") { 632 val caught1 = intercept[TestFailedException] { 633 mutable.HashMap("one" -> 1, "two" -> 2) should contain value (3) 634 } 635 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3") 636 } 637 638 it("should throw TestFailedException if contains the specified value when used with not") { 639 640 val caught1 = intercept[TestFailedException] { 641 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (2)) 642 } 643 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2") 644 645 val caught2 = intercept[TestFailedException] { 646 mutable.HashMap("one" -> 1, "two" -> 2) should not (contain value (2)) 647 } 648 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2") 649 650 val caught3 = intercept[TestFailedException] { 651 mutable.HashMap("one" -> 1, "two" -> 2) should not contain value (2) 652 } 653 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2") 654 } 655 656 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 657 658 val caught1 = intercept[TestFailedException] { 659 mutable.HashMap("one" -> 1, "two" -> 2) should { contain value (5) and (contain value (2)) } 660 } 661 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 662 663 val caught2 = intercept[TestFailedException] { 664 mutable.HashMap("one" -> 1, "two" -> 2) should ((contain value (5)) and (contain value (2))) 665 } 666 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 667 668 val caught3 = intercept[TestFailedException] { 669 mutable.HashMap("one" -> 1, "two" -> 2) should (contain value (5) and contain value (2)) 670 } 671 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 5") 672 } 673 674 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 675 676 val caught1 = intercept[TestFailedException] { 677 mutable.HashMap("one" -> 1, "two" -> 2) should { contain value (55) or (contain value (22)) } 678 } 679 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 680 681 val caught2 = intercept[TestFailedException] { 682 mutable.HashMap("one" -> 1, "two" -> 2) should ((contain value (55)) or (contain value (22))) 683 } 684 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 685 686 val caught3 = intercept[TestFailedException] { 687 mutable.HashMap("one" -> 1, "two" -> 2) should (contain value (55) or contain value (22)) 688 } 689 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 55, and Map(one -> 1, two -> 2) did not contain value 22") 690 } 691 692 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 693 694 val caught1 = intercept[TestFailedException] { 695 mutable.HashMap("one" -> 1, "two" -> 2) should { not { contain value (3) } and not { contain value (2) }} 696 } 697 assert(caught1.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 698 699 val caught2 = intercept[TestFailedException] { 700 mutable.HashMap("one" -> 1, "two" -> 2) should ((not contain value (3)) and (not contain value (2))) 701 } 702 assert(caught2.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 703 704 val caught3 = intercept[TestFailedException] { 705 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (3) and not contain value (2)) 706 } 707 assert(caught3.getMessage === "Map(one -> 1, two -> 2) did not contain value 3, but Map(one -> 1, two -> 2) contained value 2") 708 } 709 710 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 711 712 val caught1 = intercept[TestFailedException] { 713 mutable.HashMap("one" -> 1, "two" -> 2) should { not { contain value (2) } or not { contain value (2) }} 714 } 715 assert(caught1.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 716 717 val caught2 = intercept[TestFailedException] { 718 mutable.HashMap("one" -> 1, "two" -> 2) should ((not contain value (2)) or (not contain value (2))) 719 } 720 assert(caught2.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 721 722 val caught3 = intercept[TestFailedException] { 723 mutable.HashMap("one" -> 1, "two" -> 2) should (not contain value (2) or not contain value (2)) 724 } 725 assert(caught3.getMessage === "Map(one -> 1, two -> 2) contained value 2, and Map(one -> 1, two -> 2) contained value 2") 726 } 727 } 728 729 describe("on java.util.Map") { 730 731 val javaMap: java.util.Map[String, Int] = new java.util.HashMap 732 javaMap.put("one",1) 733 javaMap.put("two", 2) 734 735 it("should do nothing if map contains specified value") { 736 javaMap should contain value (2) 737 javaMap should (contain value (2)) 738 } 739 740 it("should do nothing if map does not contain the specified value and used with not") { 741 javaMap should not { contain value (3) } 742 javaMap should not contain value (3) 743 javaMap should (not contain value (3)) 744 } 745 746 it("should do nothing when map contains specified value and used in a logical-and expression") { 747 javaMap should { contain value (2) and (contain value (1)) } 748 javaMap should ((contain value (2)) and (contain value (1))) 749 javaMap should (contain value (2) and contain value (1)) 750 } 751 752 it("should do nothing when map contains specified value and used in a logical-or expression") { 753 javaMap should { contain value (9) or (contain value (1)) } 754 javaMap should ((contain value (9)) or (contain value (1))) 755 javaMap should (contain value (9) or contain value (1)) 756 } 757 758 it("should do nothing when map does not contain the specified value and used in a logical-and expression with not") { 759 javaMap should { not { contain value (5) } and not { contain value (3) }} 760 javaMap should ((not contain value (5)) and (not contain value (3))) 761 javaMap should (not contain value (5) and not contain value (3)) 762 } 763 764 it("should do nothing when map does not contain the specified value and used in a logical-or expression with not") { 765 javaMap should { not { contain value (2) } or not { contain value (3) }} 766 javaMap should ((not contain value (2)) or (not contain value (3))) 767 javaMap should (not contain value (2) or not contain value (3)) 768 } 769 770 it("should throw TestFailedException if map does not contain the specified value") { 771 val caught1 = intercept[TestFailedException] { 772 javaMap should contain value (3) 773 } 774 caught1.getMessage should (be === "{one=1, two=2} did not contain value 3" or 775 be === "{two=2, one=1} did not contain value 3") 776 } 777 778 it("should throw TestFailedException if contains the specified value when used with not") { 779 780 val caught1 = intercept[TestFailedException] { 781 javaMap should (not contain value (2)) 782 } 783 caught1.getMessage should (be === "{one=1, two=2} contained value 2" or 784 be === "{two=2, one=1} contained value 2") 785 786 val caught2 = intercept[TestFailedException] { 787 javaMap should not (contain value (2)) 788 } 789 caught2.getMessage should (be === "{one=1, two=2} contained value 2" or 790 be === "{two=2, one=1} contained value 2") 791 792 val caught3 = intercept[TestFailedException] { 793 javaMap should not contain value (2) 794 } 795 caught3.getMessage should (be === "{one=1, two=2} contained value 2" or 796 be === "{two=2, one=1} contained value 2") 797 } 798 799 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-and expression") { 800 801 val caught1 = intercept[TestFailedException] { 802 javaMap should { contain value (5) and (contain value (2)) } 803 } 804 caught1.getMessage should (be === "{one=1, two=2} did not contain value 5" or 805 be === "{two=2, one=1} did not contain value 5") 806 807 val caught2 = intercept[TestFailedException] { 808 javaMap should ((contain value (5)) and (contain value (2))) 809 } 810 caught2.getMessage should (be === "{one=1, two=2} did not contain value 5" or 811 be === "{two=2, one=1} did not contain value 5") 812 813 val caught3 = intercept[TestFailedException] { 814 javaMap should (contain value (5) and contain value (2)) 815 } 816 caught3.getMessage should (be === "{one=1, two=2} did not contain value 5" or 817 be === "{two=2, one=1} did not contain value 5") 818 } 819 820 it("should throw an TestFailedException when map doesn't contain specified value and used in a logical-or expression") { 821 822 val caught1 = intercept[TestFailedException] { 823 javaMap should { contain value (55) or (contain value (22)) } 824 } 825 caught1.getMessage should (be === "{one=1, two=2} did not contain value 55, and {one=1, two=2} did not contain value 22" or 826 be === "{two=2, one=1} did not contain value 55, and {two=2, one=1} did not contain value 22") 827 828 val caught2 = intercept[TestFailedException] { 829 javaMap should ((contain value (55)) or (contain value (22))) 830 } 831 caught2.getMessage should (be === "{one=1, two=2} did not contain value 55, and {one=1, two=2} did not contain value 22" or 832 be === "{two=2, one=1} did not contain value 55, and {two=2, one=1} did not contain value 22") 833 834 val caught3 = intercept[TestFailedException] { 835 javaMap should (contain value (55) or contain value (22)) 836 } 837 caught3.getMessage should (be === "{one=1, two=2} did not contain value 55, and {one=1, two=2} did not contain value 22" or 838 be === "{two=2, one=1} did not contain value 55, and {two=2, one=1} did not contain value 22") 839 } 840 841 it("should throw an TestFailedException when map contains specified value and used in a logical-and expression with not") { 842 843 val caught1 = intercept[TestFailedException] { 844 javaMap should { not { contain value (3) } and not { contain value (2) }} 845 } 846 caught1.getMessage should (be === "{one=1, two=2} did not contain value 3, but {one=1, two=2} contained value 2" or 847 be === "{two=2, one=1} did not contain value 3, but {two=2, one=1} contained value 2") 848 849 val caught2 = intercept[TestFailedException] { 850 javaMap should ((not contain value (3)) and (not contain value (2))) 851 } 852 caught2.getMessage should (be === "{one=1, two=2} did not contain value 3, but {one=1, two=2} contained value 2" or 853 be === "{two=2, one=1} did not contain value 3, but {two=2, one=1} contained value 2") 854 855 val caught3 = intercept[TestFailedException] { 856 javaMap should (not contain value (3) and not contain value (2)) 857 } 858 caught3.getMessage should (be === "{one=1, two=2} did not contain value 3, but {one=1, two=2} contained value 2" or 859 be === "{two=2, one=1} did not contain value 3, but {two=2, one=1} contained value 2") 860 } 861 862 it("should throw an TestFailedException when map contains specified value and used in a logical-or expression with not") { 863 864 val caught1 = intercept[TestFailedException] { 865 javaMap should { not { contain value (2) } or not { contain value (2) }} 866 } 867 caught1.getMessage should (be === "{one=1, two=2} contained value 2, and {one=1, two=2} contained value 2" or 868 be === "{two=2, one=1} contained value 2, and {two=2, one=1} contained value 2") 869 870 val caught2 = intercept[TestFailedException] { 871 javaMap should ((not contain value (2)) or (not contain value (2))) 872 } 873 caught2.getMessage should (be === "{one=1, two=2} contained value 2, and {one=1, two=2} contained value 2" or 874 be === "{two=2, one=1} contained value 2, and {two=2, one=1} contained value 2") 875 876 val caught3 = intercept[TestFailedException] { 877 javaMap should (not contain value (2) or not contain value (2)) 878 } 879 caught3.getMessage should (be === "{one=1, two=2} contained value 2, and {one=1, two=2} contained value 2" or 880 be === "{two=2, one=1} contained value 2, and {two=2, one=1} contained value 2") 881 } 882 } 883 } 884} 885