mirror of
https://github.com/metosin/reitit.git
synced 2025-12-21 18:11:12 +00:00
Test the segment-router. prefix-tree fails on complex tests...
This commit is contained in:
parent
5d7670de60
commit
4490fc1685
6 changed files with 33 additions and 14 deletions
|
|
@ -339,7 +339,7 @@
|
|||
|
||||
(defn mixed-router
|
||||
"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
|
||||
expanded options. See [[router]] for options."
|
||||
([routes]
|
||||
|
|
@ -348,7 +348,7 @@
|
|||
(let [{wild true, lookup false} (group-by impl/wild-route? routes)
|
||||
compiled (compile-routes routes opts)
|
||||
->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)
|
||||
names (find-names routes opts)]
|
||||
^{:type ::router}
|
||||
|
|
@ -399,7 +399,7 @@
|
|||
(and (= 1 (count routes)) (not wilds?)) single-static-path-router
|
||||
conflicting linear-router
|
||||
(not wilds?) lookup-router
|
||||
all-wilds? prefix-tree-router
|
||||
all-wilds? segment-router
|
||||
:else mixed-router)]
|
||||
|
||||
(when-let [conflicts (:conflicts opts)]
|
||||
|
|
|
|||
|
|
@ -34,8 +34,9 @@
|
|||
(defn wild-or-catch-all-param? [x]
|
||||
(boolean (or (wild-param x) (catch-all-param x))))
|
||||
|
||||
(defn segments [^String path]
|
||||
(into [] (.split path "/" 666)))
|
||||
(defn segments [path]
|
||||
#?(:clj (.split ^String path "/" 666)
|
||||
:cljs (.split path #"/" 666)))
|
||||
|
||||
(defn contains-wilds? [path]
|
||||
(boolean (some wild-or-catch-all-param? (segments path))))
|
||||
|
|
|
|||
|
|
@ -48,8 +48,8 @@
|
|||
(insert segment p data))
|
||||
nil paths))
|
||||
|
||||
(defn lookup [segment ^String path]
|
||||
(-lookup segment (.split path "/") {}))
|
||||
(defn lookup [segment path]
|
||||
(-lookup segment (impl/segments path) {}))
|
||||
|
||||
(comment
|
||||
(-> [["/:abba" 1]
|
||||
|
|
|
|||
|
|
@ -45,20 +45,27 @@
|
|||
#"^missing path-params for route /api/ipa/:size -> \#\{:size\}$"
|
||||
(r/match-by-name! router ::beer))))))
|
||||
|
||||
;; TODO
|
||||
(testing "complex"
|
||||
(let [router (r/router
|
||||
[["/:abba" ::abba]
|
||||
["/abba/1" ::abba1]
|
||||
["/abba/1" ::abba2]
|
||||
["/:jabba/2" ::jabba2]
|
||||
["/:abba/:dabba/doo" ::doo]
|
||||
["/abba/:dabba/boo" ::boo]] {:router r})
|
||||
["/abba/:dabba/boo" ::boo]
|
||||
#_["/:jabba/:dabba/:doo/*foo" ::wild]]
|
||||
{:router r})
|
||||
matches #(-> router (r/match-by-path %) :data :name)]
|
||||
(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 (= ::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/prefix-tree-router :prefix-tree-router
|
||||
#_#_r/prefix-tree-router :prefix-tree-router
|
||||
r/segment-router :segment-router
|
||||
r/mixed-router :mixed-router))
|
||||
|
||||
|
|
|
|||
9
test/cljc/reitit/impl_test.cljc
Normal file
9
test/cljc/reitit/impl_test.cljc
Normal 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/")))))
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
(:require [doo.runner :refer-macros [doo-tests]]
|
||||
reitit.coercion-test
|
||||
reitit.core-test
|
||||
reitit.impl-test
|
||||
reitit.middleware-test
|
||||
reitit.ring-test
|
||||
#_reitit.spec-test))
|
||||
|
|
@ -10,6 +11,7 @@
|
|||
|
||||
(doo-tests 'reitit.coercion-test
|
||||
'reitit.core-test
|
||||
'reitit.impl-test
|
||||
'reitit.middleware-test
|
||||
'reitit.ring-test
|
||||
#_'reitit.spec-test)
|
||||
|
|
|
|||
Loading…
Reference in a new issue