Skip to content

Commit

Permalink
Fix docstring extraction
Browse files Browse the repository at this point in the history
Correctly extract docstrings (and other parts) from
`function-lambda-expression`.

Closes #6
  • Loading branch information
simendsjo committed Mar 15, 2024
1 parent 9e59163 commit 0f8c229
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/doctest.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,28 @@
(with-input-from-string (docstring (or documentation ""))
(run-doctests docstring output)))

(defun parse-lambda-body (body)
(values (and (stringp (car body)) (cdr body) (pop body))
(and (consp (car body)) (eq 'declare (caar body)) (pop body))
body))

(defun parse-function-lambda-expression (function)
(multiple-value-bind (lambda-expression closure-p name) (function-lambda-expression function)
(declare (ignore closure-p))
(let ((parameters (cadr lambda-expression))
(lambda-body (cddr lambda-expression)))
(multiple-value-bind (docstring declare body) (parse-lambda-body lambda-body)
(values name parameters docstring declare body)))))

(defun extract-function-documentation-and-name (function)
;; ABCL doesn't give documentation for (documentation function 'function) for all expressions.
;; We try function-lambda-expression too
(multiple-value-bind (lambda-expression closure-p name) (function-lambda-expression function)
(declare (ignore closure-p))
(values (or (documentation function 'function) (third lambda-expression))
(symbol-name name))))
(multiple-value-bind (name parameters docstring declare body) (parse-function-lambda-expression function)
(declare (ignore parameters declare body))
(values (or (documentation function 'function)
docstring
"")
name)))

(defun test-function (function &key (output t))
"Test-function extracts and tests code snippets in <function>'s documentation
Expand Down
33 changes: 33 additions & 0 deletions tests/doctest.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@
(assert-equalp expected-output actual-output))
(values (first result) (second result))))

(defun test-parse-function-lambda-expression (function &key
(expected-parameters nil)
(expected-docstring nil)
(expected-declare nil)
(expected-body nil))
(multiple-value-bind (name parameters docstring declare body) (doctest::parse-function-lambda-expression function)
(assert-true name)
(assert-equalp expected-parameters parameters)
(assert-equalp expected-docstring docstring)
(assert-equalp expected-declare declare)
(assert-equalp expected-body body)))

(define-test parse-function-lambda-expression ()
(test-parse-function-lambda-expression (lambda ()))
(test-parse-function-lambda-expression (lambda (a b))
:expected-parameters '(a b))
(test-parse-function-lambda-expression (lambda () "body")
:expected-body '("body"))
(test-parse-function-lambda-expression (lambda () (declare) "body")
:expected-declare '(declare)
:expected-body '("body"))
(test-parse-function-lambda-expression (lambda () "docstring" "body")
:expected-docstring "docstring"
:expected-body '("body"))
(test-parse-function-lambda-expression (lambda () "docstring" (declare) "body")
:expected-docstring "docstring"
:expected-declare '(declare)
:expected-body '("body"))
(test-parse-function-lambda-expression (lambda () (declare))
:expected-declare '(declare))
(test-parse-function-lambda-expression (lambda () "docstring" (declare))
:expected-docstring "docstring"
:expected-declare '(declare)))

(define-test doctest ()
;; Test the documentation for the library itself
Expand Down

0 comments on commit 0f8c229

Please sign in to comment.