M test/suite001.janet => test/suite001.janet +6 -7
@@ 1,5 1,7 @@
(use spork/test)
-(use trolley)
+(use /trolley)
+
+(start-suite 0)
(def config "Routes' config for all tests"
{"/" :root
@@ 7,21 9,18 @@
"/real-thing.json" :real-thing
"/involved/:id/example/:example-id/detail/:detail-id" :involved})
-
-(start-suite 0)
-
(assert
(compile-routes config)
"compile routes")
(assert
(all |(= :core/peg (type $))
- (keys (compile-routes config)))
- "keys pegs")
+ (map first (compile-routes config)))
+ "items last pegs")
(assert
(deep=
- (sort (values (compile-routes config)))
+ (sort (map last (compile-routes config)))
(sort @[:home :real-thing :involved :root]))
"all values")
A test/suite002.janet => test/suite002.janet +14 -0
@@ 0,0 1,14 @@
+(use spork/test)
+(use /trolley)
+
+(start-suite 1)
+
+(def config
+ {"/stats/download.css" :download
+ "/stats/:interval" :interval})
+(assert
+ (deep= (lookup (compile-routes config) "/stats/download.css")
+ [:download @{}])
+ "lookup download")
+
+(end-suite)
M trolley.janet => trolley.janet +10 -7
@@ 15,8 15,11 @@
(def grammar
"PEG grammar to match routes with"
(peg/compile
- {:sep sep :pref pref :path ~(some ,chars)
- :param '(* :pref :path) :capture-path '(<- :path)
+ {:sep sep
+ :pref pref
+ :path ~(some ,chars)
+ :param '(* :pref :path)
+ :capture-path '(<- :path)
:main ~(some (* :sep
(+ (if :param ,(<-: :param :pref :capture-path))
(if :path ,(<-: :path :capture-path))
@@ 46,16 49,16 @@
(defn compile-routes
"Compiles PEG grammar for all routes"
[routes]
- (let [res @{}]
- (loop [[route action] :pairs routes]
- (when (string? route) (put res (compile-route route) action)))
- res))
+ (def res @[])
+ (loop [[route action] :pairs routes]
+ (when (string? route) (array/push res [(compile-route route) action])))
+ res)
(defn lookup
"Looks up uri in routes and returns action and params for the matched route"
[compiled-routes uri]
(var matched [])
- (loop [[grammar action] :pairs compiled-routes :while (empty? matched)]
+ (loop [[grammar action] :in compiled-routes :while (empty? matched)]
(when-let [args (extract-args grammar uri)] (set matched [action args])))
matched)