Skip to content

Commit

Permalink
[nop] Update example output
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Jul 31, 2023
1 parent 1a38c5b commit a345bc2
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 86 deletions.
113 changes: 70 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ Truss uses a simple `(predicate arg)` pattern that should **immediately feel fam

(square 5) ; => 25
(square nil) ; =>
;; Invariant failed at taoensso.truss.examples|9: (integer? nil)
;; {:dt #inst "2022-11-16T19:28:18.587-00:00",
;; :pred integer?,
;; Invariant failed at truss-examples[9,11]: (integer? n)
;; {:dt #inst "2023-07-31T09:56:10.295-00:00",
;; :pred clojure.core/integer?,
;; :arg {:form n, :value nil, :type nil},
;; :loc {:ns truss-examples, :line 9, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 9,
;; :column 11,
;; :file "examples/truss_examples.cljc"}}
```

#### And that's it, you know the Truss API.
Expand Down Expand Up @@ -165,32 +169,43 @@ A Truss `(have <pred> <arg>)` form will either throw or return the given argumen

;; Anything that fails the predicate will throw an error
(have string? 42) ; =>
;; Invariant failed at taoensso.truss.examples|37: (string? 42)
;; {:dt #inst "2022-11-16T19:29:49.004-00:00",
;; :pred string?,
;; Invariant failed at truss-examples[41,1]: (string? 42)
;; {:dt #inst "2023-07-31T09:58:07.927-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 37, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 41,
;; :column 1,
;; :file "examples/truss_examples.cljc"}}

;; Truss also automatically traps and handles exceptions
(have string? (/ 1 0)) ; =>
;; Invariant failed at taoensso.truss.examples|46:
;; (string? truss/undefined-arg)
;; Invariant failed at truss-examples[54,1]: (string? (/ 1 0))
;;
;; Error evaluating arg: Divide by zero
;; {:dt #inst "2022-11-16T19:30:15.945-00:00",
;; :pred string?,
;; :arg {:form (/ 1 0), :value truss/undefined-arg, :type truss/undefined-arg},
;; :loc {:ns truss-examples, :line 46, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; :err #error {
;; :cause "Divide by zero"
;; :via
;; [{:type java.lang.ArithmeticException
;; :message "Divide by zero"
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}]
;; :trace
;; [...]}
;; Error evaluating arg: Divide by zero
;; {:dt #inst "2023-07-31T09:59:06.149-00:00",
;; :pred clojure.core/string?,
;; :arg
;; {:form (/ 1 0),
;; :value truss/undefined-arg,
;; :type truss/undefined-arg},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 54,
;; :column 1,
;; :file "examples/truss_examples.cljc"},
;; :err
;; #error
;; {:cause "Divide by zero"
;; :via
;; [{:type java.lang.ArithmeticException
;; :message "Divide by zero"
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}]
;; :trace
;; [<...>]}}
```

### Destructured bindings
Expand All @@ -203,12 +218,16 @@ A Truss `(have <pred> <arg>)` form will either throw or return the given argumen
;; This won't compromise error message clarity
(let [[x y z] (have string? "foo" 42 "baz")]
(str x y z)) ; =>
;; Invariant failed at taoensso.truss.examples|74: (string? 42)
;; {:dt #inst "2022-11-16T19:32:07.397-00:00",
;; :pred string?,
;; Invariant failed at truss-examples[89,15]: (string? 42)
;; {:dt #inst "2023-07-31T10:01:00.991-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 74, :column 15, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 89,
;; :column 15,
;; :file "examples/truss_examples.cljc"}}
```

### Attaching debug data
Expand All @@ -221,12 +240,16 @@ You can attach arbitrary debug data to be displayed on violations:
(* x y)))

(my-handler {:foo :bar} 5 nil) ; =>
;; Invariant failed at taoensso.truss.examples|88: (integer? nil)
;; {:dt #inst "2022-11-16T19:33:39.842-00:00",
;; :pred integer?,
;; :arg {:form y, :value nil, :type nil},
;; :loc {:ns truss-examples, :line 88, :column 15, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; Invariant failed at truss-examples[107,15]: (integer? y)
;; {:dt #inst "2023-07-31T10:02:03.415-00:00",
;; :pred clojure.core/integer?,
;; :arg {:form y, :value nil, :type nil},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 107,
;; :column 15,
;; :file "examples/truss_examples.cljc"},
;; :data {:dynamic nil, :arg {:ring-req {:foo :bar}}}}
```

Expand Down Expand Up @@ -255,12 +278,16 @@ And you can attach shared debug data at the `binding` level:

(wrapped-ring-handler
{:method :get :uri "/" :session {:user-name "Stu"}}) ; =>
;; Invariant failed at taoensso.truss.examples|113: (string? 42)
;; {:dt #inst "2022-11-16T19:34:14.006-00:00",
;; :pred string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 113, :column 3, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; Invariant failed at truss-examples[136,3]: (string? 42)
;; {:dt #inst "2023-07-31T10:02:41.459-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 136,
;; :column 3,
;; :file "examples/truss_examples.cljc"},
;; :data {:dynamic {:ring-session {:user-name "Stu"}}, :arg nil}}
```

Expand Down
113 changes: 70 additions & 43 deletions examples/truss_examples.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

(square 5) ; => 25
(square nil) ; =>
;; Invariant failed at truss-examples|9: (integer? nil)
;; {:dt #inst "2022-11-16T19:28:18.587-00:00",
;; :pred integer?,
;; Invariant failed at truss-examples[9,11]: (integer? n)
;; {:dt #inst "2023-07-31T09:56:10.295-00:00",
;; :pred clojure.core/integer?,
;; :arg {:form n, :value nil, :type nil},
;; :loc {:ns truss-examples, :line 9, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 9,
;; :column 11,
;; :file "examples/truss_examples.cljc"}}
)

;;;; Inline assertions and bindings
Expand All @@ -35,32 +39,43 @@

;; Anything that fails the predicate will throw an error
(have string? 42) ; =>
;; Invariant failed at truss-examples|37: (string? 42)
;; {:dt #inst "2022-11-16T19:29:49.004-00:00",
;; :pred string?,
;; Invariant failed at truss-examples[41,1]: (string? 42)
;; {:dt #inst "2023-07-31T09:58:07.927-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 37, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 41,
;; :column 1,
;; :file "examples/truss_examples.cljc"}}

;; Truss also automatically traps and handles exceptions
(have string? (/ 1 0)) ; =>
;; Invariant failed at truss-examples|46:
;; (string? truss/undefined-arg)
;; Invariant failed at truss-examples[54,1]: (string? (/ 1 0))
;;
;; Error evaluating arg: Divide by zero
;; {:dt #inst "2022-11-16T19:30:15.945-00:00",
;; :pred string?,
;; :arg {:form (/ 1 0), :value truss/undefined-arg, :type truss/undefined-arg},
;; :loc {:ns truss-examples, :line 46, :column 1, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; :err #error {
;; :cause "Divide by zero"
;; :via
;; [{:type java.lang.ArithmeticException
;; :message "Divide by zero"
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}]
;; :trace
;; [...]}
;; Error evaluating arg: Divide by zero
;; {:dt #inst "2023-07-31T09:59:06.149-00:00",
;; :pred clojure.core/string?,
;; :arg
;; {:form (/ 1 0),
;; :value truss/undefined-arg,
;; :type truss/undefined-arg},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 54,
;; :column 1,
;; :file "examples/truss_examples.cljc"},
;; :err
;; #error
;; {:cause "Divide by zero"
;; :via
;; [{:type java.lang.ArithmeticException
;; :message "Divide by zero"
;; :at [clojure.lang.Numbers divide "Numbers.java" 190]}]
;; :trace
;; [<...>]}}
)

;;;; Destructured bindings
Expand All @@ -73,12 +88,16 @@
;; This won't compromise error message clarity
(let [[x y z] (have string? "foo" 42 "baz")]
(str x y z)) ; =>
;; Invariant failed at truss-examples|74: (string? 42)
;; {:dt #inst "2022-11-16T19:32:07.397-00:00",
;; :pred string?,
;; Invariant failed at truss-examples[89,15]: (string? 42)
;; {:dt #inst "2023-07-31T10:01:00.991-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 74, :column 15, :file "..."},
;; :env {:elidable? true, :*assert* true}}
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 89,
;; :column 15,
;; :file "examples/truss_examples.cljc"}}
)

;;;; Attaching debug data
Expand All @@ -89,12 +108,16 @@
(* x y)))

(my-handler {:foo :bar} 5 nil) ; =>
;; Invariant failed at truss-examples|88: (integer? nil)
;; {:dt #inst "2022-11-16T19:33:39.842-00:00",
;; :pred integer?,
;; :arg {:form y, :value nil, :type nil},
;; :loc {:ns truss-examples, :line 88, :column 15, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; Invariant failed at truss-examples[107,15]: (integer? y)
;; {:dt #inst "2023-07-31T10:02:03.415-00:00",
;; :pred clojure.core/integer?,
;; :arg {:form y, :value nil, :type nil},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 107,
;; :column 15,
;; :file "examples/truss_examples.cljc"},
;; :data {:dynamic nil, :arg {:ring-req {:foo :bar}}}}
)

Expand All @@ -121,12 +144,16 @@

(wrapped-ring-handler
{:method :get :uri "/" :session {:user-name "Stu"}}) ; =>
;; Invariant failed at truss-examples|113: (string? 42)
;; {:dt #inst "2022-11-16T19:34:14.006-00:00",
;; :pred string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :loc {:ns truss-examples, :line 113, :column 3, :file "..."},
;; :env {:elidable? true, :*assert* true},
;; Invariant failed at truss-examples[136,3]: (string? 42)
;; {:dt #inst "2023-07-31T10:02:41.459-00:00",
;; :pred clojure.core/string?,
;; :arg {:form 42, :value 42, :type java.lang.Long},
;; :env {:elidable? true, :*assert* true},
;; :loc
;; {:ns truss-examples,
;; :line 136,
;; :column 3,
;; :file "examples/truss_examples.cljc"},
;; :data {:dynamic {:ring-session {:user-name "Stu"}}, :arg nil}}
)

Expand Down

0 comments on commit a345bc2

Please sign in to comment.