Skip to content

Commit

Permalink
Merge pull request #15 from pepe/add-test
Browse files Browse the repository at this point in the history
Add simple test helper, extracted from janet-lang
  • Loading branch information
bakpakin committed Jan 19, 2021
2 parents eab487d + 56d9cef commit 714c216
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 119 deletions.
114 changes: 105 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,78 @@ arguments to `argparse`.

Run `(doc argparse/argparse)` after importing for more information.

## Misc
## Test

### Dedent
This module contains a simple test helper when you do not need a specialized
library.

Remove indentation after concatenating the arguments.
### assert

Modified version of assert, with some nice error handling.

```clojure
(misc/dedent ``
ho
hoho
hohoho
``))))) => "ho\n hoho\n hohoho"
(test/assert false "How is this?")
# => ✘ How is this?
(test/assert true "OK")
# => ✔true
```

### assert-not

Invert assert.

```clojure
(test/assert-not false "OK")
# => ✔true
```

### assert-error

Test passes if forms throw errors.

```clojure
(test/assert-error "To err is natural" (error "Bad"))
# => ✔true
```

### assert-no-error

Test passes if forms throw errors.

```clojure
(test/assert-no-error "To not err is desired" (do "Good"))
# => ✔true
```

### start-suite

Starts test suite, which counts all and passed tests.

### end-suite

Ends test suite and print summary.

### All together

Example of simple test suite.

```clojure
(import spork/test)

(test/start-suite 0)

(test/assert true "is always true")
(test/assert-not false "is always false")
(test/assert-error "To err is natural" (error "Bad"))
(test/assert-no-error "To not err is desired" (do "Good"))

(test/end-suite)

# =>

Test suite 0 finished in 0.000 soconds
4 of 4 tests passed.

```

### timeit
Expand All @@ -207,6 +267,42 @@ Elapsed time: 0.0718288 seconds
6.66666e+08
```

### capture-stdout

Runs the form and captures stdout. Returns tuple with result and captured
stdout in string.

```clojure
(capture-stdout
(do
(print "Interesting output")
true))
# => (true "Interesting output")
```

### supress-stdout

Runs the form, but supresses its stdout.

```clojure
(suppress-stdout (print "Hello world!"))
# => nil
```

## Misc

### Dedent

Remove indentation after concatenating the arguments.

```clojure
(misc/dedent ``
ho
hoho
hohoho
``))))) => "ho\n hoho\n hohoho"
```

### set*

Allow parallel mutation of multiple mutable variables. (All right
Expand All @@ -229,4 +325,4 @@ x => @[3 5]

```
[sudo] jpm install https://github.com/janet-lang/spork.git
```
```
89 changes: 89 additions & 0 deletions spork/test/init.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# 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)
(if x (++ num-tests-passed))
(if x
(do
(when (= numchecks 25)
(set numchecks 0)
(print))
(++ numchecks)
(prin "\e[32m✔\e[0m"))
(do
(prin "\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
"Test passes if forms error."
[msg & forms]
(def errsym (keyword (gensym)))
~(,assert (= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))

(defmacro assert-no-error
"Test passes if forms do not error."
[msg & forms]
(def errsym (keyword (gensym)))
~(,assert (not= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg))

(defn start-suite [x]
"Starts test suite."
(set suite-num x)
(set start-time (os/clock))
(print "\nRunning test suite " x " tests...\n"))

(defn end-suite []
"Ends test suite."
(def delta (- (os/clock) start-time))
(printf "\n\nTest suite %d finished in %.3f soconds" 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)))

(defmacro timeit
```
Time the execution of `form` using `os/clock` before and after,
and print the result to stdout. returns: result of executing `form`.
Uses `tag` (default "Elapsed time:") to tag the printout.
```
[form &opt tag]
(default tag "Elapsed time:")
(with-syms [start result end]
~(do
(def ,start (os/clock))
(def ,result ,form)
(def ,end (os/clock))
(print ,tag " " (- ,end ,start) " seconds")
,result)))

(defmacro capture-stdout
```
Runs the form and captures stdout. Returns tuple with result of the form
and a string with captured stdout.
```
[form]
(with-syms [buf res]
~(do
(def ,buf @"")
(with-dyns [:out ,buf]
(def ,res ,form)
[,res (string ,buf)]))))

(defmacro suppress-stdout [& body]
"Suppreses stdout from the body"
~(with-dyns [:out @""] ,;body))
52 changes: 0 additions & 52 deletions test/helper.janet

This file was deleted.

4 changes: 2 additions & 2 deletions test/suite0.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(use ./helper)
(use ../spork/test)
(import ../spork/fmt)

(start-suite 0)
Expand All @@ -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}"))
Expand Down
30 changes: 1 addition & 29 deletions test/suite1.janet
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
(use ./helper)
(use ../spork/test)
(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)
Expand Down
2 changes: 1 addition & 1 deletion test/suite2.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(use ./helper)
(use ../spork/test)
(import ../spork/rpc)

(start-suite 2)
Expand Down
2 changes: 1 addition & 1 deletion test/suite3.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(use ./helper)
(use ../spork/test)
(import ../spork/msg)

(start-suite 3)
Expand Down
2 changes: 1 addition & 1 deletion test/suite4.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(use ./helper)
(use ../spork/test)
(import ../spork/regex)

(start-suite 4)
Expand Down
Loading

0 comments on commit 714c216

Please sign in to comment.