Skip to content

Commit 570079c

Browse files
authored
Update basilisp.test/are macro docstring to reflect the loss of line numbers (#643)
* Update basilisp.test/are macro docstring to reflect the loss of line numbers * Changelog
1 parent 20d849a commit 570079c

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Changed
1616
* PyTest is now an optional extra dependency, rather than a required dependency (#622)
1717
* Generated Python functions corresponding to nested functions are now prefixed with the containing function name, if one exists (#632)
18+
* `basilisp.test/are` docstring now indicates that line numbers may be suppressed on assertion failures created using `are` (#643)
1819

1920
### Fixed
2021
* Fixed a bug where `seq`ing co-recursive lazy sequences would cause a stack overflow (#632)

src/basilisp/test.lpy

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,24 @@
7474
:type :failure}))))
7575

7676
(defmacro is
77-
"Assert that expr is true. Must appear inside of a deftest form."
77+
"Assert that a test condition, `expr`, is true. `is` assertion failures are recorded
78+
and reported as test failures, causing the entire containing `deftest` to be marked
79+
as failed.
80+
81+
`expr` can take multiple forms:
82+
- `(is (= expected actual))` generates a basic assertion that `expected` and `actual`
83+
are equal by `=`; error messaging will reflect that the first element is the
84+
expected value and the second element is the actual value
85+
- `(is (thrown? ExceptionType expr))` generates a basica assertion that `expr` does
86+
generate an exception of the type `ExceptionType`
87+
- `(is expr)` is the most basic assertion type that just asserts that `expr` is truthy
88+
89+
`is` assertions must appear inside of a `deftest` form."
7890
([expr]
7991
`(is ~expr (str "Test failure: " (pr-str (quote ~expr)))))
8092
([expr msg]
81-
(let [line-num (:basilisp.lang.reader/line (meta &form))]
93+
(let [line-num (or (:basilisp.lang.reader/line (meta &form))
94+
(:basilisp.lang.reader/line (meta (first &form))))]
8295
`(try
8396
~(gen-assert expr msg line-num)
8497
(catch python/Exception e#
@@ -94,26 +107,59 @@
94107
:type :error}))))))
95108

96109
(defmacro are
97-
"Assert that expr is true. Must appear inside of a deftest form."
110+
"Generate assertions using the template expression `expr`. Template expressions
111+
should be defined in terms of the symbols in the argument vector `argv`.
112+
113+
Arguments will be partitioned into groups of as many arguments are in `argv`
114+
and applied to the template expression.
115+
116+
As an example:
117+
118+
(are [res x y] (is (= res (+ x y)))
119+
3 1 2
120+
4 2 2
121+
0 -1 1)
122+
123+
This would macroexpand to create a group of assertions like this:
124+
125+
(do
126+
(is (= 3 (+ 1 2)))
127+
(is (= 4 (+ 2 2)))
128+
(is (= 0 (+ -1 1))))
129+
130+
This may be convenient for generating large numbers of identically formed assertions
131+
with different arguments.
132+
133+
Note that assertions generated with `are` typically lose line numbers in test failure
134+
reports, due to the nature of the macro generation.
135+
136+
`are` assertions must appear inside of a `deftest` form."
98137
[argv expr & args]
99138
`(template/do-template ~argv (is ~expr) ~@args))
100139

101140
(defmacro testing
102-
"Wrapper for test cases to provide additional messaging and context
103-
around the test or group of tests contained inside. Must appear inside
104-
of a deftest form."
141+
"Wrapper for test cases to provide additional messaging and context around the test
142+
or group of tests contained inside. The value of `msg` will be shown in the report
143+
with any test failures that occur inside this block.
144+
145+
`testing` macros may be nested. Each nested block message will be appended to the
146+
message from the previous block.
147+
148+
`testing` forms must appear inside of a `deftest` form."
105149
[msg & body]
106150
`(binding [*test-section* (if *test-section*
107151
(str *test-section* " :: " ~msg)
108152
~msg)]
109153
~@body))
110154

111155
(defmacro deftest
112-
"Define a new test function. Assertions can be made with the is macro.
113-
Group tests with the testing macro.
156+
"Define a new test.
157+
158+
Assertions can be made with the `is` and `are` macros. Group tests with the `testing`
159+
macro.
114160

115-
Tests defined by deftest will be run by default by the PyTest test
116-
runner using Basilisp's builtin PyTest hook."
161+
Tests defined by `deftest` will be run by default by the PyTest test runner using
162+
Basilisp's builtin PyTest hook."
117163
[name-sym & body]
118164
(let [test-num (swap! current-test-number inc)
119165
test-name-sym (vary-meta name-sym

src/basilisp/testrunner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ def _failure_msg(self, details: lmap.PersistentMap) -> str:
213213

214214
test_section = details.val_at(_TEST_SECTION_KW)
215215
line = details.val_at(_LINE_KW)
216+
line_msg = Maybe(line).map(lambda l: f":{l}").or_else_get("")
216217
section_msg = Maybe(test_section).map(lambda s: f" {s} :: ").or_else_get("")
217218

218219
return "\n".join(
219220
[
220-
f"FAIL in ({self.name}) ({self._filename}:{line})",
221+
f"FAIL in ({self.name}) ({self._filename}{line_msg})",
221222
f" {section_msg}{msg}",
222223
"",
223224
f" expected: {lrepr(expected)}",

0 commit comments

Comments
 (0)