bjosh/tests/editor.bjo
Linus Björnstam 3518b7711d Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
  parser-combinators). Syntax that is not supported raises an error that
  names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
  NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
  export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
  completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
  control under script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00

57 lines
2.7 KiB
Text

;; 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/.
;; (bjosh editor): when Enter runs a line, and completion.
;;
;; bjo test tests/editor.bjo
(import (std prelude))
(import (std simpletest))
(import (bjoterm line))
(import (bjosh editor))
(import/extern
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
(defun (main args)
(expect "a whole line runs" (input-done? "ls -l") #t)
(expect "an open quote waits" (input-done? "echo 'abc") #f)
(expect "an open double quote waits" (input-done? "echo \"abc") #f)
(expect "| at the end waits" (input-done? "ls |") #f)
(expect "&& at the end waits" (input-done? "make &&") #f)
(expect "\\ at the end waits" (input-done? "echo a \\") #f)
(expect "\\\\ at the end runs" (input-done? "echo a \\\\") #t)
(expect "a line that is not supported runs, to show the error" (input-done? "if true") #t)
(expect "two lines" (input-done? "echo 'a\nb'") #t)
(expect "the word before the cursor" (word-start "ls -l fo") 6)
(expect "after an operator" (word-start "a|b") 2)
(expect "the first word is a command" (command-position? "ls" 0) #t)
(expect "the second is not" (command-position? "ls -l" 3) #f)
(expect "after | it is" (command-position? "a | b" 4) #t)
(expect "after && it is" (command-position? "a && b" 5) #t)
(expect "spaces are quoted" (quote-name "a b") "a\\ b")
(expect "and $" (quote-name "$x") "\\$x")
(def dir (path-combine (temp-dir) (str "bjosh-editor-" (int->string process-id))))
(ignore (directory-create (path-combine dir "abd")))
(ignore (file-write-text (path-combine dir "abc.txt") ""))
(ignore (file-write-text (path-combine dir "a b") ""))
(ignore (file-write-text (path-combine dir ".hidden") ""))
(ignore (file-write-text (path-combine dir "abd/inner") ""))
(def here (current-directory))
(set-current-directory! dir)
(expect "files and directories" (file-candidates "ab") ["abc.txt " "abd/"])
(expect "not hidden files" (file-candidates "") ["a\\ b " "abc.txt " "abd/"])
(expect "hidden files for a dot" (file-candidates ".h") [".hidden "])
(expect "in a directory" (file-candidates "abd/i") ["abd/inner "])
(expect "the second word completes files" (record-ref (complete "cat ab") candidates) ["abc.txt " "abd/"])
(expect "the first word completes commands"
(any? (fun (c) (= c "jobs ")) (record-ref (complete "jo") candidates)) #t)
(expect "a first word with a / completes files" (record-ref (complete "./ab") candidates)
["./abc.txt " "./abd/"])
(set-current-directory! here)
(ignore (directory-delete-tree dir))
0)