aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorJosef Pospíšil <josef.pospisil@laststar.eu>2021-01-18 17:15:42 +0100
committerJosef Pospíšil <josef.pospisil@laststar.eu>2021-01-18 17:15:42 +0100
commit67444c7c8b7efdf8c175b3ab87fe8dbc89d4df3c (patch)
tree60313b850a802a0ac74a8bd3643fbd038db6520b /test
parentSogaiu check (diff)
Make tests uroborical
Diffstat (limited to 'test')
-rw-r--r--test/helper.janet52
-rw-r--r--test/suite0.janet4
-rw-r--r--test/suite1.janet30
-rw-r--r--test/suite2.janet2
-rw-r--r--test/suite3.janet2
-rw-r--r--test/suite4.janet2
-rw-r--r--test/suite5.janet40
-rw-r--r--test/suite6.janet4
-rw-r--r--test/suite7.janet46
9 files changed, 72 insertions, 110 deletions
diff --git a/test/helper.janet b/test/helper.janet
deleted file mode 100644
index 822eba6..0000000
--- a/test/helper.janet
+++ /dev/null
@@ -1,52 +0,0 @@
-# Helper code for running tests
-
-(var num-tests-passed 0)
-(var num-tests-run 0)
-(var suite-num 0)
-(var numchecks 0)
-(var start-time 0)
-
-(defn assert
- "Override's the default assert with some nice error handling."
- [x &opt e]
- (default e "assert error")
- (++ num-tests-run)
- (when x (++ num-tests-passed))
- (if x
- (do
- (when (= numchecks 25)
- (set numchecks 0)
- (print))
- (++ numchecks)
- (file/write stdout "\e[32m✔\e[0m"))
- (do
- (file/write stdout "\n\e[31m✘\e[0m ")
- (set numchecks 0)
- (print e)))
- x)
-
-(defn assert-not
- "Invert assert."
- [x &opt e]
- (assert (not x) e))
-
-(defmacro assert-error
- [msg & forms]
- (def errsym (keyword (gensym)))
- ~(assert (= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))
-
-(defmacro assert-no-error
- [msg & forms]
- (def errsym (keyword (gensym)))
- ~(assert (not= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))
-
-(defn start-suite [x]
- (set suite-num x)
- (set start-time (os/clock))
- (print))
-
-(defn end-suite []
- (def delta (- (os/clock) start-time))
- (printf "\n\ntest suite %d finished in %.3f seconds" suite-num delta)
- (print num-tests-passed " of " num-tests-run " tests passed.\n")
- (if (not= num-tests-passed num-tests-run) (os/exit 1)))
diff --git a/test/suite0.janet b/test/suite0.janet
index 8f0a3b3..4430ca7 100644
--- a/test/suite0.janet
+++ b/test/suite0.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/fmt)
(start-suite 0)
@@ -9,7 +9,7 @@
(fmt/format-print "(\n print\n \"HOHOHO\")"))
(assert (deep= b @"(print\n \"HOHOHO\")\n") "format-print")
- # regresion with comment in the collection literals
+# regresion with comment in the collection literals
(buffer/clear b)
(with-dyns [:out b]
(fmt/format-print "{:a 0\n:b 1 # test comment\n}"))
diff --git a/test/suite1.janet b/test/suite1.janet
index 661104c..c8f4a1f 100644
--- a/test/suite1.janet
+++ b/test/suite1.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/misc)
(start-suite 1)
@@ -6,34 +6,6 @@
#misc/dedent
(assert (= (misc/dedent " a\n b\n c\n d") "a\n b\n c\n d") "dedent")
-#misc/timeit
-(defmacro- capture-stdout
- [form]
- (with-syms [buf res]
- ~(do
- (def ,buf (buffer/new 1024))
- (with-dyns [:out ,buf]
- (def ,res ,form)
- [,res (string ,buf)]))))
-
-(do
- (def [result output]
- (capture-stdout
- (misc/timeit (loop [i :range [1 100000]] i) "foo:")))
- (assert (nil? result))
- (def m (peg/match '(* "foo: " (<- (some :S)) " seconds\n" -1) output))
- (assert (truthy? m) "timeit -- invalid output")
- (assert (scan-number (in m 0)) "timeit -- invalid number of seconds"))
-
-(do
- (def [result output]
- (capture-stdout
- (misc/timeit "someresult")))
- (assert (= result "someresult") "timeit2 -- invalid return")
- (def m (peg/match '(* "Elapsed time: " (<- (some :S)) " seconds\n" -1) output))
- (assert (truthy? m) "timeit2 -- invalid output")
- (assert (scan-number (in m 0)) "timeit2 -- invalid number of seconds"))
-
#misc/set*
(do
(var x 2)
diff --git a/test/suite2.janet b/test/suite2.janet
index 6bd0894..d978a66 100644
--- a/test/suite2.janet
+++ b/test/suite2.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/rpc)
(start-suite 2)
diff --git a/test/suite3.janet b/test/suite3.janet
index 9b845fa..f4a2dce 100644
--- a/test/suite3.janet
+++ b/test/suite3.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/msg)
(start-suite 3)
diff --git a/test/suite4.janet b/test/suite4.janet
index 2397fb7..f76fe3d 100644
--- a/test/suite4.janet
+++ b/test/suite4.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/regex)
(start-suite 4)
diff --git a/test/suite5.janet b/test/suite5.janet
index 7df7471..d0d0b75 100644
--- a/test/suite5.janet
+++ b/test/suite5.janet
@@ -1,5 +1,5 @@
-(use ./helper)
-(use ../spork/argparse)
+(use ../spork/test)
+(import ../spork/argparse)
(start-suite 5)
@@ -22,12 +22,8 @@
:help "Some option?"
:default "123"}])
-(defmacro suppress-stdout [& body]
- ~(with-dyns [:out @""]
- ,;body))
-
(with-dyns [:args @["testcase.janet" "-k" "100"]]
- (def res (suppress-stdout (argparse ;argparse-params)))
+ (def res (suppress-stdout (argparse/argparse ;argparse-params)))
(when (res "debug") (error (string "bad debug: " (res "debug"))))
(when (res "verbose") (error (string "bad verbose: " (res "verbose"))))
(assert (= (res "key") "100") (string "bad key: " (res "key")))
@@ -35,50 +31,50 @@
(assert (= (res "thing") "123") (string "bad thing: " (res "thing"))))
(with-dyns [:args @["testcase.janet" "-k" "100" "--thing"]]
- (def res (suppress-stdout (argparse ;argparse-params)))
+ (def res (suppress-stdout (argparse/argparse ;argparse-params)))
(assert-not res "Option \"thing\" missing arg, but result is non-nil."))
(with-dyns [:args @["testcase.janet" "-k" "100" "-e" "foo" "-e"]]
- (def res (suppress-stdout (argparse ;argparse-params)))
+ (def res (suppress-stdout (argparse/argparse ;argparse-params)))
(assert-not res "Option \"expr\" missing arg, but result is non-nil."))
(with-dyns [:args @["testcase.janet" "-k" "100" "-v" "--thing" "456" "-d" "-v"
"-e" "abc" "-vvv" "-e" "def"]]
- (def res (suppress-stdout (argparse ;argparse-params)))
+ (def res (suppress-stdout (argparse/argparse ;argparse-params)))
(assert (res "debug") (string "bad debug: " (res "debug")))
(assert (= (res "verbose") 5) (string "bad verbose: " (res "verbose")))
(assert (= (tuple ;(res "expr")) ["abc" "def"])
- (string "bad expr: " (string/join (res "expr") " ")))
+ (string "bad expr: " (string/join (res "expr") " ")))
(assert (= (res "thing") "456") (string "bad thing: " (res "thing")))
(assert (= (tuple ;(res :order))
["key" "verbose" "thing" "debug" "verbose"
"expr" "verbose" "verbose" "verbose" "expr"])
- (string "bad order: " (string/join (res :order) " "))))
+ (string "bad order: " (string/join (res :order) " "))))
(with-dyns [:args @["testcase.janet" "server"]]
- (def res (suppress-stdout (argparse
+ (def res (suppress-stdout (argparse/argparse
"A simple CLI tool."
:default {:kind :option})))
(assert (= (res :default) "server")
- (string "bad default " (res :default))))
+ (string "bad default " (res :default))))
(with-dyns [:args @["testcase.janet" "server" "run"]]
- (def res (suppress-stdout (argparse
+ (def res (suppress-stdout (argparse/argparse
"A simple CLI tool."
:default {:kind :accumulate})))
(assert (and (deep= (res :default) @["server" "run"]))
- (string "bad default " (res :default))))
+ (string "bad default " (res :default))))
(with-dyns [:args @["testcase.janet" "-k" "100" "--fake"]]
- (def res (suppress-stdout (argparse ;argparse-params)))
+ (def res (suppress-stdout (argparse/argparse ;argparse-params)))
(assert-not res "Option \"fake\" is not valid, but result is non-nil."))
(with-dyns [:args @["testcase.janet" "-l" "100" "--" "echo" "-n" "ok"]]
- (def res (suppress-stdout (argparse "A simple CLI tool"
- "length" {:kind :option
- :short "l"
- :help "key"}
- :default {:kind :accumulate})))
+ (def res (suppress-stdout (argparse/argparse "A simple CLI tool"
+ "length" {:kind :option
+ :short "l"
+ :help "key"}
+ :default {:kind :accumulate})))
(assert res "arguments were not parsed correctly in the presence of `--`.")
(def {"length" len :default cmd-args} res)
(assert (= len "100")
diff --git a/test/suite6.janet b/test/suite6.janet
index 74c4768..02a82c2 100644
--- a/test/suite6.janet
+++ b/test/suite6.janet
@@ -1,4 +1,4 @@
-(use ./helper)
+(use ../spork/test)
(import ../spork/path)
(start-suite 6)
@@ -7,7 +7,7 @@
"assert equal"
[x y &opt deep]
(assert ((if deep deep= =) x y)
- (string "expected " x " to equal " y)))
+ (string "expected " x " to equal " y)))
(aeq (path/posix/abspath? "/home") true)
(aeq (path/posix/abspath? "home") false)
diff --git a/test/suite7.janet b/test/suite7.janet
new file mode 100644
index 0000000..a91efb4
--- /dev/null
+++ b/test/suite7.janet
@@ -0,0 +1,46 @@
+(import ../spork/test)
+
+(test/start-suite 7)
+# test/assert
+(assert (test/assert true "assert"))
+
+# test/assert-not
+(assert (test/assert-not false "assert-not"))
+
+# test/assert-error
+(assert (test/assert-error "assert-error" (error "Bad")))
+
+# test/assert-no-error
+(assert (test/assert-no-error "assert-no-error" "Good"))
+
+# test/capture-stdout
+(test/assert (= [true "Output\n"] (test/capture-stdout (do (print "Output") true))) "capture output")
+
+# test/timeit
+(do
+ (def [result output]
+ (test/capture-stdout
+ (test/timeit (loop [i :range [1 100000]] i) "foo:")))
+ (test/assert (nil? result))
+ (def m (peg/match '(* "foo: " (<- (some :S)) " seconds\n" -1) output))
+ (test/assert (truthy? m) "timeit -- invalid output")
+ (test/assert (scan-number (in m 0)) "timeit -- invalid number of seconds"))
+
+(do
+ (def [result output]
+ (test/capture-stdout
+ (test/timeit "someresult")))
+ (test/assert (= result "someresult") "timeit2 -- invalid return")
+ (def m (peg/match '(* "Elapsed time: " (<- (some :S)) " seconds\n" -1) output))
+ (test/assert (truthy? m) "timeit2 -- invalid output")
+ (test/assert (scan-number (in m 0)) "timeit2 -- invalid number of seconds"))
+
+(test/assert
+ (= [nil "\nRunning test suite 666 tests...\n\n\e[32m\xE2\x9C\x94\e[0m\n\nTest suite 666 finished in 0.000 soconds\n12 of 12 tests passed.\n\n"])
+ (test/capture-stdout
+ (do
+ (test/start-suite 666)
+ (test/assert true "true")
+ (test/end-suite))))
+
+(test/end-suite)