Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
* Added support for the optional `attr-map?` on the `ns` macro (#1159)
* Added support for the optional pre- and post-conditions in `fn` forms (#1167)

### Fixed
* Fix a bug where `#` characters were not legal in keywords and symbols (#1149)
Expand Down
20 changes: 20 additions & 0 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -5940,6 +5940,25 @@
(let [args (first body)
arg-vec-meta (meta args)
body (rest body)
conditions (when (and (next body) (map? (first body)))
(first body))
body (if conditions
(rest body)
body)

conditions (or conditions arg-vec-meta)
pre-conds (:pre conditions)
post-conds (:post conditions)
body (if post-conds
[`(let [~'% (do ~@body)]
~@(map (fn* [c] `(assert ~c)) post-conds)
~'%)]
body)
body (if pre-conds
(concat
(map (fn* [c] `(assert ~c)) pre-conds)
body)
body)

arg-groups (split-with (partial not= '&) args)
args (first arg-groups)
Expand All @@ -5964,6 +5983,7 @@
(filter #(not= :symbol (:type %)))
(mapcat destructure-binding)
(concat rest-binding))

new-body (if (seq bindings)
[`(let* [~@bindings]
~@body)]
Expand Down
98 changes: 98 additions & 0 deletions tests/basilisp/test_core_macros.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,104 @@
(conj accum [a b c d e f v]))
[coll accum])))))))

(deftest fn-pre-and-post-condition-test
(testing "fn with map as body member does not count as condition map"
(let [f (fn [v]
{:pre [v]
:post [:nothin]})]
(is (= {:pre [1] :post [:nothin]} (f 1)))))

(testing "pre-condition only"
(testing "empty pre-condition vec"
(let [f (fn [x]
{:pre []}
(* x x))]
(is (= 1 (f 1)))
(is (= 100 (f 10)))
(is (= 0 (f 0)))
(is (= 25 (f -5)))))

(testing "single pre-condition"
(let [f (fn [x]
{:pre [(pos? x)]}
(* x x))]
(is (= 1 (f 1)))
(is (= 100 (f 10)))
(is (thrown? python/AssertionError
(f 0)))
(is (thrown? python/AssertionError
(f -5)))))

(testing "multiple pre-condition"
(let [f (fn [x]
{:pre [(pos? x) (even? x)]}
(* x x))]
(is (= 4 (f 2)))
(is (= 100 (f 10)))
(is (thrown? python/AssertionError
(f 1)))
(is (thrown? python/AssertionError
(f 0)))
(is (thrown? python/AssertionError
(f -5)))
(is (thrown? python/AssertionError
(f -6))))))

(testing "post-condition only"
(testing "empty post-condition vec"
(let [f (fn [x]
{:post []}
(* x x))]
(is (= 1 (f 1)))
(is (= 100 (f 10)))
(is (= 0 (f 0)))
(is (= 25 (f -5)))))

(testing "single post-condition"
(let [f (fn [x]
{:post [(> % 16)]}
(* x x))]
(is (= 25 (f 5)))
(is (= 100 (f 10)))
(is (thrown? python/AssertionError
(f 1)))
(is (thrown? python/AssertionError
(f -3)))))

(testing "multiple post-condition"
(let [f (fn [x]
{:post [(> % 16) (< % 225)]}
(* x x))]
(is (= 25 (f 5)))
(is (= 25 (f -5)))
(is (= 196 (f 14)))
(is (thrown? python/AssertionError
(f 1)))
(is (thrown? python/AssertionError
(f 0)))
(is (thrown? python/AssertionError
(f 15)))
(is (thrown? python/AssertionError
(f 100))))))

(testing "pre- and post-conditions"
(let [f (fn [x]
{:pre [(pos? x)]
:post [(> % 16) (< % 225)]}
(* x x))]
(is (= 25 (f 5)))
(is (= 196 (f 14)))
(is (thrown? python/AssertionError
(f 1)))
(is (thrown? python/AssertionError
(f 0)))
(is (thrown? python/AssertionError
(f 15)))
(is (thrown? python/AssertionError
(f 100)))
(is (thrown? python/AssertionError
(f -5))))))

(defmacro ^:private variadic-fn []
`(fn [& r#] r#))

Expand Down
Loading