guile.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /bin/sh
  2. # -*-scheme-*-
  3. exec ${MES-src/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests guile)' -s "$0" "$@"
  4. !#
  5. ;;; -*-scheme-*-
  6. ;;; GNU Mes --- Maxwell Equations of Software
  7. ;;; Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  8. ;;;
  9. ;;; This file is part of GNU Mes.
  10. ;;;
  11. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Mes is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (tests guile)
  24. #:use-module (ice-9 rdelim)
  25. #:use-module (mes mes-0)
  26. #:use-module (mes misc)
  27. #:use-module (mes test))
  28. (cond-expand
  29. (mes
  30. (mes-use-module (mes test))
  31. (mes-use-module (mes misc))
  32. (mes-use-module (mes guile)))
  33. (else))
  34. (pass-if "first dummy" #t)
  35. (pass-if-not "second dummy" #f)
  36. (pass-if-equal "read-string" "bla"
  37. (with-input-from-string "bla"
  38. (lambda () (read-string))))
  39. (pass-if-equal "open-input-string" "bla"
  40. (let* ((port (current-input-port))
  41. (foo (open-input-string "bla")))
  42. (set-current-input-port foo)
  43. (let ((s (read-string)))
  44. (set-current-input-port port)
  45. s)))
  46. ;; NYACC
  47. ;; === input stack =====================
  48. (define *input-stack* (make-fluid '()))
  49. (define (reset-input-stack)
  50. (fluid-set! *input-stack* '()))
  51. (define (push-input port)
  52. (let ((curr (current-input-port))
  53. (ipstk (fluid-ref *input-stack*)))
  54. (fluid-set! *input-stack* (cons curr ipstk))
  55. (set-current-input-port port)))
  56. ;; Return #f if empty
  57. (define (pop-input)
  58. (let ((ipstk (fluid-ref *input-stack*)))
  59. (if (null? ipstk) #f
  60. (begin
  61. (set-current-input-port (car ipstk))
  62. (fluid-set! *input-stack* (cdr ipstk))))))
  63. (pass-if-equal "push-input"
  64. "bla"
  65. (let ()
  66. (push-input (open-input-string "bla"))
  67. (let ((ch (read-char)))
  68. (unread-char ch))
  69. (let ((x (read-string)))
  70. (let ((pop (pop-input)))
  71. x))))
  72. (pass-if-equal "input-stack/1"
  73. "hello world!"
  74. (with-output-to-string
  75. (lambda ()
  76. (with-input-from-string "hello X!"
  77. (lambda ()
  78. (let iter ((ch (read-char)))
  79. (unless (eq? ch #\X) (write-char ch) (iter (read-char))))
  80. (push-input (open-input-string "world"))
  81. (let iter ((ch (read-char)))
  82. (unless (eof-object? ch) (write-char ch) (iter (read-char))))
  83. (pop-input)
  84. (let iter ((ch (read-char)))
  85. (unless (eof-object? ch) (write-char ch) (iter (read-char)))))))))
  86. (pass-if "input-stack/2"
  87. (let ((sp (open-input-string "abc")))
  88. (push-input sp)
  89. (and (pop-input) (not (pop-input)))))
  90. (pass-if-equal "with-input-from-string peek"
  91. #\X
  92. (with-input-from-string "X"
  93. (lambda () (peek-char))))
  94. (pass-if-equal "open-input-string peek"
  95. #\X
  96. (let ((port (open-input-string "X")))
  97. (set-current-input-port port)
  98. (peek-char)))
  99. (result 'report)