-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Assertion.scala
843 lines (728 loc) · 27.7 KB
/
Assertion.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
* Copyright 2019-2022 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zio.test
import zio.test.AssertionM.RenderParam
import zio.{Cause, Exit, ZIO}
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
/**
* An `Assertion[A]` is capable of producing assertion results on an `A`. As a
* proposition, assertions compose using logical conjunction and disjunction,
* and can be negated.
*/
final class Assertion[-A] private (
val render: Assertion.Render,
val run: (=> A) => AssertResult
) extends AssertionM[A]
with ((=> A) => AssertResult) { self =>
import zio.test.Assertion.Render._
def runM: (=> A) => AssertResultM = a => BoolAlgebraM(ZIO.succeed(run(a)))
/**
* Returns a new assertion that succeeds only if both assertions succeed.
*/
def &&[A1 <: A](that: => Assertion[A1]): Assertion[A1] =
new Assertion(infix(param(self), "&&", param(that)), actual => self.run(actual) && that.run(actual))
/**
* A symbolic alias for `label`.
*/
override def ??(string: String): Assertion[A] =
label(string)
/**
* Returns a new assertion that succeeds if either assertion succeeds.
*/
def ||[A1 <: A](that: => Assertion[A1]): Assertion[A1] =
new Assertion(infix(param(self), "||", param(that)), actual => self.run(actual) || that.run(actual))
/**
* Evaluates the assertion with the specified value.
*/
def apply(a: => A): AssertResult =
run(a)
override def canEqual(that: AssertionM[_]): Boolean = that match {
case _: Assertion[_] => true
case _ => false
}
override def equals(that: Any): Boolean = that match {
case that: Assertion[_] if that.canEqual(this) => this.toString == that.toString
case _ => false
}
override def hashCode: Int =
toString.hashCode
/**
* Labels this assertion with the specified string.
*/
override def label(string: String): Assertion[A] =
new Assertion(infix(param(self), "??", param(quoted(string))), run)
/**
* Returns the negation of this assertion.
*/
override def negate: Assertion[A] =
Assertion.not(self)
/**
* Tests the assertion to see if it would succeed on the given element.
*/
def test(a: A): Boolean =
run(a).isSuccess
/**
* Provides a meaningful string rendering of the assertion.
*/
override def toString: String =
render.toString
}
object Assertion extends AssertionVariants {
type Render = AssertionM.Render
val Render = AssertionM.Render
import Render._
/**
* Makes a new assertion that always succeeds.
*/
val anything: Assertion[Any] =
Assertion.assertion("anything")()(_ => true)
/**
* Makes a new `Assertion` from a pretty-printing and a function.
*/
def assertion[A](name: String)(params: RenderParam*)(run: (=> A) => Boolean): Assertion[A] = {
lazy val assertion: Assertion[A] = assertionDirect(name)(params: _*) { actual =>
lazy val tryActual = Try(actual)
lazy val result: AssertResult =
if (run(tryActual.get)) BoolAlgebra.success(AssertionValue(assertion, tryActual.get, result))
else BoolAlgebra.failure(AssertionValue(assertion, tryActual.get, result))
result
}
assertion
}
/**
* Makes a new `Assertion` from a pretty-printing and a function.
*/
def assertionDirect[A](
name: String
)(params: RenderParam*)(run: (=> A) => AssertResult): Assertion[A] =
new Assertion(function(name, List(params.toList)), run)
/**
* Makes a new `Assertion[A]` from a pretty-printing, a function `(=> A) =>
* Option[B]`, and an `Assertion[B]`. If the result of applying the function
* to a given value is `Some[B]`, the `Assertion[B]` will be applied to the
* resulting value to determine if the assertion is satisfied. The result of
* the `Assertion[B]` and any assertions it is composed from will be
* recursively embedded in the assert result. If the result of the function is
* `None` the `orElse` parameter will be used to determine whether the
* assertion is satisfied.
*/
def assertionRec[A, B](
name: String
)(params: RenderParam*)(
assertion: Assertion[B]
)(get: (=> A) => Option[B], orElse: AssertionData => AssertResult = _.asFailure): Assertion[A] = {
lazy val resultAssertion: Assertion[A] = assertionDirect(name)(params: _*) { a =>
lazy val tryA = Try(a)
get(tryA.get) match {
case Some(b) =>
val innerResult = assertion.run(b)
lazy val result: AssertResult =
if (innerResult.isSuccess) BoolAlgebra.success(AssertionValue(resultAssertion, tryA.get, result))
else BoolAlgebra.failure(AssertionValue(assertion, b, innerResult))
result
case None =>
orElse(AssertionData(resultAssertion, tryA.get))
}
}
resultAssertion
}
/**
* Makes a new assertion that requires a given numeric value to match a value
* with some tolerance.
*/
def approximatelyEquals[A: Numeric](reference: A, tolerance: A): Assertion[A] =
Assertion.assertion("approximatelyEquals")(param(reference), param(tolerance)) { actual =>
val referenceType = implicitly[Numeric[A]]
val max = referenceType.plus(reference, tolerance)
val min = referenceType.minus(reference, tolerance)
referenceType.gteq(actual, min) && referenceType.lteq(actual, max)
}
/**
* Makes a new assertion that requires an Iterable contain the specified
* element. See [[Assertion.exists]] if you want to require an Iterable to
* contain an element satisfying an assertion.
*/
def contains[A](element: A): Assertion[Iterable[A]] =
Assertion.assertion("contains")(param(element))(_.exists(_ == element))
/**
* Makes a new assertion that requires a `Cause` contain the specified cause.
*/
def containsCause[E](cause: Cause[E]): Assertion[Cause[E]] =
Assertion.assertion("containsCause")(param(cause))(_.contains(cause))
/**
* Makes a new assertion that requires a substring to be present.
*/
def containsString(element: String): Assertion[String] =
Assertion.assertion("containsString")(param(element))(_.contains(element))
/**
* Makes a new assertion that requires an exit value to die.
*/
def dies(assertion: Assertion[Throwable]): Assertion[Exit[Any, Any]] =
Assertion.assertionRec("dies")(param(assertion))(assertion) {
case Exit.Failure(cause) => cause.dieOption
case _ => None
}
/**
* Makes a new assertion that requires an exit value to die with an instance
* of given type (or its subtype).
*/
def diesWithA[E: ClassTag]: Assertion[Exit[E, Any]] =
dies(isSubtype[E](anything))
/**
* Makes a new assertion that requires an exception to have a certain message.
*/
def hasMessage(message: Assertion[String]): Assertion[Throwable] =
Assertion.assertionRec("hasMessage")(param(message))(message)(th => Some(th.getMessage))
/**
* Makes a new assertion that requires an exception to have certain suppressed
* exceptions.
*/
def hasSuppressed(cause: Assertion[Iterable[Throwable]]): Assertion[Throwable] =
Assertion.assertionRec("hasSuppressed")(param(cause))(cause)(th => Some(th.getSuppressed))
/**
* Makes a new assertion that requires an exception to have a certain cause.
*/
def hasThrowableCause(cause: Assertion[Throwable]): Assertion[Throwable] =
Assertion.assertionRec("hasThrowableCause")(param(cause))(cause)(th => Option(th.getCause))
/**
* Makes a new assertion that requires a given string to end with the
* specified suffix.
*/
def endsWith[A](suffix: Seq[A]): Assertion[Seq[A]] =
Assertion.assertion("endsWith")(param(suffix))(_.endsWith(suffix))
/**
* Makes a new assertion that requires a given string to end with the
* specified suffix.
*/
def endsWithString(suffix: String): Assertion[String] =
Assertion.assertion("endsWithString")(param(suffix))(_.endsWith(suffix))
/**
* Makes a new assertion that requires a given string to equal another
* ignoring case.
*/
def equalsIgnoreCase(other: String): Assertion[String] =
Assertion.assertion("equalsIgnoreCase")(param(other))(_.equalsIgnoreCase(other))
/**
* Makes a new assertion that requires an Iterable contain an element
* satisfying the given assertion. See [[Assertion.contains]] if you only need
* an Iterable to contain a given element.
*/
def exists[A](assertion: Assertion[A]): Assertion[Iterable[A]] =
Assertion.assertionRec("exists")(param(assertion))(assertion)(_.find(assertion.test))
/**
* Makes a new assertion that requires an exit value to fail.
*/
def fails[E](assertion: Assertion[E]): Assertion[Exit[E, Any]] =
Assertion.assertionRec("fails")(param(assertion))(assertion) {
case Exit.Failure(cause) => cause.failures.headOption
case _ => None
}
/**
* Makes a new assertion that requires the expression to fail with an instance
* of given type (or its subtype).
*/
def failsWithA[E: ClassTag]: Assertion[Exit[E, Any]] =
fails(isSubtype[E](anything))
/**
* Makes a new assertion that requires an exit value to fail with a cause that
* meets the specified assertion.
*/
def failsCause[E](assertion: Assertion[Cause[E]]): Assertion[Exit[E, Any]] =
Assertion.assertionRec("failsCause")(param(assertion))(assertion) {
case Exit.Failure(cause) => Some(cause)
case _ => None
}
/**
* Makes a new assertion that requires an Iterable contain only elements
* satisfying the given assertion.
*/
def forall[A](assertion: Assertion[A]): Assertion[Iterable[A]] =
Assertion.assertionRec("forall")(param(assertion))(assertion)(
_.find(!assertion.test(_)),
_.asSuccess
)
/**
* Makes a new assertion that requires an Iterable to have the same distinct
* elements as the other Iterable, though not necessarily in the same order.
*/
def hasSameElementsDistinct[A](other: Iterable[A]): Assertion[Iterable[A]] =
Assertion.assertion("hasSameElementsDistinct")(param(other))(actual => actual.toSet == other.toSet)
/**
* Makes a new assertion that requires a sequence to contain an element
* satisfying the given assertion on the given position
*/
def hasAt[A](pos: Int)(assertion: Assertion[A]): Assertion[Seq[A]] =
Assertion.assertionRec("hasAt")(param(assertion))(assertion) { actual =>
if (pos >= 0 && pos < actual.size) {
Some(actual.apply(pos))
} else {
None
}
}
/**
* Makes a new assertion that requires an Iterable contain at least one of the
* specified elements.
*/
def hasAtLeastOneOf[A](other: Iterable[A]): Assertion[Iterable[A]] =
hasIntersection(other)(hasSize(isGreaterThanEqualTo(1)))
/**
* Makes a new assertion that requires an Iterable contain at most one of the
* specified elements.
*/
def hasAtMostOneOf[A](other: Iterable[A]): Assertion[Iterable[A]] =
hasIntersection(other)(hasSize(isLessThanEqualTo(1)))
/**
* Makes a new assertion that focuses in on a field in a case class.
*
* {{{
* hasField("age", _.age, within(0, 10))
* }}}
*/
def hasField[A, B](name: String, proj: A => B, assertion: Assertion[B]): Assertion[A] =
Assertion.assertionRec("hasField")(param(quoted(name)), param(field(name)), param(assertion))(assertion) { actual =>
Some(proj(actual))
}
/**
* Makes a new assertion that requires an Iterable to contain the first
* element satisfying the given assertion.
*/
def hasFirst[A](assertion: Assertion[A]): Assertion[Iterable[A]] =
Assertion.assertionRec("hasFirst")(param(assertion))(assertion)(actual => actual.headOption)
/**
* Makes a new assertion that requires the intersection of two Iterables
* satisfy the given assertion.
*/
def hasIntersection[A](other: Iterable[A])(assertion: Assertion[Iterable[A]]): Assertion[Iterable[A]] =
Assertion.assertionRec("hasIntersection")(param(other))(assertion) { actual =>
val actualSeq = actual.toSeq
val otherSeq = other.toSeq
Some(actualSeq.intersect(otherSeq))
}
/**
* Makes a new assertion that requires a Map to have the specified key with
* value satisfying the specified assertion.
*/
def hasKey[K, V](key: K, assertion: Assertion[V]): Assertion[Map[K, V]] =
Assertion.assertionRec("hasKey")(param(key))(assertion)(_.get(key))
/**
* Makes a new assertion that requires a Map to have the specified key.
*/
def hasKey[K, V](key: K): Assertion[Map[K, V]] =
hasKey(key, anything)
/**
* Makes a new assertion that requires a Map have keys satisfying the
* specified assertion.
*/
def hasKeys[K, V](assertion: Assertion[Iterable[K]]): Assertion[Map[K, V]] =
Assertion.assertionRec("hasKeys")()(assertion)(actual => Some(actual.keys))
/**
* Makes a new assertion that requires an Iterable to contain the last element
* satisfying the given assertion.
*/
def hasLast[A](assertion: Assertion[A]): Assertion[Iterable[A]] =
Assertion.assertionRec("hasLast")(param(assertion))(assertion)(actual => actual.lastOption)
/**
* Makes a new assertion that requires an Iterable contain none of the
* specified elements.
*/
def hasNoneOf[A](other: Iterable[A]): Assertion[Iterable[A]] =
hasIntersection(other)(isEmpty)
/**
* Makes a new assertion that requires an Iterable contain exactly one of the
* specified elements.
*/
def hasOneOf[A](other: Iterable[A]): Assertion[Iterable[A]] =
hasIntersection(other)(hasSize(equalTo(1)))
/**
* Makes a new assertion that requires an Iterable to have the same elements
* as the specified Iterable, though not necessarily in the same order.
*/
def hasSameElements[A](other: Iterable[A]): Assertion[Iterable[A]] =
Assertion.assertion("hasSameElements")(param(other)) { actual =>
val actualSeq = actual.toSeq
val otherSeq = other.toSeq
actualSeq.diff(otherSeq).isEmpty && otherSeq.diff(actualSeq).isEmpty
}
/**
* Makes a new assertion that requires the size of an Iterable be satisfied by
* the specified assertion.
*/
def hasSize[A](assertion: Assertion[Int]): Assertion[Iterable[A]] =
Assertion.assertionRec("hasSize")(param(assertion))(assertion)(actual => Some(actual.size))
/**
* Makes a new assertion that requires the size of a string be satisfied by
* the specified assertion.
*/
def hasSizeString(assertion: Assertion[Int]): Assertion[String] =
Assertion.assertionRec("hasSizeString")(param(assertion))(assertion)(actual => Some(actual.size))
/**
* Makes a new assertion that requires the specified Iterable to be a subset
* of the other Iterable.
*/
def hasSubset[A](other: Iterable[A]): Assertion[Iterable[A]] =
hasIntersection(other)(hasSameElements(other))
/**
* Makes a new assertion that requires a Map have values satisfying the
* specified assertion.
*/
def hasValues[K, V](assertion: Assertion[Iterable[V]]): Assertion[Map[K, V]] =
Assertion.assertionRec("hasValues")()(assertion)(actual => Some(actual.values))
/**
* Makes a new assertion that requires the sum type be a specified term.
*
* {{{
* isCase("Some", Some.unapply, anything)
* }}}
*/
def isCase[Sum, Proj](
termName: String,
term: Sum => Option[Proj],
assertion: Assertion[Proj]
): Assertion[Sum] =
Assertion.assertionRec("isCase")(param(termName), param(unapply(termName)), param(assertion))(assertion)(term(_))
/**
* Makes a new assertion that requires an Iterable is distinct.
*/
val isDistinct: Assertion[Iterable[Any]] = {
@scala.annotation.tailrec
def loop(iterator: Iterator[Any], seen: Set[Any]): Boolean = iterator.hasNext match {
case false => true
case true =>
val x = iterator.next()
if (seen.contains(x)) false else loop(iterator, seen + x)
}
Assertion.assertion("isDistinct")()(actual => loop(actual.iterator, Set.empty))
}
/**
* Makes a new assertion that requires an Iterable to be empty.
*/
val isEmpty: Assertion[Iterable[Any]] =
Assertion.assertion("isEmpty")()(_.isEmpty)
/**
* Makes a new assertion that requires a given string to be empty.
*/
val isEmptyString: Assertion[String] =
Assertion.assertion("isEmptyString")()(_.isEmpty)
/**
* Makes a new assertion that requires a value be false.
*/
def isFalse: Assertion[Boolean] =
Assertion.assertion("isFalse")()(!_)
/**
* Makes a new assertion that requires a Failure value satisfying the
* specified assertion.
*/
def isFailure(assertion: Assertion[Throwable]): Assertion[Try[Any]] =
Assertion.assertionRec("isFailure")(param(assertion))(assertion) {
case Failure(a) => Some(a)
case Success(_) => None
}
/**
* Makes a new assertion that requires a Try value is Failure.
*/
val isFailure: Assertion[Try[Any]] =
isFailure(anything)
/**
* Makes a new assertion that requires the value be greater than the specified
* reference value.
*/
def isGreaterThan[A](reference: A)(implicit ord: Ordering[A]): Assertion[A] =
Assertion.assertion("isGreaterThan")(param(reference))(actual => ord.gt(actual, reference))
/**
* Makes a new assertion that requires the value be greater than or equal to
* the specified reference value.
*/
def isGreaterThanEqualTo[A](reference: A)(implicit ord: Ordering[A]): Assertion[A] =
Assertion.assertion("isGreaterThanEqualTo")(param(reference))(actual => ord.gteq(actual, reference))
/**
* Makes a new assertion that requires an exit value to be interrupted.
*/
def isInterrupted: Assertion[Exit[Any, Any]] =
Assertion.assertion("isInterrupted")() {
case Exit.Failure(cause) => cause.interrupted
case _ => false
}
/**
* Makes a new assertion that requires a Left value satisfying a specified
* assertion.
*/
def isLeft[A](assertion: Assertion[A]): Assertion[Either[A, Any]] =
Assertion.assertionRec("isLeft")(param(assertion))(assertion) {
case Left(a) => Some(a)
case Right(_) => None
}
/**
* Makes a new assertion that requires an Either is Left.
*/
val isLeft: Assertion[Either[Any, Any]] =
isLeft(anything)
/**
* Makes a new assertion that requires the value be less than the specified
* reference value.
*/
def isLessThan[A](reference: A)(implicit ord: Ordering[A]): Assertion[A] =
Assertion.assertion("isLessThan")(param(reference))(actual => ord.lt(actual, reference))
/**
* Makes a new assertion that requires the value be less than or equal to the
* specified reference value.
*/
def isLessThanEqualTo[A](reference: A)(implicit ord: Ordering[A]): Assertion[A] =
Assertion.assertion("isLessThanEqualTo")(param(reference))(actual => ord.lteq(actual, reference))
/**
* Makes a new assertion that requires a numeric value is negative.
*/
def isNegative[A](implicit num: Numeric[A]): Assertion[A] =
isLessThan(num.zero)
/**
* Makes a new assertion that requires a None value.
*/
val isNone: Assertion[Option[Any]] =
Assertion.assertion("isNone")()(_.isEmpty)
/**
* Makes a new assertion that requires an Iterable to be non empty.
*/
val isNonEmpty: Assertion[Iterable[Any]] =
Assertion.assertion("isNonEmpty")()(_.nonEmpty)
/**
* Makes a new assertion that requires a given string to be non empty.
*/
val isNonEmptyString: Assertion[String] =
Assertion.assertion("isNonEmptyString")()(_.nonEmpty)
/**
* Makes a new assertion that requires a null value.
*/
val isNull: Assertion[Any] =
Assertion.assertion("isNull")()(_ == null)
/**
* Makes a new assertion that requires a value to be equal to one of the
* specified values.
*/
def isOneOf[A](values: Iterable[A]): Assertion[A] =
Assertion.assertion("isOneOf")(param(values))(actual => values.exists(_ == actual))
/**
* Makes a new assertion that requires a numeric value is positive.
*/
def isPositive[A](implicit num: Numeric[A]): Assertion[A] =
isGreaterThan(num.zero)
/**
* Makes a new assertions that requires a double value is not a number (NaN).
*/
def isNaNDouble: Assertion[Double] =
Assertion.assertion("isNaNDouble")()(_.isNaN)
/**
* Makes a new assertions that requires a float value is not a number (NaN).
*/
def isNaNFloat: Assertion[Float] =
Assertion.assertion("isNaNFloat")()(_.isNaN)
/**
* Makes a new assertions that requires a double value is positive infinity.
*/
def isPosInfinityDouble: Assertion[Double] =
Assertion.assertion("isPosInfinityDouble")()(_.isPosInfinity)
/**
* Makes a new assertions that requires a float value is positive infinity.
*/
def isPosInfinityFloat: Assertion[Float] =
Assertion.assertion("isPosInfinityFloat")()(_.isPosInfinity)
/**
* Makes a new assertions that requires a double value is negative infinity.
*/
def isNegInfinityDouble: Assertion[Double] =
Assertion.assertion("isNegInfinityDouble")()(_.isNegInfinity)
/**
* Makes a new assertions that requires a float value is negative infinity.
*/
def isNegInfinityFloat: Assertion[Float] =
Assertion.assertion("isNegInfinityFloat")()(_.isNegInfinity)
/**
* Makes a new assertions that requires a double value is finite.
*/
def isFiniteDouble: Assertion[Double] =
Assertion.assertion("isFiniteDouble")()(_.abs <= Double.MaxValue)
/**
* Makes a new assertions that requires a float value is finite.
*/
def isFiniteFloat: Assertion[Float] =
Assertion.assertion("isFiniteFloat")()(_.abs <= Float.MaxValue)
/**
* Makes a new assertions that requires a double value is infinite.
*/
def isInfiniteDouble: Assertion[Double] =
Assertion.assertion("isInfiniteDouble")()(_.isInfinite)
/**
* Makes a new assertions that requires a float value is infinite.
*/
def isInfiniteFloat: Assertion[Float] =
Assertion.assertion("isInfiniteFloat")()(_.isInfinite)
/**
* Makes a new assertion that requires a Right value satisfying a specified
* assertion.
*/
def isRight[A](assertion: Assertion[A]): Assertion[Either[Any, A]] =
Assertion.assertionRec("isRight")(param(assertion))(assertion) {
case Right(a) => Some(a)
case Left(_) => None
}
/**
* Makes a new assertion that requires an Either is Right.
*/
val isRight: Assertion[Either[Any, Any]] =
isRight(anything)
/**
* Makes a new assertion that requires a Some value satisfying the specified
* assertion.
*/
def isSome[A](assertion: Assertion[A]): Assertion[Option[A]] =
Assertion.assertionRec("isSome")(param(assertion))(assertion)(identity(_))
/**
* Makes a new assertion that requires an Option is Some.
*/
val isSome: Assertion[Option[Any]] =
isSome(anything)
/**
* Makes a new assertion that requires an Iterable is sorted.
*/
def isSorted[A](implicit ord: Ordering[A]): Assertion[Iterable[A]] = {
@scala.annotation.tailrec
def loop(iterator: Iterator[A]): Boolean = iterator.hasNext match {
case false => true
case true =>
val x = iterator.next()
iterator.hasNext match {
case false => true
case true =>
val y = iterator.next()
if (ord.lteq(x, y)) loop(Iterator(y) ++ iterator) else false
}
}
Assertion.assertion("isSorted")()(actual => loop(actual.iterator))
}
/**
* Makes a new assertion that requires an Iterable is sorted in reverse order.
*/
def isSortedReverse[A](implicit ord: Ordering[A]): Assertion[Iterable[A]] =
isSorted(ord.reverse)
/**
* Makes a new assertion that requires a value have the specified type.
*
* Example:
* {{{
* assert(Duration.fromNanos(1))(isSubtype[Duration.Finite](Assertion.anything))
* }}}
*/
def isSubtype[A](assertion: Assertion[A])(implicit C: ClassTag[A]): Assertion[Any] =
Assertion.assertionRec("isSubtype")(param(className(C)))(assertion)(C.unapply(_))
/**
* Makes a new assertion that requires a Success value satisfying the
* specified assertion.
*/
def isSuccess[A](assertion: Assertion[A]): Assertion[Try[A]] =
Assertion.assertionRec("isSuccess")(param(assertion))(assertion) {
case Success(a) => Some(a)
case Failure(_) => None
}
/**
* Makes a new assertion that requires a Try value is Success.
*/
val isSuccess: Assertion[Try[Any]] =
isSuccess(anything)
/**
* Makes a new assertion that requires a value be true.
*/
def isTrue: Assertion[Boolean] =
Assertion.assertion("isTrue")()(identity(_))
/**
* Makes a new assertion that requires the value be unit.
*/
val isUnit: Assertion[Unit] =
Assertion.assertion("isUnit")()(_ => true)
/**
* Makes a new assertion that requires a value to fall within a specified min
* and max (inclusive).
*/
def isWithin[A](min: A, max: A)(implicit ord: Ordering[A]): Assertion[A] =
Assertion.assertion("isWithin")(param(min), param(max))(actual => ord.gteq(actual, min) && ord.lteq(actual, max))
/**
* Makes a new assertion that requires a numeric value is zero.
*/
def isZero[A](implicit num: Numeric[A]): Assertion[A] =
equalTo(num.zero)
/**
* Makes a new assertion that requires a given string to match the specified
* regular expression.
*/
def matchesRegex(regex: String): Assertion[String] =
Assertion.assertion("matchesRegex")(param(regex))(_.matches(regex))
/**
* Makes a new assertion that requires a numeric value is non negative.
*/
def nonNegative[A](implicit num: Numeric[A]): Assertion[A] =
isGreaterThanEqualTo(num.zero)
/**
* Makes a new assertion that requires a numeric value is non positive.
*/
def nonPositive[A](implicit num: Numeric[A]): Assertion[A] =
isLessThanEqualTo(num.zero)
/**
* Makes a new assertion that negates the specified assertion.
*/
def not[A](assertion: Assertion[A]): Assertion[A] =
Assertion.assertionDirect("not")(param(assertion))(!assertion.run(_))
/**
* Makes a new assertion that always fails.
*/
val nothing: Assertion[Any] =
Assertion.assertion("nothing")()(_ => false)
/**
* Makes a new assertion that requires a given sequence to start with the
* specified prefix.
*/
def startsWith[A](prefix: Seq[A]): Assertion[Seq[A]] =
Assertion.assertion("startsWith")(param(prefix))(_.startsWith(prefix))
/**
* Makes a new assertion that requires a given string to start with a
* specified prefix.
*/
def startsWithString(prefix: String): Assertion[String] =
Assertion.assertion("startsWithString")(param(prefix))(_.startsWith(prefix))
/**
* Makes a new assertion that requires an exit value to succeed.
*/
def succeeds[A](assertion: Assertion[A]): Assertion[Exit[Any, A]] =
Assertion.assertionRec("succeeds")(param(assertion))(assertion) {
case Exit.Success(a) => Some(a)
case _ => None
}
/**
* Makes a new assertion that requires the expression to throw.
*/
def throws[A](assertion: Assertion[Throwable]): Assertion[A] =
Assertion.assertionRec("throws")(param(assertion))(assertion) { actual =>
try {
val _ = actual
None
} catch {
case t: Throwable => Some(t)
}
}
/**
* Makes a new assertion that requires the expression to throw an instance of
* given type (or its subtype).
*/
def throwsA[E: ClassTag]: Assertion[Any] =
throws(isSubtype[E](anything))
}