https://github.com/strojure/assertie
Macros for runtime assertion in Clojure(Script).
https://github.com/strojure/assertie
assertions clojure clojurescript
Last synced: 10 months ago
JSON representation
Macros for runtime assertion in Clojure(Script).
- Host: GitHub
- URL: https://github.com/strojure/assertie
- Owner: strojure
- License: unlicense
- Created: 2022-09-06T15:04:42.000Z (over 3 years ago)
- Default Branch: default
- Last Pushed: 2023-03-08T18:17:27.000Z (almost 3 years ago)
- Last Synced: 2025-01-16T20:57:10.049Z (12 months ago)
- Topics: assertions, clojure, clojurescript
- Language: Clojure
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# assertie
Macros for runtime assertion in Clojure(Script).
[](https://clojars.org/com.github.strojure/assertie)
[](https://cljdoc.org/d/com.github.strojure/assertie)
[](https://clojurescript.org/)
[](https://github.com/strojure/assertie/actions/workflows/tests.yml)
## Motivation
Runtime asserts with better reporting about failed asserting value.
## Features
- Ex-data with info about failed asserting value.
- Predicate based assertions.
- Return `true` instead of `nil` to be used directly in :pre/:post conditions.
- Argument order is designed to be used with `doto` macro.
- `Exception`-inherited exceptions instead of `Throwable`-inherited ones.
## API
### assert-pred
Main macro to assert value/expression by predicate function.
```clojure
(ns readme.api-01-assert-pred
(:require [strojure.assertie.core :as a :include-macros true]))
(a/assert-pred "1" string?)
;=> true
(a/assert-pred (inc 0) string?)
;Assert failed: (assert-pred (inc 0) string?)
; #:asserting{:value 1, :type java.lang.Long}
(a/assert-pred (inc 0) string? "Expect string")
;Expect string - Assert failed: (assert-pred (inc 0) string?)
; #:asserting{:value 1, :type java.lang.Long}
(a/assert-pred [] (a/not-thrown first))
;=> true
(a/assert-pred 1 (a/not-thrown first))
;Assert failed: (assert-pred 1 (a/not-thrown first))
; #:asserting{:value 1, :type java.lang.Long}
;Don't know how to create ISeq from: java.lang.Long
```
### assert-expr
Secondary macro similar to `clojure.core/assert`.
```clojure
(ns readme.api-02-assert-expr
(:require [strojure.assertie.core :as a :include-macros true]))
(a/assert-expr (= 1 (inc 0)))
;=> true
(a/assert-expr (= 1 2))
;Assert failed: (assert-expr (= 1 2))
; #:asserting{:value false}
(a/assert-expr (= 1 2) "Expect 1=2")
;Expect 1=2 - Assert failed: (assert-expr (= 1 2))
; #:asserting{:value false}
```
### assert-spec
Custom macro to assert against spec. This macro is an example of implementation
of new macros on top of `assertie`.
```clojure
(ns readme.api-03-assert-spec
(:require [clojure.spec.alpha :as s]
[strojure.assertie.spec-alpha :as a :include-macros true]))
(s/def ::string string?)
(a/assert-spec "1" ::string "Message")
;=> true
(a/assert-spec 1 ::string "Message")
;Message - Assert failed: (assert-spec 1 :project.readme.example.spec-01-assert-spec/string)
; {}
;Spec assertion failed
;1 - failed: string?
; #:clojure.spec.alpha{:problems [{:path [], :pred clojure.core/string?, :val 1, :via [], :in []}], :spec :project.readme.example.spec-01-assert-spec/string, :value 1, :failure :assertion-failed}
```