Skip to content

Commit

Permalink
Add (assert-throws), minor refactor and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Mar 18, 2015
1 parent 07f22a3 commit 334bc47
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.3.0 (2015-03-19)

* Add new assertion: assert-throws
* Minor refactor with (plural?)
* Stylistic changes to MODULE_INFO (credit Alexander Burger)
* Update README.md

## 0.2.1 (2015-03-18)

* Version bump, includes README.md and working .travis.yml
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ There exists a few public functions: `(execute)`, `(report)`, and a bunch of `(a

Only a few assertions exist for the moment; more [can](#contributing)/will be added.

| Assertion + Arguments | Example |
| --------------------- | ------- |
| (assert-equal Expected Result Message) | `(assert-equal 0 0 "It must be zero")` |
| (assert-nil Result Message) | `(assert-nil NIL "It must be NIL")` |
| (assert-t Result Message) | `(assert-t T "I pity the fool!")` |
| (assert-includes String List Message) | `(assert-includes "abc" '("xyzabcdef") "It includes abc")` |
| (assert-kind-of Type Value Message) | `(assert-equal 'Number 42 "The answer..")` |
| Assertion | Arguments | Example |
| :----------| :---------: | :-------: |
| (assert-equal) | Expected Result Message | `(assert-equal 0 0 "It must be zero")` |
| (assert-nil) | Result Message | `(assert-nil NIL "It must be NIL")` |
| (assert-t) | Result Message | `(assert-t T "I pity the fool!")` |
| (assert-includes) | String List Message | `(assert-includes "abc" '("def") "It includes abc")` |
| (assert-kind-of) | Type Value Message | `(assert-equal 'Number 42 "The answer..")` |
| (assert-throws) | Type Error 'Result Message | `(assert-throws 'Err "fail" '(throw 'Err "fail") "Throws a fail")` |

### (assert-kind-of) types

Expand All @@ -75,6 +76,7 @@ There are 5 types currently defined:
* If your tests are **order dependent**, then you can: `(setq *My_tests_are_order_dependent T)`
* Colours and bold text are only displayed if your terminal supports it, and if your system has the `tput` command.
* The `(assert-includes)` function uses [sub?](http://software-lab.de/doc/refS.html#sub?) to find a substring in a string or list.
* The `(assert-throws)` function requires the `(throw)` to be [quoted](http://software-lab.de/doc/refQ.html#quote).

# Examples

Expand Down
3 changes: 2 additions & 1 deletion test/test_unit.l
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
'(assert-kind-of 'String "picolisp" "(assert-kind-of) should assert a String")
'(assert-kind-of 'Number 42 "(assert-kind-of) should assert a Number")
'(assert-kind-of 'List (1 2 3 4) "(assert-kind-of) should assert a List")
'(assert-kind-of 'Atom 'atom "(assert-kind-of) should assert a Atom") ]
'(assert-kind-of 'Atom 'atom "(assert-kind-of) should assert a Atom")
'(assert-throws 'InternalError '(UnitTestError . "Unit test throws an error") '(throw 'InternalError (cons 'UnitTestError "Unit test throws an error")) "(assert-throws) should assert a thrown error") ]
31 changes: 20 additions & 11 deletions unit.l
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(when symbols (symbols 'unit 'pico))

[setq MODULE_INFO
'(("name" "unit")
("version" "0.2.1")
("summary" "Unit Testing framework for PicoLisp")
("source" "https://github.com/aw/picolisp-unit.git")
("author" "Alexander Williams")
("license" "MIT")
("copyright" "(c) 2015 Alexander Williams, Unscramble <license@unscramble.jp>") ]
[de MODULE_INFO
("name" "unit")
("version" "0.3.0")
("summary" "Unit Testing framework for PicoLisp")
("source" "https://github.com/aw/picolisp-unit.git")
("author" "Alexander Williams")
("license" "MIT")
("copyright" "(c) 2015 Alexander Williams, Unscramble <license@unscramble.jp>") ]

(setq
*Colours '(("red" . 1)
Expand Down Expand Up @@ -37,6 +37,9 @@
List
(by '((N) (rand 1 (size List))) sort List) ]

[de plural? (String)
(when (not (= (length String) 1)) "s") ]

[de passed @
(prinl (colour "green")
(char (hex "2713"))
Expand All @@ -61,11 +64,11 @@
"----^J"
(colour "green")
*Passed
" test" (when (not (= *Passed 1)) "s")
" test" (plural? *Passed)
" passed, "
(colour "red")
(length *Failed)
" test" (when (not (= (length *Failed) 1)) "s")
" test" (plural? *Failed)
" failed"
(colour) )

Expand All @@ -74,7 +77,7 @@

(de report-failed ()
(when *Failed
(prinl "^J" (colour "red") "^J Failed test" (when (not (= (length *Failed) 1)) "s") ": ")
(prinl "^J" (colour "red") "^J Failed test" (plural? *Failed) ": ")

(mapcar
'((N) (print-error (car N) (cdddr N))
Expand Down Expand Up @@ -153,5 +156,11 @@
(passed Message)
(failed (pack Value " should be of type " Type) Result Message) ]

[de assert-throws (Type Error Result Message)
(let Result (catch Type (eval Result) NIL)
(if (= Error Result)
(passed Message)
(failed Error Result Message) ]

(de report ()
(print-report) )

0 comments on commit 334bc47

Please sign in to comment.