~pepe/trolley

dba63576faf768ba83541879004f09d6c64525ca — Josef Pospíšil 4 years ago c0a3981
Fix routing
2 files changed, 17 insertions(+), 16 deletions(-)

M src/router.janet
M test/router.janet
M src/router.janet => src/router.janet +2 -6
@@ 56,13 56,9 @@
  matched)

(defn router
  "Creates a router from routes"
  "Creates a simple router from routes"
  [routes]
  (def compiled-routes (compile-routes routes))
  (fn [path]
    (let [[action params] (lookup compiled-routes path)]
      (if action 
        action 
        (routes :not-found))))) # @todo document
  (fn [path] (lookup compiled-routes path)))



M test/router.janet => test/router.janet +15 -10
@@ 7,10 7,16 @@
    (router/compile-routes {"/" :root "/home/:id" :home}))

  (test "are compiled" compiled-routes)
  (test "has all actions" 
        (deep= @[:home :root] (values compiled-routes)))

  (def actions (values compiled-routes))
  (test "has root action" 
        (some |(= :root $) actions))
  (test "has home action" 
        (some |(= :home $) actions))

  (def first-route (first (keys compiled-routes)))
  (test "route is peg"
        (= :core/peg (type (first (keys compiled-routes))))))
        (= :core/peg (type first-route))))

(deftest "Lookup uri"
  (def compiled-routes 


@@ 18,20 24,19 @@

  (test "lookup"
        (deep= (router/lookup compiled-routes "/home/3") 
               '(:home @{:id "3"})))
               [:home @{:id "3"}]))
  (test "lookup root"
        (deep= (router/lookup compiled-routes "/") 
               '(:root @{})))
               [:root @{}]))
  (test "lookup rooty"
        (empty? (router/lookup compiled-routes "/home/"))))

(deftest "Router"
  (def router (router/router {"/" :root 
                              "/home/:id" :home 
                              :not-found :not-found}))
                              "/home/:id" :home}))
  (test "root"
        (= (router "/") :root))
        (deep= (router "/") [:root @{}]))
  (test "home"
        (= (router "/home/3") :home))
        (deep= (router "/home/3") [:home @{:id "3"}]))
  (test "not found"
        (= (router "home") :not-found)))
        (empty? (router "home"))))