binders.lisp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (import test ())
  2. (describe "A variable"
  3. (it "may not be bound using let"
  4. (with (a 0)
  5. (let [(a (succ a))
  6. (b a)]
  7. (affirm (= a 1)
  8. (= b 0)))))
  9. (it "may be bound using let*"
  10. (with (a 0)
  11. (let* [(a (succ a))
  12. (b a)]
  13. (affirm (= a 1)
  14. (= b 1)))))
  15. (it "may be recursively bound using letrec"
  16. (letrec [(x (lambda () x))]
  17. (affirm (= x (x)))))
  18. (may "be bound using when-let"
  19. (will "execute all items"
  20. (let* [(ctr 0)
  21. (state! (lambda (x) (inc! ctr) x))]
  22. (when-let [(a (state! true))
  23. (b (state! true))
  24. (c (state! false))
  25. (d (state! false))]
  26. (fail! "This should not be reached"))
  27. (affirm (= ctr 4))))
  28. (will "execute the body"
  29. (let* [(ctr 0)
  30. (state! (lambda (x) (inc! ctr) x))]
  31. (when-let [(a (state! true))
  32. (b (state! true))
  33. (c (state! true))
  34. (d (state! true))]
  35. (affirm (= ctr 4))
  36. (set! ctr -1))
  37. (affirm (= ctr -1)))))
  38. (may "be bound using when-let*"
  39. (will-not "execute all items"
  40. (let* [(ctr 0)
  41. (state! (lambda (x) (inc! ctr) x))]
  42. (when-let* [(a (state! true))
  43. (b (state! true))
  44. (c (state! false))
  45. (d (state! false))]
  46. (fail! "This should not be reached"))
  47. (affirm (= ctr 3))))
  48. (will "execute the body"
  49. (let* [(ctr 0)
  50. (state! (lambda (x) (inc! ctr) x))]
  51. (when-let* [(a (state! true))
  52. (b (state! true))
  53. (c (state! true))
  54. (d (state! true))]
  55. (affirm (= ctr 4))
  56. (set! ctr -1))
  57. (affirm (= ctr -1)))))
  58. )