commit 4ded042225e20997263888290ee319e8c919c2fb
parent 691e7e07717de7594d4b561781dae05344986755
Author: Georges Dupéron <georges.duperon@gmail.com>
Date: Thu, 23 Aug 2018 23:44:24 +0200
Tests for aand and and-let
Diffstat:
3 files changed, 88 insertions(+), 2 deletions(-)
diff --git a/scribblings/anaphoric.scrbl b/scribblings/anaphoric.scrbl
@@ -90,7 +90,8 @@ using @racket[it].
at most once (evaluation stops at the first successful
@racket[conditionᵢ]).}
-@defform[(aand conditionᵢ ... body)]{
+@defform*[[(aand)
+ (aand conditionᵢ ... body)]]{
Variant of @racket[and] which binds @racket[it]
to the value of each @racket[conditionᵢ], in scope within the
next @racket[conditionᵢ] or @racket[body]. More precisely, the value
@@ -142,7 +143,8 @@ using @racket[it].
(evaluation stops at the first successful
@racket[conditionᵢ]).}
-@defform[(and-let [identifier conditionᵢ] ... body)]{
+@defform*[[(and-let)
+ (and-let [identifier conditionᵢ] ... body)]]{
Variant of @racket[and] which binds each @racket[identifier]
to the value of its @racket[conditionᵢ], in scope within every
@racket[conditionᵢ] afterwards as well as in @racket[body].
diff --git a/test/aand-test.rkt b/test/aand-test.rkt
@@ -0,0 +1,34 @@
+#lang racket
+
+(require anaphoric/aand
+ rackunit)
+
+(define lst '(x b 2 y z a b c 1 2 3))
+
+(check-equal? (aand)
+ #t)
+
+(check-equal? (aand #f)
+ #f)
+
+(check-equal? (aand (member 'y lst))
+ '(y z a b c 1 2 3))
+
+(check-equal? (aand (member 'y lst)
+ (member 'b lst)
+ (member '2 lst))
+ '(2 y z a b c 1 2 3))
+
+(check-equal? (aand (member 'y lst)
+ (member 'b lst)
+ (member '2 it))
+ '(2 y z a b c 1 2 3))
+
+(check-equal? (aand (member 'y lst)
+ (member 'b it)
+ (member '2 it))
+ '(2 3))
+
+(check-equal? (aand (member 'absent lst)
+ (fail "aand selected wrong branch"))
+ #f)
diff --git a/test/and-let-test.rkt b/test/and-let-test.rkt
@@ -0,0 +1,50 @@
+#lang racket
+
+(require anaphoric/and-let
+ rackunit)
+
+(define lst '(x b 2 y z a b c 1 2 3))
+
+(check-equal? (and-let)
+ #t)
+
+(check-equal? (and-let 42)
+ 42)
+
+(check-equal? (and-let #f)
+ #f)
+
+(check-equal? (and-let [v1 (member 'y lst)]
+ v1)
+ '(y z a b c 1 2 3))
+
+(check-equal? (and-let [v1 (member 'y lst)]
+ [v2 (member 'b v1)]
+ [v3 (member '2 v2)]
+ (list v1 v2 v3))
+ '((y z a b c 1 2 3)
+ (b c 1 2 3)
+ (2 3)))
+
+(check-equal? (let ([v1 'outer]
+ [v2 'outer]
+ [v3 'outer])
+ (and-let [v1 (member 'y lst)]
+ [v2 (member 'b v1)]
+ [v3 (member '2 v2)]
+ (list v1 v2 v3)))
+ '((y z a b c 1 2 3)
+ (b c 1 2 3)
+ (2 3)))
+
+(check-equal? (let ([v1 'outer])
+ (and-let [v1 (member 'absent lst)]
+ (fail "aand selected wrong branch")))
+ #f)
+
+(check-equal? (let ([v1 'outer]
+ [v2 'outer])
+ (and-let [v1 (member 'y lst)]
+ [v2 (member 'x v1)]
+ (fail "aand selected wrong branch")))
+ #f)