Skip to content

Commit

Permalink
Copied from poc implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bigwheel committed Oct 11, 2018
1 parent 23acad3 commit cf30a99
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: scala

scala:
- 2.12.6

script:
- sbt test

before_cache:
- find $HOME/.ivy2/cache -name "ivydata-*.properties" -print -delete
- find $HOME/.sbt -name "*.lock" -print -delete
36 changes: 36 additions & 0 deletions build.sbt
@@ -0,0 +1,36 @@
name := "scalatest-structured-before"
organization := "com.github.bigwheel"
version := "1.0"
scalaVersion := "2.12.6"

libraryDependencies ++= Seq(
"org.scalactic" %% "scalactic" % "[3.0.5,)",
"org.scalatest" %% "scalatest" % "[3.0.5,)",
"org.scalaz" %% "scalaz-core" % "[7.2.26,)",
)

// about maven publish
publishMavenStyle := true
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
publishArtifact in Test := false
licenses := Seq("BSD-3-Clause" -> url("https://github.com/bigwheel/scalatest-structured-before/blob/master/LICENSE"))
homepage := Some(url("https://github.com/bigwheel/scalatest-structured-before"))
pomExtra := (
<scm>
<url>git@github.com:bigwheel/scalatest-structured-before.git</url>
<connection>scm:git:git@github.com:bigwheel/scalatest-structured-before.git</connection>
</scm>
<developers>
<developer>
<id>bigwheel</id>
<name>k.bigwheel</name>
<url>https://github.com/bigwheel</url>
</developer>
</developers>
)
2 changes: 1 addition & 1 deletion project/build.properties
@@ -1 +1 @@
sbt.version = 1.2.1
sbt.version = 1.2.3
49 changes: 49 additions & 0 deletions src/main/scala/com/github/bigwheel/scalatest/FunSpecEx.scala
@@ -0,0 +1,49 @@
package com.github.bigwheel.scalatest

import org.scalatest._
import org.scalactic._

import scalaz._
import Scalaz._

class FunSpecEx extends FunSpec {

private[this] type Description = (String, () => Unit)
private[this] type DescriptionOrTestTitle = \/[Description, String]
private[this] var tree: Tree[DescriptionOrTestTitle] = ("", () => ()).left[String].node()

override protected val it: ItWord = new ItWord() {
override def apply(specText: String, testTags: Tag*)(testFun: => Any /* Assertion */)(implicit pos: source.Position): Unit = {
tree = tree.loc.insertDownLast(\/-(specText).asInstanceOf[DescriptionOrTestTitle].leaf).toTree
super.apply(specText, testTags: _*)(testFun)(pos)
}
}

protected[this] def describeWithBefore(description: String)
(before: => Unit)
(fun: => Unit)
(implicit pos: org.scalactic.source.Position): Unit = {
val backup = tree
tree = (description, before _).left[String].node()
super.describe(description)(fun)(pos)
tree = backup.loc.insertDownLast(tree).toTree
}

protected override def runTest(testName: String, args: Args): Status = {
val targetTestLeaf: TreeLoc[DescriptionOrTestTitle] = tree.loc.find { treeLoc =>
val strOfDescriptionAndTest = treeLoc.path.reverse.map {
case -\/(yyy) => yyy._1
case \/-(xxx) => xxx
}
strOfDescriptionAndTest.tail.mkString(" ") == testName
}.get
val descriptions: Seq[DescriptionOrTestTitle] = targetTestLeaf.path.reverse
val befores: Seq[() => Unit] = descriptions.map {
case -\/(yyy) => yyy._2
case \/-(_) => () => ()
}
for (before <- befores) before()
super.runTest(testName, args)
}

}
36 changes: 36 additions & 0 deletions src/test/scala/com/github/bigwheel/scalatest/FunSpecExSpec.scala
@@ -0,0 +1,36 @@
package com.github.bigwheel.scalatest

import org.scalatest.Matchers

class FunSpecExSpec extends FunSpecEx with Matchers {

var subject: Int = _

describeWithBefore("When initialized by 0"){ subject = 0 } {
it("is 0") {
subject should be(0)
}

describeWithBefore("when + 5"){ subject += 5 } {
it("is 5") {
subject should be(5)
}

describeWithBefore("then - 3"){ subject -= 3 } {
it("is 2") {
subject should be(2)
}
it("also is even") {
(2 % 2) should be (0)
}
}

describeWithBefore("and * -1"){ subject *= -1 } {
it("is -5") {
subject should be(-5)
}
}
}
}

}

0 comments on commit cf30a99

Please sign in to comment.