From 554e69d914f46405ff104fd1e898c9292428d9b1 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Fri, 20 Jul 2018 12:10:51 +0200 Subject: [PATCH] add tests for routing --- src/wanijo/framework/routing.clj | 19 +++++++++++-------- test/wanijo/framework/routing_test.clj | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 test/wanijo/framework/routing_test.clj diff --git a/src/wanijo/framework/routing.clj b/src/wanijo/framework/routing.clj index 9af06d3..5c31ee4 100644 --- a/src/wanijo/framework/routing.clj +++ b/src/wanijo/framework/routing.clj @@ -30,20 +30,23 @@ :params params :id id})))))) +(defn parse-path [path params] + (reduce-kv + (fn [new-path param-id param-value] + (string/replace + new-path + (str ":" (name param-id)) + (str param-value))) + path + params)) + (defn path ([id] (path id {})) ([id params] (let [path (id @all-routes)] (check-params id path params) - (reduce-kv - (fn [new-path param-id param-value] - (string/replace - new-path - (str ":" (name param-id)) - param-value)) - path - params)))) + (parse-path path params)))) (defn raw-path [id] (id @all-routes)) diff --git a/test/wanijo/framework/routing_test.clj b/test/wanijo/framework/routing_test.clj new file mode 100644 index 0000000..acfc5b3 --- /dev/null +++ b/test/wanijo/framework/routing_test.clj @@ -0,0 +1,17 @@ +(ns wanijo.framework.routing-test + (:require [clojure.test :refer :all] + [wanijo.framework.routing :refer [parse-path]])) + +(deftest test-parse-path + (testing "no params in path" + (is (= (parse-path "a" {}) "a")) + (is (= (parse-path "/a/b" {}) "/a/b"))) + (testing "params are replaced" + (is (= (parse-path "/a/:b" {:b 1}) "/a/1")) + (is (= (parse-path "/a/:b" {:b "1"}) "/a/1")) + (is (= (parse-path ":b" {:b 1}) "1")) + (is (= (parse-path "/a/:b/c" {:b 1}) "/a/1/c"))) + (testing "additional params are ignored" + (is (= (parse-path ":a" {:a 1 :b 2}) "1"))) + (testing "all params are required" + (is (thrown? RuntimeException (parse-path ":a")))))