org.scalatest.Outcome.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.9.1 Show documentation
/* * Copyright 2001-2013 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 /** * Superclass for the possible outcomes of running a test. * *Canceled outcomes, * extracting the contained exception. * **
* *Outcomeis the result type of thewithFixturemethods of traits *Suiteandfixture.Suite, as well as their *NoArgTestandOneArgTestfunction types. * The five possible outcomes are: **
* *- *
Succeeded- indicates a test succeeded- *
Failed- indicates a test failed and contains an exception describing the failure- *
Canceled- indicates a test was canceled and contains an exception describing the cancelation- *
Pending- indicates a test was pending- *
Omitted- indicates a test was omitted* Note that "ignored" does not appear as a type of
* *Outcome, because tests are * marked as ignored on the outside and skipped over as the suite executes. So an ignored test never runs, and therefore * never has an outcome. By contrast, a test is determined to be pending by running the test * and observing the actual outcome. If the test body completes abruptly with aTestPendingException, * then the outcome was that the test was pending. ** Note:
*/ sealed abstract class Outcome { /** * Indicates whether thisOmittedis currently not used in ScalaTest, but will eventually be used * to indicate everthing except specification-text provided by mechanisms such * asGivenWhenThenhas been omitted or elided from the test body. This will enable * full specification output to be obtained without waiting for the actual test code * to execute. *Outcomerepresents a test that succeeded. * ** This class's implementation of this method always returns
* * @return true if thisfalse. *Outcomeis an instance ofSucceeded. */ val isSucceeded: Boolean = false /** * Indicates whether thisOutcomerepresents a test that failed. * ** This class's implementation of this method always returns
* * @return true if thisfalse. *Outcomeis an instance ofFailed. */ val isFailed: Boolean = false /** * Indicates whether thisOutcomerepresents a test that was canceled. * ** This class's implementation of this method always returns
* * @return true if thisfalse. *Outcomeis an instance ofCanceled. */ val isCanceled: Boolean = false /** * Indicates whether thisOutcomerepresents a test that was pending. * ** This class's implementation of this method always returns
* * @return true if thisfalse. *Outcomeis an instance ofPending. */ val isPending: Boolean = false /** * Indicates whether thisOutcomerepresents a test that was omitted. * ** This class's implementation of this method always returns
* * @return true if thisfalse. *Outcomeis an instance ofOmitted. */ val isOmitted: Boolean = false /** * Indicates whether thisOutcomerepresents a test that either failed or was canceled, in which case thisOutcomewill contain an exception. * * @return true if thisOutcomeis an instance of eitherFailedorCanceled. */ val isExceptional: Boolean = false /** * Converts thisOutcometo anOption[Throwable]. * ** This class's implementation of this method always returns
* * @return aNone. *Somewrapping the contained exception if thisOutcomeis an instance of eitherFailedorCanceled. */ def toOption: Option[Throwable] = None // Used internally to resuse the old code that was catching these exceptions when running tests. Eventually I would // like to rewrite that old code to use the result type, but it will still needs to catch and handle these exceptions // in the same way in case they come back from a user's withFixture implementation. private[scalatest] def toUnit { this match { case Succeeded => case Exceptional(e) => throw e case Pending(_) => throw new exceptions.TestPendingException case Omitted => throw new exceptions.TestOmittedException } } } /** * Superclass for the two outcomes of running a test that contain an exception:FailedandCanceled. * ** This class provides a
* *toOptionmethod that returns aSomewrapping the contained exception, and * anisExceptionalfield with the valuetrue. It's companion object provides an extractor that * enables patterns that match a test that either failed or canceled, as in: ** outcome match { * case Exceptional(ex) => // handle failed or canceled case * case _ => // handle succeeded, pending, or omitted case * } ** * @param ex theThrowablecontained in thisExceptional. */ sealed abstract class Exceptional(ex: Throwable) extends Outcome { /** * Indicates that thisOutcomerepresents a test that either failed or was canceled. * * @return true */ override val isExceptional: Boolean = true /** * Converts thisExceptionalto aSomethat wraps the contained exception. * * @return ASomewrapping the exception contained in thisExceptional. */ override def toOption: Option[Throwable] = Some(ex) } /** * Companion object to classExceptionalthat provides a factory method and an extractor that enables * patterns that match bothFailedandCanceledoutcomes and * extracts the contained exception and a factory method. */ object Exceptional { /** * Creates anExceptionalinstance given the passedThrowable. * ** If the passed
* *Throwableis an instance ofTestCanceledException, this * method will returnCanceledcontaining thatTestCanceledException. Otherwise, * it returns aFailedcontaining theThrowable. ** For example, trait
* *SeveredStackTracesuses this * factory method to sever the stack trace of the exception contained in either aFailedandCanceled* like this: ** abstract override def withFixture(test: NoArgTest): Outcome = { * super.withFixture(test) match { * case Exceptional(e: StackDepth) => Exceptional(e.severedAtStackDepth) * case o => o * } * } ** * @return aFailedorCanceledcontaining the passed exception. */ def apply(e: Throwable): Exceptional = e match { case tce: exceptions.TestCanceledException => Canceled(tce) case _ => Failed(e) } /** * Extractor enabling patterns that match bothFailedand* For example, trait
* *SeveredStackTracesuses this * extractor to sever the stack trace of the exception contained in either aFailedandCanceled* like this: ** abstract override def withFixture(test: NoArgTest): Outcome = { * super.withFixture(test) match { * case Exceptional(e: StackDepth) => Exceptional(e.severedAtStackDepth) * case o => o * } * } ** * @return aSomewrapping the contained throwable ifresis an instance of * eitherFailedorCanceled, elseNone. */ def unapply(res: Outcome): Option[Throwable] = res match { case Failed(ex) => Some(ex) case Canceled(ex) => Some(ex) case _ => None } } /** * Outcome for a test that succeeded. * ** Note: the difference between this
*/ case object Succeeded extends Outcome { /** * Indicates that thisSucceededobject and the similarly namedSucceededStatus* object is that this object indicates one test succeeded, whereas theSucceededStatusobject indicates the absence of any failed tests or * aborted suites during a run. Both are used as the result type ofSuitelifecycle methods, butSucceeded* is a possible result ofwithFixture, whereasSucceededStatusis a possible result ofrun,runNestedSuites, *runTests, orrunTest. In short,Succeededis always just about one test, whereasSucceededStatuscould be * about something larger: multiple tests or an entire suite. *Outcomerepresents a test that succeeded. * ** This class's implementation of this method always returns
* * @return true */ override val isSucceeded: Boolean = true } /** * Outcome for a test that failed, containing an exception describing the cause of the failure. * *true. ** Note: the difference between this
* * @param ex theFailedclass and the similarly namedFailedStatus* object is that an instance of this class indicates one test failed, whereas theFailedStatusobject indicates either one or more tests failed * and/or one or more suites aborted during a run. Both are used as the result type ofSuitelifecycle methods, butFailed* is a possible result ofwithFixture, whereasFailedStatusis a possible result ofrun,runNestedSuites, *runTests, orrunTest. In short,Failedis always just about one test, whereasFailedStatuscould be * about something larger: multiple tests or an entire suite. *Throwablecontained in thisFailed. */ case class Failed(ex: Throwable) extends Exceptional(ex) { /** * Indicates that thisOutcomerepresents a test that failed. * ** This class's implementation of this method always returns
* * @return true */ override val isFailed: Boolean = true } /** * Outcome for a test that was canceled, containing an exception describing the cause of the cancelation. * * @param ex thetrue. *TestCanceledExceptioncontained in thisExceptional. */ case class Canceled(ex: exceptions.TestCanceledException) extends Exceptional(ex) { /** * Indicates that thisOutcomerepresents a test that was canceled. * ** This class's implementation of this method always returns
* * @return true */ override val isCanceled: Boolean = true } /** * Companion object to classtrue. *Canceledthat provides, in addition to the extractor and factory method * provided by the compiler given its companion is a case class, a second factory method * that produces aCanceledoutcome given a string message. */ object Canceled { /** * Creates aCanceledoutcome given a string message. * ** For example, trait
* *CancelAfterFailureuses this factory method to create * aCanceledstatus if acancelRemainingflag is set, which will * be the case if a test failed previously while running the suite: ** abstract override def withFixture(test: NoArgTest): Outcome = { * if (cancelRemaining) * Canceled("Canceled by CancelOnFailure because a test failed previously") * else * super.withFixture(test) match { * case failed: Failed => * cancelRemaining = true * failed * case outcome => outcome * } * } **/ def apply(message: String): Canceled = { if (message == null) throw new NullPointerException("message was null") val e = new exceptions.TestCanceledException(message, 0) // TODO: Verify this is hte correct stack depth. e.fillInStackTrace() Canceled(e) } } /** * Outcome for a test that was pending, which contains an optional string giving more information on what exactly is needed * for the test to become non-pending. * * @param message an optional message describing the reason the test is pending */ case class Pending(message: Option[String] = None) extends Outcome { /** * Indicates that thisOutcomerepresents a test that was pending. * ** This class's implementation of this method always returns
* * @return true */ override val isPending: Boolean = true } /** * Outcome for a test that was omitted. * *true. ** Note: This outcome is currently not used in ScalaTest, but will eventually be used * to indicate everthing except specification-text provided by mechanisms such * as
*/ case object Omitted extends Outcome { /** * Indicates that thisGivenWhenThenhas been omitted from the test body. This will enable * a the specification output to be obtained without waiting for the actual test code * to execute. *Outcomerepresents a test that was omitted. * ** This class's implementation of this method always returns
* * @return true */ override val isOmitted: Boolean = true }false. *