Skip to content

Commit

Permalink
[doc] Update examples to match other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Aug 27, 2024
1 parent 906236c commit 0a3b169
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ See for intro and usage:
;; {:ns truss-examples,
;; :line 9,
;; :column 11,
;; :file "examples/truss_examples.cljc"}}
;; :file "examples.cljc"}}
```

That's everything most users will need to know, but see the [documentation](#documentation) below for more!
Expand Down
41 changes: 8 additions & 33 deletions examples/truss_examples.cljc → examples.cljc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(ns truss-examples
{:author "Peter Taoussanis (@ptaoussanis)"}
(ns examples
"Some basic Truss usage examples."
(:require [taoensso.truss :as truss :refer [have have! have?]]))

;;;; First API example

(comment
(defn square [n]
(let [n (have integer? n)]
(* n n)))
Expand All @@ -23,13 +22,10 @@
;; {:ns truss-examples,
;; :line 9,
;; :column 11,
;; :file "examples/truss_examples.cljc"}}
)
;; :file "examples.cljc"}}

;;;; Inline assertions and bindings

(comment

;; You can add an assertion inline
(println (have string? "foo"))

Expand All @@ -48,7 +44,7 @@
;; {:ns truss-examples,
;; :line 41,
;; :column 1,
;; :file "examples/truss_examples.cljc"}}
;; :file "examples.cljc"}}

;; Truss also automatically traps and handles exceptions
(have string? (/ 1 0)) ; =>
Expand All @@ -66,7 +62,7 @@
;; {:ns truss-examples,
;; :line 54,
;; :column 1,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :err
;; #error
;; {:cause "Divide by zero"
Expand All @@ -76,11 +72,9 @@
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}]
;; :trace
;; [<...>]}}
)

;;;; Destructured bindings

(comment
;; You can assert against multipe args at once
(let [[x y z] (have string? "foo" "bar" "baz")]
(str x y z)) ; => "foobarbaz"
Expand All @@ -97,12 +91,10 @@
;; {:ns truss-examples,
;; :line 89,
;; :column 15,
;; :file "examples/truss_examples.cljc"}}
)
;; :file "examples.cljc"}}

;;;; Attaching debug data

(comment
(defn my-handler [ring-req x y]
(let [[x y] (have integer? x y :data {:ring-req ring-req})]
(* x y)))
Expand All @@ -117,13 +109,11 @@
;; {:ns truss-examples,
;; :line 107,
;; :column 15,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :data {:dynamic nil, :arg {:ring-req {:foo :bar}}}}
)

;;;; Attaching dynamic debug data

(comment
(defn wrap-ring-dynamic-assertion-data
"Returns Ring handler wrapped so that assertion violation errors in handler
will include `(data-fn <ring-req>)` as debug data."
Expand Down Expand Up @@ -153,23 +143,17 @@
;; {:ns truss-examples,
;; :line 136,
;; :column 3,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :data {:dynamic {:ring-session {:user-name "Stu"}}, :arg nil}}
)

;;;; Assertions within data structures

(comment

;;; Compare
(have vector? [:a :b :c]) ; => [:a :b :c]
(have keyword? :in [:a :b :c]) ; => [:a :b :c]
)

;;;; Assertions within :pre/:post conditions

(comment

(defn square [n]
;; Note the use of `have?` instead of `have`
{:pre [(have? #(or (nil? %) (integer? %)) n)]
Expand All @@ -179,12 +163,9 @@

(square 5) ; => 25
(square nil) ; => 1
)

;;;; Special predicates

(comment

;; A predicate can be anything
(have #(and (integer? %) (odd? %) (> % 5)) 7) ; => 7

Expand Down Expand Up @@ -212,12 +193,9 @@

;; Non-nil keys
(have [:ks-nnil? #{:a :b}] {:a "A" :b nil :c "C"}) ; => Error
)

;;;; Writing custom validators

(comment

;; A custom predicate:
(defn pos-int? [x] (and (integer? x) (pos? x)))

Expand All @@ -233,15 +211,12 @@

(have-person {:name "Steve" :age 33}) ; => {:name "Steve", :age 33}
(have-person {:name "Alice" :age "33"}) ; => Error
)

;;;; Assertions without elision

(comment
(defn get-restricted-resource [ring-session]
;; This is an important security check so we'll use `have!` here instead of
;; `have` to make sure the check is never elided (skipped):
(have! string? (:auth-token ring-session))

"return-restricted-resource-content")
)
12 changes: 6 additions & 6 deletions wiki/1-Getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ What varies is the return value, and whether elision is possible.

# Examples

> All examples are from [`/examples/truss_examples.cljc`](../blob/master/examples/truss_examples.cljc)
> All examples are from [`/examples.cljc`](../blob/master/examples.cljc)
Truss's sweet spot is often in longer, complex code (difficult to show here). So these examples are mostly examples of **syntax**, not **use case**. In particular, they mostly focus on simple **argument type assertions** since those are the easiest to understand.

Expand Down Expand Up @@ -99,7 +99,7 @@ A Truss `(have <pred> <arg>)` form will either throw or return the given argumen
;; {:ns truss-examples,
;; :line 41,
;; :column 1,
;; :file "examples/truss_examples.cljc"}}
;; :file "examples.cljc"}}

;; Truss also automatically traps and handles exceptions
(have string? (/ 1 0)) ; =>
Expand All @@ -117,7 +117,7 @@ A Truss `(have <pred> <arg>)` form will either throw or return the given argumen
;; {:ns truss-examples,
;; :line 54,
;; :column 1,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :err
;; #error
;; {:cause "Divide by zero"
Expand Down Expand Up @@ -148,7 +148,7 @@ A Truss `(have <pred> <arg>)` form will either throw or return the given argumen
;; {:ns truss-examples,
;; :line 89,
;; :column 15,
;; :file "examples/truss_examples.cljc"}}
;; :file "examples.cljc"}}
```

## Attaching debug data
Expand All @@ -170,7 +170,7 @@ You can attach arbitrary debug data to be displayed on violations:
;; {:ns truss-examples,
;; :line 107,
;; :column 15,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :data {:dynamic nil, :arg {:ring-req {:foo :bar}}}}
```

Expand Down Expand Up @@ -208,7 +208,7 @@ And you can attach shared debug data at the `binding` level:
;; {:ns truss-examples,
;; :line 136,
;; :column 3,
;; :file "examples/truss_examples.cljc"},
;; :file "examples.cljc"},
;; :data {:dynamic {:ring-session {:user-name "Stu"}}, :arg nil}}
```

Expand Down

0 comments on commit 0a3b169

Please sign in to comment.