(cond-expand
 (chicken-4
  (use parley)
  (use test))
 (chicken-5
  (import (chicken port)
          (chicken string))
  (import parley test))
 (else
  (error "Unsupported CHICKEN version.")))

(define s #<#EOF
(define (a-thing a)
  (print a)
  (print "ok ok")
  a)
EOF
)

(define source #f)
(define in (make-input-port (lambda ()
                              (if (eof-object? source)
                                #!eof
                                (let ((r (car source)))
                                  (set! source (cdr source))
                                  r)))
                            (lambda ()
                              (and source
                                   (not (null? source))))
                            (cut <>)))
(set-port-name! in "(parley)")
(define out (make-output-port (lambda (s) #t)
                              (lambda () #t)))
(set-buffering-mode! (lambda (port mode) #t))

(test-group "parley-read on a not-useful term"
  (parameterize ((history          #f)
                 (wait-for-input! (lambda (port) 
                                    (set! source #!eof))))
                (let ((sl (string-split s (string #\newline))))
                  (set! source (string->list s))
                  (for-each (lambda (l)
                              (test "read line" l (parley-read in out "" #f)))
                            sl)
                  (test-assert (not (char-ready? in))))))

(test-group "parley-read on a useful term"
  (parameterize ((history          #f)
                 (wait-for-input! (lambda (port)
                                    (set! source #!eof))))
                (let ((sl (string-split s (string #\newline))))
                  (set! source (string->list s))
                  (for-each (lambda (l)
                              (test "read avail line" l (parley-read in out "" #t)))
                            sl)
                  (test-assert (not (char-ready? in)))
                  (test "history" (reverse sl) ((history) '->list))))

  (let* ((original-input '("(print 1)" "(print 2)" "(print 3)"))
         (input-cursor original-input))
    (parameterize ((history          #f)
                   (wait-for-input! (lambda (port)
                                      (if (null? input-cursor)
                                        (set! source #!eof)
                                        (begin
                                          (set! source (string->list (string-append (car input-cursor) (string #\newline))))
                                          (set! input-cursor (cdr input-cursor))))))
                   (refresh-line (lambda (state #!key redraw-line) #f)))
                  (set! source #f)
                  (test "wait then read line" "(print 1)" (parley-read in out "" #t))
                  (test "wait then read line" "(print 2)" (parley-read in out "" #t))
                  (test "wait then read line" "(print 3)" (parley-read in out "" #t))
                  (test "history" (reverse original-input) ((history) '->list)))))