diff options
| author | 2021-01-11 00:11:51 -0600 | |
|---|---|---|
| committer | 2021-01-11 00:11:51 -0600 | |
| commit | 8b834a2b27d646146a4a76cda9affce3622b34cb (patch) | |
| tree | 5ab071382e09c6308fef88cd58226ba63800c277 /test | |
| parent | Tweak some ranges in escapedchar2 (diff) | |
| parent | Merge pull request #11 from MikeBeller/timeit-setstar (diff) | |
Merge branch 'master' into range-tweaks
Diffstat (limited to 'test')
| -rw-r--r-- | test/helper.janet | 9 | ||||
| -rw-r--r-- | test/suite1.janet | 44 | ||||
| -rw-r--r-- | test/suite4.janet | 7 | ||||
| -rw-r--r-- | test/suite5.janet | 89 | ||||
| -rw-r--r-- | test/suite6.janet | 52 |
5 files changed, 199 insertions, 2 deletions
diff --git a/test/helper.janet b/test/helper.janet index 373eb81..822eba6 100644 --- a/test/helper.janet +++ b/test/helper.janet @@ -25,6 +25,11 @@ (print e))) x) +(defn assert-not + "Invert assert." + [x &opt e] + (assert (not x) e)) + (defmacro assert-error [msg & forms] (def errsym (keyword (gensym))) @@ -38,10 +43,10 @@ (defn start-suite [x] (set suite-num x) (set start-time (os/clock)) - (print "\nRunning test suite " x " tests...\n ")) + (print)) (defn end-suite [] (def delta (- (os/clock) start-time)) - (printf "\n\nTest suite %d finished in %.3f seconds" suite-num delta) + (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/suite1.janet b/test/suite1.janet index 879057b..2cf9005 100644 --- a/test/suite1.janet +++ b/test/suite1.janet @@ -2,5 +2,49 @@ (import ../spork/misc) (start-suite 1) + +#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) + (var y 3) + (misc/set* [x y] [y (+ x y)]) + (print x " " y) + (assert (and (= x 3) (= y 5)) "set* 1")) + +(do + (def x @[2 3]) + (misc/set* [[x 0] [x 1]] [(in x 1) (+ (in x 0) (in x 1))]) + (assert (deep= x @[3 5]))) + (end-suite) diff --git a/test/suite4.janet b/test/suite4.janet index 6e9efd2..9e0eeef 100644 --- a/test/suite4.janet +++ b/test/suite4.janet @@ -15,5 +15,12 @@ (assert (regex/match `\a+` `Xy`) "match 7") (assert (regex/match `\w+` `Xy0`) "match 8") +(assert (regex/match `cat|dog` "cat") "match 6") +(assert (regex/match `cat|dog` "dog") "match 7") +(assert (not (regex/match `cat|dog` "mouse")) "match 8") +(assert (regex/match `cat|dog|mouse` "mouse") "match 9") +(assert (regex/match `cat|dog|mouse` "cat") "match 10") +(assert (regex/match `cat|dog|mouse` "dog") "match 11") +(assert (regex/match `(cat|dog|mouse)+` "mousecatdog") "match 12") (end-suite) diff --git a/test/suite5.janet b/test/suite5.janet new file mode 100644 index 0000000..7df7471 --- /dev/null +++ b/test/suite5.janet @@ -0,0 +1,89 @@ +(use ./helper) +(use ../spork/argparse) + +(start-suite 5) + +(def argparse-params + ["A simple CLI tool. An example to show the capabilities of argparse." + "debug" {:kind :flag + :short "d" + :help "Set debug mode."} + "verbose" {:kind :multi + :short "v" + :help "Print debug information to stdout."} + "key" {:kind :option + :short "k" + :help "An API key for getting stuff from a server." + :required true} + "expr" {:kind :accumulate + :short "e" + :help "Search for all patterns given."} + "thing" {:kind :option + :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))) + (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"))) + (assert-not (res "expr") (string "bad expr: " (res "expr"))) + (assert (= (res "thing") "123") (string "bad thing: " (res "thing")))) + +(with-dyns [:args @["testcase.janet" "-k" "100" "--thing"]] + (def res (suppress-stdout (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))) + (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))) + (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") " "))) + (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) " ")))) + +(with-dyns [:args @["testcase.janet" "server"]] + (def res (suppress-stdout (argparse + "A simple CLI tool." + :default {:kind :option}))) + (assert (= (res :default) "server") + (string "bad default " (res :default)))) + +(with-dyns [:args @["testcase.janet" "server" "run"]] + (def res (suppress-stdout (argparse + "A simple CLI tool." + :default {:kind :accumulate}))) + (assert (and (deep= (res :default) @["server" "run"])) + (string "bad default " (res :default)))) + +(with-dyns [:args @["testcase.janet" "-k" "100" "--fake"]] + (def res (suppress-stdout (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}))) + (assert res "arguments were not parsed correctly in the presence of `--`.") + (def {"length" len :default cmd-args} res) + (assert (= len "100") + "option was not parsed correctly in the presence of `--`.") + (assert (= ["echo" "-n" "ok"] (tuple ;cmd-args)) + "unnamed arguments after `--` were not parsed correctly.")) + +(end-suite) diff --git a/test/suite6.janet b/test/suite6.janet new file mode 100644 index 0000000..74c4768 --- /dev/null +++ b/test/suite6.janet @@ -0,0 +1,52 @@ +(use ./helper) +(import ../spork/path) + +(start-suite 6) + +(defn aeq + "assert equal" + [x y &opt deep] + (assert ((if deep deep= =) x y) + (string "expected " x " to equal " y))) + +(aeq (path/posix/abspath? "/home") true) +(aeq (path/posix/abspath? "home") false) +(aeq (path/posix/abspath? "./home") false) +(aeq (path/posix/abspath? "../home") false) +(aeq (path/posix/abspath? "") false) +(aeq (path/posix/abspath? "//") true) + +(aeq (path/abspath "home") (path/join (os/cwd) "home")) +(aeq (path/posix/join "1" "2" "3") "1/2/3") +(aeq (path/posix/join "1" ".." "2" ".." "3") "3") +(aeq (path/posix/join "/home/" "for") "/home/for") + +(aeq (path/posix/normalize "/abc/../") "/") +(aeq (path/posix/normalize "/abc/abc") "/abc/abc") +(aeq (path/posix/normalize "/abc/abc/..") "/abc") +(aeq (path/posix/normalize "/abc/abc/../") "/abc/") +(aeq (path/posix/normalize "/abc/abc/../.") "/abc") +(aeq (path/posix/normalize "//////abc/abc/../.") "/abc") +(aeq (path/posix/normalize "//////.") "/") +(aeq (path/posix/normalize "//////") "/") + +(aeq (path/posix/dirname "abc/def") "abc/") +(aeq (path/posix/dirname "abc/") "abc/") +(aeq (path/posix/basename "abc") "abc") +(aeq (path/posix/dirname "abc") "./") + +(aeq (path/posix/ext "project.janet") ".janet") +(aeq (path/posix/ext "/home/pork/work/project.janet") ".janet") + +(aeq (path/posix/parts "/home/pork/.local/share") + @["" "home" "pork" ".local" "share"] true) + +(aeq (path/posix/parts ".local/share") + @[".local" "share"] true) + +(with-dyns [:path-cwd "D:\\Users\\sumbuddy"] + (aeq (path/win32/abspath "C:\\home\\pork") "C:\\home\\pork") + (aeq (path/win32/abspath "q:\\home\\pork") "q:\\home\\pork") + (aeq (path/win32/abspath "..\\home\\pork") "D:\\Users\\home\\pork")) + +(end-suite) |
