org.scalatest.fixture.FixtureWordSpec.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.8.0 Show documentation
/* * Copyright 2001-2009 Artima, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.scalatest.fixture import org.scalatest._ import FixtureNodeFamily._ import verb.{CanVerb, ResultOfAfterWordApplication, ShouldVerb, BehaveWord, MustVerb, StringVerbBlockRegistration} import scala.collection.immutable.ListSet import org.scalatest.StackDepthExceptionHelper.getStackDepth import java.util.concurrent.atomic.AtomicReference import java.util.ConcurrentModificationException import org.scalatest.events._ import Suite.anErrorThatShouldCauseAnAbort /** * A sister trait tofor traitorg.scalatest.WordSpecthat can pass a fixture object into its tests. * ** This trait behaves similarly to trait
* *org.scalatest.WordSpec, except that tests may take a fixture object. The type of the * fixture object passed is defined by the abstractFixturetype, which is declared as a member of this trait (inherited * from supertraitFixtureSuite). * This trait also inherits the abstract methodwithFixturefrom supertraitFixtureSuite. ThewithFixturemethod * takes aOneArgTest, which is a nested trait defined as a member of supertraitFixtureSuite. *OneArgTesthas anapplymethod that takes aFixture. * Thisapplymethod is responsible for running a test. * This trait'srunTestmethod delegates the actual running of each test towithFixture, passing * in the test code to run via theOneArgTestargument. ThewithFixturemethod (abstract in this trait) is responsible * for creating the fixture and passing it to the test function. ** Subclasses of this trait must, therefore, do three things differently from a plain old
* *org.scalatest.WordSpec: **
* *- define the type of the fixture object by specifying type
*Fixture- define the
*withFixturemethod- write tests that take a
*Fixture(You can also define tests that don't take aFixture.)* Here's an example: *
* ** import org.scalatest.fixture.FixtureWordSpec * import java.io.FileReader * import java.io.FileWriter * import java.io.File * * class MyWordSpec extends FixtureWordSpec { * * // 1. define type FixtureParam * type FixtureParam = FileReader * * // 2. define the withFixture method * def withFixture(test: OneArgTest) { * * val FileName = "TempFile.txt" * * // Set up the temp file needed by the test * val writer = new FileWriter(FileName) * try { * writer.write("Hello, test!") * } * finally { * writer.close() * } * * // Create the reader needed by the test * val reader = new FileReader(FileName) * * try { * // Run the test using the temp file * test(reader) * } * finally { * // Close and delete the temp file * reader.close() * val file = new File(FileName) * file.delete() * } * } * * // 3. write tests that take a fixture parameter * "A contrived example" should { * "read from the temp file" in { reader => * var builder = new StringBuilder * var c = reader.read() * while (c != -1) { * builder.append(c.toChar) * c = reader.read() * } * assert(builder.toString === "Hello, test!") * } * * "read the first char of the temp file" in { reader => * assert(reader.read() === 'H') * } * * // (You can also write tests that don't take a fixture parameter.) * "not be required" in { * without fixture { * assert(1 + 1 === 2) * } * } * } * } ** ** If the fixture you want to pass into your tests consists of multiple objects, you will need to combine * them into one object to use this trait. One good approach to passing multiple fixture objects is * to encapsulate them in a tuple. Here's an example that takes the tuple approach: *
* ** import org.scalatest.fixture.FixtureWordSpec * import scala.collection.mutable.ListBuffer * * class MyWordSpec extends FixtureWordSpec { * * type FixtureParam = (StringBuilder, ListBuffer[String]) * * def withFixture(test: OneArgTest) { * * // Create needed mutable objects * val stringBuilder = new StringBuilder("ScalaTest is ") * val listBuffer = new ListBuffer[String] * * // Invoke the test function, passing in the mutable objects * test(stringBuilder, listBuffer) * } * * "Another contrived example" should { * "mutate shared fixture objects" in { fixture => * val (builder, buffer) = fixture * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } * * "get a fresh set of mutable fixture objects" in { fixture => * val (builder, buffer) = fixture * builder.append("fun!") * assert(builder.toString === "ScalaTest is fun!") * assert(buffer.isEmpty) * } * } * } ** ** When using a tuple to pass multiple fixture objects, it is usually helpful to give names to each * individual object in the tuple with a pattern-match assignment, as is done at the beginning * of each test here with: *
* ** val (builder, buffer) = fixture ** ** Another good approach to passing multiple fixture objects is * to encapsulate them in a case class. Here's an example that takes the case class approach: *
* ** import org.scalatest.fixture.FixtureWordSpec * import scala.collection.mutable.ListBuffer * * class MyWordSpec extends FixtureWordSpec { * * case class FixtureHolder(builder: StringBuilder, buffer: ListBuffer[String]) * * type FixtureParam = FixtureHolder * * def withFixture(test: OneArgTest) { * * // Create needed mutable objects * val stringBuilder = new StringBuilder("ScalaTest is ") * val listBuffer = new ListBuffer[String] * * // Invoke the test function, passing in the mutable objects * test(FixtureHolder(stringBuilder, listBuffer)) * } * * "Another contrived example" should { * "mutate shared fixture objects" in { fixture => * import fixture._ * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } * * "get a fresh set of mutable fixture objects" in { fixture => * fixture.builder.append("fun!") * assert(fixture.builder.toString === "ScalaTest is fun!") * assert(fixture.buffer.isEmpty) * } * } * } ** ** When using a case class to pass multiple fixture objects, it can be helpful to make the names of each * individual object available as a single identifier with an import statement. This is the approach * taken by the
* *testEasymethod in the previous example. Because it imports the members * of the fixture object, the test code can just use them as unqualified identifiers: ** "mutate shared fixture objects" in { fixture => * import fixture._ * builder.append("easy!") * assert(builder.toString === "ScalaTest is easy!") * assert(buffer.isEmpty) * buffer += "sweet" * } ** ** Alternatively, you may sometimes prefer to qualify each use of a fixture object with the name * of the fixture parameter. This approach, taken by the
* *testFunmethod in the previous * example, makes it more obvious which variables in your test * are part of the passed-in fixture: ** "get a fresh set of mutable fixture objects" in { fixture => * fixture.builder.append("fun!") * assert(fixture.builder.toString === "ScalaTest is fun!") * assert(fixture.buffer.isEmpty) * } ** *Configuring fixtures and tests
* ** Sometimes you may want to write tests that are configurable. For example, you may want to write * a suite of tests that each take an open temp file as a fixture, but whose file name is specified * externally so that the file name can be can be changed from run to run. To accomplish this * the
* *OneArgTesttrait has aconfigMap* method, which will return aMap[String, Any]from which configuration information may be obtained. * TherunTestmethod of this trait will pass aOneArgTesttowithFixture* whoseconfigMapmethod returns theconfigMappassed torunTest. * Here's an example in which the name of a temp file is taken from the passedconfigMap: ** import org.scalatest.fixture.FixtureWordSpec * import java.io.FileReader * import java.io.FileWriter * import java.io.File * * class MyWordSpec extends FixtureWordSpec { * * type FixtureParam = FileReader * * def withFixture(test: OneArgTest) { * * require( * test.configMap.contains("TempFileName"), * "This suite requires a TempFileName to be passed in the configMap" * ) * * // Grab the file name from the configMap * val FileName = test.configMap("TempFileName") * * // Set up the temp file needed by the test * val writer = new FileWriter(FileName) * try { * writer.write("Hello, test!") * } * finally { * writer.close() * } * * // Create the reader needed by the test * val reader = new FileReader(FileName) * * try { * // Run the test using the temp file * test(reader) * } * finally { * // Close and delete the temp file * reader.close() * val file = new File(FileName) * file.delete() * } * } * * "A contrived example" should { * "read from the temp file" in { reader => * var builder = new StringBuilder * var c = reader.read() * while (c != -1) { * builder.append(c.toChar) * c = reader.read() * } * assert(builder.toString === "Hello, test!") * } * * "read the first char of the temp file" in { reader => * assert(reader.read() === 'H') * } * } * } ** ** If you want to pass into each test the entire
* *configMapthat was passed torunTest, you * can mix in traitConfigMapFixture. See the documentation * forConfigMapFixturefor the details, but here's a quick * example of how it looks: ** import org.scalatest.fixture.FixtureWordSpec * import org.scalatest.fixture.ConfigMapFixture * * class MyWordSpec extends FixtureWordSpec with ConfigMapFixture { * * "The final contrived example" should { * "contain hello" in { configMap => * // Use the configMap passed to runTest in the test * assert(configMap.contains("hello") * } * * "contain world" in { configMap => * assert(configMap.contains("world") * } * } * } ** **
* * @author Bill Venners */ trait FixtureWordSpec extends FixtureSuite with ShouldVerb with MustVerb with CanVerb { thisSuite => private val IgnoreTagName = "org.scalatest.Ignore" private class Bundle private( val trunk: Trunk, val currentBranch: Branch, val tagsMap: Map[String, Set[String]], // All tests, in reverse order of registration val testsList: List[FixtureTestLeaf[FixtureParam]], // Used to detect at runtime that they've stuck a describe or an it inside an it, // which should result in a TestRegistrationClosedException val registrationClosed: Boolean ) { def unpack = (trunk, currentBranch, tagsMap, testsList, registrationClosed) } private object Bundle { def apply( trunk: Trunk, currentBranch: Branch, tagsMap: Map[String, Set[String]], testsList: List[FixtureTestLeaf[FixtureParam]], registrationClosed: Boolean ): Bundle = new Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed) def initialize( trunk: Trunk, tagsMap: Map[String, Set[String]], testsList: List[FixtureTestLeaf[FixtureParam]], registrationClosed: Boolean ): Bundle = new Bundle(trunk, trunk, tagsMap, testsList, registrationClosed) } private val atomic = new AtomicReference[Bundle]( Bundle.initialize(new Trunk, Map(), List[FixtureTestLeaf[FixtureParam]](), false) ) private def updateAtomic(oldBundle: Bundle, newBundle: Bundle) { val shouldBeOldBundle = atomic.getAndSet(newBundle) if (!(shouldBeOldBundle eq oldBundle)) throw new ConcurrentModificationException(Resources("concurrentFixtureWordSpecBundleMod")) } private def registerTest(specText: String, f: FixtureParam => Any) = { val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack val testName = getTestName(specText, currentBranch) if (testsList.exists(_.testName == testName)) { throw new DuplicateTestNameException(testName, getStackDepth("Spec.scala", "it")) } val testShortName = specText val test = FixtureTestLeaf(currentBranch, testName, specText, f) currentBranch.subNodes ::= test testsList ::= test updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) testName } private class RegistrationInformer extends Informer { def apply(message: String) { if (message == null) throw new NullPointerException val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack currentBranch.subNodes ::= InfoLeaf(currentBranch, message) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) } } // The informer will be a registration informer until run is called for the first time. (This // is the registration phase of a FixtureWordSpec's lifecycle.) private final val atomicInformer = new AtomicReference[Informer](new RegistrationInformer) /** * Returns anConfigMapFixturecan also be used to facilitate writingFixtureWordSpecs that include tests * that take different fixture types. See the documentation forMultipleFixtureWordSpecfor more information. *Informerthat during test execution will forward strings (and other objects) passed to its *applymethod to the current reporter. If invoked in a constructor, it * will register the passed string for forwarding later during test execution. If invoked while this *FixtureWordSpecis being executed, such as from inside a test function, it will forward the information to * the current reporter immediately. If invoked at any other time, it will * throw an exception. This method can be called safely by any thread. */ implicit protected def info: Informer = atomicInformer.get private val zombieInformer = new Informer { private val complaint = Resources("cantCallInfoNow", "FixtureWordSpec") def apply(message: String) { if (message == null) throw new NullPointerException throw new IllegalStateException(complaint) } } /** * Register a test with the given spec text, optional tags, and test function value that takes no arguments. * An invocation of this method is called an “example.” * * This method will register the test for later execution via an invocation of one of theexecute* methods. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNamesfor an example.) The resulting test name must not have been registered previously on * thisSpecinstance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testTags the optional list of tags for this test * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrunhas been invoked on this suite * @throws NullPointerException ifspecTextor any passed test tag isnull*/ private def registerTestToRun(specText: String, testTags: List[Tag], testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("itCannotAppearInsideAnotherIt"), getStackDepth("Spec.scala", "it")) if (specText == null) throw new NullPointerException("specText was null") if (testTags.exists(_ == null)) throw new NullPointerException("a test tag was null") val testName = registerTest(specText, testFun) val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed2) = oldBundle.unpack val tagNames = Set[String]() ++ testTags.map(_.name) if (!tagNames.isEmpty) tagsMap += (testName -> tagNames) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed2)) } /** * Register a test with the given spec text and test function value that takes no arguments. * * This method will register the test for later execution via an invocation of one of theexecute* methods. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNamesfor an example.) The resulting test name must not have been registered previously on * thisSpecinstance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrunhas been invoked on this suite * @throws NullPointerException ifspecTextor any passed test tag isnull*/ /* protected def it(specText: String)(testFun: => Unit) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("itCannotAppearInsideAnotherIt"), getStackDepth("Spec.scala", "it")) it(specText, Array[Tag](): _*)(testFun) } */ /** * Register a test to ignore, which has the given spec text, optional tags, and test function value that takes no arguments. * This method will register the test for later ignoring via an invocation of one of theexecute* methods. This method exists to make it easy to ignore an existing test by changing the call toit* toignorewithout deleting or commenting out the actual test code. The test will not be executed, but a * report will be sent that indicates the test was ignored. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNamesfor an example.) The resulting test name must not have been registered previously on * thisSpecinstance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testTags the optional list of tags for this test * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrunhas been invoked on this suite * @throws NullPointerException ifspecTextor any passed test tag isnull*/ private def registerTestToIgnore(specText: String, testTags: List[Tag], testFun: FixtureParam => Any) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("ignoreCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "ignore")) if (specText == null) throw new NullPointerException("specText was null") if (testTags.exists(_ == null)) throw new NullPointerException("a test tag was null") val testName = registerTest(specText, testFun) val tagNames = Set[String]() ++ testTags.map(_.name) val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack tagsMap += (testName -> (tagNames + IgnoreTagName)) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) } /** * Register a test to ignore, which has the given spec text and test function value that takes no arguments. * This method will register the test for later ignoring via an invocation of one of theexecute* methods. This method exists to make it easy to ignore an existing test by changing the call toit* toignorewithout deleting or commenting out the actual test code. The test will not be executed, but a * report will be sent that indicates the test was ignored. The name of the test will be a concatenation of the text of all surrounding describers, * from outside in, and the passed spec text, with one space placed between each item. (See the documenation * fortestNamesfor an example.) The resulting test name must not have been registered previously on * thisSpecinstance. * * @param specText the specification text, which will be combined with the descText of any surrounding describers * to form the test name * @param testFun the test function * @throws DuplicateTestNameException if a test with the same name has been registered previously * @throws TestRegistrationClosedException if invoked afterrunhas been invoked on this suite * @throws NullPointerException ifspecTextor any passed test tag isnull*/ /* protected def ignore(specText: String)(testFun: => Unit) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("ignoreCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "ignore")) ignore(specText, Array[Tag](): _*)(testFun) } */ /** * Describe a “subject” being specified and tested by the passed function value. The * passed function value may contain more describers (defined withdescribe) and/or tests * (defined withit). This trait's implementation of this method will register the * description string and immediately invoke the passed function. */ private def registerVerbBranch(description: String, verb: String, f: () => Unit) { registerBranch(f, VerbBranch(_, description, verb)) } private def registerDescriptionBranch(description: String, f: () => Unit) { registerBranch(f, DescriptionBranch(_, description)) } private def registerBranch(f: () => Unit, constructBranch: Branch => Branch) { if (atomic.get.registrationClosed) throw new TestRegistrationClosedException(Resources("describeCannotAppearInsideAnIt"), getStackDepth("Spec.scala", "describe")) def createNewBranch() = { val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack val newBranch = constructBranch(currentBranch) // val newBranch = VerbBranch(currentBranch, description, verb) val oldBranch = currentBranch currentBranch.subNodes ::= newBranch currentBranch = newBranch updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, registrationClosed)) oldBranch } val oldBranch = createNewBranch() f() val oldBundle = atomic.get val (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack updateAtomic(oldBundle, Bundle(trunk, oldBranch, tagsMap, testsList, registrationClosed)) } /** * Class that supports the registration of tagged tests. * ** Instances of this class are returned by the
* * @author Bill Venners */ protected final class ResultOfTaggedAsInvocationOnString(specText: String, tags: List[Tag]) { /** * Supports tagged test registration. * *taggedAsmethod of * classWordSpecStringWrapper. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) in { fixture => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def in(testFun: FixtureParam => Any) { registerTestToRun(specText, tags, testFun) } /** * Supports tagged test registration, for tests that don't take a fixture. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) in { () => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def in(testFun: () => Any) { registerTestToRun(specText, tags, new NoArgTestWrapper(testFun)) } /** * Supports registration of tagged, pending tests. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) is (pending) * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def is(testFun: => PendingNothing) { registerTestToRun(specText, tags, unusedFixtureParam => testFun) } /** * Supports registration of tagged, ignored tests. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) ignore { fixture => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def ignore(testFun: FixtureParam => Any) { registerTestToIgnore(specText, tags, testFun) } /** * Supports registration of tagged, ignored tests that take no fixture parameter. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) ignore { () => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def ignore(testFun: () => Any) { registerTestToIgnore(specText, tags, new NoArgTestWrapper(testFun)) } } /** * A class that via an implicit conversion (namedWordSpec. *convertToWordSpecStringWrapper) enables * methodswhen,that,in,is,taggedAs* andignoreto be invoked onStrings. * ** This class provides much of the syntax for
* * @author Bill Venners */ protected final class WordSpecStringWrapper(string: String) { /** * Supports test registration. * *FixtureWordSpec, however, it does not add * the verb methods (should,must, andcan) toString. * Instead, these are added via theShouldVerb,MustVerb, andCanVerb* traits, whichFixtureWordSpecmixes in, to avoid a conflict with implicit conversions provided * inShouldMatchersandMustMatchers. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" in { fixture => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def in(testFun: FixtureParam => Any) { registerTestToRun(string, List(), testFun) } /** * Supports registration of tests that take no fixture. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" in { () => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def in(testFun: () => Any) { registerTestToRun(string, List(), new NoArgTestWrapper(testFun)) } /** * Supports pending test registration. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" is (pending) * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def is(testFun: => PendingNothing) { registerTestToRun(string, List(), unusedFixtre => testFun) } /** * Supports ignored test registration. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" ignore { fixture => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def ignore(testFun: FixtureParam => Any) { registerTestToIgnore(string, List(), testFun) } /** * Supports registration of ignored tests that take no fixture. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" ignore { () => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def ignore(testFun: () => Any) { registerTestToIgnore(string, List(), new NoArgTestWrapper(testFun)) } /** * Supports tagged test registration. * *WordSpec. ** For example, this method supports syntax such as the following: *
* ** "complain on peek" taggedAs(SlowTest) in { fixture => ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def taggedAs(firstTestTag: Tag, otherTestTags: Tag*) = { val tagList = firstTestTag :: otherTestTags.toList new ResultOfTaggedAsInvocationOnString(string, tagList) } /** * Registers aWordSpec. *whenclause. * ** For example, this method supports syntax such as the following: *
* ** "A Stack" when { ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def when(f: => Unit) { registerDescriptionBranch(string + " (when", f _) } /** * Registers aWordSpec. *whenclause that is followed by an after word. * ** For example, this method supports syntax such as the following: *
* ** val theUser = afterWord("the user") * * "A Stack" when theUser { ... } * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def when(resultOfAfterWordApplication: ResultOfAfterWordApplication) { registerDescriptionBranch(string + " (when " + resultOfAfterWordApplication.text, resultOfAfterWordApplication.f) } /** * Registers aWordSpec. *thatclause. * ** For example, this method supports syntax such as the following: *
* ** "a rerun button" that { * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def that(f: => Unit) { registerDescriptionBranch(string + " that", f _) } /** * Registers aWordSpec. *thatclause. * ** For example, this method supports syntax such as the following: *
* ** "a rerun button" that { * ^ ** ** For more information and examples of this method's use, see the main documentation for trait
*/ def that(resultOfAfterWordApplication: ResultOfAfterWordApplication) { registerDescriptionBranch(string + " that " + resultOfAfterWordApplication.text, resultOfAfterWordApplication.f) } } /** * Class whose instances are after words, which can be used to reduce text duplication. * *WordSpec. ** If you are repeating a word or phrase at the beginning of each string inside * a block, you can "move the word or phrase" out of the block with an after word. * You create an after word by passing the repeated word or phrase to the
* *afterWordmethod. * Once created, you can place the after word afterwhen, a verb * (should,must, orcan), or *that. (You can't place one afterinoris, the * words that introduce a test.) Here's an example that has after words used in all three * places: ** import org.scalatest.WordSpec * * class ScalaTestGUISpec extends WordSpec { * * def theUser = afterWord("the user") * def display = afterWord("display") * def is = afterWord("is") * * "The ScalaTest GUI" when theUser { * "clicks on an event report in the list box" should display { * "a blue background in the clicked-on row in the list box" in {} * "the details for the event in the details area" in {} * "a rerun button" that is { * "enabled if the clicked-on event is rerunnable" in {} * "disabled if the clicked-on event is not rerunnable" in {} * } * } * } * } ** ** Running the previous
* *WordSpecin the Scala interpreter would yield: ** scala> (new ScalaTestGUISpec).run() * The ScalaTest GUI (when the user clicks on an event report in the list box) * - should display a blue background in the clicked-on row in the list box * - should display the details for the event in the details area * - should display a rerun button that is enabled if the clicked-on event is rerunnable * - should display a rerun button that is disabled if the clicked-on event is not rerunnable **/ protected final class AfterWord(text: String) { /** * Supports the use of after words. * ** This method transforms a block of code into a
ResultOfAfterWordApplication, which * is accepted bywhen,should,must,can, andthat* methods. For more information, see the main documentationWordSpec. * */ def apply(f: => Unit) = new ResultOfAfterWordApplication(text, f _) } /** * Creates an after word that an be used to reduce text duplication. * ** If you are repeating a word or phrase at the beginning of each string inside * a block, you can "move the word or phrase" out of the block with an after word. * You create an after word by passing the repeated word or phrase to the
* *afterWordmethod. * Once created, you can place the after word afterwhen, a verb * (should,must, orcan), or *that. (You can't place one afterinoris, the * words that introduce a test.) Here's an example that has after words used in all three * places: ** import org.scalatest.WordSpec * * class ScalaTestGUISpec extends WordSpec { * * def theUser = afterWord("the user") * def display = afterWord("display") * def is = afterWord("is") * * "The ScalaTest GUI" when theUser { * "clicks on an event report in the list box" should display { * "a blue background in the clicked-on row in the list box" in {} * "the details for the event in the details area" in {} * "a rerun button" that is { * "enabled if the clicked-on event is rerunnable" in {} * "disabled if the clicked-on event is not rerunnable" in {} * } * } * } * } ** ** Running the previous
* *WordSpecin the Scala interpreter would yield: ** scala> (new ScalaTestGUISpec).run() * The ScalaTest GUI (when the user clicks on an event report in the list box) * - should display a blue background in the clicked-on row in the list box * - should display the details for the event in the details area * - should display a rerun button that is enabled if the clicked-on event is rerunnable * - should display a rerun button that is disabled if the clicked-on event is not rerunnable **/ protected def afterWord(text: String) = new AfterWord(text) /** * Implicitly convertsStrings toWordSpecStringWrapper, which enables * methodswhen,that,in,is,taggedAs* andignoreto be invoked onStrings. */ protected implicit def convertToWordSpecStringWrapper(s: String) = new WordSpecStringWrapper(s) /** * Supports the registration of subjects. * ** For example, this method enables syntax such as the following: *
* ** "A Stack" should { ... * ^ ** ** This function is passed as an implicit parameter to a
*/ protected implicit val subjectRegistrationFunction: StringVerbBlockRegistration = new StringVerbBlockRegistration { def apply(left: String, verb: String, f: () => Unit) = registerVerbBranch(left, verb, f) } /** * Supports the registration of subject descriptions with after words. * *shouldmethod * provided inShouldVerb, amustmethod * provided inMustVerb, and acanmethod * provided inCanVerb. When invoked, this function registers the * subject and executes the block. ** For example, this method enables syntax such as the following: *
* ** def provide = afterWord("provide") * * "The ScalaTest Matchers DSL" can provide { ... } * ^ ** ** This function is passed as an implicit parameter to a
*/ protected implicit val subjectWithAfterWordRegistrationFunction: (String, String, ResultOfAfterWordApplication) => Unit = { (left, verb, resultOfAfterWordApplication) => { val afterWordFunction = () => { registerDescriptionBranch(resultOfAfterWordApplication.text, resultOfAfterWordApplication.f) } registerVerbBranch(left, verb, afterWordFunction) } } /** * Ashouldmethod * provided inShouldVerb, amustmethod * provided inMustVerb, and acanmethod * provided inCanVerb. When invoked, this function registers the * subject and executes the block. *Mapwhose keys areStringtag names to which tests in thisSpecbelong, and values * theSetof test names that belong to each tag. If thisWordSpeccontains no tags, this method returns an emptyMap. * ** This trait's implementation returns tags that were passed as strings contained in
*/ override def tags: Map[String, Set[String]] = atomic.get.tagsMap private def runTestsInBranch(branch: Branch, reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], tracker: Tracker) { val stopRequested = stopper // Wrap any non-DispatchReporter, non-CatchReporter in a CatchReporter, // so that exceptions are caught and transformed // into error messages on the standard error stream. val report = wrapReporterIfNecessary(reporter) branch match { case desc @ VerbBranch(_, descriptionName, verb) => // Need to use the full name of the description, which includes all the descriptions it is nested inside // Call getPrefix and pass in this Desc, to get the full name val descriptionFullName = getPrefixWithoutVerb(desc).trim report(InfoProvided(tracker.nextOrdinal(), descriptionFullName, Some(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)), None, None, Some(IndentedText(descriptionFullName, descriptionFullName, 0)))) case _ => } branch.subNodes.reverse.foreach( _ match { case FixtureTestLeaf(parent, tn, specText, _) => if (!stopRequested()) { // TODO: Seems odd to me to check for stop here but still fire infos val (filterTest, ignoreTest) = filter(tn, tags) if (!filterTest) if (ignoreTest) { val testSucceededIcon = Resources("testSucceededIconChar") val formattedSpecText = Resources("iconPlusShortName", testSucceededIcon, getFormattedSpecTextPrefix(parent) + " " + specText) report(TestIgnored(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), tn, Some(IndentedText(formattedSpecText, specText, 1)))) } else runTest(tn, report, stopRequested, configMap, tracker) } case InfoLeaf(_, message) => val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, Some(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)), None, None, Some(IndentedText(formattedText, message, 1)))) case branch: Branch => runTestsInBranch(branch, reporter, stopRequested, filter, configMap, tracker) } ) } /** * Run a test. This trait's implementation runs the test registered with the name specified by *Tagobjects passed to * methodstestandignore. *testName. Each test's name is a concatenation of the text of all describers surrounding a test, * from outside in, and the test's spec text, with one space placed between each item. (See the documenation * fortestNamesfor an example.) * * @param testName the name of one test to execute. * @param reporter theReporterto which results will be reported * @param stopper theStopperthat will be consulted to determine whether to stop execution early. * @param configMap aMapof properties that can be used by thisSpec's executing tests. * @throws NullPointerException if any oftestName,reporter,stopper, orconfigMap* isnull. */ protected override def runTest(testName: String, reporter: Reporter, stopper: Stopper, configMap: Map[String, Any], tracker: Tracker) { if (testName == null || reporter == null || stopper == null || configMap == null) throw new NullPointerException atomic.get.testsList.find(_.testName == testName) match { case None => throw new IllegalArgumentException("Requested test doesn't exist: " + testName) case Some(test) => { val report = wrapReporterIfNecessary(reporter) val testSucceededIcon = Resources("testSucceededIconChar") val formattedSpecText = Resources("iconPlusShortName", testSucceededIcon, getFormattedSpecTextPrefix(test.parent) + " " + test.specText) // Create a Rerunner if the Spec has a no-arg constructor val hasPublicNoArgConstructor = org.scalatest.Suite.checkForPublicNoArgConstructor(getClass) val rerunnable = if (hasPublicNoArgConstructor) Some(new TestRerunner(getClass.getName, testName)) else None val testStartTime = System.currentTimeMillis // A TestStarting event won't normally show up in a specification-style output, but // will show up in a test-style output. report(TestStarting(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(MotionToSuppress), rerunnable)) val formatter = IndentedText(formattedSpecText, getFormattedSpecTextPrefix(test.parent) + " " + test.specText, 1) val informerForThisTest = new MessageRecordingInformer(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), Some(testName))) { def apply(message: String) { if (message == null) throw new NullPointerException if (shouldRecord) record(message) else { val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = " " + Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, nameInfoForCurrentThread, None, None, Some(IndentedText(formattedText, message, 2)))) } } } val oldInformer = atomicInformer.getAndSet(informerForThisTest) var testWasPending = false var swapAndCompareSucceeded = false try { test.f match { case wrapper: NoArgTestWrapper[_] => withFixture(new FixturelessTestFunAndConfigMap(testName, wrapper.test, configMap)) case f => withFixture(new TestFunAndConfigMap(testName, f, configMap)) } val duration = System.currentTimeMillis - testStartTime report(TestSucceeded(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(duration), Some(formatter), rerunnable)) } catch { case _: TestPendingException => report(TestPending(tracker.nextOrdinal(), thisSuite.suiteName, Some(thisSuite.getClass.getName), test.testName, Some(formatter))) testWasPending = true case e if !anErrorThatShouldCauseAnAbort(e) => val duration = System.currentTimeMillis - testStartTime handleFailedTest(e, false, test.testName, test.specText, formattedSpecText, rerunnable, report, tracker, duration) case e => throw e } finally { // send out any recorded messages for (message <- informerForThisTest.recordedMessages) { val infoProvidedIcon = Resources("infoProvidedIconChar") val formattedText = " " + Resources("iconPlusShortName", infoProvidedIcon, message) report(InfoProvided(tracker.nextOrdinal(), message, informerForThisTest.nameInfoForCurrentThread, Some(testWasPending), None, Some(IndentedText(formattedText, message, 2)))) } val shouldBeInformerForThisTest = atomicInformer.getAndSet(oldInformer) swapAndCompareSucceeded = shouldBeInformerForThisTest eq informerForThisTest } if (!swapAndCompareSucceeded) // Do outside finally to workaround Scala compiler bug throw new ConcurrentModificationException(Resources("concurrentInformerMod", thisSuite.getClass.getName)) } } } private def handleFailedTest(throwable: Throwable, hasPublicNoArgConstructor: Boolean, testName: String, specText: String, formattedSpecText: String, rerunnable: Option[Rerunner], report: Reporter, tracker: Tracker, duration: Long) { val message = if (throwable.getMessage != null) // [bv: this could be factored out into a helper method] throwable.getMessage else throwable.toString val formatter = IndentedText(formattedSpecText, specText, 1) report(TestFailed(tracker.nextOrdinal(), message, thisSuite.suiteName, Some(thisSuite.getClass.getName), testName, Some(throwable), Some(duration), Some(formatter), rerunnable)) } /** ** Run zero to many of this
* *Spec's tests. ** This method takes a
* *testNameparameter that optionally specifies a test to invoke. * IftestNameisSome, this trait's implementation of this method * invokesrunTeston this object, passing in: *
-
*
testName- theStringvalue of thetestNameOptionpassed * to this method
* reporter- theReporterpassed to this method, or one that wraps and delegates to it
* stopper- theStopperpassed to this method, or one that wraps and delegates to it
* configMap- theconfigMappassed to this method, or one that wraps and delegates to it
*
* This method takes a Set of tag names that should be included (tagsToInclude), and a Set
* that should be excluded (tagsToExclude), when deciding which of this Suite's tests to execute.
* If tagsToInclude is empty, all tests will be executed
* except those those belonging to tags listed in the tagsToExclude Set. If tagsToInclude is non-empty, only tests
* belonging to tags mentioned in tagsToInclude, and not mentioned in tagsToExclude
* will be executed. However, if testName is Some, tagsToInclude and tagsToExclude are essentially ignored.
* Only if testName is None will tagsToInclude and tagsToExclude be consulted to
* determine which of the tests named in the testNames Set should be run. For more information on trait tags, see the main documentation for this trait.
*
* If testName is None, this trait's implementation of this method
* invokes testNames on this Suite to get a Set of names of tests to potentially execute.
* (A testNames value of None essentially acts as a wildcard that means all tests in
* this Suite that are selected by tagsToInclude and tagsToExclude should be executed.)
* For each test in the testName Set, in the order
* they appear in the iterator obtained by invoking the elements method on the Set, this trait's implementation
* of this method checks whether the test should be run based on the tagsToInclude and tagsToExclude Sets.
* If so, this implementation invokes runTest, passing in:
*
-
*
testName- theStringname of the test to run (which will be one of the names in thetestNamesSet)
* reporter- theReporterpassed to this method, or one that wraps and delegates to it
* stopper- theStopperpassed to this method, or one that wraps and delegates to it
* configMap- theconfigMappassed to this method, or one that wraps and delegates to it
*
None, all relevant tests should be executed.
* I.e., None acts like a wildcard that means execute all relevant tests in this Spec.
* @param reporter the Reporter to which results will be reported
* @param stopper the Stopper that will be consulted to determine whether to stop execution early.
* @param tagsToInclude a Set of String tag names to include in the execution of this Spec
* @param tagsToExclude a Set of String tag names to exclude in the execution of this Spec
* @param configMap a Map of key-value pairs that can be used by this Spec's executing tests.
* @throws NullPointerException if any of testName, reporter, stopper, tagsToInclude,
* tagsToExclude, or configMap is null.
*/
protected override def runTests(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter,
configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) {
if (testName == null)
throw new NullPointerException("testName was null")
if (reporter == null)
throw new NullPointerException("reporter was null")
if (stopper == null)
throw new NullPointerException("stopper was null")
if (filter == null)
throw new NullPointerException("filter was null")
if (configMap == null)
throw new NullPointerException("configMap was null")
if (distributor == null)
throw new NullPointerException("distributor was null")
if (tracker == null)
throw new NullPointerException("tracker was null")
val stopRequested = stopper
testName match {
case None => runTestsInBranch(atomic.get.trunk, reporter, stopRequested, filter, configMap, tracker)
case Some(tn) => runTest(tn, reporter, stopRequested, configMap, tracker)
}
}
/**
* An immutable Set of test names. If this FixtureWordSpec contains no tests, this method returns an
* empty Set.
*
* * This trait's implementation of this method will return a set that contains the names of all registered tests. The set's * iterator will return those names in the order in which the tests were registered. Each test's name is composed * of the concatenation of the text of each surrounding describer, in order from outside in, and the text of the * example itself, with all components separated by a space. *
*/ override def testNames: Set[String] = ListSet(atomic.get.testsList.map(_.testName): _*) override def run(testName: Option[String], reporter: Reporter, stopper: Stopper, filter: Filter, configMap: Map[String, Any], distributor: Option[Distributor], tracker: Tracker) { val stopRequested = stopper // Set the flag that indicates registration is closed (because run has now been invoked), // which will disallow any further invocations of "describe", it", or "ignore" with // an RegistrationClosedException. val oldBundle = atomic.get var (trunk, currentBranch, tagsMap, testsList, registrationClosed) = oldBundle.unpack if (!registrationClosed) updateAtomic(oldBundle, Bundle(trunk, currentBranch, tagsMap, testsList, true)) val report = wrapReporterIfNecessary(reporter) val informerForThisSuite = new ConcurrentInformer(NameInfo(thisSuite.suiteName, Some(thisSuite.getClass.getName), None)) { def apply(message: String) { if (message == null) throw new NullPointerException report(InfoProvided(tracker.nextOrdinal(), message, nameInfoForCurrentThread)) } } atomicInformer.set(informerForThisSuite) var swapAndCompareSucceeded = false try { super.run(testName, report, stopRequested, filter, configMap, distributor, tracker) } finally { val shouldBeInformerForThisSuite = atomicInformer.getAndSet(zombieInformer) swapAndCompareSucceeded = shouldBeInformerForThisSuite eq informerForThisSuite } if (!swapAndCompareSucceeded) // Do outside finally to workaround Scala compiler bug throw new ConcurrentModificationException(Resources("concurrentInformerMod", thisSuite.getClass.getName)) } /** * Supports shared test registration inFixtureWordSpecs.
*
* * This field enables syntax such as the following: *
* ** behave like nonFullStack(stackWithOneItem) * ^ ** *
* For more information and examples of the use of WordSpec.
*
Any to
* a function from Fixture to Any, to enable no-arg tests to registered
* by methods that require a test function that takes a Fixture.
protected implicit def convertNoArgToFixtureFunction(fun: () => Any): (FixtureParam => Any) =
new NoArgTestWrapper(fun)
*/
}