1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
(import
asyncio)
(defn test-decorated-1line-function []
(defn foodec [func]
(fn [] (+ (func) 1)))
(defn [foodec] tfunction []
(* 2 2))
(assert (= (tfunction) 5)))
(defn test-decorated-multiline-function []
(defn bazdec [func]
(fn [] (+ (func) "x")))
(defn [bazdec] f []
(setv intermediate "i")
(+ intermediate "b"))
(assert (= (f) "ibx")))
(defn test-decorated-class []
(defn bardec [cls]
(setv cls.attr2 456)
cls)
(defclass [bardec] cls []
(setv attr1 123))
(assert (= cls.attr1 123))
(assert (= cls.attr2 456)))
(defn test-stacked-decorators []
(defn dec1 [f] (fn [] (+ (f) "a")))
(defn dec2 [f] (fn [] (+ (f) "b")))
(defn [dec1 dec2] f [] "c")
(assert (= (f) "cba")))
(defn test-evaluation-order []
(setv l [])
(defn foo [f]
(.append l "foo")
(fn []
(.append l "foo fn")
(f)))
(defn
[(do (.append l "dec") foo)] ; Decorator list
bar ; Function name
[[arg (do (.append l "arg") 1)]] ; Lambda list
(.append l "bar body") arg) ; Body
(.append l (bar))
(assert (= l ["dec" "arg" "foo" "foo fn" "bar body" 1])))
(defn test-decorated-defn-a []
(defn decorator [func] (fn :async [] (/ (await (func)) 2)))
(defn :async [decorator] coro-test []
(await (asyncio.sleep 0))
42)
(assert (= (asyncio.run (coro-test)) 21)))
|