Skip to content

Commit d0ff7b3

Browse files
check exception type in tests
also make more consistent exception messages
1 parent dd754fe commit d0ff7b3

File tree

11 files changed

+89
-83
lines changed

11 files changed

+89
-83
lines changed

pixie/stdlib.pxi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,13 +2004,13 @@ For more information, see http://clojure.org/special_forms#binding-forms"}
20042004
(when (empty? s)
20052005
(throw
20062006
[:pixie.stdlib/OutOfRangeException
2007-
"Index out of Range"]))
2007+
"Index out of range"]))
20082008
(if (and (pos? n) s)
20092009
(recur (next s) (dec n))
20102010
(if (zero? n)
20112011
(first s)
20122012
(throw [:pixie.stdlib/OutOfRangeException
2013-
"Index out of Range"])))))
2013+
"Index out of range"])))))
20142014
(extend -nth-not-found ISeq (fn [s n not-found]
20152015
(if (and (pos? n) s)
20162016
(recur (next s) (dec n) not-found)
@@ -2042,7 +2042,7 @@ For more information, see http://clojure.org/special_forms#binding-forms"}
20422042
(-nth [self idx]
20432043
(if (and (>= idx 0) (< idx n))
20442044
x
2045-
(throw [:pixie.stdlib/OutOfRangeException "Index out of Range"])))
2045+
(throw [:pixie.stdlib/OutOfRangeException "Index out of range"])))
20462046
(-nth-not-found [self idx not-found]
20472047
(if (and (>= idx 0) (< idx n))
20482048
x
@@ -2093,12 +2093,12 @@ For more information, see http://clojure.org/special_forms#binding-forms"}
20932093
IIndexed
20942094
(-nth [self idx]
20952095
(when (or (= start stop 0) (neg? idx))
2096-
(throw [:pixie.stdlib/OutOfRangeException "Index out of Range"]))
2096+
(throw [:pixie.stdlib/OutOfRangeException "Index out of range"]))
20972097
(let [cmp (if (< start stop) < >)
20982098
val (+ start (* idx step))]
20992099
(if (cmp val stop)
21002100
val
2101-
(throw [:pixie.stdlib/OutOfRangeException "Index out of Range"]))))
2101+
(throw [:pixie.stdlib/OutOfRangeException "Index out of range"]))))
21022102
(-nth-not-found [self idx not-found]
21032103
(let [cmp (if (< start stop) < >)
21042104
val (+ start (* idx step))]

pixie/test.pxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@
6969
exn#))
7070
([klass body]
7171
`(let [exn# (assert-throws? ~body)]
72-
(assert (= (type exn#) ~klass)
72+
(assert (= (ex-data exn#) ~klass)
7373
(str "Expected " (pr-str (quote ~body))
7474
" to throw exception of class " (pr-str ~klass)
75-
" but got " (pr-str (type exn#))))
75+
" but got " (pr-str (ex-data exn#))))
7676
exn#))
7777
([klass msg body]
7878
`(let [exn# (assert-throws? ~klass ~body)]

pixie/vm/array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pixie.vm.rt as rt
22
import pixie.vm.object as object
3-
from pixie.vm.object import affirm
3+
from pixie.vm.object import affirm, runtime_error
44
from pixie.vm.code import extend, as_var
55
from pixie.vm.numbers import Integer
66
from pixie.vm.primitives import nil
@@ -48,7 +48,8 @@ def _nth(self, idx):
4848
if ival < len(self.list()):
4949
return self.list()[ival]
5050
else:
51-
affirm(False, u"Index out of Range")
51+
runtime_error(u"Index out of range",
52+
u"pixie.stdlib/OutOfRangeException")
5253

5354
@extend(proto._nth_not_found, Array)
5455
def _nth_not_found(self, idx, not_found):

pixie/vm/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def invoke(self, args):
234234
runtime_error(u"Invalid number of arguments " + unicode(str(len(args)))
235235
+ u" for function '" + unicode(str(self._name)) + u"'. Expected "
236236
+ unicode(str(self.get_arity())),
237-
u":pixie.stdlib/InvalidArityException")
237+
u"pixie.stdlib/InvalidArityException")
238238

239239
def invoke_with(self, args, this_fn):
240240
try:

pixie/vm/persistent_vector.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
py_object = object
22
import pixie.vm.object as object
3-
from pixie.vm.object import affirm
3+
from pixie.vm.object import affirm, runtime_error
44
from pixie.vm.primitives import nil, true, false
55
from pixie.vm.numbers import Integer
66
import pixie.vm.stdlib as proto
@@ -56,15 +56,17 @@ def array_for(self, i):
5656
assert isinstance(node, Node)
5757
return node._array
5858

59-
affirm(False, u"Index out of Range")
59+
runtime_error(u"Index out of range",
60+
u"pixie.stdlib/OutOfRangeException")
6061

6162
def nth(self, i, not_found=None):
6263
if 0 <= i < self._cnt:
6364
node = self.array_for(r_uint(i))
6465
return node[i & 0x01f]
6566

6667
if not_found is None:
67-
affirm(False, u"Index out of Range")
68+
runtime_error(u"Index out of range",
69+
u"pixie.stdlib/OutOfRangeException")
6870
else:
6971
return not_found
7072

@@ -176,8 +178,8 @@ def assoc_at(self, idx, val):
176178
if idx == self._cnt:
177179
return self.conj(val)
178180
else:
179-
object.runtime_error(u"index out of range",
180-
u"pixie.stdlib/OutOfRangeException")
181+
runtime_error(u"Index out of range",
182+
u"pixie.stdlib/OutOfRangeException")
181183

182184

183185
def do_assoc(lvl, node, idx, val):
@@ -319,7 +321,8 @@ def array_for(self, i):
319321
level -= 5
320322
return node._array
321323

322-
affirm(False, u"Index out of Range")
324+
runtime_error(u"Index out of range",
325+
u"pixie.stdlib/OutOfRangeException")
323326

324327
def editable_array_for(self, i):
325328
if i >= 0 and i < self._cnt:
@@ -333,7 +336,8 @@ def editable_array_for(self, i):
333336
level -= 5
334337
return node._array
335338

336-
affirm(False, u"Index out of bounds")
339+
runtime_error(u"Index out of range",
340+
u"pixie.stdlib/OutOfRangeException")
337341

338342
def nth(self, i, not_found=nil):
339343
self.ensure_editable()

pixie/vm/string.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pixie.vm.rt as rt
2-
from pixie.vm.object import Object, Type, affirm
2+
from pixie.vm.object import Object, Type, affirm, runtime_error
33
from pixie.vm.code import extend, as_var, wrap_fn
44
from pixie.vm.primitives import nil, true, false
55
from pixie.vm.numbers import Integer, _add
@@ -52,7 +52,8 @@ def _nth(self, idx):
5252
i = idx.int_val()
5353
if 0 <= i < len(self._str):
5454
return Character(ord(self._str[i]))
55-
affirm(False, u"Index out of Range")
55+
runtime_error(u"Index out of range",
56+
u"pixie.stdlib/OutOfRangeException")
5657

5758
@extend(proto._nth_not_found, String)
5859
def _nth_not_found(self, idx, not_found):

tests/pixie/tests/test-arrays.pxi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(t/deftest test-alength
1212
(let [a (make-array 10)]
1313
(t/assert= (alength a) 10)
14-
(t/assert-throws? RuntimeException
14+
(t/assert-throws? :pixie.stdlib/AssertionException
1515
(alength []))))
1616

1717
(t/deftest test-aget-and-aset
@@ -25,10 +25,10 @@
2525
(dotimes [i 10]
2626
(t/assert= (aget a i) i))
2727

28-
(t/assert-throws? RuntimeException
28+
(t/assert-throws? :pixie.stdlib/AssertionException
2929
(aget a 1.0))
3030

31-
(t/assert-throws? RuntimeException
31+
(t/assert-throws? :pixie.stdlib/AssertionException
3232
(aset a 1.0 :foo))))
3333

3434
(t/deftest test-aconcat
@@ -44,10 +44,10 @@
4444
(dotimes [i 20]
4545
(t/assert= (aget a3 i) i)))
4646

47-
(t/assert-throws? RuntimeException
47+
(t/assert-throws? :pixie.stdlib/AssertionException
4848
(t/aconcat a1 []))
4949

50-
(t/assert-throws? RuntimeException
50+
(t/assert-throws? :pixie.stdlib/AssertionException
5151
(t/aconcat a1 '()))))
5252

5353
(t/deftest test-aslice
@@ -62,13 +62,13 @@
6262
(foreach [i (range 0 3)]
6363
(t/assert= (aget a2 i) (+ i 7))))
6464

65-
(t/assert-throws? RuntimeException
65+
(t/assert-throws? :pixie.stdlib/AssertionException
6666
(aslice [1 2 3 4] 0 2))
6767

68-
(t/assert-throws? RuntimeException
68+
(t/assert-throws? :pixie.stdlib/AssertionException
6969
(aslice '() 0 2))
7070

71-
(t/assert-throws? RuntimeException
71+
(t/assert-throws? :pixie.stdlib/AssertionException
7272
(aslice a 1.0 2))))
7373

7474

tests/pixie/tests/test-fns.pxi

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns pixie.test.test-fns
22
(require pixie.test :as t))
33

4+
(def arity-exception :pixie.stdlib/InvalidArityException)
5+
46
(t/deftest test-fn-literals
57
(t/assert= (#(+ 3 4)) 7)
68
(t/assert= (#(+ 3 %) 4) 7)
@@ -23,38 +25,36 @@
2325
arity-1-or-3 (fn arity-1-or-3 ([a]) ([a b c]))
2426
arity-0-or-1-or-3-or-more
2527
(fn arity-0-or-1-or-3-or-more ([]) ([a]) ([a b c & more]))]
26-
(t/assert-throws? RuntimeException
28+
(t/assert-throws? arity-exception
2729
"Invalid number of arguments 1 for function 'arity-0'. Expected 0"
2830
(arity-0 :foo))
29-
(t/assert-throws? RuntimeException
31+
(t/assert-throws? arity-exception
3032
"Invalid number of arguments 2 for function 'arity-0'. Expected 0"
3133
(arity-0 :foo :bar))
32-
(t/assert-throws? RuntimeException
34+
(t/assert-throws? arity-exception
3335
"Invalid number of arguments 0 for function 'arity-1'. Expected 1"
3436
(arity-1))
35-
(t/assert-throws? RuntimeException
37+
(t/assert-throws? arity-exception
3638
"Invalid number of arguments 2 for function 'arity-1'. Expected 1"
3739
(arity-1 :foo :bar))
38-
(t/assert-throws? RuntimeException
40+
(t/assert-throws? arity-exception
3941
"Invalid number of arguments 0 for function 'arity-2'. Expected 2"
4042
(arity-2))
41-
(t/assert-throws? RuntimeException
43+
(t/assert-throws? arity-exception
4244
"Invalid number of arguments 1 for function 'arity-2'. Expected 2"
4345
(arity-2 :foo))
44-
(t/assert-throws? RuntimeException
46+
(t/assert-throws? arity-exception
4547
"Wrong number of arguments 2 for function 'arity-0-or-1'. Expected 0 or 1"
4648
(arity-0-or-1 :foo :bar))
47-
(t/assert-throws? RuntimeException
49+
(t/assert-throws? arity-exception
4850
"Wrong number of arguments 3 for function 'arity-0-or-1'. Expected 0 or 1"
4951
(arity-0-or-1 :foo :bar :baz))
50-
(t/assert-throws? RuntimeException
52+
(t/assert-throws? arity-exception
5153
"Wrong number of arguments 2 for function 'arity-1-or-3'. Expected 1 or 3"
5254
(arity-1-or-3 :foo :bar))
53-
(t/assert-throws? RuntimeException
55+
(t/assert-throws? arity-exception
5456
"Wrong number of arguments 0 for function 'arity-1-or-3'. Expected 1 or 3"
5557
(arity-1-or-3))
56-
(t/assert-throws? RuntimeException
58+
(t/assert-throws? arity-exception
5759
"Wrong number of arguments 2 for function 'arity-0-or-1-or-3-or-more'. Expected 0, 1 or 3+"
5860
(arity-0-or-1-or-3-or-more :foo :bar))))
59-
60-
(t/deftest test-code-arities)

tests/pixie/tests/test-ith.pxi

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,26 @@
1515
(t/assert= (ith nil -1) nil))
1616

1717
(t/deftest test-ith-empty-always-oob
18-
(t/assert= "Index out of Range" (try (ith [] 0) (catch e (ex-msg e))))
19-
(t/assert= "Index out of Range" (try (ith [] 1) (catch e (ex-msg e))))
20-
(t/assert= "Index out of Range" (try (ith [] -1) (catch e (ex-msg e))))
21-
(t/assert= "Index out of Range" (try (ith '() 0) (catch e (ex-msg e))))
22-
(t/assert= "Index out of Range" (try (ith '() 1) (catch e (ex-msg e))))
23-
(t/assert= "Index out of Range" (try (ith '() -1) (catch e (ex-msg e))))
24-
(t/assert= "Index out of Range" (try (ith (range 0 0) 0) (catch e (ex-msg e))))
25-
(t/assert= "Index out of Range" (try (ith (range 0 0) 1) (catch e (ex-msg e))))
26-
(t/assert= "Index out of Range" (try (ith (range 0 0) -1) (catch e (ex-msg e)))))
18+
(t/assert= "Index out of range" (try (ith [] 0) (catch e (ex-msg e))))
19+
(t/assert= "Index out of range" (try (ith [] 1) (catch e (ex-msg e))))
20+
(t/assert= "Index out of range" (try (ith [] -1) (catch e (ex-msg e))))
21+
(t/assert= "Index out of range" (try (ith '() 0) (catch e (ex-msg e))))
22+
(t/assert= "Index out of range" (try (ith '() 1) (catch e (ex-msg e))))
23+
(t/assert= "Index out of range" (try (ith '() -1) (catch e (ex-msg e))))
24+
(t/assert= "Index out of range" (try (ith (range 0 0) 0) (catch e (ex-msg e))))
25+
(t/assert= "Index out of range" (try (ith (range 0 0) 1) (catch e (ex-msg e))))
26+
(t/assert= "Index out of range" (try (ith (range 0 0) -1) (catch e (ex-msg e)))))
2727

2828
(t/deftest test-ith-out-of-bounds
2929
(let [v [1 2 3 4 5]
3030
l '(1 2 3 4 5)
3131
r (range 1 6)]
32-
(t/assert= "Index out of Range" (try (ith v 5) (catch e (ex-msg e))))
33-
(t/assert= "Index out of Range" (try (ith l 5) (catch e (ex-msg e))))
34-
(t/assert= "Index out of Range" (try (ith r 5) (catch e (ex-msg e))))
35-
(t/assert= "Index out of Range" (try (ith v -6) (catch e (ex-msg e))))
36-
(t/assert= "Index out of Range" (try (ith l -6) (catch e (ex-msg e))))
37-
(t/assert= "Index out of Range" (try (ith r -6) (catch e (ex-msg e))))))
32+
(t/assert= "Index out of range" (try (ith v 5) (catch e (ex-msg e))))
33+
(t/assert= "Index out of range" (try (ith l 5) (catch e (ex-msg e))))
34+
(t/assert= "Index out of range" (try (ith r 5) (catch e (ex-msg e))))
35+
(t/assert= "Index out of range" (try (ith v -6) (catch e (ex-msg e))))
36+
(t/assert= "Index out of range" (try (ith l -6) (catch e (ex-msg e))))
37+
(t/assert= "Index out of range" (try (ith r -6) (catch e (ex-msg e))))))
3838

3939
(t/deftest test-ith-doseq
4040
(let [v [1 2 3 4 5]

tests/pixie/tests/test-readeval.pxi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@
1818
(t/assert= (read-string "(foo (bar (baz)))") '(foo (bar (baz)))))
1919

2020
(t/deftest test-list-unclosed-list-fail
21-
(t/assert-throws? RuntimeException
21+
(t/assert-throws? :pixie.stdlib/AssertionException
2222
"Unmatched list open '('"
2323
(read-string "("))
24-
(t/assert-throws? RuntimeException
24+
(t/assert-throws? :pixie.stdlib/AssertionException
2525
"Unmatched list open '('"
2626
(read-string "((foo bar)")))
2727

2828
(t/deftest test-vector-unclosed-list-fail
29-
(t/assert-throws? RuntimeException
29+
(t/assert-throws? :pixie.stdlib/AssertionException
3030
"Unmatched vector open '['"
3131
(read-string "["))
32-
(t/assert-throws? RuntimeException
32+
(t/assert-throws? :pixie.stdlib/AssertionException
3333
"Unmatched vector open '['"
3434
(read-string "[[foo bar]")))
3535

3636
(t/deftest test-map-unclosed-list-fail
37-
(t/assert-throws? RuntimeException
37+
(t/assert-throws? :pixie.stdlib/AssertionException
3838
"Unmatched map open '{'"
3939
(read-string "{"))
40-
(t/assert-throws? RuntimeException
40+
(t/assert-throws? :pixie.stdlib/AssertionException
4141
"Unmatched map open '{'"
4242
(read-string "{foo {a b}")))
4343

4444
(t/deftest test-set-unclosed-list-fail
45-
(t/assert-throws? RuntimeException
45+
(t/assert-throws? :pixie.stdlib/AssertionException
4646
"Unmatched set open '#{'"
4747
(read-string "#{"))
48-
(t/assert-throws? RuntimeException
48+
(t/assert-throws? :pixie.stdlib/AssertionException
4949
"Unmatched set open '#{'"
5050
(read-string "#{foo #{a}")))
5151

5252
(t/deftest test-string-unclosed-fail
53-
(t/assert-throws? RuntimeException
53+
(t/assert-throws? :pixie.stdlib/AssertionException
5454
"Unmatched string quote '\"'"
5555
(read-string "\""))
56-
(t/assert-throws? RuntimeException
56+
(t/assert-throws? :pixie.stdlib/AssertionException
5757
"Unmatched string quote '\"'"
5858
(read-string "\"foo")))
5959

0 commit comments

Comments
 (0)