Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Run all of the standard library's examples as tests
Browse files Browse the repository at this point in the history
This required fixing up a couple of files, which I guess is the whole
point :).
  • Loading branch information
SquidDev committed Aug 4, 2017
1 parent 4df597f commit 5e90787
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 92 deletions.
13 changes: 6 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ URN ?= ${LUA} bin/urn.lua

LIBS := $(shell find lib -type f -name "*.lisp")
MAIN_TESTS := $(shell find tests -type f -name '*.lisp' ! -name '*-helpers.lisp')
DOC_TESTS := base \
function \
list \
string \
type \
DOC_TESTS := $(filter-out test_prelude,$(LIBS:lib/%.lisp=test_%))

ifeq (${TIME},1)
LUA_FLAGS += --time
Expand All @@ -23,7 +19,7 @@ ifeq (${QUIET},1)
TEST_FLAGS += --quiet
endif

.PHONY: ${MAIN_TESTS} ${DOC_TESTS} all test compiler_test docs
.PHONY: ${MAIN_TESTS} ${DOC_TESTS} all test compiler_test docs tasks

all: ${OUT_DIR}/urn
compiler_test: all test
Expand All @@ -44,7 +40,7 @@ ${MAIN_TESTS}:

${DOC_TESTS}:
$(eval TMP := $(shell mktemp -d))
${URN} plugins/doc-test --run -o ${TMP} -- ${TEST_FLAGS} $@
${URN} plugins/doc-test --run -o ${TMP} -- ${TEST_FLAGS} $(@:test_%=%)
@rm -rf ${TMP}.lisp ${TMP}.lua ${TMP}

docs:
Expand All @@ -61,3 +57,6 @@ publish_docs: docs
git commit -m "Update docs"
git push origin gh-pages
git checkout master

tasks:
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
76 changes: 41 additions & 35 deletions lib/binders.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
### Example
```cl
(let* [(foo 1)
(bar (+ foo 1))]
foo
> (let* [(foo 1)
. (bar (+ foo 1))]
. bar)
out = 2
```"
(base/with (len (n vars))
(cond
Expand All @@ -58,9 +59,10 @@
### Example
```cl
(let [(foo 1)
(bar 2)]
(+ foo bar))
> (let [(foo 1)
. (bar 2)]
. (+ foo bar))
out = 3
```"
`((lambda ,(cars vars)
,@body)
Expand All @@ -71,15 +73,17 @@
evaluating BODY.
```cl
(when-let [(foo 1)
(bar nil)]
foo)
> (when-let [(foo 1)
. (bar nil)]
. foo)
out = nil
```
Does not evaluate `foo`, while
```
(when-let [(foo 1)
(bar 2)]
(+ foo bar))
> (when-let [(foo 1)
. (bar 2)]
. (+ foo bar))
out = 3
```
does."
`((lambda ,(cars vars)
Expand All @@ -94,10 +98,11 @@
### Example
```cl
(when-let* [(foo 1)
(bar nil)
(baz 2)
(+ foo baz))
> (when-let* [(foo 1)
. (bar nil)
. (baz 2)]
. (+ foo baz))
out = nil
```
Since `1` is truthy, it is evaluated and bound to `foo`, however,
Expand All @@ -116,8 +121,8 @@
### Example
```cl
(when-with (foo (get-idx bar :baz))
(print! foo))
> (when-with (foo (.> bar :baz))
. (print! foo))
```
When `bar` has an index `baz`, it will be bound to `foo` and
Expand All @@ -138,13 +143,13 @@
### Example
```
> (letrec [(is-even? (lambda (n)
(or (= 0 n)
(is-odd? (pred n)))))
(is-odd? (lambda (n)
(and (! (= 0 n))
(is-even? (pred n)))))]
(is-odd? 11))
true
. (or (= 0 n)
. (is-odd? (pred n)))))
. (is-odd? (lambda (n)
. (and (! (= 0 n))
. (is-even? (pred n)))))]
. (is-odd? 11))
out = true
```"
`((lambda ,(cars vars)
,@(map make-setting vars)
Expand All @@ -154,7 +159,7 @@
`((or (and (getmetatable ,x)
(get-idx (getmetatable ,x) :--finalise))
(get-idx ,x :close)
(lambda ()))))
(lambda ())), x))

(defmacro use (var &body)
"Bind each variable in VAR, checking for truthyness between bindings,
Expand All @@ -170,9 +175,10 @@
### Example:
```
> (use [(file (io/open \"temp\"))] \\
> (use [(file (io/open \"tests/data/hello.txt\"))]
. (print! (self file :read \"*a\")))
*contents of temp*
\"Hello, world!\"
out = nil
```"
`(when-let* ,var
,@body
Expand All @@ -182,12 +188,12 @@
(defmacro loop (vs test &body)
"A general iteration helper.
```cl
(loop [(var0 val0)
(var1 val1)
...]
[test test-body ...]
body ...)
```cl :no-test
> (loop [(var0 val0)
. (var1 val1)
. ...]
. [test test-body ...]
. body ...)
```
Bind all the variables given in VS. Each iteration begins by
Expand All @@ -203,7 +209,7 @@
```cl
> (loop [(o '())
(l '(1 2 3))]
. (l '(1 2 3))]
. [(empty? l) o]
. (recur (cons (car l) o) (cdr l)))
out = (3 2 1)
Expand Down
4 changes: 2 additions & 2 deletions lib/extra/benchmark.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Note that documentation strings and other attributes are not
preserved, and that the definition must be of the form
```
```cl :no-test
(defun foo (bar) ...)
``` "
(destructuring-bind [(defun ?name ?args . ?body) body]
Expand All @@ -26,7 +26,7 @@
Note that documentation strings and other attributes are not
preserved, and that the definition must be of the form
```
```cl :no-test
(defun foo (bar) ...)
``` "
(destructuring-bind [(defun ?name ?args . ?body) body]
Expand Down
17 changes: 10 additions & 7 deletions lib/extra/check.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
### Example:
```
> (check [(number a)] \\
> (check [(number a)]
. (= a a))
(= a a) passed 100 tests.
nil
> (check [(number a)] \\
> (check [(number a)]
. (= a (+ 1 a)))
(= a (+ 1 a)) falsified after 1 iteration(s)
falsifying set of values:
Expand Down Expand Up @@ -127,8 +127,9 @@
Example:
```cl
(check [(number a)]
(tripping tonumber tostring))
> (check [(number a)]
. (tripping tonumber tostring a))
out = true
```"
(let* [(y (gensym))]
`(let* [(,y ,x)]
Expand All @@ -139,8 +140,9 @@
Example:
```cl
(check [(number a)]
(=:= id (compose id id) a))
> (check [(number a)]
. (=:= id (compose id id) a))
out = true
```"
(let* [(y (gensym))]
`(let* [(,y ,x)]
Expand All @@ -156,7 +158,8 @@
name.
Example:
```cl
(forall a (eq? a (id a)))
> (forall a (eq? a (id a)))
out = true
```"
(let* [(v (if (list? var)
var
Expand Down
56 changes: 28 additions & 28 deletions lib/extra/io.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
Returns nil if it failed.
### Example
```
> (read-all! \"hello.txt\")
\"Hello, world!\"
```cl
> (read-all! \"tests/data/hello.txt\")
out = \"Hello, world!\"
```"
(read-all-mode! path false))

Expand All @@ -23,21 +23,21 @@
Returns nil if it failed.
### Example
```
> (read-lines! \"lines.txt\")
(\"This is the first line.\" \"This is the second.\")
```cl
> (read-lines! \"tests/data/lines.txt\")
out = (\"This is the first line.\" \"This is the second.\")
```"
(when-with (data (read-all! path))
(split data "\n")))
(string/split data "\n")))

(defun read-bytes! (path)
"Reads the data from the file at PATH and returns it as a list of bytes
(numbers). Returns nil if it failed.
### Example
```
> (read-bytes! \"abc.txt\")
(97 98 99)
```cl
> (read-bytes! \"tests/data/abc.txt\")
out = (97 98 99)
```"
(when-with (data (read-all-mode! path true))
(letrec [(string->bytes (str idx)
Expand All @@ -64,9 +64,9 @@
does. Returns true if it succeeded or false if it failed.
### Example
```
> (write-all! \"hello.txt\" \"Hello, world!\")
true
```cl
> (write-all! \"tests/data/hello_.txt\" \"Hello, world!\")
out = true
```"
(write-all-mode! path false false data))

Expand All @@ -77,9 +77,9 @@
does. Returns true if it succeeded or false if it failed.
### Example
```
> (write-lines! \"lines.txt\" `(\"This is the first line.\" \"This is the second.\"))
true
```cl
> (write-lines! \"tests/data/lines_.txt\" `(\"This is the first line.\" \"This is the second.\"))
out = true
```"
(write-all! path (concat data "\n")))

Expand All @@ -90,9 +90,9 @@
does. Returns true if it succeeded or false if it failed.
### Example
```
> (write-bytes! \"abc.txt\" `(97 98 99))
true
```cl
> (write-bytes! \"tests/data/abc_.txt\" `(97 98 99))
out = true
```"
(letrec [(bytes->string (bytes idx)
(if (> idx (n bytes))
Expand All @@ -107,9 +107,9 @@
Returns true if it succeeded or false if it failed.
### Example
```
> (append-all! \"hello.txt\" \" Some appended text.\")
true
```cl
> (append-all! \"tests/data/hello_.txt\" \" Some appended text.\")
out = true
```"
(write-all-mode! path true false data))

Expand All @@ -120,9 +120,9 @@
or false if it failed.
### Example
```
> (append-lines! \"lines.txt\" `(\" Here's another line:\" \"Another line.\"))
true
```cl
> (append-lines! \"tests/data/lines_.txt\" `(\" Here's another line:\" \"Another line.\"))
out = true
```"
(append-all! path (concat data "\n")))

Expand All @@ -133,9 +133,9 @@
or false if it failed.
### Example
```
> (append-bytes! \"abc.txt\" `(100 101 102))
true
```cl
> (append-bytes! \"tests/data/abc_.txt\" `(100 101 102))
out = true
```"
(letrec [(bytes->string (bytes idx)
(if (> idx (n bytes))
Expand Down
2 changes: 1 addition & 1 deletion lib/extra/test.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
(,time (any (lambda (,'x) (or (= ,'x "--time") (= ,'x "-t"))) arg))]
,@body

(when (and ,quiet (> ,tests-total 0))
(when (and ,quiet (or (> ,tests-total 0) (> (n ,tests-pending) 0)))
;; If we've been outputting dots then add a new line
(print!))

Expand Down
Loading

0 comments on commit 5e90787

Please sign in to comment.