Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Code #1831

Open
wants to merge 2 commits into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -75,8 +75,8 @@ object SharedHelpers extends Assertions with LineNumberHelper {
// This now needs to be thread safe, because I'm setting it in one thread
// and asserting using it from a different thread in Async tests.
class EventRecordingReporter extends Reporter {
private var eventList: List[Event] = List()
def eventsReceived = synchronized { eventList.reverse }
private var eventList: List[Event] = List.empty[Event]
def eventsReceived: List[Any] = synchronized { eventList.reverse }
def testSucceededEventsReceived: List[TestSucceeded] = {
synchronized {
eventsReceived filter {
Expand Down
Expand Up @@ -16,26 +16,19 @@
package org.scalatest

import org.scalatest.exceptions.TestFailedException
import scala.util.{Failure, Success, Try}

trait ReturnsNormallyThrowsAssertion {

def returnsNormally(f: => Unit): Boolean = {
try {
f
true
def returnsNormally(f: => Unit): Boolean =
Try(f) match {
case Success(_) => true
case Failure(_) => false
}
catch {
case e: Throwable => false
}
}

def throwsTestFailedException(f: => Unit): Boolean = {
try {
f
false
}
catch {
case e: TestFailedException => true
def throwsTestFailedException(f: => Unit): Boolean =
Try(f) match {
case Success(_) => true
case Failure(_: TestFailedException) => false
}
}
}
6 changes: 3 additions & 3 deletions common-test/src/main/scala/org/scalatest/SharedHelpers.scala
Expand Up @@ -75,7 +75,7 @@ object SharedHelpers extends Assertions with LineNumberHelper {
// and asserting using it from a different thread in Async tests.
class EventRecordingReporter extends Reporter {
private var eventList: List[Event] = List()
def eventsReceived = synchronized { eventList.reverse }
def eventsReceived: List[Any] = synchronized { eventList.reverse }
def testSucceededEventsReceived: List[TestSucceeded] = {
synchronized {
eventsReceived filter {
Expand Down Expand Up @@ -1941,12 +1941,12 @@ object SharedHelpers extends Assertions with LineNumberHelper {
case other => builder.append(other)
(openBracket, multilineBracket)
}
transform(itr, newOpenBracket, builder, newMultilineBracket)
transform(itr = itr, openBracket = newOpenBracket, builder = builder, multilineBracket = newMultilineBracket)
}
}
val itr = str.toCharArray.iterator.buffered
val builder = new StringBuilder
transform(itr, 0, builder)
transform(itr = itr, openBracket = 0, builder = builder)
builder.toString
}
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ import org.scalatest._

class ExampleSuite extends FunSuite {

override def withFixture(test: NoArgTest) = {
override def withFixture(test: NoArgTest): Any = {

try super.withFixture(test)
catch {
Expand Down
Expand Up @@ -22,7 +22,7 @@ class SetSpec extends WordSpec {
"A Set" when {
"empty" should {
"have size 0" in {
assert(Set.empty.size === 0)
assert(Set.empty[Int].size === 0)
}

"produce NoSuchElementException when head is invoked" in {
Expand Down
Expand Up @@ -22,14 +22,14 @@ private class SbtReporter(suiteId: String, fullyQualifiedName: String, fingerpri

import org.scalatest.events._

private def getTestSelector(eventSuiteId: String, testName: String) = {
private def getTestSelector(eventSuiteId: String, testName: String): Any = {
if (suiteId == eventSuiteId)
new TestSelector(testName)
else
new NestedTestSelector(eventSuiteId, testName)
}

private def getSuiteSelector(eventSuiteId: String) = {
private def getSuiteSelector(eventSuiteId: String): Any = {
if (suiteId == eventSuiteId)
new SuiteSelector
else
Expand All @@ -47,17 +47,17 @@ private class SbtReporter(suiteId: String, fullyQualifiedName: String, fingerpri
event match {
// the results of running an actual test
case t: TestPending =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Pending, new OptionalThrowable, t.duration.getOrElse(0)))
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Pending, new OptionalThrowable, t.duration.fold(0)(identity)))
case t: TestFailed =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Failure, getOptionalThrowable(t.throwable), t.duration.getOrElse(0)))
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Failure, getOptionalThrowable(t.throwable), t.duration.fold(0)(identity)))
case t: TestSucceeded =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Success, new OptionalThrowable, t.duration.getOrElse(0)))
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Success, new OptionalThrowable, t.duration.fold(0)(identity)))
case t: TestIgnored =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Ignored, new OptionalThrowable, -1))
case t: TestCanceled =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Canceled, new OptionalThrowable, t.duration.getOrElse(0)))
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getTestSelector(t.suiteId, t.testName), SbtStatus.Canceled, new OptionalThrowable, t.duration.fold(0)(identity)))
case t: SuiteAborted =>
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getSuiteSelector(t.suiteId), SbtStatus.Error, getOptionalThrowable(t.throwable), t.duration.getOrElse(0)))
eventHandler.handle(ScalaTestSbtEvent(fullyQualifiedName, fingerprint, getSuiteSelector(t.suiteId), SbtStatus.Error, getOptionalThrowable(t.throwable), t.duration.fold(0)(identity)))
case _ =>
}
}
Expand Down
Expand Up @@ -29,6 +29,6 @@ object DoNotSignal extends Signaler {
*
* @param testThread unused by this strategy
*/
def apply(testThread: Thread): Unit = {} // Don't do nuthin
def apply(testThread: Thread): Unit = {} // Do nothing
}

Expand Up @@ -179,7 +179,7 @@ trait Waiters extends PatienceConfiguration {
* specifies the number of dismissals to wait for before returning normally from an <code>await</code>
* call on a <code>Waiter</code>.
*/
def dismissals(value: Int) = Dismissals(value)
def dismissals(value: Int): Waiters.this.Dismissals = Dismissals(value)

/**
* Class that facilitates performing assertions outside the main test thread, such as assertions in callback methods
Expand Down
Expand Up @@ -52,7 +52,6 @@ abstract class StackDepthException(
posOrStackDepthFun match {
case Right(null) => throw new NullArgumentException("posOrStackDepthFun was Right(null)")
case Left(null) => throw new NullArgumentException("posOrStackDepthFun was Left(null)")
case _ =>
}

val position: Option[source.Position] = posOrStackDepthFun.left.toOption
Expand Down
Expand Up @@ -169,7 +169,7 @@ private[scalatest] object StackDepthExceptionHelper extends Serializable {
if (sdf == null) throw new NullArgumentException("sdf was null")
pos match {
case Some(null) => throw new NullArgumentException("pos was Some(null)")
case _ =>
case None =>
}
pos match {
case Some(pos) => Left(pos)
Expand Down
117 changes: 46 additions & 71 deletions scalatest/src/main/scala/org/scalatest/tools/JUnitXmlReporter.scala
Expand Up @@ -47,10 +47,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
private val events = Set.empty[Event]
private val propertiesXml = genPropertiesXml

//
// Records events in 'events' set. Generates xml from events upon receipt
// of SuiteCompleted or SuiteAborted events.
//
// Records events in 'events' set. Generates xml from events upon receipt of SuiteCompleted or SuiteAborted events.
def apply(event: Event): Unit = {
events += event

Expand All @@ -65,10 +62,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
}
}

//
// Writes the xml file for a single test suite. Removes processed
// events from the events Set as they are used.
//
// Writes the xml file for a single test suite. Removes processed events from the events Set as they are used.
private def writeSuiteFile(endEvent: Event, suiteId: String): Unit = {
require(endEvent.isInstanceOf[SuiteCompleted] ||
endEvent.isInstanceOf[SuiteAborted])
Expand All @@ -82,22 +76,24 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
out.close()
}

//
// Constructs a Testsuite object corresponding to a specified
// SuiteCompleted or SuiteAborted event.
//
// Scans events reported so far and builds the Testsuite from events
// associated with the specified suite. Removes events from
// the class's events Set as they are consumed.
//
// Only looks at events that have the same ordinal prefix as the
// end event being processed (where an event's ordinal prefix is its
// ordinal list with last element removed). Events with the same
// prefix get processed sequentially, so filtering this way eliminates
// events from any nested suites being processed concurrently
// that have not yet completed when the parent's SuiteCompleted or
// SuiteAborted event is processed.
//

/*
Constructs a Testsuite object corresponding to a specified
SuiteCompleted or SuiteAborted event.

Scans events reported so far and builds the Testsuite from events
associated with the specified suite. Removes events from
the class's events Set as they are consumed.

Only looks at events that have the same ordinal prefix as the
end event being processed (where an event's ordinal prefix is its
ordinal list with last element removed). Events with the same
prefix get processed sequentially, so filtering this way eliminates
events from any nested suites being processed concurrently
that have not yet completed when the parent's SuiteCompleted or
SuiteAborted event is processed.
*/

private def getTestsuite(endEvent: Event, suiteId: String): Testsuite = {
require(endEvent.isInstanceOf[SuiteCompleted] ||
endEvent.isInstanceOf[SuiteAborted])
Expand Down Expand Up @@ -208,21 +204,21 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
testsuite
}

//
// Finds the indexes for the SuiteStarted and SuiteCompleted or
// SuiteAborted endpoints of a test suite within an ordered array of
// events, given the terminating SuiteCompleted or SuiteAborted event.
//
// Searches sequentially through the array to find the specified
// SuiteCompleted event and its preceding SuiteStarting event.
//
// (The orderedEvents array does not contain any SuiteStarting events
// from nested suites running concurrently because of the ordinal-prefix
// filtering performed in getTestsuite(). It does not contain any from
// nested suites running sequentially because those get removed when they
// are processed upon occurrence of their corresponding SuiteCompleted
// events.)
//
/*
Finds the indexes for the SuiteStarted and SuiteCompleted or
SuiteAborted endpoints of a test suite within an ordered array of
events, given the terminating SuiteCompleted or SuiteAborted event.

Searches sequentially through the array to find the specified
SuiteCompleted event and its preceding SuiteStarting event.

(The orderedEvents array does not contain any SuiteStarting events
from nested suites running concurrently because of the ordinal-prefix
filtering performed in getTestsuite(). It does not contain any from
nested suites running sequentially because those get removed when they
are processed upon occurrence of their corresponding SuiteCompleted
events.)
*/
private def locateSuite(orderedEvents: Array[Event],
endEvent: Event):
(Int, Int) = {
Expand Down Expand Up @@ -272,14 +268,15 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
private def idxAdjustmentForRecordedEvents(recordedEvents: collection.immutable.IndexedSeq[RecordableEvent]) =
recordedEvents.filter(e => e.isInstanceOf[InfoProvided] || e.isInstanceOf[MarkupProvided]).size

//
// Constructs a Testcase object from events in orderedEvents array.
//
// Accepts a TestStarting event and its index within orderedEvents.
// Returns a Testcase object plus the index to its corresponding
// test completion event. Removes events from class's events Set
// as they are processed.
//
/*
Constructs a Testcase object from events in orderedEvents array.

Accepts a TestStarting event and its index within orderedEvents.
Returns a Testcase object plus the index to its corresponding
test completion event. Removes events from class's events Set
as they are processed.
*/

private def processTest(orderedEvents: Array[Event],
startEvent: TestStarting, startIndex: Int):
(Int, Testcase) = {
Expand Down Expand Up @@ -337,9 +334,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
(endIndex, testcase)
}

//
// Creates an xml string describing a run of a test suite.
//
def xmlify(testsuite: Testsuite): String = {
val xmlVal =
<testsuite
Expand Down Expand Up @@ -384,10 +379,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + withCDATA
}

//
// Returns string representation of stack trace for specified Throwable,
// including any nested exceptions.
//
// Returns string representation of stack trace for specified Throwable, including any nested exceptions.
def getStackTrace(throwable: Throwable): String = {
"" + throwable +
Array.concat(throwable.getStackTrace).mkString("\n at ",
Expand All @@ -401,10 +393,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
}
}

//
// Generates <failure> xml for TestFailed event, if specified Option
// contains one.
//
// Generates <failure> xml for TestFailed event, if specified Option contains one.
private def failureXml(failureOption: Option[TestFailed]): xml.NodeSeq = {
failureOption match {
case None =>
Expand All @@ -428,10 +417,8 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
}
}

//
// Returns toString value of option contents if Some, or empty string if
// None.
//
private def strVal(option: Option[Any]): String = {
option match {
case Some(x) => "" + x
Expand All @@ -450,9 +437,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
case e: UnknownHostException => "unknown"
}

//
// Generates <properties> element of xml.
//
private def genPropertiesXml: xml.Elem = {
val sysprops = System.getProperties

Expand All @@ -464,9 +449,7 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
</properties>
}

//
// Returns a list of the names of properties in a Properties object.
//
private def propertyNames(props: Properties): List[String] = {
val listBuf = new ListBuffer[String]

Expand All @@ -478,35 +461,27 @@ private[scalatest] class JUnitXmlReporter(directory: String) extends Reporter {
listBuf.toList
}

//
// Formats timestamp into a string for display, e.g. "2009-08-31T14:59:37"
//
private def formatTimeStamp(timeStamp: Long): String = {
val dateFmt = new SimpleDateFormat("yyyy-MM-dd")
val timeFmt = new SimpleDateFormat("HH:mm:ss")
dateFmt.format(timeStamp) + "T" + timeFmt.format(timeStamp)
}

//
// Throws an exception if an unexpected Event is encountered.
//
def unexpected(event: Event): Unit = {
throw new RuntimeException("unexpected event [" + event + "]")
}

//
// Class to hold information about an execution of a test suite.
//
private[scalatest] case class Testsuite(name: String, timeStamp: Long) {
var errors = 0
var failures = 0
var time = 0L
val testcases = new ListBuffer[Testcase]
}

//
// Class to hold information about an execution of a testcase.
//
private[scalatest] case class Testcase(name: String, className: Option[String],
timeStamp: Long) {
var time = 0L
Expand Down