hy/tests/native_tests/contrib/meth.hy
Gergely Nagy cbc2eed900
defclass reimagined
defclass now has a new syntax:

 (defclass Name [BaseList]
   [property value
    property value] ;; optional

   (defn method [self]
     self.property))

Anything after the optional property list (which will be translated to a
setv within the class context) will be added to the class body. This
allows one to have side effects and complex expressions within the class
definition.

As a side effect, defining methods is much more friendly now!

Closes #850.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
2015-08-04 16:43:07 +02:00

54 lines
1.9 KiB
Hy

(require hy.contrib.meth)
(defclass FakeMeth []
"Mocking decorator class"
[rules {}]
(defn route [self rule &kwargs options]
(fn [f]
(assoc self.rules rule (, f options))
f)))
(defn test_route []
(let [[app (FakeMeth)]]
(route get-index "/" [] (str "Hy world!"))
(setv app-rules (getattr app "rules"))
(assert (in "/" app-rules))
(let [[(, rule-fun rule-opt) (get app-rules "/")]]
(assert (not (empty? rule-opt)))
(assert (in "GET" (get rule-opt "methods")))
(assert (= (getattr rule-fun "__name__") "get_index"))
(assert (= "Hy world!" (rule-fun))))))
(defn test_post_route []
(let [[app (FakeMeth)]]
(post-route get-index "/" [] (str "Hy world!"))
(setv app-rules (getattr app "rules"))
(assert (in "/" app-rules))
(let [[(, rule-fun rule-opt) (get app-rules "/")]]
(assert (not (empty? rule-opt)))
(assert (in "POST" (get rule-opt "methods")))
(assert (= (getattr rule-fun "__name__") "get_index"))
(assert (= "Hy world!" (rule-fun))))))
(defn test_put_route []
(let [[app (FakeMeth)]]
(put-route get-index "/" [] (str "Hy world!"))
(setv app-rules (getattr app "rules"))
(assert (in "/" app-rules))
(let [[(, rule-fun rule-opt) (get app-rules "/")]]
(assert (not (empty? rule-opt)))
(assert (in "PUT" (get rule-opt "methods")))
(assert (= (getattr rule-fun "__name__") "get_index"))
(assert (= "Hy world!" (rule-fun))))))
(defn test_delete_route []
(let [[app (FakeMeth)]]
(delete-route get-index "/" [] (str "Hy world!"))
(setv app-rules (getattr app "rules"))
(assert (in "/" app-rules))
(let [[(, rule-fun rule-opt) (get app-rules "/")]]
(assert (not (empty? rule-opt)))
(assert (in "DELETE" (get rule-opt "methods")))
(assert (= (getattr rule-fun "__name__") "get_index"))
(assert (= "Hy world!" (rule-fun))))))