From f913c2aa8c64a14854d7a5fce919a3ae7a579d5e Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Mon, 18 Nov 2013 07:58:40 +0100 Subject: [PATCH 1/3] meth wasn't accepting correctly its parameters Now also accepts more than 1 statement in the code part of the macro refactored to use only 1 codebase (no more route != route-with-macros) --- hy/contrib/meth.hy | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/hy/contrib/meth.hy b/hy/contrib/meth.hy index 1f4ae70..b6301f2 100644 --- a/hy/contrib/meth.hy +++ b/hy/contrib/meth.hy @@ -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] "Post request" - `(route-with-methods ~name ~path ~params ~code ["POST"])) + `(route-with-methods ~name ~path ["GET"] ~params ~@code)) -(defmacro put-route [name path params code] +(defmacro post-route [name path params &rest code] + "Post request" + `(route-with-methods ~name ~path ["POST"] ~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!")) From aeb0cd4c72ced7aebb0edb5025bbaaecbb684cba Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Mon, 9 Dec 2013 11:49:17 +0100 Subject: [PATCH 2/3] Fixed docstring --- hy/contrib/meth.hy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hy/contrib/meth.hy b/hy/contrib/meth.hy index b6301f2..18203ea 100644 --- a/hy/contrib/meth.hy +++ b/hy/contrib/meth.hy @@ -11,7 +11,7 @@ ;; Some macro examples (defmacro route [name path params &rest code] - "Post request" + "Get request" `(route-with-methods ~name ~path ["GET"] ~params ~@code)) (defmacro post-route [name path params &rest code] From 0109234eb7fea7a3e5307b0ecbae09e7c8975749 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Fri, 20 Dec 2013 20:29:48 +0100 Subject: [PATCH 3/3] Added meth tests by mocking a Flask app --- tests/__init__.py | 1 + tests/contrib/__init__.hy | 0 tests/contrib/test_meth.hy | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 tests/contrib/__init__.hy create mode 100644 tests/contrib/test_meth.hy diff --git a/tests/__init__.py b/tests/__init__.py index 5e935a2..c58d7b4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,3 +11,4 @@ from .native_tests.unless import * # noqa from .native_tests.when import * # noqa from .native_tests.with_decorator import * # noqa from .native_tests.core import * # noqa +from .contrib.test_meth import * # noqa diff --git a/tests/contrib/__init__.hy b/tests/contrib/__init__.hy new file mode 100644 index 0000000..e69de29 diff --git a/tests/contrib/test_meth.hy b/tests/contrib/test_meth.hy new file mode 100644 index 0000000..2f13cfd --- /dev/null +++ b/tests/contrib/test_meth.hy @@ -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))))))