Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix render and add test

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
commit 673d8f40ad22b6e9f0cd7f1de4147401487d5f7a 1 parent 9305443
@ibdknox ibdknox authored
Showing with 17 additions and 9 deletions.
  1. +11 −9 src/noir/core.clj
  2. +6 −0 test/noir/test/core.clj
View
20 src/noir/core.clj
@@ -60,7 +60,7 @@
(parse-route)
(parse-destruct-body)))
-(defmacro defpage
+(defmacro defpage
"Adds a route to the server whose content is the the result of evaluating the body.
The function created is passed the params of the request and the destruct param allows
you to destructure that meaningfully for use in the body.
@@ -84,7 +84,7 @@
(swap! route-funcs assoc ~(keyword fn-name#) ~fn-name#)
(swap! noir-routes assoc ~(keyword fn-name#) (~action# ~url# {params# :params} (~fn-name# params#))))))
-(defmacro defpartial
+(defmacro defpartial
"Create a function that returns html using hiccup. The function is callable with the given name."
[fname params & body]
`(defn ~fname ~params
@@ -126,19 +126,21 @@
((var throwf) "could not find route fn:" ~route-fn))
(url-for* route-fn# ~arg-map)))))) ;; use ns-resolve to resolve at runtime (rather than compile time), to avoid circular dependencies between views.
-(defn render
+(defn render
"Renders the content for a route by calling the page like a function
with the given param map. Accepts either '/vals' or [:post '/vals']"
[route & [params]]
- (let [{fn-name :route-fn} (parse-args route)
- func (get @route-funcs (keyword fn-name))]
- (func params)))
+ (if (fn? route)
+ (route params)
+ (let [[{fn-name :fn-name :as res}] (parse-route [{} [route]])
+ func (get @route-funcs (keyword fn-name))]
+ (func params))))
-(defmacro pre-route
+(defmacro pre-route
"Adds a route to the beginning of the route table and passes the entire request
to be destructured and used in the body. These routes are the only ones to make
- an ordering gaurantee. They will always be in order of ascending specificity (e.g. /* ,
- /admin/* , /admin/user/*) Pre-routes are usually used for filtering, like redirecting
+ an ordering gaurantee. They will always be in order of ascending specificity (e.g. /* ,
+ /admin/* , /admin/user/*) Pre-routes are usually used for filtering, like redirecting
a section based on privileges:
(pre-route '/admin/*' {} (when-not (is-admin?) (redirect '/login')))"
View
6 test/noir/test/core.clj
@@ -60,6 +60,7 @@
(defpage "/test" {:keys [nme]}
(str "Hello " nme))
+
(deftest route-test
(-> (send-request "/test" {"nme" "chris"})
(has-status 200)
@@ -140,6 +141,11 @@
(defpage [:post "/post-route"] {:keys [nme]}
(str "Post " nme))
+(deftest render-test
+ (is (= "named-route" (render foo)))
+ (is (= "Hello chris" (render "/test" {:nme "chris"})))
+ (is (= "Post chris") (render [:post "/post-route"] {:nme "chris"})))
+
(deftest route-post-test
(-> (send-request [:post "/post-route"] {"nme" "chris"})
(has-status 200)
Please sign in to comment.
Something went wrong with that request. Please try again.