Skip to content

Commit 950d229

Browse files
committed
Add assertEqWithTolerance for Instant, and assertThrows{,A}
Closes #180
1 parent 882d76b commit 950d229

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

project/Build.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ object Microlibs {
217217
Dep.univEq .value,
218218
Dep.univEqCats.value,
219219
))
220+
.jsSettings(libraryDependencies += Dep.scalaJsJavaTime.value % Test)
220221

221222
lazy val typesJVM = types.jvm
222223
lazy val typesJS = types.js

project/Dependencies.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Dependencies {
1818
def kindProjector = "0.13.2"
1919
def nyaya = "1.0.0"
2020
def scalaCheck = "1.15.4"
21-
def scalaJsJavaTime = "1.0.0"
21+
def scalaJsJavaTime = "2.4.0"
2222
def utest = "0.8.0"
2323
}
2424

@@ -31,7 +31,7 @@ object Dependencies {
3131
val nyayaTest = Def.setting("com.github.japgolly.nyaya" %%% "nyaya-test" % Ver.nyaya)
3232
val scalaCheck = Def.setting("org.scalacheck" %%% "scalacheck" % Ver.scalaCheck)
3333
val scalaCompiler = Def.setting("org.scala-lang" % "scala-compiler" % scalaVersion.value)
34-
val scalaJsJavaTime = Def.setting("org.scala-js" %%% "scalajs-java-time" % Ver.scalaJsJavaTime cross CrossVersion.for3Use2_13)
34+
val scalaJsJavaTime = Def.setting("io.github.cquiroz" %%% "scala-java-time" % Ver.scalaJsJavaTime)
3535
val sourceCode = Def.setting("com.lihaoyi" %%% "sourcecode" % Ver.sourceCode)
3636
val univEq = Def.setting("com.github.japgolly.univeq" %%% "univeq" % Ver.univEq)
3737
val univEqCats = Def.setting("com.github.japgolly.univeq" %%% "univeq-cats" % Ver.univEq)

test-util/shared/src/main/scala/japgolly/microlibs/testutil/TestUtil.scala

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import japgolly.microlibs.testutil.TestUtilInternals._
66
import japgolly.univeq.UnivEq
77
import japgolly.univeq.UnivEqCats.catsEqFromUnivEq
88
import java.io.ByteArrayOutputStream
9+
import java.time.{Duration, Instant}
910
import scala.annotation.tailrec
1011
import scala.io.AnsiColor._
12+
import scala.reflect.ClassTag
1113
import sourcecode.Line
1214

1315
trait TestUtilWithoutUnivEq
@@ -442,15 +444,81 @@ trait TestUtilWithoutUnivEq
442444
println(
443445
s"""
444446
|${YELLOW_B}${BLACK}assertEqWithTolerance failed:$RESET$titleSuffix
445-
|${BOLD_BRIGHT_GREEN}expect: $expect$RESET
446-
|${BOLD_BRIGHT_RED}actual: $actual$RESET
447-
|${BOLD_BRIGHT_RED} delta: $d$RESET
448-
|$YELLOW tol: $tolerance$RESET
447+
|${BOLD_BRIGHT_GREEN} expect: $expect$RESET
448+
|${BOLD_BRIGHT_RED} actual: $actual$RESET
449+
|${BOLD_BRIGHT_RED} delta: $d$RESET
450+
|${YELLOW}tolerance: $tolerance$RESET
449451
|""".stripMargin)
450452
fail(s"$errorPrefix$actual$expect by $d which exceeds tolerance of $tolerance")
451453
}
452454
}
453455

456+
457+
def assertEqWithTolerance(actual: Instant, expect: Instant)(implicit l: Line): Unit =
458+
_assertEqWithToleranceInstant(None, actual, expect)
459+
460+
def assertEqWithTolerance(name: => String, actual: Instant, expect: Instant)(implicit l: Line): Unit =
461+
_assertEqWithToleranceInstant(Some(name), actual, expect)
462+
463+
def assertEqWithTolerance(actual: Instant, expect: Instant, tolerance: Duration)(implicit l: Line): Unit =
464+
_assertEqWithToleranceInstant(None, actual, expect, tolerance)
465+
466+
def assertEqWithTolerance(name: => String, actual: Instant, expect: Instant, tolerance: Duration)(implicit l: Line): Unit =
467+
_assertEqWithToleranceInstant(Some(name), actual, expect, tolerance)
468+
469+
private def _assertEqWithToleranceInstant(_name: => Option[String], actual: Instant, expect: Instant, tolerance: Duration = Duration.ofMillis(100))(implicit l: Line): Unit = {
470+
val d = Duration.between(actual, expect).abs()
471+
if (d.compareTo(tolerance) > 0) {
472+
val name = _name
473+
val titleSuffix = name.fold("")(n => s" ${BOLD_BRIGHT_YELLOW}$n$RESET")
474+
val errorPrefix = name.fold("")(n => s"[$n] ")
475+
println(
476+
s"""
477+
|${YELLOW_B}${BLACK}assertEqWithTolerance failed:$RESET$titleSuffix
478+
|${BOLD_BRIGHT_GREEN} expect: $expect$RESET
479+
|${BOLD_BRIGHT_RED} actual: $actual$RESET
480+
|${BOLD_BRIGHT_RED} delta: $d$RESET
481+
|${YELLOW}tolernace: $tolerance$RESET
482+
|""".stripMargin)
483+
fail(s"$errorPrefix$actual$expect by $d which exceeds tolerance of $tolerance")
484+
}
485+
}
486+
487+
def assertThrows(body: => Any)(implicit l: Line): Throwable =
488+
_assertThrows("assertThrows", None, body)
489+
490+
def assertThrows(name: => String, body: => Any)(implicit l: Line): Throwable =
491+
_assertThrows("assertThrows", Some(name), body)
492+
493+
private def _assertThrows(method: String, name: => Option[String], body: => Any)(implicit l: Line): Throwable = {
494+
val error: Throwable =
495+
try {
496+
body
497+
null
498+
} catch {
499+
case t: Throwable => t
500+
}
501+
if (error eq null)
502+
fail(s"${descMethod(method, name)} failed. No exception was thrown.")
503+
else
504+
error
505+
}
506+
507+
def assertThrowsA[T <: Throwable: ClassTag](body: => Any)(implicit l: Line): T =
508+
_assertThrowsA[T](None, body)
509+
510+
def assertThrowsA[T <: Throwable: ClassTag](name: => String, body: => Any)(implicit l: Line): T =
511+
_assertThrowsA[T](Some(name), body)
512+
513+
private def _assertThrowsA[T <: Throwable](name: => Option[String], body: => Any)(implicit l: Line, ct: ClassTag[T]): T = {
514+
val method = "assertThrowsA"
515+
_assertThrows(method, name, body) match {
516+
case t: T => t
517+
case t =>
518+
t.printStackTrace()
519+
fail(s"${descMethod(method, name)} failed. ${t.getClass.getName} is not an instance of ${ct.runtimeClass.getName}")
520+
}
521+
}
454522
}
455523

456524
trait TestUtil

test-util/shared/src/test/scala/japgolly/microlibs/testutil/TestUtilTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package japgolly.microlibs.testutil
22

3+
import java.time.{Duration, Instant}
34
import utest._
45

56
object TestUtilTest extends TestSuite {
@@ -20,7 +21,20 @@ object TestUtilTest extends TestSuite {
2021
case _: java.lang.AssertionError => ()
2122
}
2223
}
24+
}
25+
26+
"assertEqWithToleranceDouble" - {
27+
assertEqWithTolerance(10, 12, 2)
28+
assertEqWithTolerance(10, 12, 3)
29+
assertThrows(assertEqWithTolerance(10, 12, 1))
30+
}
2331

32+
"assertEqWithToleranceInstant" - {
33+
val a = Instant.now()
34+
val b = a.plus(Duration.ofSeconds(2))
35+
assertEqWithTolerance(a, b, Duration.ofSeconds(2))
36+
assertEqWithTolerance(a, b, Duration.ofSeconds(3))
37+
assertThrows(assertEqWithTolerance(a, b, Duration.ofSeconds(1)))
2438
}
2539
}
2640
}

0 commit comments

Comments
 (0)