Test the segment-router. prefix-tree fails on complex tests...

This commit is contained in:
Tommi Reiman 2017-11-24 09:42:53 +02:00
parent 5d7670de60
commit 4490fc1685
6 changed files with 33 additions and 14 deletions

View file

@ -339,7 +339,7 @@
(defn mixed-router (defn mixed-router
"Creates two routers: [[lookup-router]] or [[single-static-path-router]] for "Creates two routers: [[lookup-router]] or [[single-static-path-router]] for
static routes and [[prefix-tree-router]] for wildcard routes. All static routes and [[segment-router]] for wildcard routes. All
routes should be non-conflicting. Takes resolved routes and optional routes should be non-conflicting. Takes resolved routes and optional
expanded options. See [[router]] for options." expanded options. See [[router]] for options."
([routes] ([routes]
@ -348,7 +348,7 @@
(let [{wild true, lookup false} (group-by impl/wild-route? routes) (let [{wild true, lookup false} (group-by impl/wild-route? routes)
compiled (compile-routes routes opts) compiled (compile-routes routes opts)
->static-router (if (= 1 (count lookup)) single-static-path-router lookup-router) ->static-router (if (= 1 (count lookup)) single-static-path-router lookup-router)
wildcard-router (prefix-tree-router wild opts) wildcard-router (segment-router wild opts)
static-router (->static-router lookup opts) static-router (->static-router lookup opts)
names (find-names routes opts)] names (find-names routes opts)]
^{:type ::router} ^{:type ::router}
@ -399,7 +399,7 @@
(and (= 1 (count routes)) (not wilds?)) single-static-path-router (and (= 1 (count routes)) (not wilds?)) single-static-path-router
conflicting linear-router conflicting linear-router
(not wilds?) lookup-router (not wilds?) lookup-router
all-wilds? prefix-tree-router all-wilds? segment-router
:else mixed-router)] :else mixed-router)]
(when-let [conflicts (:conflicts opts)] (when-let [conflicts (:conflicts opts)]

View file

@ -34,8 +34,9 @@
(defn wild-or-catch-all-param? [x] (defn wild-or-catch-all-param? [x]
(boolean (or (wild-param x) (catch-all-param x)))) (boolean (or (wild-param x) (catch-all-param x))))
(defn segments [^String path] (defn segments [path]
(into [] (.split path "/" 666))) #?(:clj (.split ^String path "/" 666)
:cljs (.split path #"/" 666)))
(defn contains-wilds? [path] (defn contains-wilds? [path]
(boolean (some wild-or-catch-all-param? (segments path)))) (boolean (some wild-or-catch-all-param? (segments path))))

View file

@ -48,8 +48,8 @@
(insert segment p data)) (insert segment p data))
nil paths)) nil paths))
(defn lookup [segment ^String path] (defn lookup [segment path]
(-lookup segment (.split path "/") {})) (-lookup segment (impl/segments path) {}))
(comment (comment
(-> [["/:abba" 1] (-> [["/:abba" 1]

View file

@ -45,20 +45,27 @@
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$" #"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
(r/match-by-name! router ::beer)))))) (r/match-by-name! router ::beer))))))
;; TODO
(testing "complex" (testing "complex"
(let [router (r/router (let [router (r/router
[["/:abba" ::abba] [["/:abba" ::abba]
["/abba/1" ::abba1] ["/abba/1" ::abba2]
["/:abba/:dabba/doo" ::doo] ["/:jabba/2" ::jabba2]
["/abba/:dabba/boo" ::boo]] {:router r}) ["/:abba/:dabba/doo" ::doo]
["/abba/:dabba/boo" ::boo]
#_["/:jabba/:dabba/:doo/*foo" ::wild]]
{:router r})
matches #(-> router (r/match-by-path %) :data :name)] matches #(-> router (r/match-by-path %) :data :name)]
(is (= ::abba (matches "/abba"))) (is (= ::abba (matches "/abba")))
(is (= ::abba1 (matches "/abba/1"))) (is (= ::abba2 (matches "/abba/1")))
(is (= ::jabba2 (matches "/abba/2")))
(is (= ::doo (matches "/abba/1/doo"))) (is (= ::doo (matches "/abba/1/doo")))
(is (= ::boo (matches "/abba/1/boo")))))) (is (= ::boo (matches "/abba/1/boo")))
#_(is (= ::wild (matches "/olipa/kerran/avaruus/vaan/ei/toista/kertaa")))
)))
r/linear-router :linear-router r/linear-router :linear-router
r/prefix-tree-router :prefix-tree-router #_#_r/prefix-tree-router :prefix-tree-router
r/segment-router :segment-router r/segment-router :segment-router
r/mixed-router :mixed-router)) r/mixed-router :mixed-router))

View file

@ -0,0 +1,9 @@
(ns reitit.impl-test
(:require [clojure.test :refer [deftest testing is are]]
[reitit.impl :as impl]))
(deftest segments-test
(is (= ["" "api" "ipa" "beer" "craft" "bisse"]
(into [] (impl/segments "/api/ipa/beer/craft/bisse"))))
(is (= ["" "a" "" "b" "" "c" ""]
(into [] (impl/segments "/a//b//c/")))))

View file

@ -2,6 +2,7 @@
(:require [doo.runner :refer-macros [doo-tests]] (:require [doo.runner :refer-macros [doo-tests]]
reitit.coercion-test reitit.coercion-test
reitit.core-test reitit.core-test
reitit.impl-test
reitit.middleware-test reitit.middleware-test
reitit.ring-test reitit.ring-test
#_reitit.spec-test)) #_reitit.spec-test))
@ -10,6 +11,7 @@
(doo-tests 'reitit.coercion-test (doo-tests 'reitit.coercion-test
'reitit.core-test 'reitit.core-test
'reitit.impl-test
'reitit.middleware-test 'reitit.middleware-test
'reitit.ring-test 'reitit.ring-test
#_'reitit.spec-test) #_'reitit.spec-test)