2017-10-20 18:00:47 +03:00
|
|
|
(defmodule IO
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden color-table)
|
2018-02-23 19:25:15 +03:00
|
|
|
(def color-table
|
2017-10-20 18:00:47 +03:00
|
|
|
[[@"black" @"30"]
|
|
|
|
[@"red" @"31"]
|
|
|
|
[@"green" @"32"]
|
|
|
|
[@"yellow" @"33"]
|
|
|
|
[@"blue" @"34"]
|
|
|
|
[@"magenta" @"35"]
|
|
|
|
[@"cyan" @"36"]
|
|
|
|
[@"white" @"37"]
|
|
|
|
[@"reset" @"0"]
|
|
|
|
[@"none" @"0"]
|
|
|
|
[@"bold" @"1"]
|
|
|
|
[@"italic" @"3"]
|
|
|
|
[@"underline" @"4"]
|
|
|
|
[@"blink-slow" @"5"]
|
|
|
|
[@"blink-rapid" @"6"]
|
|
|
|
[@"bg-black" @"40"]
|
|
|
|
[@"bg-red" @"41"]
|
|
|
|
[@"bg-green" @"42"]
|
|
|
|
[@"bg-yellow" @"43"]
|
|
|
|
[@"bg-blue" @"44"]
|
|
|
|
[@"bg-magenta" @"45"]
|
|
|
|
[@"bg-cyan" @"46"]
|
|
|
|
[@"bg-white" @"47"]])
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden len-color-table)
|
2018-05-20 10:57:51 +03:00
|
|
|
(def len-color-table (Array.length &color-table))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden color-name-to-ansi)
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn color-name-to-ansi [cname]
|
|
|
|
(let [res @""]
|
|
|
|
(do
|
|
|
|
(for [i 0 len-color-table]
|
2018-02-23 19:25:15 +03:00
|
|
|
(if (String.= cname (Array.nth (Array.nth &color-table i) 0))
|
|
|
|
(set! res @(Array.nth (Array.nth &color-table i) 1))
|
2017-10-20 18:00:47 +03:00
|
|
|
()))
|
2018-05-20 07:04:47 +03:00
|
|
|
(StringCopy.append @"\x1b[" (StringCopy.append res @"m")))))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden color)
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn color [cname]
|
|
|
|
(print &(color-name-to-ansi cname)))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmodule Test
|
|
|
|
(deftype State [passed Int, failed Int])
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden handler)
|
2017-11-25 21:19:15 +03:00
|
|
|
(defn handler [state expected actual descr what op]
|
2017-10-20 18:00:47 +03:00
|
|
|
(if (op expected actual)
|
|
|
|
(do
|
|
|
|
(IO.color "green")
|
2018-06-15 17:38:34 +03:00
|
|
|
(IO.println &(str* @"Test '" @descr @"' passed"))
|
2017-10-20 18:00:47 +03:00
|
|
|
(IO.color "reset")
|
|
|
|
(State.update-passed (State.copy state) Int.inc))
|
|
|
|
(do
|
|
|
|
(IO.color "red")
|
2018-06-15 17:38:34 +03:00
|
|
|
(IO.println &(str* @"Test '" @descr @"' failed:"))
|
|
|
|
(IO.print &(str* @"\tExpected " @what @": '"))
|
2017-10-20 18:00:47 +03:00
|
|
|
(IO.print &(str expected))
|
2018-06-15 17:38:34 +03:00
|
|
|
(IO.println &(str* @"', actual value: '" (str actual) @"'"))
|
2017-10-20 18:00:47 +03:00
|
|
|
(IO.color "reset")
|
|
|
|
(State.update-failed (State.copy state) Int.inc))))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-op "Assert that op returns true when given x and y.")
|
2017-11-25 21:19:15 +03:00
|
|
|
(defn assert-op [state x y descr op]
|
|
|
|
(handler state x y descr "value" op))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-equal "Assert that x and y are equal. Equality needs to be implemented for their type.")
|
2017-11-25 21:19:15 +03:00
|
|
|
(defn assert-equal [state x y descr]
|
|
|
|
(handler state x y descr "value" =))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-not-equal "Assert that x and y are not equal. Equality needs to be implemented for their type.")
|
2017-11-25 21:19:15 +03:00
|
|
|
(defn assert-not-equal [state x y descr]
|
|
|
|
(handler state x y descr "not value" /=))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-true "Assert that x is true.")
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn assert-true [state x descr]
|
2017-11-25 21:19:15 +03:00
|
|
|
(assert-equal state true x descr))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-false "Assert that x is false.")
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn assert-false [state x descr]
|
2017-11-25 21:19:15 +03:00
|
|
|
(assert-equal state false x descr))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc reset "Reset test state.")
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn reset [state]
|
|
|
|
(State.set-failed (State.set-passed state 0) 0))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden run-child)
|
2018-05-23 14:57:55 +03:00
|
|
|
(defn run-child [x]
|
|
|
|
(let [pid (System.fork)
|
|
|
|
status 0]
|
|
|
|
(if (= pid 0)
|
2018-05-23 16:13:13 +03:00
|
|
|
(do
|
|
|
|
(x)
|
|
|
|
0)
|
2018-05-23 14:57:55 +03:00
|
|
|
(do
|
|
|
|
(ignore (System.wait (address status)))
|
|
|
|
(System.get-exit-status status)))))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden handle-signal)
|
2018-05-23 16:13:13 +03:00
|
|
|
(defn handle-signal [x] (System.exit x))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(hidden run-child-signals)
|
2018-05-23 16:13:13 +03:00
|
|
|
(defn run-child-signals [x]
|
|
|
|
(let [pid (System.fork)
|
|
|
|
status 0]
|
|
|
|
(if (= pid 0)
|
|
|
|
(do
|
|
|
|
(System.signal System.signal-abort handle-signal)
|
|
|
|
(System.signal System.signal-fpe handle-signal)
|
|
|
|
(System.signal System.signal-ill handle-signal)
|
|
|
|
(System.signal System.signal-segv handle-signal)
|
|
|
|
(System.signal System.signal-term handle-signal)
|
|
|
|
(x)
|
|
|
|
0)
|
|
|
|
(do
|
|
|
|
(ignore (System.wait (address status)))
|
|
|
|
(System.get-exit-status status)))))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-exit "Assert that function f exits with exit code exit-code.")
|
|
|
|
(defn assert-exit [state exit-code f descr]
|
|
|
|
(assert-equal state exit-code (run-child f) descr))
|
2018-05-23 14:57:55 +03:00
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc assert-exit "Assert that function f aborts with OS signal signal.")
|
2018-05-23 16:13:13 +03:00
|
|
|
(defn assert-signal [state signal x descr]
|
|
|
|
(assert-equal state signal (run-child-signals x) descr))
|
|
|
|
|
2018-06-14 11:49:28 +03:00
|
|
|
(doc print-test-results "Print test results.")
|
2017-10-20 18:00:47 +03:00
|
|
|
(defn print-test-results [state]
|
2018-01-26 23:23:29 +03:00
|
|
|
(let [passed @(State.passed state)
|
|
|
|
failed @(State.failed state)]
|
2017-10-20 18:00:47 +03:00
|
|
|
(do
|
|
|
|
(IO.println "Results:")
|
|
|
|
(if (Int.> (Int.+ passed failed) 0)
|
|
|
|
(do
|
|
|
|
(IO.color "green")
|
2018-05-20 07:04:47 +03:00
|
|
|
(if (Int.> passed 0) (IO.print &(StringCopy.append @"\t|" (String.repeat passed "="))) ())
|
2017-10-20 18:00:47 +03:00
|
|
|
(if (Int.= failed 0) (IO.print "|") ())
|
|
|
|
(IO.color "red")
|
|
|
|
(if (Int.= passed 0) (IO.print "\t|") ())
|
2018-05-20 07:04:47 +03:00
|
|
|
(if (Int.> failed 0) (IO.print &(StringCopy.append (String.repeat failed "=") @"|")) ())
|
2017-10-20 18:00:47 +03:00
|
|
|
(IO.println ""))
|
|
|
|
())
|
|
|
|
(IO.color "green")
|
|
|
|
(IO.print "\tPassed: ")
|
|
|
|
(IO.print &(Int.str passed))
|
|
|
|
(IO.color "red")
|
|
|
|
(IO.print "\tFailed: ")
|
|
|
|
(IO.println &(Int.str failed))
|
|
|
|
(IO.color "reset")
|
|
|
|
(State.copy state))))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defdynamic with-test-internal [name forms]
|
2018-05-20 10:57:51 +03:00
|
|
|
(if (= (length forms) 1)
|
2018-02-02 09:19:10 +03:00
|
|
|
(list (list 'set! name (list 'ref (car forms))))
|
|
|
|
(cons (list 'set! name (list 'ref (car forms)))
|
2017-10-20 18:00:47 +03:00
|
|
|
(with-test-internal name (cdr forms)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defmacro with-test [name :rest forms]
|
2017-12-17 17:19:31 +03:00
|
|
|
(list 'let [name '&(Test.State.init 0 0)]
|
2017-11-22 15:48:58 +03:00
|
|
|
(cons-last
|
2018-01-26 23:23:29 +03:00
|
|
|
(list 'Int.copy (list 'Test.State.failed name))
|
2017-11-22 15:48:58 +03:00
|
|
|
(cons 'do (with-test-internal name forms)))))
|
2017-10-20 18:00:47 +03:00
|
|
|
|
|
|
|
(defmacro deftest [name state-name :rest forms]
|
|
|
|
(list 'defn name []
|
2017-12-17 17:19:31 +03:00
|
|
|
(list 'let [state-name '&(Test.State.init 0 0)]
|
2017-10-20 18:00:47 +03:00
|
|
|
(cons-last
|
|
|
|
(list Test.print-test-results state-name)
|
|
|
|
(cons 'do (with-test-internal state-name forms))))))
|