Skip to content

Commit c4ec7d5

Browse files
committed
Add utility function to remove test(s) from a specific suite.
The scenario that showed the need to add this function: - add some experimental test cases, with inconvenient test keys (e.g. a string or an uninterned symbol) - fiveam will add the tests without signaling any errors (this would also need to be handled in separate commit; i.e., to reject attempts to define tests with bad test key. - run test suites as usual, and you'll have some problems: tests with string keys will result in method dispatch errors, and tests with uninterned symbol keys will get stuck in the tests hash table, without being able to modify or clean them up using REM-TEST. This added utility function, REM-TESTS, allows cleaning up tests such as these ones. Check its docstring for more details.
1 parent e43d6c8 commit c4ec7d5

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/test.lisp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@
2929
(deletef (%test-names *test*) key)
3030
(remhash key (%tests *test*)))
3131

32+
(defun rem-tests (key &key test-suite-key test-sym-name)
33+
"Remove test(s) identified by `key` from suite identified by `test-suite-key` (or from suite specified
34+
by `*suite*` if not provided or provided with NIL value. Equality test is EQL by default, and the
35+
`test-sym-name` keyword argument may be used to override it with string equality (case-insensitive). This
36+
is convenient in case the test to be removed was added with an uninterned symbol as identity (e.g. by
37+
mistake). Note that in the case of uninterned symbol, multiple entries could be present to the hash table
38+
with the same symbol name. This is since each symbol has a different address. This function would remove
39+
all such entries (hence, the s in the function name)."
40+
(labels ((key-string-equal-p (key-in key-iter)
41+
"Equality test for key, based on string value (case-insensitive)."
42+
(string-equal (string key-in) (string key-iter))))
43+
(let* ((test-suite (if test-suite-key
44+
(get-test test-suite-key)
45+
*suite*))
46+
(test-bundle (tests test-suite))
47+
(tests-map (%tests test-bundle))
48+
(result nil))
49+
(deletef (%test-names test-bundle) key :test (or (and test-sym-name #'key-string-equal-p) #'eql))
50+
(if test-sym-name
51+
(loop for k being the hash-keys of tests-map
52+
do (when (key-string-equal-p key k)
53+
(setf result (remhash k tests-map))))
54+
(setf result (remhash key tests-map)))
55+
result)))
56+
3257
(defun test-names ()
3358
(reverse (%test-names *test*)))
3459

0 commit comments

Comments
 (0)