Merge branch 'meth-fix' of https://github.com/Willyfrog/hy into Willyfrog-meth-fix

Conflicts:
	tests/__init__.py
This commit is contained in:
Nicolas Dandrimont 2014-01-02 00:53:40 +01:00
commit 77baf3b9f3
4 changed files with 69 additions and 15 deletions

View File

@ -1,31 +1,30 @@
;;; Meth
;; based on paultag's meth library to access a Flask based application
(defmacro route [name path params code]
"Default get request"
`(let [[deco (.route app ~path)]]
(with-decorator deco
(defn ~name ~params ~@code))))
(defmacro route-with-methods [name path params code methods]
(defmacro route-with-methods [name path methods params &rest code]
"Same as route but with an extra methods array to specify HTTP methods"
`(let [[deco (kwapply (.route app ~path)
{"methods" ~methods})]]
(with-decorator deco
(defn ~name ~params ~@code))))
(defn ~name ~params
(progn ~@code)))))
;; Some macro examples
(defmacro post-route [name path params code]
(defmacro route [name path params &rest code]
"Get request"
`(route-with-methods ~name ~path ["GET"] ~params ~@code))
(defmacro post-route [name path params &rest code]
"Post request"
`(route-with-methods ~name ~path ~params ~code ["POST"]))
`(route-with-methods ~name ~path ["POST"] ~params ~@code))
(defmacro put-route [name path params code]
(defmacro put-route [name path params &rest code]
"Put request"
`(route-with-methods ~name ~path ~params ~code ["PUT"]))
`(route-with-methods ~name ~path ["PUT"] ~params ~@code))
(defmacro delete-route [name path params code]
(defmacro delete-route [name path params &rest code]
"Delete request"
`(route-with-methods ~name ~path ~params ~code ["DELETE"]))
`(route-with-methods ~name ~path ["DELETE"] ~params ~@code))
;;; Simple example application
@ -34,7 +33,7 @@
;; (import [flask [Flask]])
;; (setv app (Flask "__main__"))
;; (require methy)
;; (require hy.contrib.meth)
;; (print "setup / with GET")
;; (route get-index "/" [] (str "Hy world!"))

View File

@ -14,3 +14,4 @@ from .native_tests.core import * # noqa
from .native_tests.reader_macros import * # noqa
from .native_tests.with_test import * # noqa
from .native_tests.contrib.anaphoric import * # noqa
from .contrib.test_meth import * # noqa

View File

View File

@ -0,0 +1,54 @@
(require hy.contrib.meth)
(defclass FakeMeth []
"Mocking decorator class"
[[rules {}]
[route (fn [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))))))