2019-02-07 08:57:35 -05:00
|
|
|
;; Copyright 2019 the authors.
|
2017-04-27 14:16:57 -07:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
2017-02-22 15:36:52 -08:00
|
|
|
(defn test-decorated-1line-function []
|
|
|
|
(defn foodec [func]
|
|
|
|
(fn [] (+ (func) 1)))
|
|
|
|
(with-decorator foodec
|
|
|
|
(defn tfunction []
|
|
|
|
(* 2 2)))
|
|
|
|
(assert (= (tfunction) 5)))
|
2013-04-28 17:14:22 +02:00
|
|
|
|
|
|
|
|
2017-02-22 15:36:52 -08:00
|
|
|
(defn test-decorated-multiline-function []
|
|
|
|
(defn bazdec [func]
|
|
|
|
(fn [] (+ (func) "x")))
|
|
|
|
(with-decorator bazdec
|
|
|
|
(defn f []
|
|
|
|
(setv intermediate "i")
|
|
|
|
(+ intermediate "b")))
|
|
|
|
(assert (= (f) "ibx")))
|
2013-04-28 17:14:22 +02:00
|
|
|
|
|
|
|
|
2017-02-22 15:36:52 -08:00
|
|
|
(defn test-decorated-class []
|
|
|
|
(defn bardec [cls]
|
|
|
|
(setv cls.attr2 456)
|
|
|
|
cls)
|
|
|
|
(with-decorator bardec
|
|
|
|
(defclass cls []
|
2019-07-02 12:02:03 -04:00
|
|
|
(setv attr1 123)))
|
2017-02-22 15:36:52 -08:00
|
|
|
(assert (= cls.attr1 123))
|
|
|
|
(assert (= cls.attr2 456)))
|
|
|
|
|
|
|
|
|
2015-01-14 19:42:02 +00:00
|
|
|
(defn test-decorator-clobbing []
|
|
|
|
"NATIVE: Tests whether nested decorators work"
|
|
|
|
(do
|
2017-03-23 15:11:55 -07:00
|
|
|
(defn dec1 [f] (fn [] (+ (f) 1)))
|
|
|
|
(defn dec2 [f] (fn [] (+ (f) 2)))
|
2015-01-14 19:42:02 +00:00
|
|
|
(with-decorator dec1
|
|
|
|
(with-decorator dec2
|
|
|
|
(defn f [] 1)))
|
|
|
|
(assert (= (f) 4))))
|