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