1/* 2 * Copyright 2001-2009 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.fixture 17 18import org.scalatest._ 19import events.TestFailed 20 21class FixtureWordSpecSpec extends org.scalatest.Spec with PrivateMethodTester with SharedHelpers { 22 23 describe("A FixtureWordSpec") { 24 25 it("should return the test names in order of registration from testNames") { 26 val a = new FixtureWordSpec { 27 type FixtureParam = String 28 def withFixture(test: OneArgTest) {} 29 "Something" should { 30 "do that" in { fixture => 31 } 32 "do this" in { fixture => 33 } 34 } 35 } 36 37 expect(List("Something should do that", "Something should do this")) { 38 a.testNames.iterator.toList 39 } 40 41 val b = new FixtureWordSpec { 42 type FixtureParam = String 43 def withFixture(test: OneArgTest) {} 44 } 45 46 expect(List[String]()) { 47 b.testNames.iterator.toList 48 } 49 50 val c = new FixtureWordSpec { 51 type FixtureParam = String 52 def withFixture(test: OneArgTest) {} 53 "Something" should { 54 "do this" in { fixture => 55 } 56 "do that" in { fixture => 57 } 58 } 59 } 60 61 expect(List("Something should do this", "Something should do that")) { 62 c.testNames.iterator.toList 63 } 64 } 65 66 it("should throw DuplicateTestNameException if a duplicate test name registration is attempted") { 67 68 intercept[DuplicateTestNameException] { 69 new FixtureWordSpec { 70 type FixtureParam = String 71 def withFixture(test: OneArgTest) {} 72 "should test this" in { fixture => } 73 "should test this" in { fixture => } 74 } 75 } 76 intercept[DuplicateTestNameException] { 77 new FixtureWordSpec { 78 type FixtureParam = String 79 def withFixture(test: OneArgTest) {} 80 "should test this" in { fixture => } 81 "should test this" ignore { fixture => } 82 } 83 } 84 intercept[DuplicateTestNameException] { 85 new FixtureWordSpec { 86 type FixtureParam = String 87 def withFixture(test: OneArgTest) {} 88 "should test this" ignore { fixture => } 89 "should test this" ignore { fixture => } 90 } 91 } 92 intercept[DuplicateTestNameException] { 93 new FixtureWordSpec { 94 type FixtureParam = String 95 def withFixture(test: OneArgTest) {} 96 "should test this" ignore { fixture => } 97 "should test this" in { fixture => } 98 } 99 } 100 } 101 102 it("should pass in the fixture to every test method") { 103 val a = new FixtureWordSpec { 104 type FixtureParam = String 105 val hello = "Hello, world!" 106 def withFixture(test: OneArgTest) { 107 test(hello) 108 } 109 "Something" should { 110 "do this" in { fixture => 111 assert(fixture === hello) 112 } 113 "do that" in { fixture => 114 assert(fixture === hello) 115 } 116 } 117 } 118 val rep = new EventRecordingReporter 119 a.run(None, rep, new Stopper {}, Filter(), Map(), None, new Tracker()) 120 assert(!rep.eventsReceived.exists(_.isInstanceOf[TestFailed])) 121 } 122 it("should throw NullPointerException if a null test tag is provided") { 123 // it 124 intercept[NullPointerException] { 125 new FixtureWordSpec { 126 type FixtureParam = String 127 def withFixture(test: OneArgTest) {} 128 "hi" taggedAs(null) in { fixture => } 129 } 130 } 131 val caught = intercept[NullPointerException] { 132 new FixtureWordSpec { 133 type FixtureParam = String 134 def withFixture(test: OneArgTest) {} 135 "hi" taggedAs(mytags.SlowAsMolasses, null) in { fixture => } 136 } 137 } 138 assert(caught.getMessage === "a test tag was null") 139 intercept[NullPointerException] { 140 new FixtureWordSpec { 141 type FixtureParam = String 142 def withFixture(test: OneArgTest) {} 143 "hi" taggedAs(mytags.SlowAsMolasses, null, mytags.WeakAsAKitten) in { fixture => } 144 } 145 } 146 // ignore 147 intercept[NullPointerException] { 148 new FixtureWordSpec { 149 type FixtureParam = String 150 def withFixture(test: OneArgTest) {} 151 "hi" taggedAs(null) ignore { fixture => } 152 } 153 } 154 val caught2 = intercept[NullPointerException] { 155 new FixtureWordSpec { 156 type FixtureParam = String 157 def withFixture(test: OneArgTest) {} 158 "hi" taggedAs(mytags.SlowAsMolasses, null) ignore { fixture => } 159 } 160 } 161 assert(caught2.getMessage === "a test tag was null") 162 intercept[NullPointerException] { 163 new FixtureWordSpec { 164 type FixtureParam = String 165 def withFixture(test: OneArgTest) {} 166 "hi" taggedAs(mytags.SlowAsMolasses, null, mytags.WeakAsAKitten) ignore { fixture => } 167 } 168 } 169 } 170 it("should return a correct tags map from the tags method") { 171 172 val a = new FixtureWordSpec { 173 type FixtureParam = String 174 def withFixture(test: OneArgTest) {} 175 "test this" ignore { fixture => } 176 "test that" in { fixture => } 177 } 178 expect(Map("test this" -> Set("org.scalatest.Ignore"))) { 179 a.tags 180 } 181 182 val b = new FixtureWordSpec { 183 type FixtureParam = String 184 def withFixture(test: OneArgTest) {} 185 "test this" in { fixture => } 186 "test that" ignore { fixture => } 187 } 188 expect(Map("test that" -> Set("org.scalatest.Ignore"))) { 189 b.tags 190 } 191 192 val c = new FixtureWordSpec { 193 type FixtureParam = String 194 def withFixture(test: OneArgTest) {} 195 "test this" ignore { fixture => } 196 "test that" ignore { fixture => } 197 } 198 expect(Map("test this" -> Set("org.scalatest.Ignore"), "test that" -> Set("org.scalatest.Ignore"))) { 199 c.tags 200 } 201 202 val d = new FixtureWordSpec { 203 type FixtureParam = String 204 def withFixture(test: OneArgTest) {} 205 "test this" taggedAs(mytags.SlowAsMolasses) in { fixture => } 206 "test that" taggedAs(mytags.SlowAsMolasses) ignore { fixture => } 207 } 208 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses"), "test that" -> Set("org.scalatest.Ignore", "org.scalatest.SlowAsMolasses"))) { 209 d.tags 210 } 211 212 val e = new FixtureWordSpec { 213 type FixtureParam = String 214 def withFixture(test: OneArgTest) {} 215 "test this" in { fixture => } 216 "test that" in { fixture => } 217 } 218 expect(Map()) { 219 e.tags 220 } 221 222 val f = new FixtureWordSpec { 223 type FixtureParam = String 224 def withFixture(test: OneArgTest) {} 225 "test this" taggedAs(mytags.SlowAsMolasses, mytags.WeakAsAKitten) in { fixture => } 226 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => } 227 } 228 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses", "org.scalatest.WeakAsAKitten"), "test that" -> Set("org.scalatest.SlowAsMolasses"))) { 229 f.tags 230 } 231 232 val g = new FixtureWordSpec { 233 type FixtureParam = String 234 def withFixture(test: OneArgTest) {} 235 "test this" taggedAs(mytags.SlowAsMolasses, mytags.WeakAsAKitten) in { fixture => } 236 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => } 237 } 238 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses", "org.scalatest.WeakAsAKitten"), "test that" -> Set("org.scalatest.SlowAsMolasses"))) { 239 g.tags 240 } 241 } 242 it("should return a correct tags map from the tags method using is (pending)") { 243 244 val a = new FixtureWordSpec { 245 type FixtureParam = String 246 def withFixture(test: OneArgTest) {} 247 "test this" ignore { fixture => } 248 "test that" is (pending) 249 } 250 expect(Map("test this" -> Set("org.scalatest.Ignore"))) { 251 a.tags 252 } 253 254 val b = new FixtureWordSpec { 255 type FixtureParam = String 256 def withFixture(test: OneArgTest) {} 257 "test this" is (pending) 258 "test that" ignore { fixture => } 259 } 260 expect(Map("test that" -> Set("org.scalatest.Ignore"))) { 261 b.tags 262 } 263 264 val c = new FixtureWordSpec { 265 type FixtureParam = String 266 def withFixture(test: OneArgTest) {} 267 "test this" ignore { fixture => } 268 "test that" ignore { fixture => } 269 } 270 expect(Map("test this" -> Set("org.scalatest.Ignore"), "test that" -> Set("org.scalatest.Ignore"))) { 271 c.tags 272 } 273 274 val d = new FixtureWordSpec { 275 type FixtureParam = String 276 def withFixture(test: OneArgTest) {} 277 "test this" taggedAs(mytags.SlowAsMolasses) is (pending) 278 "test that" taggedAs(mytags.SlowAsMolasses) ignore { fixture => } 279 } 280 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses"), "test that" -> Set("org.scalatest.Ignore", "org.scalatest.SlowAsMolasses"))) { 281 d.tags 282 } 283 284 val e = new FixtureWordSpec { 285 type FixtureParam = String 286 def withFixture(test: OneArgTest) {} 287 "test this" is (pending) 288 "test that" is (pending) 289 } 290 expect(Map()) { 291 e.tags 292 } 293 294 val f = new FixtureWordSpec { 295 type FixtureParam = String 296 def withFixture(test: OneArgTest) {} 297 "test this" taggedAs(mytags.SlowAsMolasses, mytags.WeakAsAKitten) is (pending) 298 "test that" taggedAs(mytags.SlowAsMolasses) is (pending) 299 } 300 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses", "org.scalatest.WeakAsAKitten"), "test that" -> Set("org.scalatest.SlowAsMolasses"))) { 301 f.tags 302 } 303 304 val g = new FixtureWordSpec { 305 type FixtureParam = String 306 def withFixture(test: OneArgTest) {} 307 "test this" taggedAs(mytags.SlowAsMolasses, mytags.WeakAsAKitten) is (pending) 308 "test that" taggedAs(mytags.SlowAsMolasses) is (pending) 309 } 310 expect(Map("test this" -> Set("org.scalatest.SlowAsMolasses", "org.scalatest.WeakAsAKitten"), "test that" -> Set("org.scalatest.SlowAsMolasses"))) { 311 g.tags 312 } 313 } 314 class TestWasCalledSuite extends FixtureWordSpec { 315 type FixtureParam = String 316 def withFixture(test: OneArgTest) { test("hi") } 317 var theTestThisCalled = false 318 var theTestThatCalled = false 319 "run this" in { fixture => theTestThisCalled = true } 320 "run that, maybe" in { fixture => theTestThatCalled = true } 321 } 322 323 it("should execute all tests when run is called with testName None") { 324 325 val b = new TestWasCalledSuite 326 b.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker) 327 assert(b.theTestThisCalled) 328 assert(b.theTestThatCalled) 329 } 330 331 it("should execute one test when run is called with a defined testName") { 332 333 val a = new TestWasCalledSuite 334 a.run(Some("run this"), SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker) 335 assert(a.theTestThisCalled) 336 assert(!a.theTestThatCalled) 337 } 338 339 it("should report as ignored, and not run, tests marked ignored") { 340 341 val a = new FixtureWordSpec { 342 type FixtureParam = String 343 def withFixture(test: OneArgTest) { test("hi") } 344 var theTestThisCalled = false 345 var theTestThatCalled = false 346 "test this" in { fixture => theTestThisCalled = true } 347 "test that" in { fixture => theTestThatCalled = true } 348 } 349 350 val repA = new TestIgnoredTrackingReporter 351 a.run(None, repA, new Stopper {}, Filter(), Map(), None, new Tracker) 352 assert(!repA.testIgnoredReceived) 353 assert(a.theTestThisCalled) 354 assert(a.theTestThatCalled) 355 356 val b = new FixtureWordSpec { 357 type FixtureParam = String 358 def withFixture(test: OneArgTest) { test("hi") } 359 var theTestThisCalled = false 360 var theTestThatCalled = false 361 "test this" ignore { fixture => theTestThisCalled = true } 362 "test that" in { fixture => theTestThatCalled = true } 363 } 364 365 val repB = new TestIgnoredTrackingReporter 366 b.run(None, repB, new Stopper {}, Filter(), Map(), None, new Tracker) 367 assert(repB.testIgnoredReceived) 368 assert(repB.lastEvent.isDefined) 369 assert(repB.lastEvent.get.testName endsWith "test this") 370 assert(!b.theTestThisCalled) 371 assert(b.theTestThatCalled) 372 373 val c = new FixtureWordSpec { 374 type FixtureParam = String 375 def withFixture(test: OneArgTest) { test("hi") } 376 var theTestThisCalled = false 377 var theTestThatCalled = false 378 "test this" in { fixture => theTestThisCalled = true } 379 "test that" ignore { fixture => theTestThatCalled = true } 380 } 381 382 val repC = new TestIgnoredTrackingReporter 383 c.run(None, repC, new Stopper {}, Filter(), Map(), None, new Tracker) 384 assert(repC.testIgnoredReceived) 385 assert(repC.lastEvent.isDefined) 386 assert(repC.lastEvent.get.testName endsWith "test that", repC.lastEvent.get.testName) 387 assert(c.theTestThisCalled) 388 assert(!c.theTestThatCalled) 389 390 // The order I want is order of appearance in the file. 391 // Will try and implement that tomorrow. Subtypes will be able to change the order. 392 val d = new FixtureWordSpec { 393 type FixtureParam = String 394 def withFixture(test: OneArgTest) { test("hi") } 395 var theTestThisCalled = false 396 var theTestThatCalled = false 397 "test this" ignore { fixture => theTestThisCalled = true } 398 "test that" ignore { fixture => theTestThatCalled = true } 399 } 400 401 val repD = new TestIgnoredTrackingReporter 402 d.run(None, repD, new Stopper {}, Filter(), Map(), None, new Tracker) 403 assert(repD.testIgnoredReceived) 404 assert(repD.lastEvent.isDefined) 405 assert(repD.lastEvent.get.testName endsWith "test that") // last because should be in order of appearance 406 assert(!d.theTestThisCalled) 407 assert(!d.theTestThatCalled) 408 } 409 410 it("should run a test marked as ignored if run is invoked with that testName") { 411 // If I provide a specific testName to run, then it should ignore an Ignore on that test 412 // method and actually invoke it. 413 val e = new FixtureWordSpec { 414 type FixtureParam = String 415 def withFixture(test: OneArgTest) { test("hi") } 416 var theTestThisCalled = false 417 var theTestThatCalled = false 418 "test this" ignore { fixture => theTestThisCalled = true } 419 "test that" in { fixture => theTestThatCalled = true } 420 } 421 422 val repE = new TestIgnoredTrackingReporter 423 e.run(Some("test this"), repE, new Stopper {}, Filter(), Map(), None, new Tracker) 424 assert(!repE.testIgnoredReceived) 425 assert(e.theTestThisCalled) 426 assert(!e.theTestThatCalled) 427 } 428 429 it("should run only those tests selected by the tags to include and exclude sets") { 430 431 // Nothing is excluded 432 val a = new FixtureWordSpec { 433 type FixtureParam = String 434 def withFixture(test: OneArgTest) { test("hi") } 435 var theTestThisCalled = false 436 var theTestThatCalled = false 437 "test this" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThisCalled = true } 438 "test that" in { fixture => theTestThatCalled = true } 439 } 440 val repA = new TestIgnoredTrackingReporter 441 a.run(None, repA, new Stopper {}, Filter(), Map(), None, new Tracker) 442 assert(!repA.testIgnoredReceived) 443 assert(a.theTestThisCalled) 444 assert(a.theTestThatCalled) 445 446 // SlowAsMolasses is included, one test should be excluded 447 val b = new FixtureWordSpec { 448 type FixtureParam = String 449 def withFixture(test: OneArgTest) { test("hi") } 450 var theTestThisCalled = false 451 var theTestThatCalled = false 452 "test this" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThisCalled = true } 453 "test that" in { fixture => theTestThatCalled = true } 454 } 455 val repB = new TestIgnoredTrackingReporter 456 b.run(None, repB, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set()), Map(), None, new Tracker) 457 assert(!repB.testIgnoredReceived) 458 assert(b.theTestThisCalled) 459 assert(!b.theTestThatCalled) 460 461 // SlowAsMolasses is included, and both tests should be included 462 val c = new FixtureWordSpec { 463 type FixtureParam = String 464 def withFixture(test: OneArgTest) { test("hi") } 465 var theTestThisCalled = false 466 var theTestThatCalled = false 467 "test this" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThisCalled = true } 468 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 469 } 470 val repC = new TestIgnoredTrackingReporter 471 c.run(None, repB, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set()), Map(), None, new Tracker) 472 assert(!repC.testIgnoredReceived) 473 assert(c.theTestThisCalled) 474 assert(c.theTestThatCalled) 475 476 // SlowAsMolasses is included. both tests should be included but one ignored 477 val d = new FixtureWordSpec { 478 type FixtureParam = String 479 def withFixture(test: OneArgTest) { test("hi") } 480 var theTestThisCalled = false 481 var theTestThatCalled = false 482 "test this" taggedAs(mytags.SlowAsMolasses) ignore { fixture => theTestThisCalled = true } 483 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 484 } 485 val repD = new TestIgnoredTrackingReporter 486 d.run(None, repD, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.Ignore")), Map(), None, new Tracker) 487 assert(repD.testIgnoredReceived) 488 assert(!d.theTestThisCalled) 489 assert(d.theTestThatCalled) 490 491 // SlowAsMolasses included, FastAsLight excluded 492 val e = new FixtureWordSpec { 493 type FixtureParam = String 494 def withFixture(test: OneArgTest) { test("hi") } 495 var theTestThisCalled = false 496 var theTestThatCalled = false 497 var theTestTheOtherCalled = false 498 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) in { fixture => theTestThisCalled = true } 499 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 500 "test the other" in { fixture => theTestTheOtherCalled = true } 501 } 502 val repE = new TestIgnoredTrackingReporter 503 e.run(None, repE, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.FastAsLight")), 504 Map(), None, new Tracker) 505 assert(!repE.testIgnoredReceived) 506 assert(!e.theTestThisCalled) 507 assert(e.theTestThatCalled) 508 assert(!e.theTestTheOtherCalled) 509 510 // An Ignored test that was both included and excluded should not generate a TestIgnored event 511 val f = new FixtureWordSpec { 512 type FixtureParam = String 513 def withFixture(test: OneArgTest) { test("hi") } 514 var theTestThisCalled = false 515 var theTestThatCalled = false 516 var theTestTheOtherCalled = false 517 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) ignore { fixture => theTestThisCalled = true } 518 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 519 "test the other" in { fixture => theTestTheOtherCalled = true } 520 } 521 val repF = new TestIgnoredTrackingReporter 522 f.run(None, repF, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.FastAsLight")), 523 Map(), None, new Tracker) 524 assert(!repF.testIgnoredReceived) 525 assert(!f.theTestThisCalled) 526 assert(f.theTestThatCalled) 527 assert(!f.theTestTheOtherCalled) 528 529 // An Ignored test that was not included should not generate a TestIgnored event 530 val g = new FixtureWordSpec { 531 type FixtureParam = String 532 def withFixture(test: OneArgTest) { test("hi") } 533 var theTestThisCalled = false 534 var theTestThatCalled = false 535 var theTestTheOtherCalled = false 536 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) in { fixture => theTestThisCalled = true } 537 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 538 "test the other" ignore { fixture => theTestTheOtherCalled = true } 539 } 540 val repG = new TestIgnoredTrackingReporter 541 g.run(None, repG, new Stopper {}, Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.FastAsLight")), 542 Map(), None, new Tracker) 543 assert(!repG.testIgnoredReceived) 544 assert(!g.theTestThisCalled) 545 assert(g.theTestThatCalled) 546 assert(!g.theTestTheOtherCalled) 547 548 // No tagsToInclude set, FastAsLight excluded 549 val h = new FixtureWordSpec { 550 type FixtureParam = String 551 def withFixture(test: OneArgTest) { test("hi") } 552 var theTestThisCalled = false 553 var theTestThatCalled = false 554 var theTestTheOtherCalled = false 555 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) in { fixture => theTestThisCalled = true } 556 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 557 "test the other" in { fixture => theTestTheOtherCalled = true } 558 } 559 val repH = new TestIgnoredTrackingReporter 560 h.run(None, repH, new Stopper {}, Filter(None, Set("org.scalatest.FastAsLight")), Map(), None, new Tracker) 561 assert(!repH.testIgnoredReceived) 562 assert(!h.theTestThisCalled) 563 assert(h.theTestThatCalled) 564 assert(h.theTestTheOtherCalled) 565 566 // No tagsToInclude set, SlowAsMolasses excluded 567 val i = new FixtureWordSpec { 568 type FixtureParam = String 569 def withFixture(test: OneArgTest) { test("hi") } 570 var theTestThisCalled = false 571 var theTestThatCalled = false 572 var theTestTheOtherCalled = false 573 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) in { fixture => theTestThisCalled = true } 574 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => theTestThatCalled = true } 575 "test the other" in { fixture => theTestTheOtherCalled = true } 576 } 577 val repI = new TestIgnoredTrackingReporter 578 i.run(None, repI, new Stopper {}, Filter(None, Set("org.scalatest.SlowAsMolasses")), Map(), None, new Tracker) 579 assert(!repI.testIgnoredReceived) 580 assert(!i.theTestThisCalled) 581 assert(!i.theTestThatCalled) 582 assert(i.theTestTheOtherCalled) 583 584 // No tagsToInclude set, SlowAsMolasses excluded, TestIgnored should not be received on excluded ones 585 val j = new FixtureWordSpec { 586 type FixtureParam = String 587 def withFixture(test: OneArgTest) { test("hi") } 588 var theTestThisCalled = false 589 var theTestThatCalled = false 590 var theTestTheOtherCalled = false 591 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) ignore { fixture => theTestThisCalled = true } 592 "test that" taggedAs(mytags.SlowAsMolasses) ignore { fixture => theTestThatCalled = true } 593 "test the other" in { fixture => theTestTheOtherCalled = true } 594 } 595 val repJ = new TestIgnoredTrackingReporter 596 j.run(None, repJ, new Stopper {}, Filter(None, Set("org.scalatest.SlowAsMolasses")), Map(), None, new Tracker) 597 assert(!repI.testIgnoredReceived) 598 assert(!j.theTestThisCalled) 599 assert(!j.theTestThatCalled) 600 assert(j.theTestTheOtherCalled) 601 602 // Same as previous, except Ignore specifically mentioned in excludes set 603 val k = new FixtureWordSpec { 604 type FixtureParam = String 605 def withFixture(test: OneArgTest) { test("hi") } 606 var theTestThisCalled = false 607 var theTestThatCalled = false 608 var theTestTheOtherCalled = false 609 "test this" taggedAs(mytags.SlowAsMolasses, mytags.FastAsLight) ignore { fixture => theTestThisCalled = true } 610 "test that" taggedAs(mytags.SlowAsMolasses) ignore { fixture => theTestThatCalled = true } 611 "test the other" ignore { fixture => theTestTheOtherCalled = true } 612 } 613 val repK = new TestIgnoredTrackingReporter 614 k.run(None, repK, new Stopper {}, Filter(None, Set("org.scalatest.SlowAsMolasses", "org.scalatest.Ignore")), Map(), None, new Tracker) 615 assert(repK.testIgnoredReceived) 616 assert(!k.theTestThisCalled) 617 assert(!k.theTestThatCalled) 618 assert(!k.theTestTheOtherCalled) 619 } 620 621 it("should return the correct test count from its expectedTestCount method") { 622 623 val a = new FixtureWordSpec { 624 type FixtureParam = String 625 def withFixture(test: OneArgTest) { test("hi") } 626 "test this" in { fixture => } 627 "test that" in { fixture => } 628 } 629 assert(a.expectedTestCount(Filter()) === 2) 630 631 val b = new FixtureWordSpec { 632 type FixtureParam = String 633 def withFixture(test: OneArgTest) { test("hi") } 634 "test this" ignore { fixture => } 635 "test that" in { fixture => } 636 } 637 assert(b.expectedTestCount(Filter()) === 1) 638 639 val c = new FixtureWordSpec { 640 type FixtureParam = String 641 def withFixture(test: OneArgTest) { test("hi") } 642 "test this" taggedAs(mytags.FastAsLight) in { fixture => } 643 "test that" in { fixture => } 644 } 645 assert(c.expectedTestCount(Filter(Some(Set("org.scalatest.FastAsLight")), Set())) === 1) 646 assert(c.expectedTestCount(Filter(None, Set("org.scalatest.FastAsLight"))) === 1) 647 648 val d = new FixtureWordSpec { 649 type FixtureParam = String 650 def withFixture(test: OneArgTest) { test("hi") } 651 "test this" taggedAs(mytags.FastAsLight, mytags.SlowAsMolasses) in { fixture => } 652 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => } 653 "test the other thing" in { fixture => } 654 } 655 assert(d.expectedTestCount(Filter(Some(Set("org.scalatest.FastAsLight")), Set())) === 1) 656 assert(d.expectedTestCount(Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.FastAsLight"))) === 1) 657 assert(d.expectedTestCount(Filter(None, Set("org.scalatest.SlowAsMolasses"))) === 1) 658 assert(d.expectedTestCount(Filter()) === 3) 659 660 val e = new FixtureWordSpec { 661 type FixtureParam = String 662 def withFixture(test: OneArgTest) { test("hi") } 663 "test this" taggedAs(mytags.FastAsLight, mytags.SlowAsMolasses) in { fixture => } 664 "test that" taggedAs(mytags.SlowAsMolasses) in { fixture => } 665 "test the other thing" ignore { fixture => } 666 } 667 assert(e.expectedTestCount(Filter(Some(Set("org.scalatest.FastAsLight")), Set())) === 1) 668 assert(e.expectedTestCount(Filter(Some(Set("org.scalatest.SlowAsMolasses")), Set("org.scalatest.FastAsLight"))) === 1) 669 assert(e.expectedTestCount(Filter(None, Set("org.scalatest.SlowAsMolasses"))) === 0) 670 assert(e.expectedTestCount(Filter()) === 2) 671 672 val f = new Suites(a, b, c, d, e) 673 assert(f.expectedTestCount(Filter()) === 10) 674 } 675 676 it("should generate a TestPending message when the test body is (pending)") { 677 val a = new FixtureWordSpec { 678 type FixtureParam = String 679 val hello = "Hello, world!" 680 def withFixture(test: OneArgTest) { 681 test(hello) 682 } 683 684 "should do this" is (pending) 685 686 "should do that" in { fixture => 687 assert(fixture === hello) 688 } 689 "should do something else" in { fixture => 690 assert(fixture === hello) 691 pending 692 } 693 } 694 val rep = new EventRecordingReporter 695 a.run(None, rep, new Stopper {}, Filter(), Map(), None, new Tracker()) 696 val tp = rep.testPendingEventsReceived 697 assert(tp.size === 2) 698 } 699 it("should generate a test failure if a Throwable, or an Error other than direct Error subtypes " + 700 "known in JDK 1.5, excluding AssertionError") { 701 val a = new FixtureWordSpec { 702 type FixtureParam = String 703 val hello = "Hello, world!" 704 def withFixture(test: OneArgTest) { 705 test(hello) 706 } 707 "This WordSpec" should { 708 "throw AssertionError" in { s => throw new AssertionError } 709 "throw plain old Error" in { s => throw new Error } 710 "throw Throwable" in { s => throw new Throwable } 711 } 712 } 713 val rep = new EventRecordingReporter 714 a.run(None, rep, new Stopper {}, Filter(), Map(), None, new Tracker()) 715 val tf = rep.testFailedEventsReceived 716 assert(tf.size === 3) 717 } 718 it("should propagate out Errors that are direct subtypes of Error in JDK 1.5, other than " + 719 "AssertionError, causing Suites and Runs to abort.") { 720 val a = new FixtureWordSpec { 721 type FixtureParam = String 722 val hello = "Hello, world!" 723 def withFixture(test: OneArgTest) { 724 test(hello) 725 } 726 "This WordSpec" should { 727 "throw AssertionError" in { s => throw new OutOfMemoryError } 728 } 729 } 730 intercept[OutOfMemoryError] { 731 a.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 732 } 733 } 734 it("should send InfoProvided events with aboutAPendingTest set to true for info " + 735 "calls made from a test that is pending") { 736 val a = new FixtureWordSpec with GivenWhenThen { 737 type FixtureParam = String 738 val hello = "Hello, world!" 739 def withFixture(test: OneArgTest) { 740 test(hello) 741 } 742 "A WordSpec" should { 743 "do something" in { s => 744 given("two integers") 745 when("one is subracted from the other") 746 then("the result is the difference between the two numbers") 747 pending 748 } 749 } 750 } 751 val rep = new EventRecordingReporter 752 a.run(None, rep, new Stopper {}, Filter(), Map(), None, new Tracker()) 753 val ip = rep.infoProvidedEventsReceived 754 assert(ip.size === 4) 755 for (event <- ip) { 756 assert(event.message == "A WordSpec" || event.aboutAPendingTest.isDefined && event.aboutAPendingTest.get) 757 } 758 } 759 it("should send InfoProvided events with aboutAPendingTest set to false for info " + 760 "calls made from a test that is not pending") { 761 val a = new FixtureWordSpec with GivenWhenThen { 762 type FixtureParam = String 763 val hello = "Hello, world!" 764 def withFixture(test: OneArgTest) { 765 test(hello) 766 } 767 "A WordSpec" should { 768 "do something" in { s => 769 given("two integers") 770 when("one is subracted from the other") 771 then("the result is the difference between the two numbers") 772 assert(1 + 1 === 2) 773 } 774 } 775 } 776 val rep = new EventRecordingReporter 777 a.run(None, rep, new Stopper {}, Filter(), Map(), None, new Tracker()) 778 val ip = rep.infoProvidedEventsReceived 779 assert(ip.size === 4) 780 for (event <- ip) { 781 assert(event.message == "A WordSpec" || event.aboutAPendingTest.isDefined && !event.aboutAPendingTest.get) 782 } 783 } 784 it("should allow both tests that take fixtures and tests that don't") { 785 val a = new FixtureWordSpec { 786 787 type FixtureParam = String 788 def withFixture(test: OneArgTest) { 789 test("Hello, world!") 790 } 791 792 var takesNoArgsInvoked = false 793 var takesAFixtureInvoked = false 794 795 "A WordSpec" should { 796 "take no args" in { () => takesNoArgsInvoked = true } 797 "take a fixture" in { s => takesAFixtureInvoked = true } 798 } 799 } 800 801 a.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 802 assert(a.testNames.size === 2, a.testNames) 803 assert(a.takesNoArgsInvoked) 804 assert(a.takesAFixtureInvoked) 805 } 806 it("should work with test functions whose inferred result type is not Unit") { 807 val a = new FixtureWordSpec { 808 809 type FixtureParam = String 810 def withFixture(test: OneArgTest) { 811 test("Hello, world!") 812 } 813 814 var takesNoArgsInvoked = false 815 var takesAFixtureInvoked = false 816 "A WordSpec" should { 817 "take no args" in { () => takesNoArgsInvoked = true; true } 818 "take a fixture" in { s => takesAFixtureInvoked = true; true } 819 } 820 } 821 822 assert(!a.takesNoArgsInvoked) 823 assert(!a.takesAFixtureInvoked) 824 a.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 825 assert(a.testNames.size === 2, a.testNames) 826 assert(a.takesNoArgsInvoked) 827 assert(a.takesAFixtureInvoked) 828 } 829 it("should work with ignored tests whose inferred result type is not Unit") { 830 val a = new FixtureWordSpec { 831 type FixtureParam = String 832 def withFixture(test: OneArgTest) { test("hi") } 833 var takeNoArgsInvoked = false 834 var takeAFixtureInvoked = false 835 "A WordSpec" should { 836 "take no args" ignore { () => takeNoArgsInvoked = true; "hi" } 837 "take a fixture" ignore { s => takeAFixtureInvoked = true; 42 } 838 } 839 } 840 841 assert(!a.takeNoArgsInvoked) 842 assert(!a.takeAFixtureInvoked) 843 val reporter = new EventRecordingReporter 844 a.run(None, reporter, new Stopper {}, Filter(), Map(), None, new Tracker) 845 assert(reporter.testIgnoredEventsReceived.size === 2) 846 assert(!a.takeNoArgsInvoked) 847 assert(!a.takeAFixtureInvoked) 848 } 849 it("should pass a NoArgTest to withFixture for tests that take no fixture") { 850 class MySpec extends FixtureWordSpec { 851 type FixtureParam = String 852 var aNoArgTestWasPassed = false 853 var aOneArgTestWasPassed = false 854 override def withFixture(test: NoArgTest) { 855 aNoArgTestWasPassed = true 856 } 857 def withFixture(test: OneArgTest) { 858 aOneArgTestWasPassed = true 859 } 860 "do something" in { () => 861 assert(1 + 1 === 2) 862 } 863 } 864 865 val s = new MySpec 866 s.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 867 assert(s.aNoArgTestWasPassed) 868 assert(!s.aOneArgTestWasPassed) 869 } 870 it("should not pass a NoArgTest to withFixture for tests that take a Fixture") { 871 class MySpec extends FixtureWordSpec { 872 type FixtureParam = String 873 var aNoArgTestWasPassed = false 874 var aOneArgTestWasPassed = false 875 override def withFixture(test: NoArgTest) { 876 aNoArgTestWasPassed = true 877 } 878 def withFixture(test: OneArgTest) { 879 aOneArgTestWasPassed = true 880 } 881 "do something" in { fixture => 882 assert(1 + 1 === 2) 883 } 884 } 885 886 val s = new MySpec 887 s.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 888 assert(!s.aNoArgTestWasPassed) 889 assert(s.aOneArgTestWasPassed) 890 } 891 it("should pass a NoArgTest that invokes the no-arg test when the " + 892 "NoArgTest's no-arg apply method is invoked") { 893 894 class MySpec extends FixtureWordSpec { 895 type FixtureParam = String 896 var theNoArgTestWasInvoked = false 897 def withFixture(test: OneArgTest) { 898 // Shouldn't be called, but just in case don't invoke a OneArgTest 899 } 900 "do something" in { () => 901 theNoArgTestWasInvoked = true 902 } 903 } 904 905 val s = new MySpec 906 s.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 907 assert(s.theNoArgTestWasInvoked) 908 } 909 it("should pass the correct test name in the OneArgTest passed to withFixture") { 910 val a = new FixtureWordSpec { 911 type FixtureParam = String 912 var correctTestNameWasPassed = false 913 def withFixture(test: OneArgTest) { 914 correctTestNameWasPassed = test.name == "do something" 915 test("hi") 916 } 917 "do something" in { fixture => } 918 } 919 a.run(None, SilentReporter, new Stopper {}, Filter(), Map(), None, new Tracker()) 920 assert(a.correctTestNameWasPassed) 921 } 922 it("should pass the correct config map in the OneArgTest passed to withFixture") { 923 val a = new FixtureWordSpec { 924 type FixtureParam = String 925 var correctConfigMapWasPassed = false 926 def withFixture(test: OneArgTest) { 927 correctConfigMapWasPassed = (test.configMap == Map("hi" -> 7)) 928 test("hi") 929 } 930 "do something" in { fixture => } 931 } 932 a.run(None, SilentReporter, new Stopper {}, Filter(), Map("hi" -> 7), None, new Tracker()) 933 assert(a.correctConfigMapWasPassed) 934 } 935 describe("(when a nesting rule has been violated)") { 936 937 it("should, if they call a describe from within an it clause, result in a TestFailedException when running the test") { 938 939 class MySpec extends FixtureWordSpec { 940 type FixtureParam = String 941 def withFixture(test: OneArgTest) { test("hi") } 942 "should blow up" in { fixture => 943 "in the wrong place, at the wrong time" should { 944 } 945 } 946 } 947 948 val spec = new MySpec 949 ensureTestFailedEventReceived(spec, "should blow up") 950 } 951 it("should, if they call a describe with a nested it from within an it clause, result in a TestFailedException when running the test") { 952 953 class MySpec extends FixtureWordSpec { 954 type FixtureParam = String 955 def withFixture(test: OneArgTest) { test("hi") } 956 "should blow up" in { fixture => 957 "in the wrong place, at the wrong time" should { 958 "should never run" in { fixture => 959 assert(1 === 1) 960 } 961 } 962 } 963 } 964 965 val spec = new MySpec 966 ensureTestFailedEventReceived(spec, "should blow up") 967 } 968 it("should, if they call a nested it from within an it clause, result in a TestFailedException when running the test") { 969 970 class MySpec extends FixtureWordSpec { 971 type FixtureParam = String 972 def withFixture(test: OneArgTest) { test("hi") } 973 "should blow up" in { fixture => 974 "should never run" in { fixture => 975 assert(1 === 1) 976 } 977 } 978 } 979 980 val spec = new MySpec 981 ensureTestFailedEventReceived(spec, "should blow up") 982 } 983 it("should, if they call a nested it with tags from within an it clause, result in a TestFailedException when running the test") { 984 985 class MySpec extends FixtureWordSpec { 986 type FixtureParam = String 987 def withFixture(test: OneArgTest) { test("hi") } 988 "should blow up" in { fixture => 989 "should never run" taggedAs(mytags.SlowAsMolasses) in { fixture => 990 assert(1 === 1) 991 } 992 } 993 } 994 995 val spec = new MySpec 996 ensureTestFailedEventReceived(spec, "should blow up") 997 } 998 it("should, if they call a describe with a nested ignore from within an it clause, result in a TestFailedException when running the test") { 999 1000 class MySpec extends FixtureWordSpec { 1001 type FixtureParam = String 1002 def withFixture(test: OneArgTest) { test("hi") } 1003 "should blow up" in { fixture => 1004 "in the wrong place, at the wrong time" should { 1005 "should never run" ignore { fixture => 1006 assert(1 === 1) 1007 } 1008 } 1009 } 1010 } 1011 1012 val spec = new MySpec 1013 ensureTestFailedEventReceived(spec, "should blow up") 1014 } 1015 it("should, if they call a nested ignore from within an it clause, result in a TestFailedException when running the test") { 1016 1017 class MySpec extends FixtureWordSpec { 1018 type FixtureParam = String 1019 def withFixture(test: OneArgTest) { test("hi") } 1020 "should blow up" in { fixture => 1021 "should never run" ignore { fixture => 1022 assert(1 === 1) 1023 } 1024 } 1025 } 1026 1027 val spec = new MySpec 1028 ensureTestFailedEventReceived(spec, "should blow up") 1029 } 1030 it("should, if they call a nested ignore with tags from within an it clause, result in a TestFailedException when running the test") { 1031 1032 class MySpec extends FixtureWordSpec { 1033 type FixtureParam = String 1034 def withFixture(test: OneArgTest) { test("hi") } 1035 "should blow up" in { fixture => 1036 "should never run" taggedAs(mytags.SlowAsMolasses) ignore { fixture => 1037 assert(1 === 1) 1038 } 1039 } 1040 } 1041 1042 val spec = new MySpec 1043 ensureTestFailedEventReceived(spec, "should blow up") 1044 } 1045 } 1046 } 1047} 1048