@@ -6,8 +6,10 @@ import japgolly.microlibs.testutil.TestUtilInternals._
6
6
import japgolly .univeq .UnivEq
7
7
import japgolly .univeq .UnivEqCats .catsEqFromUnivEq
8
8
import java .io .ByteArrayOutputStream
9
+ import java .time .{Duration , Instant }
9
10
import scala .annotation .tailrec
10
11
import scala .io .AnsiColor ._
12
+ import scala .reflect .ClassTag
11
13
import sourcecode .Line
12
14
13
15
trait TestUtilWithoutUnivEq
@@ -442,15 +444,81 @@ trait TestUtilWithoutUnivEq
442
444
println(
443
445
s """
444
446
| ${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
449
451
| """ .stripMargin)
450
452
fail(s " $errorPrefix$actual ≠ $expect by $d which exceeds tolerance of $tolerance" )
451
453
}
452
454
}
453
455
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
+ }
454
522
}
455
523
456
524
trait TestUtil
0 commit comments