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._ 19 20class ShouldBeSymbolSpec extends Spec with ShouldMatchers with EmptyMocks { 21 22 describe("The be ('symbol) syntax") { 23 24 it("should do nothing if the object has an appropriately named method, which returns true") { 25 emptyMock should be ('empty) 26 isEmptyMock should be ('empty) 27 } 28 29 it("should throw TestFailedException if no <symbol> or is<Symbol> method exists") { 30 val ex1 = intercept[TestFailedException] { 31 noPredicateMock should be ('empty) 32 } 33 ex1.getMessage should equal ("NoPredicateMock has neither an empty nor an isEmpty method") 34 // Check message for name that starts with a consonant (should use a instead of an) 35 val ex2 = intercept[TestFailedException] { 36 noPredicateMock should be ('full) 37 } 38 ex2.getMessage should equal ("NoPredicateMock has neither a full nor an isFull method") 39 } 40 41 it("should do nothing if the object has an appropriately named method, which returns true, even if the method contains operator characters") { 42 43 val opNames = new OperatorNames 44 45 opNames should be ('op_21_!) 46 opNames should be ('op_23_#) 47 opNames should be ('op_25_%) 48 opNames should be ('op_26_&) 49 opNames should be ('op_2a_*) 50 opNames should be ('op_2b_+) 51 opNames should be ('op_2d_-) 52 opNames should be ('op_2f_/) 53 opNames should be ('op_3a_:) 54 opNames should be ('op_3c_<) 55 opNames should be ('op_3d_=) 56 opNames should be ('op_3e_>) 57 opNames should be ('op_3f_?) 58 opNames should be ('op_40_@) 59 opNames should be ('op_5c_\) 60 opNames should be ('op_5e_^) 61 opNames should be ('op_7c_|) 62 opNames should be ('op_7e_~) 63 64 opNames should be (Symbol("!!!")) 65 opNames should be (Symbol("###")) 66 opNames should be (Symbol("%%%")) 67 opNames should be (Symbol("&&&")) 68 opNames should be (Symbol("***")) 69 opNames should be (Symbol("+++")) 70 opNames should be (Symbol("---")) 71 opNames should be (Symbol("/")) 72 opNames should be (Symbol(":::")) 73 opNames should be (Symbol("<<<")) 74 opNames should be (Symbol("===")) 75 opNames should be (Symbol(">>>")) 76 opNames should be (Symbol("???")) 77 opNames should be (Symbol("@@@")) 78 opNames should be (Symbol("\\\\\\")) 79 opNames should be (Symbol("^^^")) 80 opNames should be (Symbol("|||")) 81 opNames should be (Symbol("~~~")) 82 } 83 84 it("should do nothing if the object has an appropriately named method, which returns false when used with not") { 85 notEmptyMock should not { be ('empty) } 86 notEmptyMock should not be ('empty) 87 isNotEmptyMock should not { be ('empty) } 88 isNotEmptyMock should not be ('empty) 89 } 90 91 it("should throw TestFailedException if no <symbol> or is<Symbol> method exists, when used with not") { 92 val ex1 = intercept[TestFailedException] { 93 noPredicateMock should not { be ('empty) } 94 } 95 ex1.getMessage should equal ("NoPredicateMock has neither an empty nor an isEmpty method") 96 val ex2 = intercept[TestFailedException] { 97 noPredicateMock should not (be ('full)) 98 } 99 ex2.getMessage should equal ("NoPredicateMock has neither a full nor an isFull method") 100 val ex3 = intercept[TestFailedException] { 101 noPredicateMock should not be ('empty) 102 } 103 ex3.getMessage should equal ("NoPredicateMock has neither an empty nor an isEmpty method") 104 val ex4 = intercept[TestFailedException] { 105 noPredicateMock should not be ('full) 106 } 107 ex4.getMessage should equal ("NoPredicateMock has neither a full nor an isFull method") 108 } 109 110 it("should do nothing if the object has an appropriately named method, which returns true, when used in a logical-and expression") { 111 emptyMock should ((be ('empty)) and (be ('empty))) 112 emptyMock should (be ('empty) and (be ('empty))) 113 emptyMock should (be ('empty) and be ('empty)) 114 isEmptyMock should ((be ('empty)) and (be ('empty))) 115 isEmptyMock should (be ('empty) and (be ('empty))) 116 isEmptyMock should (be ('empty) and be ('empty)) 117 } 118 119 it("should do nothing if the object has an appropriately named method, which returns true, when used in a logical-or expression") { 120 121 emptyMock should ((be ('full)) or (be ('empty))) 122 emptyMock should (be ('full) or (be ('empty))) 123 emptyMock should (be ('full) or be ('empty)) 124 isEmptyMock should ((be ('full)) or (be ('empty))) 125 isEmptyMock should (be ('full) or (be ('empty))) 126 isEmptyMock should (be ('full) or be ('empty)) 127 128 emptyMock should ((be ('empty)) or (be ('full))) 129 emptyMock should (be ('empty) or (be ('full))) 130 emptyMock should (be ('empty) or be ('full)) 131 isEmptyMock should ((be ('empty)) or (be ('full))) 132 isEmptyMock should (be ('empty) or (be ('full))) 133 isEmptyMock should (be ('empty) or be ('full)) 134 } 135 136 it("should do nothing if the object has an appropriately named method, which returns false, when used in a logical-and expression with not") { 137 138 notEmptyMock should (not (be ('empty)) and not (be ('empty))) 139 notEmptyMock should ((not be ('empty)) and (not be ('empty))) 140 notEmptyMock should (not be ('empty) and not be ('empty)) 141 142 isNotEmptyMock should (not (be ('empty)) and not (be ('empty))) 143 isNotEmptyMock should ((not be ('empty)) and (not be ('empty))) 144 isNotEmptyMock should (not be ('empty) and not be ('empty)) 145 } 146 147 it("should do nothing if the object has an appropriately named method, which returns false, when used in a logical-or expression with not") { 148 149 notEmptyMock should (not (be ('empty)) or not (be ('empty))) 150 notEmptyMock should ((not be ('empty)) or (not be ('empty))) 151 notEmptyMock should (not be ('empty) or not be ('empty)) 152 153 isNotEmptyMock should (not (be ('empty)) or not (be ('empty))) 154 isNotEmptyMock should ((not be ('empty)) or (not be ('empty))) 155 isNotEmptyMock should (not be ('empty) or not be ('empty)) 156 157 notEmptyMock should (not (be ('full)) or not (be ('empty))) 158 notEmptyMock should ((not be ('full)) or (not be ('empty))) 159 notEmptyMock should (not be ('full) or not be ('empty)) 160 161 isNotEmptyMock should (not (be ('full)) or not (be ('empty))) 162 isNotEmptyMock should ((not be ('full)) or (not be ('empty))) 163 isNotEmptyMock should (not be ('full) or not be ('empty)) 164 } 165 166 it("should throw TestFailedException if the object has an appropriately named method, which returns false") { 167 val caught1 = intercept[TestFailedException] { 168 notEmptyMock should be ('empty) 169 } 170 assert(caught1.getMessage === "NotEmptyMock was not empty") 171 val caught2 = intercept[TestFailedException] { 172 isNotEmptyMock should be ('empty) 173 } 174 assert(caught2.getMessage === "IsNotEmptyMock was not empty") 175 } 176 177 it("should throw TestFailedException if the object has an appropriately named method, which returns true when used with not") { 178 val caught1 = intercept[TestFailedException] { 179 emptyMock should not { be ('empty) } 180 } 181 assert(caught1.getMessage === "EmptyMock was empty") 182 val caught2 = intercept[TestFailedException] { 183 emptyMock should not be ('empty) 184 } 185 assert(caught2.getMessage === "EmptyMock was empty") 186 val caught3 = intercept[TestFailedException] { 187 isEmptyMock should not { be ('empty) } 188 } 189 assert(caught3.getMessage === "IsEmptyMock was empty") 190 val caught4 = intercept[TestFailedException] { 191 isEmptyMock should not be ('empty) 192 } 193 assert(caught4.getMessage === "IsEmptyMock was empty") 194 } 195 196 it("should throw TestFailedException if the object has an appropriately named method, which returns false, when used in a logical-and expression") { 197 val caught1 = intercept[TestFailedException] { 198 emptyMock should ((be ('empty)) and (be ('full))) 199 } 200 assert(caught1.getMessage === "EmptyMock was empty, but EmptyMock was not full") 201 val caught2 = intercept[TestFailedException] { 202 emptyMock should (be ('empty) and (be ('full))) 203 } 204 assert(caught2.getMessage === "EmptyMock was empty, but EmptyMock was not full") 205 val caught3 = intercept[TestFailedException] { 206 emptyMock should (be ('empty) and be ('full)) 207 } 208 assert(caught3.getMessage === "EmptyMock was empty, but EmptyMock was not full") 209 val caught4 = intercept[TestFailedException] { 210 isEmptyMock should ((be ('empty)) and (be ('full))) 211 } 212 assert(caught4.getMessage === "IsEmptyMock was empty, but IsEmptyMock was not full") 213 val caught5 = intercept[TestFailedException] { 214 isEmptyMock should (be ('empty) and (be ('full))) 215 } 216 assert(caught5.getMessage === "IsEmptyMock was empty, but IsEmptyMock was not full") 217 val caught6 = intercept[TestFailedException] { 218 isEmptyMock should (be ('empty) and be ('full)) 219 } 220 assert(caught6.getMessage === "IsEmptyMock was empty, but IsEmptyMock was not full") 221 } 222 223 it("should throw TestFailedException if the object has an appropriately named method, which returns false, when used in a logical-or expression") { 224 225 val caught1 = intercept[TestFailedException] { 226 notEmptyMock should ((be ('empty)) or (be ('empty))) 227 } 228 assert(caught1.getMessage === "NotEmptyMock was not empty, and NotEmptyMock was not empty") 229 val caught2 = intercept[TestFailedException] { 230 notEmptyMock should (be ('empty) or (be ('empty))) 231 } 232 assert(caught2.getMessage === "NotEmptyMock was not empty, and NotEmptyMock was not empty") 233 val caught3 = intercept[TestFailedException] { 234 notEmptyMock should (be ('empty) or be ('empty)) 235 } 236 assert(caught3.getMessage === "NotEmptyMock was not empty, and NotEmptyMock was not empty") 237 val caught4 = intercept[TestFailedException] { 238 isNotEmptyMock should ((be ('empty)) or (be ('empty))) 239 } 240 assert(caught4.getMessage === "IsNotEmptyMock was not empty, and IsNotEmptyMock was not empty") 241 val caught5 = intercept[TestFailedException] { 242 isNotEmptyMock should (be ('empty) or (be ('empty))) 243 } 244 assert(caught5.getMessage === "IsNotEmptyMock was not empty, and IsNotEmptyMock was not empty") 245 val caught6 = intercept[TestFailedException] { 246 isNotEmptyMock should (be ('empty) or be ('empty)) 247 } 248 assert(caught6.getMessage === "IsNotEmptyMock was not empty, and IsNotEmptyMock was not empty") 249 } 250 251 it("should throw TestFailedException if the object has an appropriately named method, which returns true, when used in a logical-and expression with not") { 252 253 val caught1 = intercept[TestFailedException] { 254 emptyMock should (not (be ('full)) and not (be ('empty))) 255 } 256 assert(caught1.getMessage === "EmptyMock was not full, but EmptyMock was empty") 257 val caught2 = intercept[TestFailedException] { 258 emptyMock should ((not be ('full)) and (not be ('empty))) 259 } 260 assert(caught2.getMessage === "EmptyMock was not full, but EmptyMock was empty") 261 val caught3 = intercept[TestFailedException] { 262 emptyMock should (not be ('full) and not be ('empty)) 263 } 264 assert(caught3.getMessage === "EmptyMock was not full, but EmptyMock was empty") 265 val caught4 = intercept[TestFailedException] { 266 isEmptyMock should (not (be ('full)) and not (be ('empty))) 267 } 268 assert(caught4.getMessage === "IsEmptyMock was not full, but IsEmptyMock was empty") 269 val caught5 = intercept[TestFailedException] { 270 isEmptyMock should ((not be ('full)) and (not be ('empty))) 271 } 272 assert(caught5.getMessage === "IsEmptyMock was not full, but IsEmptyMock was empty") 273 val caught6 = intercept[TestFailedException] { 274 isEmptyMock should (not be ('full) and not be ('empty)) 275 } 276 assert(caught6.getMessage === "IsEmptyMock was not full, but IsEmptyMock was empty") 277 // Check that the error message "short circuits" 278 val caught7 = intercept[TestFailedException] { 279 emptyMock should (not (be ('empty)) and not (be ('full))) 280 } 281 assert(caught7.getMessage === "EmptyMock was empty") 282 } 283 284 it("should throw TestFailedException if the object has an appropriately named method, which returns true, when used in a logical-or expression with not") { 285 286 val caught1 = intercept[TestFailedException] { 287 emptyMock should (not (be ('empty)) or not (be ('empty))) 288 } 289 assert(caught1.getMessage === "EmptyMock was empty, and EmptyMock was empty") 290 val caught2 = intercept[TestFailedException] { 291 emptyMock should ((not be ('empty)) or (not be ('empty))) 292 } 293 assert(caught2.getMessage === "EmptyMock was empty, and EmptyMock was empty") 294 val caught3 = intercept[TestFailedException] { 295 emptyMock should (not be ('empty) or not be ('empty)) 296 } 297 assert(caught3.getMessage === "EmptyMock was empty, and EmptyMock was empty") 298 val caught4 = intercept[TestFailedException] { 299 isEmptyMock should (not (be ('empty)) or not (be ('empty))) 300 } 301 assert(caught4.getMessage === "IsEmptyMock was empty, and IsEmptyMock was empty") 302 val caught5 = intercept[TestFailedException] { 303 isEmptyMock should ((not be ('empty)) or (not be ('empty))) 304 } 305 assert(caught5.getMessage === "IsEmptyMock was empty, and IsEmptyMock was empty") 306 val caught6 = intercept[TestFailedException] { 307 isEmptyMock should (not be ('empty) or not be ('empty)) 308 } 309 assert(caught6.getMessage === "IsEmptyMock was empty, and IsEmptyMock was empty") 310 } 311 312 describe("(for the different types that have implicit conversions for should methods)") { 313 314 // FOR: implicit def convertToCollectionShouldWrapper[T](o: Collection[T])... 315 it("should work on a scala.Collection") { 316 val emptySet = Set[Int]() 317 emptySet should be ('empty) 318 val nonEmptySet = Set(1, 2, 3) 319 nonEmptySet should not { be ('empty) } 320 val caught1 = intercept[TestFailedException] { 321 nonEmptySet should be ('empty) 322 } 323 assert(caught1.getMessage === "Set(1, 2, 3) was not empty") 324 val caught2 = intercept[TestFailedException] { 325 nonEmptySet should not { be ('hasDefiniteSize) } 326 } 327 assert(caught2.getMessage === "Set(1, 2, 3) was hasDefiniteSize") 328 val caught3 = intercept[TestFailedException] { 329 nonEmptySet should not { be ('happy) } 330 } 331 assert(caught3.getMessage === "Set(1, 2, 3) has neither a happy nor an isHappy method") 332 } 333 334 // FOR: implicit def convertToSeqShouldWrapper[T](o: Seq[T])... 335 it("should work on a scala.Seq") { 336 import scala.collection.mutable.ListBuffer 337 val emptyListBuffer = new ListBuffer[Int] 338 emptyListBuffer should be ('empty) 339 val nonEmptyListBuffer = new ListBuffer[Int] 340 nonEmptyListBuffer += 1 341 nonEmptyListBuffer += 2 342 nonEmptyListBuffer += 3 343 nonEmptyListBuffer should not { be ('empty) } 344 val caught1 = intercept[TestFailedException] { 345 nonEmptyListBuffer should be ('empty) 346 } 347 assert(caught1.getMessage === "ListBuffer(1, 2, 3) was not empty") 348 val caught2 = intercept[TestFailedException] { 349 nonEmptyListBuffer should not { be ('hasDefiniteSize) } 350 } 351 assert(caught2.getMessage === "ListBuffer(1, 2, 3) was hasDefiniteSize") 352 val caught3 = intercept[TestFailedException] { 353 nonEmptyListBuffer should not { be ('happy) } 354 } 355 assert(caught3.getMessage === "ListBuffer(1, 2, 3) has neither a happy nor an isHappy method") 356 } 357 358 // implicit def convertToArrayShouldWrapper[T](o: Array[T]): ArrayShouldWrapper[T] = new ArrayShouldWrapper[T](o) 359/* This no longer works as of Scala 2.8 360 it("should work on a scala.Array") { 361 val emptyArray = new Array[Int](0) 362 emptyArray should be ('empty) 363 val nonEmptyArray = Array(1, 2, 3) 364 nonEmptyArray should not be ('empty) 365 val caught1 = intercept[TestFailedException] { 366 nonEmptyArray should be ('empty) 367 } 368 assert(caught1.getMessage === "Array(1, 2, 3) was not empty") 369 val caught2 = intercept[TestFailedException] { 370 nonEmptyArray should not { be ('hasDefiniteSize) } 371 } 372 assert(caught2.getMessage === "Array(1, 2, 3) was hasDefiniteSize") 373 val caught3 = intercept[TestFailedException] { 374 nonEmptyArray should not { be ('happy) } 375 } 376 assert(caught3.getMessage === "Array(1, 2, 3) has neither a happy nor an isHappy method") 377 } 378*/ 379 380 // FOR: implicit def convertToListShouldWrapper[T](o: List[T])... 381 it("should work on a scala.List") { 382 val emptyList = List[Int]() 383 emptyList should be ('empty) 384 val nonEmptyList = List(1, 2, 3) 385 nonEmptyList should not { be ('empty) } 386 val caught1 = intercept[TestFailedException] { 387 nonEmptyList should be ('empty) 388 } 389 assert(caught1.getMessage === "List(1, 2, 3) was not empty") 390 val caught2 = intercept[TestFailedException] { 391 nonEmptyList should not { be ('hasDefiniteSize) } 392 } 393 assert(caught2.getMessage === "List(1, 2, 3) was hasDefiniteSize") 394 val caught3 = intercept[TestFailedException] { 395 nonEmptyList should not { be ('happy) } 396 } 397 assert(caught3.getMessage === "List(1, 2, 3) has neither a happy nor an isHappy method") 398 } 399 400 // implicit def convertToMapShouldWrapper[K, V](o: scala.collection.Map[K, V])... 401 it("should work on a scala.Map") { 402 val emptyMap = Map[Int, String]() 403 emptyMap should be ('empty) 404 val nonEmptyMap = Map("one" -> 1, "two" -> 2, "three" -> 3) 405 nonEmptyMap should not { be ('empty) } 406 val caught1 = intercept[TestFailedException] { 407 nonEmptyMap should be ('empty) 408 } 409 assert(caught1.getMessage === "Map(one -> 1, two -> 2, three -> 3) was not empty") 410 val caught2 = intercept[TestFailedException] { 411 nonEmptyMap should not { be ('hasDefiniteSize) } 412 } 413 assert(caught2.getMessage === "Map(one -> 1, two -> 2, three -> 3) was hasDefiniteSize") 414 val caught3 = intercept[TestFailedException] { 415 nonEmptyMap should not { be ('happy) } 416 } 417 assert(caught3.getMessage === "Map(one -> 1, two -> 2, three -> 3) has neither a happy nor an isHappy method") 418 } 419 420 // implicit def convertToStringShouldWrapper[K, V](o: String): StringShouldWrapper = new StringShouldWrapper(o) 421 it("should work on a String") { 422 val caught3 = intercept[TestFailedException] { 423 "howdy" should not be ('happy) 424 } 425 assert(caught3.getMessage === "\"howdy\" has neither a happy nor an isHappy method") 426 } 427 428 // FOR: implicit def convertToJavaCollectionShouldWrapper[T](o: java.util.Collection[T])... 429 it("should work on a java.util.Collection") { 430 val emptySet = new java.util.HashSet[Int] 431 emptySet should be ('empty) 432 val nonEmptySet = new java.util.HashSet[Int] 433 nonEmptySet.add(1) 434 nonEmptySet.add(2) 435 nonEmptySet.add(3) 436 nonEmptySet should not { be ('empty) } 437 val caught1 = intercept[TestFailedException] { 438 nonEmptySet should be ('empty) 439 } 440 assert(caught1.getMessage endsWith "] was not empty") 441 val caught3 = intercept[TestFailedException] { 442 nonEmptySet should not { be ('happy) } 443 } 444 assert(caught3.getMessage endsWith "] has neither a happy nor an isHappy method") 445 } 446 447 // FOR: implicit def convertToJavaListShouldWrapper[T](o: java.util.List[T])... 448 it("should work on a java.util.List") { 449 val emptyList = new java.util.ArrayList[Int] 450 emptyList should be ('empty) 451 val nonEmptyList = new java.util.ArrayList[Int] 452 nonEmptyList.add(1) 453 nonEmptyList.add(2) 454 nonEmptyList.add(3) 455 nonEmptyList should not { be ('empty) } 456 val caught1 = intercept[TestFailedException] { 457 nonEmptyList should be ('empty) 458 } 459 assert(caught1.getMessage === "[1, 2, 3] was not empty") 460 val caught3 = intercept[TestFailedException] { 461 nonEmptyList should not { be ('happy) } 462 } 463 assert(caught3.getMessage === "[1, 2, 3] has neither a happy nor an isHappy method") 464 } 465 } 466 } 467 468 describe("The be matcher") { 469 470 describe("(for symbols)") { 471 472 // TODO: Make sure to write a test for each conversion, because some are using ShouldMethodsForAny instead 473 // of ShouldMethodsForAnyRef. 474 it("should be invokable from be a Symbol and be an Symbol") { 475 val emptySet = Set() 476 emptySet should be a ('empty) 477 emptySet should be an ('empty) 478 val nonEmptySet = Set(1, 2, 3) 479 nonEmptySet should not { be a ('empty) } 480 nonEmptySet should not { be an ('empty) } 481 } 482 483 it("should call empty when passed 'empty") { 484 class EmptyMock { 485 def empty: Boolean = true 486 } 487 class NonEmptyMock { 488 def empty: Boolean = false 489 } 490 (new EmptyMock) should be ('empty) 491 (new NonEmptyMock) should not { be ('empty) } 492 // (new NonEmptyMock) shouldNot be ('empty) 493 } 494 495// STOLE FROM HERE 496 it("should call the Scala=style method if both an empty and an isEmpty method exist") { 497 class EmptyMock { 498 def empty: Boolean = true 499 def isEmpty: Boolean = false 500 override def toString = "EmptyMock" 501 } 502 class NonEmptyMock { 503 def empty: Boolean = false 504 def isEmpty: Boolean = true 505 override def toString = "NonEmptyMock" 506 } 507 (new EmptyMock) should be ('empty) 508 (new NonEmptyMock) should not { be ('empty) } 509 } 510 511 it("should access an 'empty' val when passed 'empty") { 512 class EmptyMock { 513 val empty: Boolean = true 514 } 515 class NonEmptyMock { 516 val empty: Boolean = false 517 } 518 (new EmptyMock) should be ('empty) 519 (new NonEmptyMock) should not { be ('empty) } 520 } 521 } 522 } 523 524 describe("the be ('empty) syntax") { 525 526 it("should call isEmpty") { 527 val emptySet = Set[Int]() 528 emptySet should be ('empty) 529 val nonEmptySet = Set(1, 2, 3) 530 nonEmptySet should not { be ('empty) } 531 } 532 533 it("should call empty when passed 'empty") { 534 class EmptyMock { 535 def empty: Boolean = true 536 } 537 class NonEmptyMock { 538 def empty: Boolean = false 539 } 540 (new EmptyMock) should be ('empty) 541 (new NonEmptyMock) should not { be ('empty) } 542 // (new NonEmptyMock) shouldNot be ('empty) 543 } 544 545 it("should throw TestFailedException if no empty or isEmpty method") { 546 class EmptyMock { 547 override def toString = "EmptyMock" 548 } 549 class NonEmptyMock { 550 override def toString = "NonEmptyMock" 551 } 552 val ex1 = intercept[TestFailedException] { 553 (new EmptyMock) should be ('empty) 554 } 555 ex1.getMessage should equal ("EmptyMock has neither an empty nor an isEmpty method") 556 val ex2 = intercept[TestFailedException] { 557 (new NonEmptyMock) should not { be ('empty) } 558 } 559 ex2.getMessage should equal ("NonEmptyMock has neither an empty nor an isEmpty method") 560 } 561 562 it("should call the Scala-style method if both an empty and an isEmpty method exist") { 563 class EmptyMock { 564 def empty: Boolean = true 565 def isEmpty: Boolean = false 566 override def toString = "EmptyMock" 567 } 568 class NonEmptyMock { 569 def empty: Boolean = false 570 def isEmpty: Boolean = true 571 override def toString = "NonEmptyMock" 572 } 573 (new EmptyMock) should be ('empty) 574 (new NonEmptyMock) should not { be ('empty) } 575 } 576 577 it("should access an 'empty' val when passed 'empty") { 578 class EmptyMock { 579 val empty: Boolean = true 580 } 581 class NonEmptyMock { 582 val empty: Boolean = false 583 } 584 (new EmptyMock) should be ('empty) 585 (new NonEmptyMock) should not { be ('empty) } 586 // (new NonEmptyMock) shouldNot be ('empty) 587 } 588 } 589 590 describe("The be 'defined syntax") { 591 592 it("should do nothing when used with a Some") { 593 val someString: Some[String] = Some("hi") 594 someString should be ('defined) 595 val optionString: Option[String] = Some("hi") 596 optionString should be ('defined) 597 } 598 599 it("should throw TestFailedException when used with a None") { 600 val none: None.type = None 601 val caught1 = intercept[TestFailedException] { 602 none should be ('defined) 603 } 604 assert(caught1.getMessage === "None was not defined") 605 val option: Option[Int] = None 606 val caught2 = intercept[TestFailedException] { 607 option should be ('defined) 608 } 609 assert(caught2.getMessage === "None was not defined") 610 } 611 612 it("should call defined") { 613 class DefinedMock { 614 def defined: Boolean = true 615 } 616 class NonDefinedMock { 617 def defined: Boolean = false 618 } 619 (new DefinedMock) should be ('defined) 620 (new NonDefinedMock) should not { be ('defined) } 621 // (new NonDefinedMock) shouldNot be ('defined) 622 } 623 624 it("should throw TestFailedException if no defined or isDefined method") { 625 class DefinedMock { 626 override def toString = "DefinedMock" 627 } 628 class NonDefinedMock { 629 override def toString = "NonDefinedMock" 630 } 631 val ex1 = intercept[TestFailedException] { 632 (new DefinedMock) should be ('defined) 633 } 634 ex1.getMessage should equal ("DefinedMock has neither a defined nor an isDefined method") 635 val ex2 = intercept[TestFailedException] { 636 (new NonDefinedMock) should not { be ('defined) } 637 } 638 ex2.getMessage should equal ("NonDefinedMock has neither a defined nor an isDefined method") 639 } 640 641 it("should call the Scala-style method if both a defined and an isDefined method exist") { 642 class DefinedMock { 643 def defined: Boolean = true 644 def isDefined: Boolean = false 645 override def toString = "DefinedMock" 646 } 647 class NonDefinedMock { 648 def defined: Boolean = false 649 def isDefined: Boolean = true 650 override def toString = "NonDefinedMock" 651 } 652 (new DefinedMock) should be ('defined) 653 (new NonDefinedMock) should not { be ('defined) } 654 } 655 656 it("should access an 'defined' val") { 657 class DefinedMock { 658 val defined: Boolean = true 659 } 660 class NonDefinedMock { 661 val defined: Boolean = false 662 } 663 (new DefinedMock) should be ('defined) 664 (new NonDefinedMock) should not { be ('defined) } 665 // (new NonDefinedMock) shouldNot be ('defined) 666 } 667 } 668} 669