bjosh/tests/editor.bjo
Linus Björnstam 5012a5d993 Suggestions for cd and paths, and a directory history; bjoterm 0.1.2
The editor suggests, as fish does, the newest cd line of the history whose
directory exists, else the directories or files that start with the word.
prevd, nextd and cdh move in the directory history, and so do Alt-Left and
Alt-Right on an empty line.

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

75 lines
3.8 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/"])
;; Suggestions.
(ignore (directory-create (path-combine dir "abd/deeper")))
(ignore (directory-create (path-combine dir "other")))
(expect "cd suggests the only directory" (suggest "cd ot" []) (Some "her/"))
(expect "and not files" (suggest "cd abc" []) None)
(expect "the common prefix of several" (suggest "cd abd/" []) (Some "deeper/"))
(expect "a path argument suggests files, with no space" (suggest "cat abc" []) (Some ".txt"))
(expect "several files, their common prefix" (suggest "cat a" []) None)
(expect "in a directory" (suggest "cat abd/in" []) (Some "ner"))
(expect "not the command" (suggest "ab" []) None)
(expect "not an empty argument" (suggest "cat " []) None)
(expect "not a second line" (suggest "echo 'a\ncat abc" []) None)
(expect "cd from the history, newest first"
(suggest "cd a" ["cd abd" "cd abd/deeper" "ls"]) (Some "bd/deeper"))
(expect "not a directory that is gone"
(suggest "cd o" ["cd other" "cd old-gone"]) (Some "ther"))
(expect "only cd lines" (suggest "cd o" ["cdx other"]) (Some "ther/"))
(set-current-directory! here)
(ignore (directory-delete-tree dir))
0)