From d0ff7b3ea8f31888cd3957a81a8ba9f18d3fc6db Mon Sep 17 00:00:00 2001 From: Thomas Mulvaney Date: Sat, 19 Mar 2016 10:07:06 +0000 Subject: [PATCH] check exception type in tests also make more consistent exception messages --- pixie/stdlib.pxi | 10 ++++---- pixie/test.pxi | 4 ++-- pixie/vm/array.py | 5 ++-- pixie/vm/code.py | 2 +- pixie/vm/persistent_vector.py | 18 +++++++++------ pixie/vm/string.py | 5 ++-- tests/pixie/tests/test-arrays.pxi | 16 ++++++------- tests/pixie/tests/test-fns.pxi | 26 ++++++++++----------- tests/pixie/tests/test-ith.pxi | 30 ++++++++++++------------ tests/pixie/tests/test-readeval.pxi | 20 ++++++++-------- tests/pixie/tests/test-stdlib.pxi | 36 ++++++++++++++--------------- 11 files changed, 89 insertions(+), 83 deletions(-) diff --git a/pixie/stdlib.pxi b/pixie/stdlib.pxi index b1a39041..1fe4bf4a 100644 --- a/pixie/stdlib.pxi +++ b/pixie/stdlib.pxi @@ -2004,13 +2004,13 @@ For more information, see http://clojure.org/special_forms#binding-forms"} (when (empty? s) (throw [:pixie.stdlib/OutOfRangeException - "Index out of Range"])) + "Index out of range"])) (if (and (pos? n) s) (recur (next s) (dec n)) (if (zero? n) (first s) (throw [:pixie.stdlib/OutOfRangeException - "Index out of Range"]))))) + "Index out of range"]))))) (extend -nth-not-found ISeq (fn [s n not-found] (if (and (pos? n) s) (recur (next s) (dec n) not-found) @@ -2042,7 +2042,7 @@ For more information, see http://clojure.org/special_forms#binding-forms"} (-nth [self idx] (if (and (>= idx 0) (< idx n)) x - (throw [:pixie.stdlib/OutOfRangeException "Index out of Range"]))) + (throw [:pixie.stdlib/OutOfRangeException "Index out of range"]))) (-nth-not-found [self idx not-found] (if (and (>= idx 0) (< idx n)) x @@ -2093,12 +2093,12 @@ For more information, see http://clojure.org/special_forms#binding-forms"} IIndexed (-nth [self idx] (when (or (= start stop 0) (neg? idx)) - (throw [:pixie.stdlib/OutOfRangeException "Index out of Range"])) + (throw [:pixie.stdlib/OutOfRangeException "Index out of range"])) (let [cmp (if (< start stop) < >) val (+ start (* idx step))] (if (cmp val stop) val - (throw [:pixie.stdlib/OutOfRangeException "Index out of Range"])))) + (throw [:pixie.stdlib/OutOfRangeException "Index out of range"])))) (-nth-not-found [self idx not-found] (let [cmp (if (< start stop) < >) val (+ start (* idx step))] diff --git a/pixie/test.pxi b/pixie/test.pxi index 2f622977..f7263a3e 100644 --- a/pixie/test.pxi +++ b/pixie/test.pxi @@ -69,10 +69,10 @@ exn#)) ([klass body] `(let [exn# (assert-throws? ~body)] - (assert (= (type exn#) ~klass) + (assert (= (ex-data exn#) ~klass) (str "Expected " (pr-str (quote ~body)) " to throw exception of class " (pr-str ~klass) - " but got " (pr-str (type exn#)))) + " but got " (pr-str (ex-data exn#)))) exn#)) ([klass msg body] `(let [exn# (assert-throws? ~klass ~body)] diff --git a/pixie/vm/array.py b/pixie/vm/array.py index 4ca97813..89a9cddb 100644 --- a/pixie/vm/array.py +++ b/pixie/vm/array.py @@ -1,6 +1,6 @@ import pixie.vm.rt as rt import pixie.vm.object as object -from pixie.vm.object import affirm +from pixie.vm.object import affirm, runtime_error from pixie.vm.code import extend, as_var from pixie.vm.numbers import Integer from pixie.vm.primitives import nil @@ -48,7 +48,8 @@ def _nth(self, idx): if ival < len(self.list()): return self.list()[ival] else: - affirm(False, u"Index out of Range") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") @extend(proto._nth_not_found, Array) def _nth_not_found(self, idx, not_found): diff --git a/pixie/vm/code.py b/pixie/vm/code.py index 5a54ea25..d0df72e3 100644 --- a/pixie/vm/code.py +++ b/pixie/vm/code.py @@ -234,7 +234,7 @@ def invoke(self, args): runtime_error(u"Invalid number of arguments " + unicode(str(len(args))) + u" for function '" + unicode(str(self._name)) + u"'. Expected " + unicode(str(self.get_arity())), - u":pixie.stdlib/InvalidArityException") + u"pixie.stdlib/InvalidArityException") def invoke_with(self, args, this_fn): try: diff --git a/pixie/vm/persistent_vector.py b/pixie/vm/persistent_vector.py index 2740caf5..196382f0 100644 --- a/pixie/vm/persistent_vector.py +++ b/pixie/vm/persistent_vector.py @@ -1,6 +1,6 @@ py_object = object import pixie.vm.object as object -from pixie.vm.object import affirm +from pixie.vm.object import affirm, runtime_error from pixie.vm.primitives import nil, true, false from pixie.vm.numbers import Integer import pixie.vm.stdlib as proto @@ -56,7 +56,8 @@ def array_for(self, i): assert isinstance(node, Node) return node._array - affirm(False, u"Index out of Range") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") def nth(self, i, not_found=None): if 0 <= i < self._cnt: @@ -64,7 +65,8 @@ def nth(self, i, not_found=None): return node[i & 0x01f] if not_found is None: - affirm(False, u"Index out of Range") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") else: return not_found @@ -176,8 +178,8 @@ def assoc_at(self, idx, val): if idx == self._cnt: return self.conj(val) else: - object.runtime_error(u"index out of range", - u"pixie.stdlib/OutOfRangeException") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") def do_assoc(lvl, node, idx, val): @@ -319,7 +321,8 @@ def array_for(self, i): level -= 5 return node._array - affirm(False, u"Index out of Range") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") def editable_array_for(self, i): if i >= 0 and i < self._cnt: @@ -333,7 +336,8 @@ def editable_array_for(self, i): level -= 5 return node._array - affirm(False, u"Index out of bounds") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") def nth(self, i, not_found=nil): self.ensure_editable() diff --git a/pixie/vm/string.py b/pixie/vm/string.py index 17327fa9..74b8fd85 100644 --- a/pixie/vm/string.py +++ b/pixie/vm/string.py @@ -1,5 +1,5 @@ import pixie.vm.rt as rt -from pixie.vm.object import Object, Type, affirm +from pixie.vm.object import Object, Type, affirm, runtime_error from pixie.vm.code import extend, as_var, wrap_fn from pixie.vm.primitives import nil, true, false from pixie.vm.numbers import Integer, _add @@ -52,7 +52,8 @@ def _nth(self, idx): i = idx.int_val() if 0 <= i < len(self._str): return Character(ord(self._str[i])) - affirm(False, u"Index out of Range") + runtime_error(u"Index out of range", + u"pixie.stdlib/OutOfRangeException") @extend(proto._nth_not_found, String) def _nth_not_found(self, idx, not_found): diff --git a/tests/pixie/tests/test-arrays.pxi b/tests/pixie/tests/test-arrays.pxi index ecd651cb..08996022 100644 --- a/tests/pixie/tests/test-arrays.pxi +++ b/tests/pixie/tests/test-arrays.pxi @@ -11,7 +11,7 @@ (t/deftest test-alength (let [a (make-array 10)] (t/assert= (alength a) 10) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (alength [])))) (t/deftest test-aget-and-aset @@ -25,10 +25,10 @@ (dotimes [i 10] (t/assert= (aget a i) i)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (aget a 1.0)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (aset a 1.0 :foo)))) (t/deftest test-aconcat @@ -44,10 +44,10 @@ (dotimes [i 20] (t/assert= (aget a3 i) i))) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (t/aconcat a1 [])) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (t/aconcat a1 '())))) (t/deftest test-aslice @@ -62,13 +62,13 @@ (foreach [i (range 0 3)] (t/assert= (aget a2 i) (+ i 7)))) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (aslice [1 2 3 4] 0 2)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (aslice '() 0 2)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException (aslice a 1.0 2)))) diff --git a/tests/pixie/tests/test-fns.pxi b/tests/pixie/tests/test-fns.pxi index 23915d0c..9a166b24 100644 --- a/tests/pixie/tests/test-fns.pxi +++ b/tests/pixie/tests/test-fns.pxi @@ -1,6 +1,8 @@ (ns pixie.test.test-fns (require pixie.test :as t)) +(def arity-exception :pixie.stdlib/InvalidArityException) + (t/deftest test-fn-literals (t/assert= (#(+ 3 4)) 7) (t/assert= (#(+ 3 %) 4) 7) @@ -23,38 +25,36 @@ arity-1-or-3 (fn arity-1-or-3 ([a]) ([a b c])) arity-0-or-1-or-3-or-more (fn arity-0-or-1-or-3-or-more ([]) ([a]) ([a b c & more]))] - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 1 for function 'arity-0'. Expected 0" (arity-0 :foo)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 2 for function 'arity-0'. Expected 0" (arity-0 :foo :bar)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 0 for function 'arity-1'. Expected 1" (arity-1)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 2 for function 'arity-1'. Expected 1" (arity-1 :foo :bar)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 0 for function 'arity-2'. Expected 2" (arity-2)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Invalid number of arguments 1 for function 'arity-2'. Expected 2" (arity-2 :foo)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Wrong number of arguments 2 for function 'arity-0-or-1'. Expected 0 or 1" (arity-0-or-1 :foo :bar)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Wrong number of arguments 3 for function 'arity-0-or-1'. Expected 0 or 1" (arity-0-or-1 :foo :bar :baz)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Wrong number of arguments 2 for function 'arity-1-or-3'. Expected 1 or 3" (arity-1-or-3 :foo :bar)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Wrong number of arguments 0 for function 'arity-1-or-3'. Expected 1 or 3" (arity-1-or-3)) - (t/assert-throws? RuntimeException + (t/assert-throws? arity-exception "Wrong number of arguments 2 for function 'arity-0-or-1-or-3-or-more'. Expected 0, 1 or 3+" (arity-0-or-1-or-3-or-more :foo :bar)))) - -(t/deftest test-code-arities) diff --git a/tests/pixie/tests/test-ith.pxi b/tests/pixie/tests/test-ith.pxi index 0591165c..f152bb31 100644 --- a/tests/pixie/tests/test-ith.pxi +++ b/tests/pixie/tests/test-ith.pxi @@ -15,26 +15,26 @@ (t/assert= (ith nil -1) nil)) (t/deftest test-ith-empty-always-oob - (t/assert= "Index out of Range" (try (ith [] 0) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith [] 1) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith [] -1) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith '() 0) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith '() 1) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith '() -1) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith (range 0 0) 0) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith (range 0 0) 1) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith (range 0 0) -1) (catch e (ex-msg e))))) + (t/assert= "Index out of range" (try (ith [] 0) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith [] 1) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith [] -1) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith '() 0) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith '() 1) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith '() -1) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith (range 0 0) 0) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith (range 0 0) 1) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith (range 0 0) -1) (catch e (ex-msg e))))) (t/deftest test-ith-out-of-bounds (let [v [1 2 3 4 5] l '(1 2 3 4 5) r (range 1 6)] - (t/assert= "Index out of Range" (try (ith v 5) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith l 5) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith r 5) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith v -6) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith l -6) (catch e (ex-msg e)))) - (t/assert= "Index out of Range" (try (ith r -6) (catch e (ex-msg e)))))) + (t/assert= "Index out of range" (try (ith v 5) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith l 5) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith r 5) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith v -6) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith l -6) (catch e (ex-msg e)))) + (t/assert= "Index out of range" (try (ith r -6) (catch e (ex-msg e)))))) (t/deftest test-ith-doseq (let [v [1 2 3 4 5] diff --git a/tests/pixie/tests/test-readeval.pxi b/tests/pixie/tests/test-readeval.pxi index 636dca23..d3915f6f 100644 --- a/tests/pixie/tests/test-readeval.pxi +++ b/tests/pixie/tests/test-readeval.pxi @@ -18,42 +18,42 @@ (t/assert= (read-string "(foo (bar (baz)))") '(foo (bar (baz))))) (t/deftest test-list-unclosed-list-fail - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched list open '('" (read-string "(")) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched list open '('" (read-string "((foo bar)"))) (t/deftest test-vector-unclosed-list-fail - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched vector open '['" (read-string "[")) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched vector open '['" (read-string "[[foo bar]"))) (t/deftest test-map-unclosed-list-fail - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched map open '{'" (read-string "{")) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched map open '{'" (read-string "{foo {a b}"))) (t/deftest test-set-unclosed-list-fail - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched set open '#{'" (read-string "#{")) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched set open '#{'" (read-string "#{foo #{a}"))) (t/deftest test-string-unclosed-fail - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched string quote '\"'" (read-string "\"")) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Unmatched string quote '\"'" (read-string "\"foo"))) diff --git a/tests/pixie/tests/test-stdlib.pxi b/tests/pixie/tests/test-stdlib.pxi index 6e1984e8..8bdd2c3c 100644 --- a/tests/pixie/tests/test-stdlib.pxi +++ b/tests/pixie/tests/test-stdlib.pxi @@ -117,31 +117,31 @@ ;; throws error for bad index (try (nth [1 2 3] 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth '(1 2 3) 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth (make-array 3) 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth (range 4) 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth "hithere" 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth [] 0) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth '() 0) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth (repeat 99 nil) 99) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) (try (nth (range 0 0) 0) - (catch ex (t/assert= (ex-msg ex) "Index out of Range"))) + (catch ex (t/assert= (ex-msg ex) "Index out of range"))) ;; if not-found is specified, uses that for out of range (t/assert= (nth [1 2 3] 99 :default) :default) @@ -670,7 +670,7 @@ {1 2, 2 2, 3 2, 4 1})) (t/deftest test-condp - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/MissingClauseException "No matching clause!" (condp :dont-call-me :dont-use-me)) (let [f (fn [x] @@ -683,7 +683,7 @@ (t/assert= (f 9) :whatever))) (t/deftest test-case - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/MissingClauseException "No matching clause!" (case :no-matter-what)) (let [f (fn [x] @@ -705,10 +705,10 @@ (t/assert= (instance? [Symbol Keyword] 'a) true) (t/assert= (instance? [Symbol Keyword] 42) false) (t/assert= (instance? [] :x) false) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "c must be a type" (instance? :not-a-type 123)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "c must be a type" (instance? [Keyword :also-not-a-type] 123))) @@ -725,10 +725,10 @@ (t/assert= (satisfies? [] :xyz) true) (t/assert= (satisfies? [ILookup IIndexed] [1 2]) true) (t/assert= (satisfies? [ILookup IIndexed] {1 2}) false) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "proto must be a Protocol" (satisfies? :not-a-proto 123)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "proto must be a Protocol" (satisfies? [IIndexed :also-not-a-proto] [1 2])) (defprotocol IFoo (foo [this])) @@ -763,14 +763,14 @@ (let [f (fn ([a] {:pre [(even? a)] :post [(= % 6)]} (/ a 2)) ([a b] {:pre [(= (+ 1 a) b)] :post [(odd? %)]} (+ a b)))] (t/assert= 6 (f 12)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Assert failed: (even? a)" (f 13)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Assert failed: (= % 6)" (f 14)) (t/assert= 15 (f 7 8)) - (t/assert-throws? RuntimeException + (t/assert-throws? :pixie.stdlib/AssertionException "Assert failed: (= (+ 1 a) b)" (f 1 1))))