2024-02-06 22:39:08 +00:00
|
|
|
#!/bin/flesh
|
2023-03-20 16:22:51 -07:00
|
|
|
|
2024-02-06 22:39:08 +00:00
|
|
|
;; Flesh: Flexible Shell
|
|
|
|
|
;; Copyright (C) 2021 Ava Affine
|
2023-03-20 16:22:51 -07:00
|
|
|
;;
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;; USERLIB-TESTS
|
2023-03-17 13:24:31 -07:00
|
|
|
;; this file implements unit tests for handwritten userlib functions
|
|
|
|
|
|
2023-03-20 16:22:51 -07:00
|
|
|
(def passed
|
2024-07-10 13:22:28 -07:00
|
|
|
"prints if a test has passed"
|
2023-03-17 13:24:31 -07:00
|
|
|
(test)
|
|
|
|
|
(echo (concat "PASSED: " test)))
|
|
|
|
|
|
2023-03-20 16:22:51 -07:00
|
|
|
(def failed
|
2024-07-10 13:22:28 -07:00
|
|
|
"prints if a test has failed"
|
2023-03-17 13:24:31 -07:00
|
|
|
(test)
|
2023-06-20 01:25:19 +00:00
|
|
|
(let (())
|
|
|
|
|
(echo (concat "FAILED: " test))
|
|
|
|
|
(exit 1)))
|
2023-03-20 17:16:44 -07:00
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
(def test-cases "all test cases"
|
|
|
|
|
(("set updates var"
|
2023-03-17 13:24:31 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((test-val 0))
|
2023-03-20 16:22:51 -07:00
|
|
|
(set (q test-val) 3)
|
2023-03-17 13:24:31 -07:00
|
|
|
(eq? test-val 3))))
|
2023-03-20 21:07:16 -07:00
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("prepend prepends to list"
|
2023-03-17 13:24:31 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((list (2 3 4))
|
|
|
|
|
(list (prepend 1 list))
|
2023-03-20 16:22:51 -07:00
|
|
|
(list-head (pop list)))
|
2023-03-17 13:24:31 -07:00
|
|
|
(eq? (car list-head) 1))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("map applies function across list"
|
2023-03-20 16:22:51 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((list (1 2 3))
|
|
|
|
|
(adder (lambda (x) (add 1 x))))
|
|
|
|
|
(eq? (map adder list) (2 3 4)))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("reduce function adds numbers"
|
2023-03-20 21:07:16 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((list (1 2 3))
|
|
|
|
|
(adder (lambda (x y) (add x y))))
|
|
|
|
|
(eq? (reduce adder list) (add 1 2 3)))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("cond evaluates the first branch that returns true"
|
2023-06-13 00:18:11 +02:00
|
|
|
(quote
|
|
|
|
|
(let ((switch-one false)
|
|
|
|
|
(switch-two false)
|
|
|
|
|
(switch-three false))
|
|
|
|
|
(cond (q
|
|
|
|
|
((false (toggle switch-one))
|
|
|
|
|
(true (toggle switch-two))
|
|
|
|
|
(true (toggle switch-three)))))
|
|
|
|
|
(and (not switch-one) switch-two (not switch-three)))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("cond doesnt do anything if all the branches are false"
|
2023-06-13 00:18:11 +02:00
|
|
|
(quote
|
|
|
|
|
(let ((switch-one false)
|
|
|
|
|
(switch-two false)
|
|
|
|
|
(switch-three false))
|
|
|
|
|
(cond (q
|
|
|
|
|
((false (toggle switch-one))
|
|
|
|
|
(false (toggle switch-two))
|
|
|
|
|
(false (toggle switch-three)))))
|
|
|
|
|
(and (not switch-one) (not switch-two) (not switch-three)))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("cond returns the result of the branch that is evaluated"
|
2023-06-13 00:18:11 +02:00
|
|
|
(quote
|
2024-07-10 13:22:28 -07:00
|
|
|
(let ((variable "2"))
|
2023-06-13 00:18:11 +02:00
|
|
|
(set (q variable)
|
|
|
|
|
(cond (q
|
2024-07-10 13:22:28 -07:00
|
|
|
((true "1")))))
|
|
|
|
|
(eq? variable "1"))))
|
2023-06-13 00:18:11 +02:00
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("cond does not choke on nil evaluated result"
|
|
|
|
|
(quote
|
|
|
|
|
(cdr ((cond (q ((true (assert true)))))
|
|
|
|
|
true))))
|
|
|
|
|
|
|
|
|
|
("contains? finds elem in list"
|
2023-03-20 22:03:10 -07:00
|
|
|
(quote
|
|
|
|
|
(contains? (1 2 3) 1)))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("contains? finds last elem in list"
|
2023-03-20 22:03:10 -07:00
|
|
|
(quote
|
|
|
|
|
(contains? (1 2 3) 3)))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("contains? doesnt find elem not in list"
|
2023-03-20 22:03:10 -07:00
|
|
|
(quote
|
|
|
|
|
(not (contains? (1 2 3) 4))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("get-paths properly splits path into segments"
|
2023-03-23 12:07:28 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((PATH "/seg1:/seg2")
|
|
|
|
|
(split-path (get-paths)))
|
|
|
|
|
(and (eq? (len split-path) 2)
|
|
|
|
|
(contains? split-path "/seg1")
|
|
|
|
|
(contains? split-path "/seg2")))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("add-path properly adds a new path segment to PATH"
|
2023-03-23 12:07:28 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((PATH "/seg1:/seg2")
|
|
|
|
|
(new-path "/seg3"))
|
|
|
|
|
(add-path new-path)
|
|
|
|
|
(contains? (get-paths) new-path))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("join operates as expected"
|
2024-06-01 08:55:53 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((l ("1" 2 (3))))
|
|
|
|
|
(eq? (join l ".")
|
|
|
|
|
"1.2.(3)"))))
|
|
|
|
|
|
2024-07-10 13:22:28 -07:00
|
|
|
("extend extends sets"
|
2024-06-11 17:07:49 -07:00
|
|
|
(quote
|
|
|
|
|
(let ((s1 (1 2 3))
|
|
|
|
|
(s2 (4 5 6)))
|
|
|
|
|
(eq? (extend s1 s2)
|
|
|
|
|
(1 2 3 4 5 6)))))
|
|
|
|
|
|
2023-03-17 13:24:31 -07:00
|
|
|
;; add more test cases here
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
;; run all test cases, print output
|
2023-03-20 16:22:51 -07:00
|
|
|
(let ((test-iter (pop test-cases)))
|
|
|
|
|
(while (gt? (len test-iter) 1)
|
|
|
|
|
(let ((test (car test-iter))
|
|
|
|
|
(remaining (cdr test-iter))
|
|
|
|
|
(test-name (car test))
|
|
|
|
|
(test-body (cdr test)))
|
|
|
|
|
(if (eval test-body)
|
|
|
|
|
(passed test-name)
|
|
|
|
|
(failed test-name))
|
2023-03-20 17:16:44 -07:00
|
|
|
(set (q test-iter) (pop remaining)))))
|