changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/cli/tests/pkg.lisp

changeset 689: 2e7d93b892a5
parent: 517c65b51e6b
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 01 Oct 2024 22:29:08 -0400
permissions: -rw-r--r--
description: cli shell tests init
1 ;;; cli/tests.lisp --- CLI Tests
2 
3 ;;
4 
5 ;;; Code:
6 (defpackage :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 :dat/sxp))
8 
9 (in-package :cli/tests)
10 (defsuite :cli)
11 (in-suite :cli)
12 
13 ;; TODO: needs to be compiled outside scope of test - contender for
14 ;; fixture API
15 (defprompt tpfoo :prompt "testing:")
16 
17 (deftest cli-prompt (:skip t)
18  "Test CLI prompts"
19  (defvar tcoll nil)
20  (defvar thist nil)
21  (let ((*standard-input* (make-string-input-stream
22  (format nil "~A~%~A~%~%" "foobar" "foobar"))))
23  ;; prompts
24  (is (string= (tpfoo-prompt) "foobar"))
25  (is (string= "foobar"
26  (completing-read "nothing: " tcoll :history thist :default "foobar")))))
27 
28 (deftest progress ()
29  (flet ((%step () (cli/progress::update 1)))
30  (let ((*progress-bar-enabled* t)
31  (n 100))
32  (with-progress-bar (n "TEST: # of steps = ~a" n)
33  (dotimes (i n) (%step))))))
34 
35 (deftest spark ()
36  (is (string=
37  (spark '(1 5 22 13 5))
38  "▁▂█▅▂"))
39  (is (string=
40  (spark '(5.5 20))
41  "▁█"))
42  (is (string=
43  (spark '(1 2 3 4 100 5 10 20 50 300))
44  "▁▁▁▁▃▁▁▁▂█"))
45  (is (string=
46  (spark '(1 50 100))
47  "▁▄█"))
48  (is (string=
49  (spark '(2 4 8))
50  "▁▃█"))
51  (is (string=
52  (spark '(1 2 3 4 5))
53  "▁▂▄▆█"))
54  (is (string=
55  (spark '(0 30 55 80 33 150))
56  "▁▂▃▄▂█"))
57  ;; null
58  (is (string=
59  (spark '())
60  ""))
61  ;; singleton
62  (is (string=
63  (spark '(42))
64  "▁"))
65  ;; constant
66  (is (string=
67  (spark '(42 42))
68  "▁▁"))
69  ;; min/max
70  (is (string=
71  (spark '(0 30 55 80 33 150) :min -100)
72  "▃▄▅▆▄█"))
73  (is (string=
74  (spark '(0 30 55 80 33 150) :max 50)
75  "▁▅██▅█"))
76  (is (string=
77  (spark '(0 30 55 80 33 150) :min 30 :max 80)
78  "▁▁▄█▁█"))
79  ;; double-float, minus
80  (is (string=
81  (spark '(1.000000000005d0 0.000000000005d0 1.0d0))
82  "█▁▇"))
83  (is (string=
84  (spark '(-1 0 -1))
85  "▁█▁"))
86  (is (string=
87  (spark '(-1.000000000005d0 0.000000000005d0 -1.0d0))
88  "▁█▁"))
89  ;; *ticks*
90  (let ((ternary '(-1 0 1 -1 1 0 0 -1 1 1 0)))
91  (is (string=
92  (spark ternary)
93  "▁▄█▁█▄▄▁██▄"))
94  (is (string=
95  (let ((*ticks* #(#\_ #\- #\¯)))
96  (spark ternary))
97  "_-¯_¯--_¯¯-"))
98  (is (string=
99  (let ((*ticks* #(#\▄ #\⎯ #\▀)))
100  (spark ternary))
101  "▄⎯▀▄▀⎯⎯▄▀▀⎯"))
102  (is (string=
103  (let ((*ticks* #(#\E #\O)))
104  (spark '(4 8 15 22 42) :key (lambda (n) (mod n 2))))
105  "EEOEE")))
106  ;; key
107  (flet ((range (start end) (loop for i from start below end collect i))
108  (fib (n) (loop for x = 0 then y
109  and y = 1 then (+ x y)
110  repeat n
111  finally (return x)))
112  (fac (n) (labels ((rec (n acc) (if (<= n 1) acc (rec (1- n) (* n acc)))))
113  (rec n 1))))
114  (is (string=
115  (spark (range 0 51)
116  :key (lambda (x) (sin (* x pi 1/4))))
117  "▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█"))
118  (is (string=
119  (spark (range 0 51)
120  :key (lambda (x) (cos (* x pi 1/4))))
121  "█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄▂▁▂▄▆█▆▄"))
122 
123  (is (string=
124  (spark (range 0 51)
125  :key (lambda (x) (abs (cis (* x pi 1/4)))))
126  "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"))
127 
128  (is (string=
129  (spark (range 0 51)
130  :key (lambda (x) (float (phase (cis (* x pi 1/4))) 1.0)))
131  "▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆▇█▁▁▂▄▅▆"))
132 
133  (is (string=
134  (spark (range 1 7) :key #'log)
135  "▁▃▅▆▇█"))
136 
137  (is (string=
138  (spark (range 1 7) :key #'sqrt)
139  "▁▃▄▅▆█"))
140  (is (string=
141  (spark (range 1 7))
142  "▁▂▃▅▆█"))
143  (is (string=
144  (spark (range 1 7) :key #'fib)
145  "▁▁▂▃▅█"))
146  (is (string=
147  (spark (range 1 7) :key #'exp)
148  "▁▁▁▁▃█"))
149  (is (string=
150  (spark (range 1 7) :key #'fac)
151  "▁▁▁▁▂█"))
152  (is (string=
153  (spark (range 1 7) :key #'isqrt)
154  "▁▁▁███"))
155  ;; misc
156  (flet ((lbits (n) (spark (map 'list #'digit-char-p (write-to-string n :base 2)))))
157  (is (string=
158  (lbits 42)
159  "█▁█▁█▁"))
160  (is (string=
161  (lbits 43)
162  "█▁█▁██"))
163  (is (string=
164  (lbits 44)
165  "█▁██▁▁"))
166  (is (string=
167  (lbits 45)
168  "█▁██▁█")))
169 
170  ;; VSPARK
171  (is (string=
172  (vspark '())
173  ""))
174  ;; singleton
175  (is (string=
176  (vspark '(1))
177  "
178 1 1.5 2
179 ˫-----------------------+------------------------˧
180 
181 "))
182 
183  ;; constant
184  (is (string=
185  (vspark '(1 1))
186  "
187 1 1.5 2
188 ˫-----------------------+------------------------˧
189 
190 
191 "))
192 
193 
194  (is (string=
195  (vspark '(0 30 55 80 33 150))
196  "
197 0 75 150
198 ˫-----------------------+------------------------˧
199 
200 ██████████▏
201 ██████████████████▍
202 ██████████████████████████▋
203 ███████████▏
204 ██████████████████████████████████████████████████
205 "))
206 
207 
208  ;; min, max
209 
210  (is (string=
211  (vspark '(0 30 55 80 33 150) :min -100)
212  "
213 -100 25 150
214 ˫-----------------------+------------------------˧
215 ████████████████████▏
216 ██████████████████████████▏
217 ███████████████████████████████▏
218 ████████████████████████████████████▏
219 ██████████████████████████▋
220 ██████████████████████████████████████████████████
221 "))
222 
223  (is (string=
224  (vspark '(0 30 55 80 33 150) :max 50)
225  "
226 0 25 50
227 ˫-----------------------+------------------------˧
228 
229 ██████████████████████████████▏
230 ██████████████████████████████████████████████████
231 ██████████████████████████████████████████████████
232 █████████████████████████████████▏
233 ██████████████████████████████████████████████████
234 "))
235 
236 
237  (is (string=
238  (vspark '(0 30 55 80 33 150) :min 30 :max 80)
239  "
240 30 55 80
241 ˫-----------------------+------------------------˧
242 
243 
244 █████████████████████████▏
245 ██████████████████████████████████████████████████
246 ███▏
247 ██████████████████████████████████████████████████
248 "))
249 
250  ;; labels
251  (is (string=
252  (vspark '(1 0 .5) :labels '("on" "off" "unknown")
253  :size 1
254  :scale? nil)
255  "
256  on █
257  off ▏
258 unknown ▌
259 "))
260 
261  (is (string=
262  (vspark '(1 0 .5) :labels '("on" "off")
263  :size 1
264  :scale? nil)
265  "
266  on █
267 off ▏
268 
269 "))
270 
271  (is (string=
272  (vspark '(1 0) :labels '("on" "off" "unknown")
273  :size 1
274  :scale? nil)
275  "
276  on █
277 off ▏
278 "))
279 
280  ;; key
281  (is (string=
282  (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4))))
283  "
284 -1.0 0.0 1.0
285 ˫-----------------------+------------------------˧
286 █████████████████████████▏
287 ██████████████████████████████████████████▋
288 ██████████████████████████████████████████████████
289 ██████████████████████████████████████████▋
290 █████████████████████████▏
291 ███████▍
292 
293 ███████▍
294 ████████████████████████▉
295 "))
296 
297  ;; size
298  (is (string=
299  (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4)))
300  :size 10)
301  "
302 -1.0 1.0
303 ˫--------˧
304 █████▏
305 ████████▌
306 ██████████
307 ████████▌
308 █████▏
309 █▌
310 
311 █▌
312 ████▉
313 "))
314 
315  ;; scale (mid-point)
316  (is (string=
317  (vspark '(0 1 2 3 4 5 6 7 8) :key (lambda (x) (sin (* x pi 1/4)))
318  :size 20)
319  "
320 -1.0 0.0 1.0
321 ˫--------+---------˧
322 ██████████▏
323 █████████████████▏
324 ████████████████████
325 █████████████████▏
326 ██████████▏
327 ██▉
328 
329 ██▉
330 █████████▉
331 "))
332 
333  (let ((life-expectancies '(("Africa" 56)
334  ("Americans" 76)
335  ("South-East Asia" 67)
336  ("Europe" 76)
337  ("Eastern Mediterranean" 68)
338  ("Western Pacific" 76)
339  ("Global" 70))))
340 
341  (is (string=
342  (vspark life-expectancies :key #'second)
343  "
344 56 66 76
345 ˫-----------------------+------------------------˧
346 
347 ██████████████████████████████████████████████████
348 ███████████████████████████▌
349 ██████████████████████████████████████████████████
350 ██████████████████████████████▏
351 ██████████████████████████████████████████████████
352 ███████████████████████████████████▏
353 "))
354 
355  ;; newline?
356  (is (string=
357  (vspark life-expectancies :key #'second :scale? nil :newline? nil)
358  "▏
359 ██████████████████████████████████████████████████
360 ███████████████████████████▌
361 ██████████████████████████████████████████████████
362 ██████████████████████████████▏
363 ██████████████████████████████████████████████████
364 ███████████████████████████████████▏"))
365 
366  ;; scale?
367  (is (string=
368  (vspark life-expectancies :key #'second :scale? nil)
369  "
370 
371 ██████████████████████████████████████████████████
372 ███████████████████████████▌
373 ██████████████████████████████████████████████████
374 ██████████████████████████████▏
375 ██████████████████████████████████████████████████
376 ███████████████████████████████████▏
377 "))
378 
379  ;; labels
380  (is (string=
381  (vspark life-expectancies
382  :key #'second
383  :labels (mapcar #'first life-expectancies))
384  "
385  56 66 76
386  ˫------------+-------------˧
387  Africa ▏
388  Americans ████████████████████████████
389  South-East Asia ███████████████▍
390  Europe ████████████████████████████
391 Eastern Mediterranean ████████████████▊
392  Western Pacific ████████████████████████████
393  Global ███████████████████▋
394 "))
395 
396  ;; title
397  (is (string=
398  (vspark life-expectancies
399  :min 50 :max 80
400  :key #'second
401  :labels (mapcar #'first life-expectancies)
402  :title "Life Expectancy")
403  "
404  Life Expectancy
405  50 65 80
406  ˫------------+-------------˧
407  Africa █████▋
408  Americans ████████████████████████▎
409  South-East Asia ███████████████▉
410  Europe ████████████████████████▎
411 Eastern Mediterranean ████████████████▊
412  Western Pacific ████████████████████████▎
413  Global ██████████████████▋
414 "))
415 
416  (is (string=
417  (spark (range 0 15) :key #'fib)
418  "▁▁▁▁▁▁▁▁▁▁▂▂▃▅█"))
419 
420  (is (string=
421  (vspark (range 0 15) :key #'fib)
422  "
423 0 188.5 377
424 ˫-----------------------+------------------------˧
425 
426 
427 
428 
429 
430 
431 █▏
432 █▊
433 ██▊
434 ████▌
435 ███████▍
436 ███████████▊
437 ███████████████████▏
438 ██████████████████████████████▉
439 ██████████████████████████████████████████████████
440 ")))))
441 
442 (deftest repl ())
443 
444 (deftest env ()
445  (ld-library-path-list)
446  (is (exec-path-list))
447  (is (find-exe "sbcl")))
448 
449 (deftest sbcl-tools ()
450  (with-sbcl (:noinform t :quit t)
451  (print 1)))