a3c9ee0e3ebe93ea6b0620bc48b3dd2fae7f02a3 — Josef Pospíšil 3 years ago 7ded305
Fix tests and project.janet
2 files changed, 88 insertions(+), 55 deletions(-)

M project.janet
M test/trolley.janet
M project.janet => project.janet +5 -5
@@ 1,11 1,11 @@
  :name "trolley"
  :description "General router"
	:author "Josef Pospíšil"
  :description "Router, resolver and lookup development kit"
  :author "Josef Pospíšil"
  :license "MIT"
  :url "https://github.com/pepe/trolley"
  :repo "git+https://github.com/pepe/trolley"
  :dependencies ["https://github.com/joy-framework/tester"])
  :url "https://git.sr.ht/~pepe/trolley"
  :repo "https://git.sr.ht/~pepe/trolley"
  :dependencies ["spork"])

  :source @["trolley.janet"])

M test/trolley.janet => test/trolley.janet +83 -50
@@ 1,52 1,85 @@
(import tester :prefix "")
(import ../trolley :as trolley)
(use spork/test)
(use trolley)

(def config "Routes' config for all tests"
  {"/" :root "/home/:id" :home "/real-thing.json" :real-thing
    "/involved/:id/example/:example-id/detail/:detail-id" :involved})

(deftest "Compile routes"
  (let [compiled-routes (trolley/compile-routes config)
        actions (values compiled-routes) 
        routes (keys compiled-routes)]
    (test "are compiled" compiled-routes)
    (test "has root action" 
          (some |(= $ :root) actions))
    (test "has home action" 
          (some |(= $ :home) actions))
    (test "routes are all PEGs"
          (all |(= (type $) :core/peg) routes))))

(deftest "Lookup uri"
  (let [compiled-routes (trolley/compile-routes config)]
    (test "lookup"
         (deep= (trolley/lookup compiled-routes "/home/3") 
                [:home @{:id "3"}]))
   (test "lookup root"
         (deep= (trolley/lookup compiled-routes "/") 
                [:root @{}]))
   (test "lookup real thing"
         (deep= (trolley/lookup compiled-routes "/real-thing.json") 
                [:real-thing @{}]) )
   (test "lookup rooty"
         (empty? (trolley/lookup compiled-routes "/home/")))))

(deftest "Router"
  (let [router (trolley/router config)]
    (test "root"
          (deep= (router "/") [:root @{}]))
    (test "home"
          (deep= (router "/home/3") [:home @{:id "3"}]))
    (test "real thing json"
          (deep= (router "/real-thing.json") [:real-thing @{}]))
    (test "not found"
          (empty? (router "home")))))

(deftest "Path for"
  (let [path-for (trolley/resolver config)]
    (test "root"
          (= (path-for :root) "/"))
    (test "home"
          (= (path-for :home @{:id 3}) "/home/3"))
    (test "involved"
          (= (path-for :involved @{:id 1 :example-id 2 :detail-id 3}) "/involved/1/example/2/detail/3"))))
  {"/" :root
   "/home/:id" :home
   "/real-thing.json" :real-thing
   "/involved/:id/example/:example-id/detail/:detail-id" :involved})

(start-suite 0)

  (compile-routes config)
  "compile routes")

  (all |(= :core/peg (type $))
       (keys (compile-routes config)))
  "keys pegs")

    (sort (values (compile-routes config)))
    (sort @[:home :real-thing :involved :root]))
  "all values")

  (deep= (lookup (compile-routes config) "/home/3")
         [:home @{:id "3"}])
  "lookup home")

  (deep= (lookup (compile-routes config) "/")
         [:root @{}])
  "lookup root")

  (deep= (lookup (compile-routes config) "/real-thing.json")
         [:real-thing @{}])
  "lookup real-thing")

  (empty? (lookup (compile-routes config) "/home/"))
  "lookup wrong")

(assert (router config) "router")

    ((router config) "/")
    [:root @{}])
  "router root")

    ((router config) "/home/3")
    [:home @{:id "3"}])
  "router home")

  (deep= ((router config) "/real-thing.json") [:real-thing @{}])
  "router json")

  (empty? ((router config) "home"))
  "wrong route")

(assert (resolver config) "resolver")

(assert (= ((resolver config) :root) "/") "resolve home")

    ((resolver config) :home @{:id 3})
  "resolve home")

    ((resolver config) :involved @{:id 1 :example-id 2 :detail-id 3})