Truss provides a set of macros for efficient runtime assertions that throw detailed exceptions when a predicate fails. The most common macro is have, which returns the argument on success or throws a truss/ex-info on failure.
When an assertion fails, the resulting exception includes a timestamp, the failed predicate, the tested argument, the source location, and the current Truss context (*ctx*).
Key features:
- Return value: Returns the argument(s) on success.
- Multiple arguments: Can return multiple arguments as a vector.
- Default predicate: Omitting the predicate defaults to
some? (non-nil check). - Custom data: Use
:data to attach arbitrary information to the thrown exception. - Collection assertions: Use
:in to assert over elements within a collection. - Special predicates: Supports composite predicates like
:or and size/membership checks like :ks<=, :el, and :n<=.
;; Basic usage
(have string? "foo") ; => "foo"
(have string? 5) ; => Throws detailed exception
;; Multiple args
(have string? "foo" "bar") ; => ["foo" "bar"]
;; Default to some? (non-nil)
(have "foo") ; => "foo"
(have nil) ; => Throws detailed exception
;; Add custom data to exception
(have string? "foo" :data {:user-id 101}) ; => "foo"
;; Assert inside collections
(have string? :in #{"foo" "bar"}) ; => #{"foo" "bar"}
;; Special predicates
(have [:or nil? string?] "foo") ; => "foo"
(have [:ks<= #{:a :b}] my-map) ; => Map, or throws
(have [:el #{:a :b :c}] my-arg) ; => Arg, or throws
(have [:n<= 10] my-coll) ; => Coll, or throws