2018-01-01 16:38:33 +01:00
|
|
|
;; Copyright 2018 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
2015-09-29 22:57:36 +02:00
|
|
|
;; Tests where the emitted code relies on Python 3.
|
2017-04-26 23:00:11 +02:00
|
|
|
;; conftest.py skips this file when running on Python 2.
|
2014-06-09 22:33:32 +02:00
|
|
|
|
2014-05-01 22:31:45 +02:00
|
|
|
|
|
|
|
(defn test-exception-cause []
|
|
|
|
(try (raise ValueError :from NameError)
|
2017-09-18 21:50:41 +02:00
|
|
|
(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
|
2017-02-04 18:07:27 +01:00
|
|
|
(defn kwonly-foo-default-false [&kwonly [foo False]] foo)
|
2017-07-16 01:54:43 +02:00
|
|
|
(assert (= (kwonly-foo-default-false) False))
|
|
|
|
(assert (= (kwonly-foo-default-false :foo True) True))
|
2015-03-15 22:59:54 +01:00
|
|
|
;; keyword-only without default ...
|
2017-02-04 18:07:27 +01:00
|
|
|
(defn kwonly-foo-no-default [&kwonly foo] foo)
|
|
|
|
(setv attempt-to-omit-default (try
|
2017-09-18 21:50:41 +02:00
|
|
|
(kwonly-foo-no-default)
|
|
|
|
(except [e [Exception]] e)))
|
2017-02-04 18:07:27 +01:00
|
|
|
;; works
|
2017-07-16 01:54:43 +02:00
|
|
|
(assert (= (kwonly-foo-no-default :foo "quux") "quux"))
|
2017-02-04 18:07:27 +01:00
|
|
|
;; 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])))
|
2015-03-15 22:59:54 +01:00
|
|
|
;; keyword-only with other arg types works
|
2017-02-04 18:07:27 +01:00
|
|
|
(defn function-of-various-args [a b &rest args &kwonly foo &kwargs kwargs]
|
|
|
|
(, a b args foo kwargs))
|
2017-07-16 01:54:43 +02:00
|
|
|
(assert (= (function-of-various-args 1 2 3 4 :foo 5 :bar 6 :quux 7)
|
2017-02-04 18:07:27 +01:00
|
|
|
(, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7}))))
|
2017-07-10 18:04:24 +02:00
|
|
|
|
|
|
|
|
2017-07-17 22:34:39 +02:00
|
|
|
(defn test-extended-unpacking-1star-lvalues []
|
|
|
|
(setv [x #*y] [1 2 3 4])
|
|
|
|
(assert (= x 1))
|
|
|
|
(assert (= y [2 3 4]))
|
|
|
|
(setv [a #*b c] "ghijklmno")
|
|
|
|
(assert (= a "g"))
|
|
|
|
(assert (= b (list "hijklmn")))
|
|
|
|
(assert (= c "o")))
|
|
|
|
|
|
|
|
|
2017-07-10 18:04:24 +02:00
|
|
|
(defn test-yield-from []
|
|
|
|
"NATIVE: testing yield from"
|
|
|
|
(defn yield-from-test []
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [i (range 3)]
|
2017-07-10 18:04:24 +02:00
|
|
|
(yield i))
|
|
|
|
(yield-from [1 2 3]))
|
|
|
|
(assert (= (list (yield-from-test)) [0 1 2 1 2 3])))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-yield-from-exception-handling []
|
|
|
|
"NATIVE: Ensure exception handling in yield from works right"
|
|
|
|
(defn yield-from-subgenerator-test []
|
|
|
|
(yield 1)
|
|
|
|
(yield 2)
|
|
|
|
(yield 3)
|
|
|
|
(assert 0))
|
|
|
|
(defn yield-from-test []
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [i (range 3)]
|
2017-09-18 21:50:41 +02:00
|
|
|
(yield i))
|
2017-07-10 18:04:24 +02:00
|
|
|
(try
|
2017-09-18 21:50:41 +02:00
|
|
|
(yield-from (yield-from-subgenerator-test))
|
|
|
|
(except [e AssertionError]
|
|
|
|
(yield 4))))
|
2017-07-10 18:04:24 +02:00
|
|
|
(assert (= (list (yield-from-test)) [0 1 2 1 2 3 4])))
|
2017-09-18 21:50:41 +02:00
|
|
|
|
|
|
|
(require [hy.contrib.walk [let]])
|
|
|
|
|
|
|
|
(defn test-let-optional []
|
|
|
|
(let [a 1
|
|
|
|
b 6
|
|
|
|
d 2]
|
|
|
|
(defn foo [&kwonly [a a] b [c d]]
|
|
|
|
(, a b c))
|
|
|
|
(assert (= (foo :b "b")
|
|
|
|
(, 1 "b" 2)))
|
|
|
|
(assert (= (foo :b 20 :a 10 :c 30)
|
|
|
|
(, 10 20 30)))))
|
|
|
|
|
2018-02-12 00:26:29 +01:00
|
|
|
(defn test-pep-3115 []
|
|
|
|
(defclass member-table [dict]
|
|
|
|
[--init-- (fn [self] (setv self.member-names []))
|
|
|
|
|
|
|
|
--setitem-- (fn [self key value]
|
|
|
|
(if (not-in key self)
|
|
|
|
(.append self.member-names key))
|
|
|
|
(dict.--setitem-- self key value))])
|
|
|
|
|
|
|
|
(defclass OrderedClass [type]
|
|
|
|
[--prepare-- (classmethod (fn [metacls name bases] (member-table)))
|
|
|
|
|
|
|
|
--new-- (fn [cls name bases classdict]
|
|
|
|
(setv result (type.--new-- cls name bases (dict classdict)))
|
|
|
|
(setv result.member-names classdict.member-names)
|
|
|
|
result)])
|
|
|
|
|
|
|
|
(defclass MyClass [:metaclass OrderedClass]
|
|
|
|
[method1 (fn [self] (pass))
|
|
|
|
method2 (fn [self] (pass))])
|
|
|
|
|
|
|
|
(assert (= (. (MyClass) member-names)
|
|
|
|
["__module__" "__qualname__" "method1" "method2"])))
|