Skip to content

Commit

Permalink
Merge pull request #263 from com-lihaoyi/topic/merge
Browse files Browse the repository at this point in the history
Support concatenation and grouping of `Tests` instances
  • Loading branch information
japgolly authored Sep 6, 2022
2 parents eae17c7 + 7c91264 commit 257ab4b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,12 @@ libraries are currently at.
Changelog
=========

0.8.1
-----

* Add `++` to `Tests` so that test suites can be concatenated
* Add `.prefix(name: String)` to `Tests` to nest all of its tests under a single test group with a given name

0.8.0
-----

Expand Down
28 changes: 27 additions & 1 deletion utest/src/utest/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,31 @@ import scala.collection.mutable
* test to run you can feed the `List[Int]` path of that test in the `nameTree`
* into the `callTree` to execute it and return the result.
*/
case class Tests(nameTree: Tree[String], callTree: TestCallTree)
case class Tests(nameTree: Tree[String], callTree: TestCallTree) {

def prefix(name: String): Tests = {
val newNameTree = Tree(nameTree.value, nameTree.children.map(_.prefix(name)): _*)
val newCallTree = callTree.mapInner {
case Right(ts) => Right(ts.map(_.prefix))
case l@ Left(_) => l
}
Tests(newNameTree, newCallTree)
}

def ++(t: Tests): Tests = {
val newNameTree = Tree(nameTree.value, (nameTree.children ++ t.nameTree.children): _*)
val newCallTree = new TestCallTree({
val a = callTree.evalInner()
val b = t.callTree.evalInner()
(a, b) match {
case (Right(x), Right(y)) => Right(x ++ y)
case (Left (_), Right(y)) => Right(y)
case (Right(x), Left (_)) => Right(x)
case (Left (_), Left (y)) => Left(y)
}
})
Tests(newNameTree, newCallTree)
}
}

object Tests extends TestsVersionSpecific
12 changes: 11 additions & 1 deletion utest/src/utest/framework/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ object TestPath{
* executable, which when run either returns a Left(result) or a
* Right(sequence) of child nodes which you can execute.
*/
class TestCallTree(inner: => Either[Any, IndexedSeq[TestCallTree]]){
class TestCallTree(inner: => Either[Any, IndexedSeq[TestCallTree]]) {

def evalInner() =
inner

def mapInner(f: Either[Any, IndexedSeq[TestCallTree]] => Either[Any, IndexedSeq[TestCallTree]]): TestCallTree =
new TestCallTree(f(inner))

def prefix: TestCallTree =
new TestCallTree(Right(IndexedSeq.empty[TestCallTree] :+ this))

/**
* Runs the test in this [[TestCallTree]] at the specified `path`. Called
* by the [[TestTreeSeq.run]] method and usually not called manually.
Expand Down
3 changes: 3 additions & 0 deletions utest/src/utest/framework/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ case class Tree[+T](value: T, children: Tree[T]*){
else children.toIterator.flatMap(_.leaves)
}

def prefix[A >: T](p: A): Tree[A] =
Tree(p, this)

}
44 changes: 44 additions & 0 deletions utest/test/src/test/utest/MergeTestsTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package test.utest

import utest.framework.Tree
import utest._

abstract class MergeSubTests1 extends TestSuite {
var run = Vector.empty[Int]
override def tests = Tests {
"one" - { run :+= 1 }
test("two") { run :+= 2 }
"three" - { run :+= 3 }
}
}

abstract class MergeSubTests2 extends TestSuite {
var run = Vector.empty[Int]
override def tests = Tests {
"one" - { run :+= 1 }
test("two") { run :+= 2 }
}
}

object MergeTestsTest extends TestSuite {

val x = new MergeSubTests1 {}
val y = new MergeSubTests2 {}

val local = Tests {
"makeSureTestsRan" - {
test("x") { assert(x.run == Vector(1, 2, 3) ) }
test("y") { assert(y.run == Vector(1, 2) ) }
}
}

override def tests =
x.tests.prefix("fst") ++
y.tests.prefix("snd") ++
local

override def utestAfterAll(): Unit = {
assert(x.run == Vector(1, 2, 3))
assert(y.run == Vector(1, 2))
}
}

0 comments on commit 257ab4b

Please sign in to comment.