From 4ded042225e20997263888290ee319e8c919c2fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Thu, 23 Aug 2018 23:44:24 +0200 Subject: [PATCH] Tests for aand and and-let --- scribblings/anaphoric.scrbl | 6 +++-- test/aand-test.rkt | 34 +++++++++++++++++++++++++ test/and-let-test.rkt | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 test/aand-test.rkt create mode 100644 test/and-let-test.rkt diff --git a/scribblings/anaphoric.scrbl b/scribblings/anaphoric.scrbl index fdb847e..c025362 100644 --- 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 new file mode 100644 index 0000000..165781b --- /dev/null +++ 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 new file mode 100644 index 0000000..2bdd54a --- /dev/null +++ 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)