Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Split Harness into Test and Section, add testOnly #28

Merged
merged 5 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions benchmarks/src/main/scala/testz/benchmarks/PureBenchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ class BulkPureBenchmarks {
var numSuites: Int = _

object FailedSuite {
def tests[T](harness: Harness[T]): T = {
import harness._

namedSection("long, long section name")(
def tests[T](test: Test[Result, T], section: Section[T]): T = {
section.named("long, long section name")(
test("test number 0")(() => Fail()),
List.tabulate(perSuite - 1)(n =>
test("test number " + (n + 1))(() => Fail())
Expand All @@ -78,10 +76,8 @@ class BulkPureBenchmarks {
}

object SucceededSuite {
def tests[T](harness: Harness[T]): T = {
import harness._

namedSection("long, long section name")(
def tests[T](test: Test[Result, T], section: Section[T]): T = {
section.named("long, long section name")(
test("test number 0")(() => Succeed()),
List.tabulate(perSuite - 1)(n =>
test("test number " + (n + 1))(() => Succeed())
Expand All @@ -99,13 +95,15 @@ class BulkPureBenchmarks {
else
(Console.print(_), () => ())

val harness = PureHarness.makeFromPrinter(
val test = PureHarness.test(
(res, ls) => runner.printStrs(runner.printTest(res, ls), print)
)

val section = PureHarness.section

val suites: List[() => Future[TestOutput]] =
List.fill(numSuites)(() =>
Future.successful(FailedSuite.tests(harness)((), Nil))
Future.successful(FailedSuite.tests(test, section)((), Nil))
)

val result = Await.result(runner(suites, print, global), Duration.Inf)
Expand All @@ -124,13 +122,15 @@ class BulkPureBenchmarks {
else
(Console.print(_), () => ())

val harness = PureHarness.makeFromPrinter(
val test = PureHarness.test(
(res, ls) => runner.printStrs(runner.printTest(res, ls), print)
)

val section = PureHarness.section

val suites: List[() => Future[TestOutput]] =
List.fill(numSuites)(() =>
Future.successful(SucceededSuite.tests(harness)((), Nil))
Future.successful(SucceededSuite.tests(test, section)((), Nil))
)

val result = Await.result(runner(suites, print, global), Duration.Inf)
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ val specs2JVM = specs2.jvm
val specs2JS = specs2.js

val extras = crossProject(JSPlatform, JVMPlatform).in(file("extras"))
.dependsOn(core, resource)
.dependsOn(core, resource, stdlib)
.settings(name := "testz-extras")
.settings(standardSettings ++ publishSettings)
.enablePlugins(AutomateHeaderPlugin)
Expand Down
68 changes: 25 additions & 43 deletions core/shared/src/main/scala/testz/core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@

package testz

/**
* I *hate* having to define this in testz,
* but it's utterly basic.
*/
abstract class NT[F[_], G[_]] {
def apply[A](fa: F[A]): G[A]
}

/**
A type for test results.
A two-branch sum, either `Succeed()`, or `Fail()`.
Expand All @@ -38,7 +46,7 @@ sealed abstract class Result

final class Fail private() extends Result {
override val toString: String = "Fail"
override def equals(other: Any): Boolean = other.asInstanceOf[AnyRef] eq this
override def equals(other: Any): Boolean = other.isInstanceOf[Fail]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this faster?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more portable from what I can tell.

}

object Fail {
Expand All @@ -49,7 +57,7 @@ object Fail {

final class Succeed private() extends Result {
override val toString: String = "Succeed"
override def equals(other: Any): Boolean = other.asInstanceOf[AnyRef] eq this
override def equals(other: Any): Boolean = other.isInstanceOf[Succeed]
}

object Succeed {
Expand All @@ -64,49 +72,23 @@ object Result {
else Fail()
}

/**
The most boring test `Harness` you can think of:
pure tests, with sections.
Any harness type should be convertible to a `Harness`;
it's the lingua franca of tests. If you write tests using
`Harness`, they can be adapted to work with any suite type later.
*/
abstract class Harness[T] {
def test(name: String)(assertions: () => Result): T

def namedSection(name: String)(test1: T, tests: T*): T

def section(test1: T, tests: T*): T
}

/**
A test harness with test results in the effect `F[_]`.
*/
abstract class EffectHarness[F[_], T] {
def test(name: String)(assertions: () => F[Result]): T
abstract class Test[R, T] { self =>
def apply(name: String)(assertions: () => R): T

def namedSection(name: String)(test1: T, tests: T*): T

def section(test1: T, tests: T*): T
}

object EffectHarness {
def toHarness[F[_], T]
(self: EffectHarness[F, T])
(pure: Result => F[Result]): Harness[T] = new Harness[T] {
def test
(name: String)
(assertions: () => Result)
: T = self.test(name)(() => pure(assertions()))

def namedSection
(name: String)
(test1: T, tests: T*)
: T = self.namedSection(name)(test1, tests: _*)
final def contramap[S](f: S => R): Test[S, T] =
new Test[S, T] {
def apply(name: String)(assertions: () => S): T =
self(name)(() => f(assertions()))
}

def section
(test1: T, tests: T*)
: T = self.section(test1, tests: _*)
final def map[U](f: T => U): Test[R, U] =
new Test[R, U] {
def apply(name: String)(assertions: () => R): U =
f(self(name)(assertions))
}
}

abstract class Section[T] {
def named(name: String)(test1: T, tests: T*): T
def apply(test1: T, tests: T*): T
}
1 change: 1 addition & 0 deletions core/shared/src/main/scala/testz/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

package object testz {
type Id[A] = A
def assert(b: Boolean): Result =
if (b) Succeed() else Fail()
}
85 changes: 42 additions & 43 deletions extras/shared/src/main/scala/testz/extras/DocHarness.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,57 @@
package testz
package extras

import resource._

import scala.collection.mutable.ListBuffer

object DocHarness {
type Uses[R] = (String, ListBuffer[String]) => Unit

def makeEffR[F[_]]: EffectResourceHarness[F, Uses] =
new EffectResourceHarness[F, Uses] {
def test[R]
(name: String)
(assertions: R => F[Result])
: Uses[R] = {
(indent, buf) =>
buf += (indent + " " + name)
}

def namedSection[R](
name: String
)(
test1: Uses[R],
tests: Uses[R]*
): Uses[R] = {
(indent, buf) =>
val newIndent = indent + " "
buf += (newIndent + "[" + name + "]")
test1(newIndent, buf)
tests.foreach(_(newIndent, buf))
}
def test[R]: Test[R, Uses[Unit]] =
rTest[R].toTest[Unit]

def section[R](
test1: Uses[R],
tests: Uses[R]*
): Uses[R] = {
(indent, buf) =>
val newIndent = indent + " "
test1(newIndent, buf)
tests.foreach(_(newIndent, buf))
}
def rTest[R]: RTest[R, Uses] = new RTest[R, Uses] {
def apply[Resource]
(name: String)
(assertions: Resource => R)
: Uses[Resource] =
(indent, buf) => buf += (indent + " " + name)
}

def bracket[R, I]
(init: () => F[I])
(cleanup: I => F[Unit])
(tests: Uses[(I, R)]
): Uses[R] = tests
val rSection: RSection[Uses] = new RSection[Uses] {
def named[Resource](
name: String
)(
test1: Uses[Resource],
tests: Uses[Resource]*
): Uses[Resource] = {
(indent, buf) =>
val newIndent = indent + " "
buf += (newIndent + "[" + name + "]")
test1(newIndent, buf)
tests.foreach(_(newIndent, buf))
}

def makeEff[F[_]]: EffectHarness[F, Uses[Unit]] =
EffectResourceHarness.toEffectHarness(makeEffR[F])

def makeR: ResourceHarness[Uses] =
EffectResourceHarness.toResourceHarness(makeEffR[λ[X => X]])
def apply[Resource](
test1: Uses[Resource],
tests: Uses[Resource]*
): Uses[Resource] = {
(indent, buf) =>
val newIndent = indent + " "
test1(newIndent, buf)
tests.foreach(_(newIndent, buf))
}
}

def make: Harness[DocHarness.Uses[Unit]] =
EffectHarness.toHarness[λ[X => X], Uses[Unit]](makeEff[λ[X => X]])(result => result)
val section: Section[Uses[Unit]] =
rSection.toSection[Unit]

def bracket[F[_]]: Bracket[Uses, F] = new Bracket[Uses, F] {
def apply[Resource, I]
(init: () => F[I])
(cleanup: I => F[Unit])
(tests: Uses[(I, Resource)]
): Uses[Resource] = tests
}
}
62 changes: 62 additions & 0 deletions extras/shared/src/main/scala/testz/extras/TestOnly.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2018, Edmund Noble
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package testz
package extras

import scala.concurrent.Future

import runner.TestOutput

object TestOnly {
def apply[X, T](pred: X => Boolean)
(grab: (X => T) => T)
(ze: T)(in: T)
: T = {
grab(ls => if (pred(ls)) in else ze)
}

def pure(pred: List[String] => Boolean): PureHarness.Uses NT PureHarness.Uses =
new (PureHarness.Uses NT PureHarness.Uses) {
def apply[R](in: PureHarness.Uses[R]): PureHarness.Uses[R] =
TestOnly[List[String], PureHarness.Uses[R]](pred)(
f => (r, ls) => f(ls)(r, ls)
)((_, _) => TestOutput.empty)(in)
}

def future(pred: List[String] => Boolean): FutureHarness.Uses NT FutureHarness.Uses =
new (FutureHarness.Uses NT FutureHarness.Uses) {
def apply[R](in: FutureHarness.Uses[R]): FutureHarness.Uses[R] =
TestOnly[List[String], FutureHarness.Uses[R]](pred)(
f => (r, ls) => f(ls)(r, ls)
)((_, _) => Future.successful(TestOutput.empty))(in)
}

}
Loading