Remove uses of let
from various tests
This commit is contained in:
parent
99638ba2df
commit
d72abb39f1
@ -15,20 +15,20 @@
|
||||
walk-form)))
|
||||
|
||||
(defn test-walk []
|
||||
(let [acc '()]
|
||||
(assert (= (walk (partial collector acc) identity walk-form)
|
||||
[None None]))
|
||||
(assert (= acc walk-form)))
|
||||
(let [acc []]
|
||||
(assert (= (walk identity (partial collector acc) walk-form)
|
||||
None))
|
||||
(assert (= acc [walk-form]))))
|
||||
(setv acc '())
|
||||
(assert (= (walk (partial collector acc) identity walk-form)
|
||||
[None None]))
|
||||
(assert (= acc walk-form))
|
||||
(setv acc [])
|
||||
(assert (= (walk identity (partial collector acc) walk-form)
|
||||
None))
|
||||
(assert (= acc [walk-form])))
|
||||
|
||||
(defn test-walk-iterators []
|
||||
(let [acc []]
|
||||
(assert (= (walk (fn [x] (* 2 x)) (fn [x] x)
|
||||
(drop 1 [1 [2 [3 [4]]]]))
|
||||
[[2 [3 [4]] 2 [3 [4]]]]))))
|
||||
(setv acc [])
|
||||
(assert (= (walk (fn [x] (* 2 x)) (fn [x] x)
|
||||
(drop 1 [1 [2 [3 [4]]]]))
|
||||
[[2 [3 [4]] 2 [3 [4]]]])))
|
||||
|
||||
(defn test-macroexpand-all []
|
||||
(assert (= (macroexpand-all '(with [a 1 b 2 c 3] (for [d c] foo)))
|
||||
|
@ -561,11 +561,11 @@
|
||||
(setv res (list (take-nth 3 [1 2 3 None 5 6])))
|
||||
(assert-equal res [1 None])
|
||||
;; using 0 should raise ValueError
|
||||
(let [passed False]
|
||||
(try
|
||||
(setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
|
||||
(except [ValueError] (setv passed True)))
|
||||
(assert passed)))
|
||||
(setv passed False)
|
||||
(try
|
||||
(setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
|
||||
(except [ValueError] (setv passed True)))
|
||||
(assert passed))
|
||||
|
||||
(defn test-take-while []
|
||||
"NATIVE: testing the take-while function"
|
||||
|
@ -36,9 +36,9 @@
|
||||
(+ self.x value))])
|
||||
(assert (= B.x 42))
|
||||
(assert (= (.y (B) 5) 47))
|
||||
(let [b (B)]
|
||||
(setv B.x 0)
|
||||
(assert (= (.y b 1) 1))))
|
||||
(setv b (B))
|
||||
(setv B.x 0)
|
||||
(assert (= (.y b 1) 1)))
|
||||
|
||||
|
||||
(defn test-defclass-dynamic-inheritance []
|
||||
|
@ -59,7 +59,7 @@
|
||||
[3 6 9])
|
||||
(assert-equal (list (ap-map (* it 3) []))
|
||||
[])
|
||||
(assert-equal (let [v 1 f 1] (list (ap-map (it v f) [(fn [a b] (+ a b))])))
|
||||
(assert-equal (do (setv v 1 f 1) (list (ap-map (it v f) [(fn [a b] (+ a b))])))
|
||||
[2]))
|
||||
|
||||
(defn test-ap-map-when []
|
||||
@ -83,9 +83,9 @@
|
||||
|
||||
(defn test-ap-dotimes []
|
||||
"NATIVE: testing anaphoric dotimes"
|
||||
(assert-equal (let [n []] (ap-dotimes 3 (.append n 3)) n)
|
||||
(assert-equal (do (setv n []) (ap-dotimes 3 (.append n 3)) n)
|
||||
[3 3 3])
|
||||
(assert-equal (let [n []] (ap-dotimes 3 (.append n it)) n)
|
||||
(assert-equal (do (setv n []) (ap-dotimes 3 (.append n it)) n)
|
||||
[0 1 2]))
|
||||
|
||||
(defn test-ap-first []
|
||||
|
@ -81,75 +81,75 @@
|
||||
|
||||
(defn test-augassign-add []
|
||||
"NATIVE: test augassign add"
|
||||
(let [x 1]
|
||||
(+= x 41)
|
||||
(assert (= x 42))))
|
||||
(setv x 1)
|
||||
(+= x 41)
|
||||
(assert (= x 42)))
|
||||
|
||||
(defn test-augassign-sub []
|
||||
"NATIVE: test augassign sub"
|
||||
(let [x 1]
|
||||
(-= x 41)
|
||||
(assert (= x -40))))
|
||||
(setv x 1)
|
||||
(-= x 41)
|
||||
(assert (= x -40)))
|
||||
|
||||
(defn test-augassign-mult []
|
||||
"NATIVE: test augassign mult"
|
||||
(let [x 1]
|
||||
(*= x 41)
|
||||
(assert (= x 41))))
|
||||
(setv x 1)
|
||||
(*= x 41)
|
||||
(assert (= x 41)))
|
||||
|
||||
(defn test-augassign-div []
|
||||
"NATIVE: test augassign div"
|
||||
(let [x 42]
|
||||
(/= x 2)
|
||||
(assert (= x 21))))
|
||||
(setv x 42)
|
||||
(/= x 2)
|
||||
(assert (= x 21)))
|
||||
|
||||
(defn test-augassign-floordiv []
|
||||
"NATIVE: test augassign floordiv"
|
||||
(let [x 42]
|
||||
(//= x 2)
|
||||
(assert (= x 21))))
|
||||
(setv x 42)
|
||||
(//= x 2)
|
||||
(assert (= x 21)))
|
||||
|
||||
(defn test-augassign-mod []
|
||||
"NATIVE: test augassign mod"
|
||||
(let [x 42]
|
||||
(%= x 2)
|
||||
(assert (= x 0))))
|
||||
(setv x 42)
|
||||
(%= x 2)
|
||||
(assert (= x 0)))
|
||||
|
||||
(defn test-augassign-pow []
|
||||
"NATIVE: test augassign pow"
|
||||
(let [x 2]
|
||||
(**= x 3)
|
||||
(assert (= x 8))))
|
||||
(setv x 2)
|
||||
(**= x 3)
|
||||
(assert (= x 8)))
|
||||
|
||||
(defn test-augassign-lshift []
|
||||
"NATIVE: test augassign lshift"
|
||||
(let [x 2]
|
||||
(<<= x 2)
|
||||
(assert (= x 8))))
|
||||
(setv x 2)
|
||||
(<<= x 2)
|
||||
(assert (= x 8)))
|
||||
|
||||
(defn test-augassign-rshift []
|
||||
"NATIVE: test augassign rshift"
|
||||
(let [x 8]
|
||||
(>>= x 1)
|
||||
(assert (= x 4))))
|
||||
(setv x 8)
|
||||
(>>= x 1)
|
||||
(assert (= x 4)))
|
||||
|
||||
(defn test-augassign-bitand []
|
||||
"NATIVE: test augassign bitand"
|
||||
(let [x 8]
|
||||
(&= x 1)
|
||||
(assert (= x 0))))
|
||||
(setv x 8)
|
||||
(&= x 1)
|
||||
(assert (= x 0)))
|
||||
|
||||
(defn test-augassign-bitor []
|
||||
"NATIVE: test augassign bitand"
|
||||
(let [x 0]
|
||||
(|= x 2)
|
||||
(assert (= x 2))))
|
||||
(setv x 0)
|
||||
(|= x 2)
|
||||
(assert (= x 2)))
|
||||
|
||||
(defn test-augassign-bitxor []
|
||||
"NATIVE: test augassign bitand"
|
||||
(let [x 1]
|
||||
(^= x 1)
|
||||
(assert (= x 0))))
|
||||
(setv x 1)
|
||||
(^= x 1)
|
||||
(assert (= x 0)))
|
||||
|
||||
(defn overflow-int-to-long []
|
||||
"NATIVE: test if int does not raise an overflow exception"
|
||||
@ -159,19 +159,19 @@
|
||||
(defclass HyTestMatrix [list]
|
||||
[--matmul--
|
||||
(fn [self other]
|
||||
(let [n (len self)
|
||||
(setv n (len self)
|
||||
m (len (. other [0]))
|
||||
result []]
|
||||
(for [i (range m)]
|
||||
(let [result-row []]
|
||||
(for [j (range n)]
|
||||
(let [dot-product 0]
|
||||
(for [k (range (len (. self [0])))]
|
||||
(+= dot-product (* (. self [i] [k])
|
||||
(. other [k] [j]))))
|
||||
(.append result-row dot-product)))
|
||||
(.append result result-row)))
|
||||
result))])
|
||||
result [])
|
||||
(for [i (range m)]
|
||||
(setv result-row [])
|
||||
(for [j (range n)]
|
||||
(setv dot-product 0)
|
||||
(for [k (range (len (. self [0])))]
|
||||
(+= dot-product (* (. self [i] [k])
|
||||
(. other [k] [j]))))
|
||||
(.append result-row dot-product))
|
||||
(.append result result-row))
|
||||
result)])
|
||||
|
||||
(def first-test-matrix (HyTestMatrix [[1 2 3]
|
||||
[4 5 6]
|
||||
@ -191,15 +191,16 @@
|
||||
(assert (= (@ first-test-matrix second-test-matrix)
|
||||
product-of-test-matrices))
|
||||
;; Python <= 3.4
|
||||
(let [matmul-attempt (try (@ first-test-matrix second-test-matrix)
|
||||
(except [e [Exception]] e))]
|
||||
(do
|
||||
(setv matmul-attempt (try (@ first-test-matrix second-test-matrix)
|
||||
(except [e [Exception]] e)))
|
||||
(assert (isinstance matmul-attempt NameError)))))
|
||||
|
||||
(defn test-augassign-matmul []
|
||||
"NATIVE: test augmented-assignment matrix multiplication"
|
||||
(let [matrix first-test-matrix
|
||||
(setv matrix first-test-matrix
|
||||
matmul-attempt (try (@= matrix second-test-matrix)
|
||||
(except [e [Exception]] e))]
|
||||
(if PY35
|
||||
(assert (= product-of-test-matrices matrix))
|
||||
(assert (isinstance matmul-attempt NameError)))))
|
||||
(except [e [Exception]] e)))
|
||||
(if PY35
|
||||
(assert (= product-of-test-matrices matrix))
|
||||
(assert (isinstance matmul-attempt NameError))))
|
||||
|
@ -132,11 +132,12 @@
|
||||
(import [astor.codegen [to_source]])
|
||||
(import [hy.importer [import_buffer_to_ast]])
|
||||
(setv macro1 "(defmacro nif [expr pos zero neg]
|
||||
(let [g (gensym)]
|
||||
`(let [~g ~expr]
|
||||
(cond [(pos? ~g) ~pos]
|
||||
[(zero? ~g) ~zero]
|
||||
[(neg? ~g) ~neg]))))
|
||||
(setv g (gensym))
|
||||
`(do
|
||||
(setv ~g ~expr)
|
||||
(cond [(pos? ~g) ~pos]
|
||||
[(zero? ~g) ~zero]
|
||||
[(neg? ~g) ~neg])))
|
||||
|
||||
(print (nif (inc -1) 1 0 -1))
|
||||
")
|
||||
@ -158,7 +159,8 @@
|
||||
(import [hy.importer [import_buffer_to_ast]])
|
||||
(setv macro1 "(defmacro nif [expr pos zero neg]
|
||||
(with-gensyms [a]
|
||||
`(let [~a ~expr]
|
||||
`(do
|
||||
(setv ~a ~expr)
|
||||
(cond [(pos? ~a) ~pos]
|
||||
[(zero? ~a) ~zero]
|
||||
[(neg? ~a) ~neg]))))
|
||||
@ -180,7 +182,8 @@
|
||||
(import [astor.codegen [to_source]])
|
||||
(import [hy.importer [import_buffer_to_ast]])
|
||||
(setv macro1 "(defmacro/g! nif [expr pos zero neg]
|
||||
`(let [~g!res ~expr]
|
||||
`(do
|
||||
(setv ~g!res ~expr)
|
||||
(cond [(pos? ~g!res) ~pos]
|
||||
[(zero? ~g!res) ~zero]
|
||||
[(neg? ~g!res) ~neg])))
|
||||
@ -208,7 +211,8 @@
|
||||
(import [astor.codegen [to_source]])
|
||||
(import [hy.importer [import_buffer_to_ast]])
|
||||
(setv macro1 "(defmacro! nif [expr pos zero neg]
|
||||
`(let [~g!res ~expr]
|
||||
`(do
|
||||
(setv ~g!res ~expr)
|
||||
(cond [(pos? ~g!res) ~pos]
|
||||
[(zero? ~g!res) ~zero]
|
||||
[(neg? ~g!res) ~neg])))
|
||||
|
@ -15,24 +15,23 @@
|
||||
(defn test-kwonly []
|
||||
"NATIVE: test keyword-only arguments"
|
||||
;; keyword-only with default works
|
||||
(let [kwonly-foo-default-false (fn [&kwonly [foo False]] foo)]
|
||||
(assert (= (apply kwonly-foo-default-false) False))
|
||||
(assert (= (apply kwonly-foo-default-false [] {"foo" True}) True)))
|
||||
(defn kwonly-foo-default-false [&kwonly [foo False]] foo)
|
||||
(assert (= (apply kwonly-foo-default-false) False))
|
||||
(assert (= (apply kwonly-foo-default-false [] {"foo" True}) True))
|
||||
;; keyword-only without default ...
|
||||
(let [kwonly-foo-no-default (fn [&kwonly foo] foo)
|
||||
attempt-to-omit-default (try
|
||||
(kwonly-foo-no-default)
|
||||
(except [e [Exception]] e))]
|
||||
;; works
|
||||
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
|
||||
;; raises TypeError with appropriate message if not supplied
|
||||
(assert (isinstance attempt-to-omit-default TypeError))
|
||||
(assert (in "missing 1 required keyword-only argument: 'foo'"
|
||||
(. attempt-to-omit-default args [0]))))
|
||||
(defn kwonly-foo-no-default [&kwonly foo] foo)
|
||||
(setv attempt-to-omit-default (try
|
||||
(kwonly-foo-no-default)
|
||||
(except [e [Exception]] e)))
|
||||
;; works
|
||||
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
|
||||
;; raises TypeError with appropriate message if not supplied
|
||||
(assert (isinstance attempt-to-omit-default TypeError))
|
||||
(assert (in "missing 1 required keyword-only argument: 'foo'"
|
||||
(. attempt-to-omit-default args [0])))
|
||||
;; keyword-only with other arg types works
|
||||
(let [function-of-various-args
|
||||
(fn [a b &rest args &kwonly foo &kwargs kwargs]
|
||||
(, a b args foo kwargs))]
|
||||
(assert (= (apply function-of-various-args
|
||||
[1 2 3 4] {"foo" 5 "bar" 6 "quux" 7})
|
||||
(, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7})))))
|
||||
(defn function-of-various-args [a b &rest args &kwonly foo &kwargs kwargs]
|
||||
(, a b args foo kwargs))
|
||||
(assert (= (apply function-of-various-args
|
||||
[1 2 3 4] {"foo" 5 "bar" 6 "quux" 7})
|
||||
(, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7}))))
|
||||
|
@ -1,51 +1,51 @@
|
||||
(defn test-shadow-addition []
|
||||
"NATIVE: test shadow addition"
|
||||
(let [x +]
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1 2 3 4) 10))
|
||||
(assert (= (x 1 2 3 4 5) 15))
|
||||
; with strings
|
||||
(assert (= (x "a" "b" "c")
|
||||
"abc"))
|
||||
; with lists
|
||||
(assert (= (x ["a"] ["b"] ["c"])
|
||||
["a" "b" "c"]))))
|
||||
(setv x +)
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1 2 3 4) 10))
|
||||
(assert (= (x 1 2 3 4 5) 15))
|
||||
; with strings
|
||||
(assert (= (x "a" "b" "c")
|
||||
"abc"))
|
||||
; with lists
|
||||
(assert (= (x ["a"] ["b"] ["c"])
|
||||
["a" "b" "c"])))
|
||||
|
||||
|
||||
(defn test-shadow-subtraction []
|
||||
"NATIVE: test shadow subtraction"
|
||||
(let [x -]
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) -1))
|
||||
(assert (= (x 2 1) 1))
|
||||
(assert (= (x 2 1 1) 0))))
|
||||
(setv x -)
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) -1))
|
||||
(assert (= (x 2 1) 1))
|
||||
(assert (= (x 2 1 1) 0)))
|
||||
|
||||
|
||||
(defn test-shadow-multiplication []
|
||||
"NATIVE: test shadow multiplication"
|
||||
(let [x *]
|
||||
(assert (= (x) 1))
|
||||
(assert (= (x 3) 3))
|
||||
(assert (= (x 3 3) 9))))
|
||||
(setv x *)
|
||||
(assert (= (x) 1))
|
||||
(assert (= (x 3) 3))
|
||||
(assert (= (x 3 3) 9)))
|
||||
|
||||
|
||||
(defn test-shadow-division []
|
||||
"NATIVE: test shadow division"
|
||||
(let [x /]
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) 1))
|
||||
(assert (= (x 8 2) 4))
|
||||
(assert (= (x 8 2 2) 2))
|
||||
(assert (= (x 8 2 2 2) 1))))
|
||||
(setv x /)
|
||||
(assert (try
|
||||
(x)
|
||||
(except [TypeError] True)
|
||||
(else (raise AssertionError))))
|
||||
(assert (= (x 1) 1))
|
||||
(assert (= (x 8 2) 4))
|
||||
(assert (= (x 8 2 2) 2))
|
||||
(assert (= (x 8 2 2 2) 1)))
|
||||
|
||||
|
||||
(defn test-shadow-compare []
|
||||
@ -70,24 +70,24 @@
|
||||
[2 2]]]
|
||||
(assert (= (apply x args) (not (apply y args))))))
|
||||
|
||||
(let [s-lt <
|
||||
(setv s-lt <
|
||||
s-gt >
|
||||
s-le <=
|
||||
s-ge >=
|
||||
s-eq =
|
||||
s-ne !=]
|
||||
(assert (apply s-lt [1 2 3]))
|
||||
(assert (not (apply s-lt [3 2 1])))
|
||||
(assert (apply s-gt [3 2 1]))
|
||||
(assert (not (apply s-gt [1 2 3])))
|
||||
(assert (apply s-le [1 1 2 2 3 3]))
|
||||
(assert (not (apply s-le [1 1 2 2 1 1])))
|
||||
(assert (apply s-ge [3 3 2 2 1 1]))
|
||||
(assert (not (apply s-ge [3 3 2 2 3 3])))
|
||||
(assert (apply s-eq [1 1 1 1 1]))
|
||||
(assert (not (apply s-eq [1 1 2 1 1])))
|
||||
(assert (apply s-ne [1 2 3 4 5]))
|
||||
(assert (not (apply s-ne [1 1 2 3 4]))))
|
||||
s-ne !=)
|
||||
(assert (apply s-lt [1 2 3]))
|
||||
(assert (not (apply s-lt [3 2 1])))
|
||||
(assert (apply s-gt [3 2 1]))
|
||||
(assert (not (apply s-gt [1 2 3])))
|
||||
(assert (apply s-le [1 1 2 2 3 3]))
|
||||
(assert (not (apply s-le [1 1 2 2 1 1])))
|
||||
(assert (apply s-ge [3 3 2 2 1 1]))
|
||||
(assert (not (apply s-ge [3 3 2 2 3 3])))
|
||||
(assert (apply s-eq [1 1 1 1 1]))
|
||||
(assert (not (apply s-eq [1 1 2 1 1])))
|
||||
(assert (apply s-ne [1 2 3 4 5]))
|
||||
(assert (not (apply s-ne [1 1 2 3 4])))
|
||||
|
||||
; Make sure chained comparisons use `and`, not `&`.
|
||||
; https://github.com/hylang/hy/issues/1191
|
||||
|
Loading…
Reference in New Issue
Block a user