router-type => router-name

This commit is contained in:
Tommi Reiman 2017-09-08 08:17:45 +03:00
parent ec628bd05e
commit 249a54f9bc
3 changed files with 14 additions and 9 deletions

View file

@ -87,7 +87,7 @@ Creating a router:
`:mixed-router` is created (both static & wild routes are found):
```clj
(reitit/router-type router)
(reitit/router-name router)
; :mixed-router
```

View file

@ -93,7 +93,7 @@
[p m (if compile (compile route opts))])
(defprotocol Router
(router-type [this])
(router-name [this])
(routes [this])
(options [this])
(route-names [this])
@ -146,7 +146,7 @@
lookup (impl/fast-map lookup)]
(reify
Router
(router-type [_]
(router-name [_]
:linear-router)
(routes [_]
routes)
@ -190,7 +190,7 @@
data (impl/fast-map data)
lookup (impl/fast-map lookup)]
(reify Router
(router-type [_]
(router-name [_]
:lookup-router)
(routes [_]
routes)
@ -220,7 +220,7 @@
lookup-router (lookup-router lookup opts)
names (find-names routes opts)]
(reify Router
(router-type [_]
(router-name [_]
:mixed-router)
(routes [_]
routes)

View file

@ -9,8 +9,8 @@
(testing "linear-router"
(let [router (reitit/router ["/api" ["/ipa" ["/:size" ::beer]]])]
(is (= :linear-router (reitit/router-type router)))
(is (= [["/api/ipa/:size" {:name ::beer}]]
(is (= :linear-router (reitit/router-name router)))
(reitit/routes router)))
(is (= true (map? (reitit/options router))))
(is (= (reitit/map->Match
@ -27,6 +27,7 @@
(reitit/match-by-name router ::beer {:size "large"})))
(is (= nil (reitit/match-by-name router "ILLEGAL")))
(is (= [::beer] (reitit/route-names router)))
(testing "name-based routing with missing parameters"
(is (= (reitit/map->PartialMatch
{:template "/api/ipa/:size"
@ -42,8 +43,8 @@
(testing "lookup-router"
(let [router (reitit/router ["/api" ["/ipa" ["/large" ::beer]]])]
(is (= :lookup-router (reitit/router-type router)))
(is (= [["/api/ipa/large" {:name ::beer}]]
(is (= :lookup-router (reitit/router-name router)))
(reitit/routes router)))
(is (= true (map? (reitit/options router))))
(is (= (reitit/map->Match
@ -60,6 +61,7 @@
(reitit/match-by-name router ::beer {:size "large"})))
(is (= nil (reitit/match-by-name router "ILLEGAL")))
(is (= [::beer] (reitit/route-names router)))
(testing "can't be created with wildcard routes"
(is (thrown-with-msg?
ExceptionInfo
@ -69,6 +71,7 @@
["/api/:version/ping"] {})))))))
(testing "route coercion & compilation"
(testing "custom compile"
(let [compile-times (atom 0)
coerce (fn [[path meta] _]
@ -86,6 +89,7 @@
["/crap"]]]
{:coerce coerce
:compile compile})]
(testing "routes are coerced"
(is (= [["/api/ping" {:name ::ping
:path "/api/ping",
@ -100,6 +104,7 @@
(is result)
(is (= "/api/pong" (result)))
(is (= 2 @compile-times))))))
(testing "default compile"
(let [router (reitit/router ["/ping" (constantly "ok")])]
(let [{:keys [result]} (reitit/match-by-path router "/ping")]
@ -109,9 +114,9 @@
(testing "custom router"
(let [router (reitit/router ["/ping"] {:router (fn [_ _]
(reify Router
(reitit/router-type [_]
(reitit/router-name [_]
::custom)))})]
(is (= ::custom (reitit/router-type router)))))
(is (= ::custom (reitit/router-name router)))))
(testing "bide sample"
(let [routes [["/auth/login" :auth/login]