From 966670afafd851b743ff7781ee2ab6de7b1c65ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Endsj=C3=B8?= Date: Mon, 8 Apr 2024 13:29:19 +0200 Subject: [PATCH] Fix typo in test-file --- src/doctest.lisp | 3 +- test-file.tst | 103 --------------------------------------------- tests/doctest.lisp | 13 ++++++ 3 files changed, 14 insertions(+), 105 deletions(-) delete mode 100644 test-file.tst diff --git a/src/doctest.lisp b/src/doctest.lisp index 2a85a99..0d6b16c 100644 --- a/src/doctest.lisp +++ b/src/doctest.lisp @@ -313,8 +313,7 @@ See also the documentation string for test." (multiple-value-bind (tests-failed tests-passed) - (with-open-file (docstring filename :direction :input) - (test-docstrting docstring :output output :test test)) + (test-docstring (uiop:read-file-string filename) :output output :test test) (print-results filename 'file output tests-failed tests-passed))) (defun test-package (package &key (output t) (test #'equalp)) diff --git a/test-file.tst b/test-file.tst deleted file mode 100644 index 04df6ed..0000000 --- a/test-file.tst +++ /dev/null @@ -1,103 +0,0 @@ - Test extracts and tests code snippets embedded in the documentation string - of . It returns the number of tests failed and passed and prints a - description to . - - In order to have a code snippet evaluated as a doctest it must be preceded by - two '>' characters followed by whitespace. That combination will cause the - next form to be read and evaluated, and the next or the two next forms after - that to be read (but not evaluated). - - Here is the simplest possible example: - - >> 1 ; NOTE! You can use comments to clarify! - 1 - - If you expect more than one value you should wrap it in a multiple-value- - list to create one form. - - >> (multiple-value-list (values 1 2)) - (1 2) - - Newlines and other whitespace (including comments) doesn't particularly - matter. We could just as well have written >> (multiple-value-list (values 1 - 2)) (1 2) instead. - - If you test a thing that doesn't have a documentation string, test will - return NIL. - - >> (defun sqr (x) - (* x x)) - SQR - >> (doctest:test #'sqr) - NIL - - If you need to test that a function signals a condition for certain inputs - you can use the name of the condition as the expected return value. - - >> (sqr 'x) - TYPE-ERROR - - If we add a documentation string for sqr with a doctest, we can verify that - tests can fail as well. - - >> (defun sqr (x) - "Returns squared. - - This test will fail: - >> (sqr 3) 3" - (* x x)) - SQR - - Testing sqr with test should now return 1 failed and 0 passed. - - >> (multiple-value-list (doctest:test #'sqr)) - (1 0) - - If you need to test the output of a function you can add an expected output - form (written as -> ) *between* the function call and the - return value. Expected output must be one form so you should either use a - string or wrap it in '|' characters. - - >> (defun sqr (x) - "Prints * = to standard output and returns NIL. - - This test will pass, - - >> (sqr 2) - -> |2 * 2 = 4| - NIL - - as will this, because it ignores the output. - - >> (sqr 2) - NIL - - Perhaps surprisingly, this will pass as well, - - >> (sqr 2) -> |2*2=4| NIL - - the reason it passes even though it doesn't exactly match the - actual output is because the comparison is done after all - whitespace characters are removed. - - This test will fail because expected output doesn't match the - actual output. - - >> (sqr 2) - -> |Blah blah blah| - NIL" - (format t "~A * ~A = ~A" x x (* x x))) - SQR - - Testing sqr with test should now return 1 failed and 2 passed. It should - also inform us that: - - (SQR 2) printed "2 * 2 = 4", expected "Blah blah blah". - Results for SQR (FUNCTION): 1 of 4 failed. - - NOTE! Whitespace is ignored when output is compared. - - >> (multiple-value-list (doctest:test #'sqr :output T)) - -> |[4] (SQR 2) printed "2 * 2 = 4", expected "Blah blah blah". - Results for SQR (FUNCTION): 1 of 4 failed.| - (1 3) \ No newline at end of file diff --git a/tests/doctest.lisp b/tests/doctest.lisp index 608b929..58a786e 100644 --- a/tests/doctest.lisp +++ b/tests/doctest.lisp @@ -101,3 +101,16 @@ (defmacro test-macro () :macro) (setf (documentation 'test-macro 'function) ">> (sijo-doctest/tests::test-macro) :macro") (assert-doctest (values 0 1) 'test-macro))) + +(define-test doctest-file () + (multiple-value-bind (file-failed file-passed) + (uiop:with-temporary-file (:pathname test-file) + (unwind-protect (progn + (uiop:delete-file-if-exists test-file) + (with-open-file (stream test-file :direction :output :external-format :utf-8) + (write-string (documentation #'doctest:test 'function) stream)) + (doctest:test test-file)) + (uiop:delete-file-if-exists test-file))) + (multiple-value-bind (doctest-failed doctest-passed) (doctest:test #'doctest:test) + (assert-eql doctest-failed file-failed) + (assert-eql doctest-passed file-passed))))