2019-02-07 14:57:35 +01:00
|
|
|
;; Copyright 2019 the authors.
|
2017-12-30 23:31:06 +01:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
|
|
|
;; Tests where the emitted code relies on Python ≥3.6.
|
|
|
|
;; conftest.py skips this file when running on Python <3.6.
|
|
|
|
|
|
|
|
(import [asyncio [get-event-loop sleep]])
|
2019-10-07 20:14:49 +02:00
|
|
|
(import [typing [get-type-hints List Dict]])
|
2017-12-30 23:31:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn run-coroutine [coro]
|
|
|
|
"Run a coroutine until its done in the default event loop."""
|
|
|
|
(.run_until_complete (get-event-loop) (coro)))
|
|
|
|
|
|
|
|
|
2018-06-12 19:57:57 +02:00
|
|
|
(defn test-for-async []
|
2017-12-30 23:31:06 +01:00
|
|
|
(defn/a numbers []
|
|
|
|
(for [i [1 2]]
|
|
|
|
(yield i)))
|
|
|
|
|
|
|
|
(run-coroutine
|
|
|
|
(fn/a []
|
|
|
|
(setv x 0)
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [:async a (numbers)]
|
2017-12-30 23:31:06 +01:00
|
|
|
(setv x (+ x a)))
|
|
|
|
(assert (= x 3)))))
|
|
|
|
|
2018-06-12 19:57:57 +02:00
|
|
|
(defn test-for-async-else []
|
2017-12-30 23:31:06 +01:00
|
|
|
(defn/a numbers []
|
|
|
|
(for [i [1 2]]
|
|
|
|
(yield i)))
|
|
|
|
|
|
|
|
(run-coroutine
|
|
|
|
(fn/a []
|
|
|
|
(setv x 0)
|
2018-06-12 19:57:57 +02:00
|
|
|
(for [:async a (numbers)]
|
2017-12-30 23:31:06 +01:00
|
|
|
(setv x (+ x a))
|
|
|
|
(else (setv x (+ x 50))))
|
|
|
|
(assert (= x 53)))))
|
2018-02-12 00:26:29 +01:00
|
|
|
|
2019-08-12 23:09:32 +02:00
|
|
|
(defn test-variable-annotations []
|
|
|
|
(defclass AnnotationContainer []
|
|
|
|
(setv ^int x 1 y 2)
|
|
|
|
(^bool z))
|
|
|
|
|
|
|
|
(setv annotations (get-type-hints AnnotationContainer))
|
|
|
|
(assert (= (get annotations "x") int))
|
|
|
|
(assert (= (get annotations "z") bool)))
|
|
|
|
|
2019-10-07 20:14:49 +02:00
|
|
|
(defn test-of []
|
|
|
|
(assert (= (of str) str))
|
|
|
|
(assert (= (of List int) (get List int)))
|
|
|
|
(assert (= (of Dict str str) (get Dict (, str str)))))
|
|
|
|
|
2018-02-12 00:26:29 +01:00
|
|
|
(defn test-pep-487 []
|
|
|
|
(defclass QuestBase []
|
2019-07-02 18:02:03 +02:00
|
|
|
(defn --init-subclass-- [cls swallow &kwargs kwargs]
|
2019-07-02 18:10:01 +02:00
|
|
|
(setv cls.swallow swallow)))
|
2018-02-12 00:26:29 +01:00
|
|
|
|
|
|
|
(defclass Quest [QuestBase :swallow "african"])
|
|
|
|
(assert (= (. (Quest) swallow) "african")))
|