dynamic-scope.test 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;;; -*- scheme -*-
  2. ;;;; dynamic-scop.test --- test suite for dynamic scoping constructs
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This program is free software; you can redistribute it and/or modify
  7. ;;;; it under the terms of the GNU General Public License as published by
  8. ;;;; the Free Software Foundation; either version 2, or (at your option)
  9. ;;;; any later version.
  10. ;;;;
  11. ;;;; This program is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;;; GNU General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU General Public License
  17. ;;;; along with this software; see the file COPYING. If not, write to
  18. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;;;; Boston, MA 02110-1301 USA
  20. (define-module (test-suite test-dynamic-scope)
  21. :use-module (test-suite lib))
  22. (define exception:missing-expr
  23. (cons 'syntax-error "Missing expression"))
  24. (define exception:bad-binding
  25. (cons 'syntax-error "Bad binding"))
  26. (define exception:duplicate-binding
  27. (cons 'syntax-error "Duplicate binding"))
  28. (define global-a 0)
  29. (define (fetch-global-a) global-a)
  30. (with-test-prefix "dynamic scope"
  31. (pass-if "@bind binds"
  32. (= (@bind ((global-a 1)) (fetch-global-a)) 1))
  33. (pass-if "@bind unbinds"
  34. (begin
  35. (set! global-a 0)
  36. (@bind ((global-a 1)) (fetch-global-a))
  37. (= global-a 0)))
  38. (pass-if-exception "duplicate @binds"
  39. exception:duplicate-binding
  40. (eval '(@bind ((a 1) (a 2)) (+ a a))
  41. (interaction-environment)))
  42. (pass-if-exception "@bind missing expression"
  43. exception:missing-expr
  44. (eval '(@bind ((global-a 1)))
  45. (interaction-environment)))
  46. (pass-if-exception "@bind bad bindings"
  47. exception:bad-binding
  48. (eval '(@bind (a) #f)
  49. (interaction-environment)))
  50. (pass-if-exception "@bind bad bindings"
  51. exception:bad-binding
  52. (eval '(@bind ((a)) #f)
  53. (interaction-environment)))
  54. (pass-if "@bind and dynamic-wind"
  55. (letrec ((co-routine #f)
  56. (spawn (lambda (proc)
  57. (set! co-routine proc)))
  58. (yield (lambda (val)
  59. (call-with-current-continuation
  60. (lambda (k)
  61. (let ((next co-routine))
  62. (set! co-routine k)
  63. (next val)))))))
  64. (spawn (lambda (val)
  65. (@bind ((global-a 'inside))
  66. (yield global-a)
  67. (yield global-a))))
  68. (set! global-a 'outside)
  69. (let ((inside-a (yield #f)))
  70. (let ((outside-a global-a))
  71. (let ((inside-a2 (yield #f)))
  72. (and (eq? inside-a 'inside)
  73. (eq? outside-a 'outside)
  74. (eq? inside-a2 'inside))))))))