2015-09-29 22:57:36 +02:00
|
|
|
;; Tests where the emitted code relies on Python 3.
|
2014-05-01 22:31:45 +02:00
|
|
|
;; Conditionally included in nosetests runs.
|
|
|
|
|
2014-06-09 22:33:32 +02:00
|
|
|
(import [hy._compat [PY33]])
|
|
|
|
(import [hy.errors [HyCompileError]])
|
|
|
|
|
|
|
|
|
2014-05-01 22:31:45 +02:00
|
|
|
|
|
|
|
(defn test-exception-cause []
|
|
|
|
(try (raise ValueError :from NameError)
|
|
|
|
(except [e [ValueError]]
|
|
|
|
(assert (= (type (. e __cause__)) NameError)))))
|
2015-03-15 22:59:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-kwonly []
|
|
|
|
"NATIVE: test keyword-only arguments"
|
|
|
|
;; keyword-only with default works
|
2015-08-17 09:07:32 +02:00
|
|
|
(let [kwonly-foo-default-false (fn [&kwonly [foo false]] foo)]
|
2015-03-15 22:59:54 +01:00
|
|
|
(assert (= (apply kwonly-foo-default-false) false))
|
|
|
|
(assert (= (apply kwonly-foo-default-false [] {"foo" true}) true)))
|
|
|
|
;; keyword-only without default ...
|
2015-08-17 09:07:32 +02:00
|
|
|
(let [kwonly-foo-no-default (fn [&kwonly foo] foo)
|
|
|
|
attempt-to-omit-default (try
|
|
|
|
(kwonly-foo-no-default)
|
|
|
|
(except [e [Exception]] e))]
|
2015-03-15 22:59:54 +01:00
|
|
|
;; 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
|
2015-08-17 09:07:32 +02:00
|
|
|
(let [function-of-various-args
|
|
|
|
(fn [a b &rest args &kwonly foo &kwargs kwargs]
|
|
|
|
(, a b args foo kwargs))]
|
2015-03-15 22:59:54 +01:00
|
|
|
(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})))))
|