bjoterm/tests/prompt.bjo

65 lines
2.7 KiB
Text
Raw Normal View History

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; `line-read` with a prompt of two lines: each drawing goes up to the
;; prompt's first row, and the continuation prompt is as wide as its last
;; line.
;;
;; bjo test tests/prompt.bjo
;;
;; The test runs itself again under `script` on a screen of 40 columns and
;; 20 rows. `script` does not keep a screen, so the test checks the escape
;; sequences the editor writes.
(import (std prelude))
(import (std simpletest))
(import (std run))
(import (bjoterm terminal))
(import (bjoterm line))
(import/extern
(command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise)))
(: (-> void))
(defun (child)
(match (terminal-open)
((Err e) (println (str "open: " (terminal-error-message e))))
((Ok term)
(def ed (make-line-editor))
(def prompt "\u{1B}[1mtop line\u{1B}[0m\n> ")
(loop (:for _ (range 0 2))
(:do (match (line-read ed term prompt)
((LineText text) (println (str "result: " (written text))))
(_ (println "result: no text"))))))))
(: (-> void))
(defun (parent)
(def self (str "dotnet " (array-ref (command-line) 0)))
;; "ab" a key at a time, then a second input of two lines: C-j starts the
;; second line.
(def keys (string-join ["sleep 2" "printf a" "sleep 0.3" "printf b" "sleep 0.3" "printf '\\r'" "sleep 0.5"
"printf '(c'" "sleep 0.3" "printf '\\012'" "sleep 0.3" "printf 'd)\\r'" "sleep 0.5"]
"; "))
(def command (str "(" keys ") | script -qec 'stty cols 40 rows 20; " self " child' /dev/null"))
(def out (match (run/output '(sh "-c" ,command))
((Ok (Tuple _ text)) (string-replace text "\r" ""))
((Err e) (run-error-message e))))
(def redraw (str (cursor-up 1) clear-below "\u{1B}[1mtop line\u{1B}[0m\n> "))
(expect-true "the input is read" (string-contains? out "result: \"ab\""))
(expect-true "a key goes up to the prompt's first row, and draws it again"
(string-contains? out (str redraw "a")))
(expect-true "and the next key too" (string-contains? out (str redraw "ab")))
(expect-true "an input of two lines" (string-contains? out "result: \"(c\\nd)\""))
(expect-true "the continuation prompt is as wide as the prompt's last line"
(string-contains? out "\n> (c\n d)"))
(unless (string-contains? out "result: \"(c")
(println out)))
(defun (main args)
(cond
((and (= (vec-length args) 1) (= (vec-ref args 0) "child")) (child))
((file-exists? "/usr/bin/script") (parent))
(else (println "skipped: no script command")))
0)