Mercurial > core / lisp/lib/cli/tests.lisp
changeset 584: |
35bb0d5ec95e |
parent: |
571685ae64f1
|
child: |
cc13027df6fa |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Sat, 10 Aug 2024 00:30:45 -0400 |
permissions: |
-rw-r--r-- |
description: |
bug fixes, added freedesktop.org.xml rule. more work on prolog/dql - considering lib/lang+forrth.. |
1 ;;; cli/tests.lisp --- CLI Tests 7 (:use :cl :std :rt :cli :cli/shell :cli/progress :cli/spark :cli/repl :cli/ansi :cli/prompt :cli/clap :cli/tools/sbcl)) 9 (in-package :cli/tests) 10 (declaim (optimize (debug 3) (safety 3))) 20 (cursor-position 5 15) 22 (cursor-position 10 15) 24 (with-input-from-string (in (format nil "test~%~%")) 25 (let ((a (read-line in))) 26 (cursor-position 12 15) 35 (print "bold underline") 37 (print "bold underline reverse") 39 (print "underline reverse") 45 (print "bold underline reverse") 51 "Display the 256 color palette." 53 (loop for i from 0 to 255 do 58 (loop for i from 0 to 255 do 68 "Hide and show the cursor." 69 (princ "Cursor visible:") 73 (princ "Cursor invisible:") 78 (princ "Cursor visible:") 84 "Switch to and back from the alternate screen buffer." 85 (princ "Normal screen buffer. ") 88 (save-cursor-position) 89 (use-alternate-screen-buffer) 91 (princ "Alternate screen buffer.") 94 (use-normal-screen-buffer) 95 (restore-cursor-position) 96 (princ "Back to Normal screen buffer.") 101 "Set individual termios flags to enable raw and disable echo mode. 103 Enabling raw mode allows read-char to return immediately after a key is pressed. 105 In the default cooked mode, the entry has to be confirmed by pressing enter." 106 (set-tty-mode t :ignbrk nil 125 (cursor-position 1 1) 127 (let ((a (read-char))) 128 (cursor-position 10 5) 132 (set-tty-mode t :echo t 144 "Use combination modes that consist of several individual flags. 146 Cooked and raw are opposite modes. Enabling cooked disbles raw and vice versa." 147 (set-tty-mode t :cooked nil) 149 (cursor-position 1 1) 151 (let ((a (read-char))) 152 (cursor-position 3 1) 155 (set-tty-mode t :raw nil)) 158 "Why doesnt calling the stty utility work?" 159 (uiop:run-program "stty raw -echo" :ignore-error-status t) 161 (cursor-position 1 1) 163 (let ((a (read-char))) 164 (cursor-position 2 1) 167 (uiop:run-program "stty -raw echo" :ignore-error-status t)) 170 "Query terminal size with ANSI escape sequences." 171 ;; Put the terminal into raw mode so we can read the "user input" 172 ;; of the reply char by char 173 ;; Turn off the echo or the sequence will be displayed 174 (set-tty-mode t :cooked nil :echo nil) 175 (save-cursor-position) 176 ;; Go to the bottom right corner of the terminal by attempting 177 ;; to go to some high value of row and column 178 (cursor-position 999 999) 180 ;; The terminal returns an escape sequence to the standard input 181 (device-status-report) 183 ;; The reply isnt immediately available, the terminal does need 184 ;; some time to answer 186 ;; The reply has to be read as if the user typed an escape sequence 187 (loop for i = (read-char-no-hang *standard-input* nil) 190 ;; Put the terminal back into its initial cooked state 191 (set-tty-mode t :raw nil :echo t) 192 (restore-cursor-position) 193 ;; Return the read sequence as a list of characters. 197 (with-input-from-string (in (format nil "~%~%")) 204 ;; TODO: needs to be compiled outside scope of test - contender for 206 (defprompt tpfoo :prompt "testing:") 208 (deftest cli-prompt () 212 (let ((*standard-input* (make-string-input-stream 213 (format nil "~A~%~A~%~%" "foobar" "foobar")))) 215 (is (string= (tpfoo-prompt) "foobar")) 216 (is (string= "foobar" 217 (completing-read "nothing: " tcoll :history thist :default "foobar"))))) 219 (defparameter *opts* '((:name "foo" :global t :description "bar") 220 (:name "bar" :description "foo"))) 222 (defparameter *cmd1* (make-cli :cmd :name "holla" :opts *opts* :description "cmd1 description")) 223 (defparameter *cmd2* (make-cli :cmd :name "ayo" :cmds #(*cmd1*) :opts *opts* :description "cmd1 description")) 224 (defparameter *cmds* (make-cmds '(:name "baz" :description "baz" :opts *opts*))) 226 (defparameter *cli* (make-cli :cli :opts *opts* :cmds *cmds* :description "test cli")) 228 (deftest clap-basic () 229 "test basic CLAP functionality." 231 (is (eq (make-shorty "test") #\t)) 232 (is (equalp (proc-args cli '("-f" "baz" "--bar" "fax")) ;; not eql 234 (list (make-cli-node 'opt (find-short-opts cli #\f)) 235 (make-cli-node 'cmd (find-cmd cli "baz")) 236 (make-cli-node 'opt (find-opts cli "bar")) 237 (make-cli-node 'arg "fax"))))) 238 (is (parse-args cli '("--bar" "baz" "-f" "yaks"))) 240 (with-output-to-string (s) 241 (print-version cli s) 243 (print-help cli s)))) 244 (is (string= "foobar" (cli/clap::parse-string-opt "foobar"))))) 246 (make-opt-parser thing *arg*) 248 (deftest clap-opts () 250 (is (reduce (lambda (x y) (when x (when y t))) 251 (loop for k across *cli-opt-kinds* collect (cli-opt-kind-p k)))) 252 (is (parse-thing-opt t)) 253 (is (null (parse-thing-opt nil)))) 256 (flet ((%step () (cli/progress::update 1))) 257 (let ((*progress-bar-enabled* t) 259 (with-progress-bar (n "TEST: # of steps = ~a" n) 260 (dotimes (i n) (%step)))))) 264 (spark '(1 5 22 13 5)) 270 (spark '(1 2 3 4 100 5 10 20 50 300)) 282 (spark '(0 30 55 80 33 150)) 298 (spark '(0 30 55 80 33 150) :min -100) 301 (spark '(0 30 55 80 33 150) :max 50) 304 (spark '(0 30 55 80 33 150) :min 30 :max 80) 306 ;; double-float, minus 308 (spark '(1.000000000005d0 0.000000000005d0 1.0d0)) 314 (spark '(-1.000000000005d0 0.000000000005d0 -1.0d0)) 317 (let ((ternary '(-1 0 1 -1 1 0 0 -1 1 1 0))) 322 (let ((*ticks* #(#\_ #\- #\¯))) 326 (let ((*ticks* #(#\▄ #\⎯ #\▀))) 330 (let ((*ticks* #(#\E #\O))) 331 (spark '(4 8 15 22 42) :key (lambda (n) (mod n 2)))) 334 (flet ((range (start end) (loop for i from start below end collect i)) 335 (fib (n) (loop for x = 0 then y 336 and y = 1 then (+ x y) 339 (fac (n) (labels ((rec (n acc) (if (<= n 1) acc (rec (1- n) (* n acc))))) 343 :key (lambda (x) (sin (* x pi 1/4)))) 344 "▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█")) 347 :key (lambda (x) (cos (* x pi 1/4)))) 348 "█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄")) 352 :key (lambda (x) (abs (cis (* x pi 1/4))))) 353 "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁")) 357 :key (lambda (x) (float (phase (cis (* x pi 1/4))) 1.0))) 358 "▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆")) 361 (spark (range 1 7) :key #'log) 365 (spark (range 1 7) :key #'sqrt) 371 (spark (range 1 7) :key #'fib) 374 (spark (range 1 7) :key #'exp) 377 (spark (range 1 7) :key #'fac) 380 (spark (range 1 7) :key #'isqrt) 383 (flet ((lbits (n) (spark (map 'list #'digit-char-p (write-to-string n :base 2))))) 406 ˫-----------------------+------------------------˧ 415 ˫-----------------------+------------------------˧ 422 (vspark '(0 30 55 80 33 150)) 425 ˫-----------------------+------------------------˧ 429 ██████████████████████████▋ 431 ██████████████████████████████████████████████████ 438 (vspark '(0 30 55 80 33 150) :min -100) 441 ˫-----------------------+------------------------˧ 442 ████████████████████▏ 443 ██████████████████████████▏ 444 ███████████████████████████████▏ 445 ████████████████████████████████████▏ 446 ██████████████████████████▋ 447 ██████████████████████████████████████████████████ 451 (vspark '(0 30 55 80 33 150) :max 50) 454 ˫-----------------------+------------------------˧ 456 ██████████████████████████████▏ 457 ██████████████████████████████████████████████████ 458 ██████████████████████████████████████████████████ 459 █████████████████████████████████▏ 460 ██████████████████████████████████████████████████ 465 (vspark '(0 30 55 80 33 150) :min 30 :max 80) 468 ˫-----------------------+------------------------˧ 471 █████████████████████████▏ 472 ██████████████████████████████████████████████████ 474 ██████████████████████████████████████████████████ 479 (vspark '(1 0 .5) :labels '("on" "off" "unknown") 489 (vspark '(1 0 .5) :labels '("on" "off") 499 (vspark '(1 0) :labels '("on" "off" "unknown") 509 (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4)))) 512 ˫-----------------------+------------------------˧ 513 █████████████████████████▏ 514 ██████████████████████████████████████████▋ 515 ██████████████████████████████████████████████████ 516 ██████████████████████████████████████████▋ 517 █████████████████████████▏ 521 ████████████████████████▉ 526 (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4))) 544 (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4))) 560 (let ((life-expectancies '(("Africa" 56) 562 ("South-East Asia" 67) 564 ("Eastern Mediterranean" 68) 565 ("Western Pacific" 76) 569 (vspark life-expectancies :key #'second) 572 ˫-----------------------+------------------------˧ 574 ██████████████████████████████████████████████████ 575 ███████████████████████████▌ 576 ██████████████████████████████████████████████████ 577 ██████████████████████████████▏ 578 ██████████████████████████████████████████████████ 579 ███████████████████████████████████▏ 584 (vspark life-expectancies :key #'second :scale? nil :newline? nil) 586 ██████████████████████████████████████████████████ 587 ███████████████████████████▌ 588 ██████████████████████████████████████████████████ 589 ██████████████████████████████▏ 590 ██████████████████████████████████████████████████ 591 ███████████████████████████████████▏")) 595 (vspark life-expectancies :key #'second :scale? nil) 598 ██████████████████████████████████████████████████ 599 ███████████████████████████▌ 600 ██████████████████████████████████████████████████ 601 ██████████████████████████████▏ 602 ██████████████████████████████████████████████████ 603 ███████████████████████████████████▏ 608 (vspark life-expectancies 610 :labels (mapcar #'first life-expectancies)) 613 ˫------------+-------------˧ 615 Americans ████████████████████████████ 616 South-East Asia ███████████████▍ 617 Europe ████████████████████████████ 618 Eastern Mediterranean ████████████████▊ 619 Western Pacific ████████████████████████████ 620 Global ███████████████████▋ 625 (vspark life-expectancies 628 :labels (mapcar #'first life-expectancies) 629 :title "Life Expectancy") 633 ˫------------+-------------˧ 635 Americans ████████████████████████▎ 636 South-East Asia ███████████████▉ 637 Europe ████████████████████████▎ 638 Eastern Mediterranean ████████████████▊ 639 Western Pacific ████████████████████████▎ 640 Global ██████████████████▋ 644 (spark (range 0 15) :key #'fib) 648 (vspark (range 0 15) :key #'fib) 651 ˫-----------------------+------------------------˧ 665 ██████████████████████████████▉ 666 ██████████████████████████████████████████████████ 672 (is (ld-library-path-list)) 673 (is (exec-path-list)) 674 (is (find-exe "sbcl"))) 676 (deftest clap-ast ()) 678 (compile (defmain (:exit nil :export nil) 679 (let ((test-target t)) 682 (deftest main-output () 683 (is (not (funcall 'main)))) 685 (deftest sbcl-tools () 686 (with-sbcl (:noinform t :quit t)