Remove uses of let from various tests

This commit is contained in:
Kodi Arfer 2017-02-04 09:07:27 -08:00
parent 99638ba2df
commit d72abb39f1
8 changed files with 157 additions and 153 deletions

View File

@ -15,20 +15,20 @@
walk-form))) walk-form)))
(defn test-walk [] (defn test-walk []
(let [acc '()] (setv acc '())
(assert (= (walk (partial collector acc) identity walk-form) (assert (= (walk (partial collector acc) identity walk-form)
[None None])) [None None]))
(assert (= acc walk-form))) (assert (= acc walk-form))
(let [acc []] (setv acc [])
(assert (= (walk identity (partial collector acc) walk-form) (assert (= (walk identity (partial collector acc) walk-form)
None)) None))
(assert (= acc [walk-form])))) (assert (= acc [walk-form])))
(defn test-walk-iterators [] (defn test-walk-iterators []
(let [acc []] (setv acc [])
(assert (= (walk (fn [x] (* 2 x)) (fn [x] x) (assert (= (walk (fn [x] (* 2 x)) (fn [x] x)
(drop 1 [1 [2 [3 [4]]]])) (drop 1 [1 [2 [3 [4]]]]))
[[2 [3 [4]] 2 [3 [4]]]])))) [[2 [3 [4]] 2 [3 [4]]]])))
(defn test-macroexpand-all [] (defn test-macroexpand-all []
(assert (= (macroexpand-all '(with [a 1 b 2 c 3] (for [d c] foo))) (assert (= (macroexpand-all '(with [a 1 b 2 c 3] (for [d c] foo)))

View File

@ -561,11 +561,11 @@
(setv res (list (take-nth 3 [1 2 3 None 5 6]))) (setv res (list (take-nth 3 [1 2 3 None 5 6])))
(assert-equal res [1 None]) (assert-equal res [1 None])
;; using 0 should raise ValueError ;; using 0 should raise ValueError
(let [passed False] (setv passed False)
(try (try
(setv res (list (take-nth 0 [1 2 3 4 5 6 7]))) (setv res (list (take-nth 0 [1 2 3 4 5 6 7])))
(except [ValueError] (setv passed True))) (except [ValueError] (setv passed True)))
(assert passed))) (assert passed))
(defn test-take-while [] (defn test-take-while []
"NATIVE: testing the take-while function" "NATIVE: testing the take-while function"

View File

@ -36,9 +36,9 @@
(+ self.x value))]) (+ self.x value))])
(assert (= B.x 42)) (assert (= B.x 42))
(assert (= (.y (B) 5) 47)) (assert (= (.y (B) 5) 47))
(let [b (B)] (setv b (B))
(setv B.x 0) (setv B.x 0)
(assert (= (.y b 1) 1)))) (assert (= (.y b 1) 1)))
(defn test-defclass-dynamic-inheritance [] (defn test-defclass-dynamic-inheritance []

View File

@ -59,7 +59,7 @@
[3 6 9]) [3 6 9])
(assert-equal (list (ap-map (* it 3) [])) (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])) [2]))
(defn test-ap-map-when [] (defn test-ap-map-when []
@ -83,9 +83,9 @@
(defn test-ap-dotimes [] (defn test-ap-dotimes []
"NATIVE: testing anaphoric 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]) [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])) [0 1 2]))
(defn test-ap-first [] (defn test-ap-first []

View File

@ -81,75 +81,75 @@
(defn test-augassign-add [] (defn test-augassign-add []
"NATIVE: test augassign add" "NATIVE: test augassign add"
(let [x 1] (setv x 1)
(+= x 41) (+= x 41)
(assert (= x 42)))) (assert (= x 42)))
(defn test-augassign-sub [] (defn test-augassign-sub []
"NATIVE: test augassign sub" "NATIVE: test augassign sub"
(let [x 1] (setv x 1)
(-= x 41) (-= x 41)
(assert (= x -40)))) (assert (= x -40)))
(defn test-augassign-mult [] (defn test-augassign-mult []
"NATIVE: test augassign mult" "NATIVE: test augassign mult"
(let [x 1] (setv x 1)
(*= x 41) (*= x 41)
(assert (= x 41)))) (assert (= x 41)))
(defn test-augassign-div [] (defn test-augassign-div []
"NATIVE: test augassign div" "NATIVE: test augassign div"
(let [x 42] (setv x 42)
(/= x 2) (/= x 2)
(assert (= x 21)))) (assert (= x 21)))
(defn test-augassign-floordiv [] (defn test-augassign-floordiv []
"NATIVE: test augassign floordiv" "NATIVE: test augassign floordiv"
(let [x 42] (setv x 42)
(//= x 2) (//= x 2)
(assert (= x 21)))) (assert (= x 21)))
(defn test-augassign-mod [] (defn test-augassign-mod []
"NATIVE: test augassign mod" "NATIVE: test augassign mod"
(let [x 42] (setv x 42)
(%= x 2) (%= x 2)
(assert (= x 0)))) (assert (= x 0)))
(defn test-augassign-pow [] (defn test-augassign-pow []
"NATIVE: test augassign pow" "NATIVE: test augassign pow"
(let [x 2] (setv x 2)
(**= x 3) (**= x 3)
(assert (= x 8)))) (assert (= x 8)))
(defn test-augassign-lshift [] (defn test-augassign-lshift []
"NATIVE: test augassign lshift" "NATIVE: test augassign lshift"
(let [x 2] (setv x 2)
(<<= x 2) (<<= x 2)
(assert (= x 8)))) (assert (= x 8)))
(defn test-augassign-rshift [] (defn test-augassign-rshift []
"NATIVE: test augassign rshift" "NATIVE: test augassign rshift"
(let [x 8] (setv x 8)
(>>= x 1) (>>= x 1)
(assert (= x 4)))) (assert (= x 4)))
(defn test-augassign-bitand [] (defn test-augassign-bitand []
"NATIVE: test augassign bitand" "NATIVE: test augassign bitand"
(let [x 8] (setv x 8)
(&= x 1) (&= x 1)
(assert (= x 0)))) (assert (= x 0)))
(defn test-augassign-bitor [] (defn test-augassign-bitor []
"NATIVE: test augassign bitand" "NATIVE: test augassign bitand"
(let [x 0] (setv x 0)
(|= x 2) (|= x 2)
(assert (= x 2)))) (assert (= x 2)))
(defn test-augassign-bitxor [] (defn test-augassign-bitxor []
"NATIVE: test augassign bitand" "NATIVE: test augassign bitand"
(let [x 1] (setv x 1)
(^= x 1) (^= x 1)
(assert (= x 0)))) (assert (= x 0)))
(defn overflow-int-to-long [] (defn overflow-int-to-long []
"NATIVE: test if int does not raise an overflow exception" "NATIVE: test if int does not raise an overflow exception"
@ -159,19 +159,19 @@
(defclass HyTestMatrix [list] (defclass HyTestMatrix [list]
[--matmul-- [--matmul--
(fn [self other] (fn [self other]
(let [n (len self) (setv n (len self)
m (len (. other [0])) m (len (. other [0]))
result []] result [])
(for [i (range m)] (for [i (range m)]
(let [result-row []] (setv result-row [])
(for [j (range n)] (for [j (range n)]
(let [dot-product 0] (setv dot-product 0)
(for [k (range (len (. self [0])))] (for [k (range (len (. self [0])))]
(+= dot-product (* (. self [i] [k]) (+= dot-product (* (. self [i] [k])
(. other [k] [j])))) (. other [k] [j]))))
(.append result-row dot-product))) (.append result-row dot-product))
(.append result result-row))) (.append result result-row))
result))]) result)])
(def first-test-matrix (HyTestMatrix [[1 2 3] (def first-test-matrix (HyTestMatrix [[1 2 3]
[4 5 6] [4 5 6]
@ -191,15 +191,16 @@
(assert (= (@ first-test-matrix second-test-matrix) (assert (= (@ first-test-matrix second-test-matrix)
product-of-test-matrices)) product-of-test-matrices))
;; Python <= 3.4 ;; Python <= 3.4
(let [matmul-attempt (try (@ first-test-matrix second-test-matrix) (do
(except [e [Exception]] e))] (setv matmul-attempt (try (@ first-test-matrix second-test-matrix)
(except [e [Exception]] e)))
(assert (isinstance matmul-attempt NameError))))) (assert (isinstance matmul-attempt NameError)))))
(defn test-augassign-matmul [] (defn test-augassign-matmul []
"NATIVE: test augmented-assignment matrix multiplication" "NATIVE: test augmented-assignment matrix multiplication"
(let [matrix first-test-matrix (setv matrix first-test-matrix
matmul-attempt (try (@= matrix second-test-matrix) matmul-attempt (try (@= matrix second-test-matrix)
(except [e [Exception]] e))] (except [e [Exception]] e)))
(if PY35 (if PY35
(assert (= product-of-test-matrices matrix)) (assert (= product-of-test-matrices matrix))
(assert (isinstance matmul-attempt NameError))))) (assert (isinstance matmul-attempt NameError))))

View File

@ -132,11 +132,12 @@
(import [astor.codegen [to_source]]) (import [astor.codegen [to_source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro nif [expr pos zero neg] (setv macro1 "(defmacro nif [expr pos zero neg]
(let [g (gensym)] (setv g (gensym))
`(let [~g ~expr] `(do
(cond [(pos? ~g) ~pos] (setv ~g ~expr)
[(zero? ~g) ~zero] (cond [(pos? ~g) ~pos]
[(neg? ~g) ~neg])))) [(zero? ~g) ~zero]
[(neg? ~g) ~neg])))
(print (nif (inc -1) 1 0 -1)) (print (nif (inc -1) 1 0 -1))
") ")
@ -158,7 +159,8 @@
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro nif [expr pos zero neg] (setv macro1 "(defmacro nif [expr pos zero neg]
(with-gensyms [a] (with-gensyms [a]
`(let [~a ~expr] `(do
(setv ~a ~expr)
(cond [(pos? ~a) ~pos] (cond [(pos? ~a) ~pos]
[(zero? ~a) ~zero] [(zero? ~a) ~zero]
[(neg? ~a) ~neg])))) [(neg? ~a) ~neg]))))
@ -180,7 +182,8 @@
(import [astor.codegen [to_source]]) (import [astor.codegen [to_source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro/g! nif [expr pos zero neg] (setv macro1 "(defmacro/g! nif [expr pos zero neg]
`(let [~g!res ~expr] `(do
(setv ~g!res ~expr)
(cond [(pos? ~g!res) ~pos] (cond [(pos? ~g!res) ~pos]
[(zero? ~g!res) ~zero] [(zero? ~g!res) ~zero]
[(neg? ~g!res) ~neg]))) [(neg? ~g!res) ~neg])))
@ -208,7 +211,8 @@
(import [astor.codegen [to_source]]) (import [astor.codegen [to_source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro! nif [expr pos zero neg] (setv macro1 "(defmacro! nif [expr pos zero neg]
`(let [~g!res ~expr] `(do
(setv ~g!res ~expr)
(cond [(pos? ~g!res) ~pos] (cond [(pos? ~g!res) ~pos]
[(zero? ~g!res) ~zero] [(zero? ~g!res) ~zero]
[(neg? ~g!res) ~neg]))) [(neg? ~g!res) ~neg])))

View File

@ -15,24 +15,23 @@
(defn test-kwonly [] (defn test-kwonly []
"NATIVE: test keyword-only arguments" "NATIVE: test keyword-only arguments"
;; keyword-only with default works ;; keyword-only with default works
(let [kwonly-foo-default-false (fn [&kwonly [foo False]] foo)] (defn kwonly-foo-default-false [&kwonly [foo False]] foo)
(assert (= (apply kwonly-foo-default-false) False)) (assert (= (apply kwonly-foo-default-false) False))
(assert (= (apply kwonly-foo-default-false [] {"foo" True}) True))) (assert (= (apply kwonly-foo-default-false [] {"foo" True}) True))
;; keyword-only without default ... ;; keyword-only without default ...
(let [kwonly-foo-no-default (fn [&kwonly foo] foo) (defn kwonly-foo-no-default [&kwonly foo] foo)
attempt-to-omit-default (try (setv attempt-to-omit-default (try
(kwonly-foo-no-default) (kwonly-foo-no-default)
(except [e [Exception]] e))] (except [e [Exception]] e)))
;; works ;; works
(assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux")) (assert (= (apply kwonly-foo-no-default [] {"foo" "quux"}) "quux"))
;; raises TypeError with appropriate message if not supplied ;; raises TypeError with appropriate message if not supplied
(assert (isinstance attempt-to-omit-default TypeError)) (assert (isinstance attempt-to-omit-default TypeError))
(assert (in "missing 1 required keyword-only argument: 'foo'" (assert (in "missing 1 required keyword-only argument: 'foo'"
(. attempt-to-omit-default args [0])))) (. attempt-to-omit-default args [0])))
;; keyword-only with other arg types works ;; keyword-only with other arg types works
(let [function-of-various-args (defn function-of-various-args [a b &rest args &kwonly foo &kwargs kwargs]
(fn [a b &rest args &kwonly foo &kwargs kwargs] (, a b args foo kwargs))
(, a b args foo kwargs))] (assert (= (apply function-of-various-args
(assert (= (apply function-of-various-args [1 2 3 4] {"foo" 5 "bar" 6 "quux" 7})
[1 2 3 4] {"foo" 5 "bar" 6 "quux" 7}) (, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7}))))
(, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7})))))

View File

@ -1,51 +1,51 @@
(defn test-shadow-addition [] (defn test-shadow-addition []
"NATIVE: test shadow addition" "NATIVE: test shadow addition"
(let [x +] (setv x +)
(assert (try (assert (try
(x) (x)
(except [TypeError] True) (except [TypeError] True)
(else (raise AssertionError)))) (else (raise AssertionError))))
(assert (= (x 1 2 3 4) 10)) (assert (= (x 1 2 3 4) 10))
(assert (= (x 1 2 3 4 5) 15)) (assert (= (x 1 2 3 4 5) 15))
; with strings ; with strings
(assert (= (x "a" "b" "c") (assert (= (x "a" "b" "c")
"abc")) "abc"))
; with lists ; with lists
(assert (= (x ["a"] ["b"] ["c"]) (assert (= (x ["a"] ["b"] ["c"])
["a" "b" "c"])))) ["a" "b" "c"])))
(defn test-shadow-subtraction [] (defn test-shadow-subtraction []
"NATIVE: test shadow subtraction" "NATIVE: test shadow subtraction"
(let [x -] (setv x -)
(assert (try (assert (try
(x) (x)
(except [TypeError] True) (except [TypeError] True)
(else (raise AssertionError)))) (else (raise AssertionError))))
(assert (= (x 1) -1)) (assert (= (x 1) -1))
(assert (= (x 2 1) 1)) (assert (= (x 2 1) 1))
(assert (= (x 2 1 1) 0)))) (assert (= (x 2 1 1) 0)))
(defn test-shadow-multiplication [] (defn test-shadow-multiplication []
"NATIVE: test shadow multiplication" "NATIVE: test shadow multiplication"
(let [x *] (setv x *)
(assert (= (x) 1)) (assert (= (x) 1))
(assert (= (x 3) 3)) (assert (= (x 3) 3))
(assert (= (x 3 3) 9)))) (assert (= (x 3 3) 9)))
(defn test-shadow-division [] (defn test-shadow-division []
"NATIVE: test shadow division" "NATIVE: test shadow division"
(let [x /] (setv x /)
(assert (try (assert (try
(x) (x)
(except [TypeError] True) (except [TypeError] True)
(else (raise AssertionError)))) (else (raise AssertionError))))
(assert (= (x 1) 1)) (assert (= (x 1) 1))
(assert (= (x 8 2) 4)) (assert (= (x 8 2) 4))
(assert (= (x 8 2 2) 2)) (assert (= (x 8 2 2) 2))
(assert (= (x 8 2 2 2) 1)))) (assert (= (x 8 2 2 2) 1)))
(defn test-shadow-compare [] (defn test-shadow-compare []
@ -70,24 +70,24 @@
[2 2]]] [2 2]]]
(assert (= (apply x args) (not (apply y args)))))) (assert (= (apply x args) (not (apply y args))))))
(let [s-lt < (setv s-lt <
s-gt > s-gt >
s-le <= s-le <=
s-ge >= s-ge >=
s-eq = s-eq =
s-ne !=] s-ne !=)
(assert (apply s-lt [1 2 3])) (assert (apply s-lt [1 2 3]))
(assert (not (apply s-lt [3 2 1]))) (assert (not (apply s-lt [3 2 1])))
(assert (apply s-gt [3 2 1])) (assert (apply s-gt [3 2 1]))
(assert (not (apply s-gt [1 2 3]))) (assert (not (apply s-gt [1 2 3])))
(assert (apply s-le [1 1 2 2 3 3])) (assert (apply s-le [1 1 2 2 3 3]))
(assert (not (apply s-le [1 1 2 2 1 1]))) (assert (not (apply s-le [1 1 2 2 1 1])))
(assert (apply s-ge [3 3 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 (not (apply s-ge [3 3 2 2 3 3])))
(assert (apply s-eq [1 1 1 1 1])) (assert (apply s-eq [1 1 1 1 1]))
(assert (not (apply s-eq [1 1 2 1 1]))) (assert (not (apply s-eq [1 1 2 1 1])))
(assert (apply s-ne [1 2 3 4 5])) (assert (apply s-ne [1 2 3 4 5]))
(assert (not (apply s-ne [1 1 2 3 4])))) (assert (not (apply s-ne [1 1 2 3 4])))
; Make sure chained comparisons use `and`, not `&`. ; Make sure chained comparisons use `and`, not `&`.
; https://github.com/hylang/hy/issues/1191 ; https://github.com/hylang/hy/issues/1191