neman / neman / web / tests / test.clj

;; Copyright (c) 2008 Krešimir Šojat. All rights reserved.  The use and
;; distribution terms for this software are covered by the Common
;; Public License 1.0 (
;; which can be found in the file CPL.TXT at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.

(ns user
    (net.ksojat.neman [web :as web]))
  (:use clojure.contrib.test-is))

(deftest web-convertor
  (is (thrown? Exception (web/convertor :default))))

(deftest web-is-view
  (let [v (web/view [] "test view")]
    (are (= _1 _2)
      (web/view? v)  true
      (web/view? {}) false)))

(deftest web-call-view
  (let [v (web/view [a b] "test view")]
    (are (thrown? _1 _2)
      Exception (web/call-view v {'a 1})))
  (let [v (web/partial (web/view [x] "test view") {'x 100})]
    (is (thrown? Exception (web/call-view v {'x 200})))))

(deftest web-partial-view
  (let [v0 (web/view [a] "test view")
        v1 (web/partial v0 {'a 100})]
    (is (thrown? Exception (web/partial v1 {'a 200}))
    (is (thrown? Exception (web/partial v1 {'b 100}))))))

(deftest web-rule-parsing
  (are (= _1 _2)
    (web/rule-parts "/index/<name>") '(["/index/" "name"])
    (web/rule-parts "/show-page")    '(["/show-page" nil])
    (web/rule-parts "<name>")        '(["" "name"])
    (web/rule-parts "<name")         nil
    (web/rule-parts "name>")         nil))

(deftest web-rule-regex
  (are (= _1 _2)
    (web/rule-regex "/login" {}) #"/login"))

(deftest web-rule-names
  (are (= _1 _2)
    ('a 'b) (web/rule-names "<a><b>")
    '()     (web/rule-names "/show")
    '()     (web/rule-names "")))

(deftest web-absolute-url-for
  (binding [web/*rules* {[:test :view] "/test/view/<a>"}
            web/*paths* {:test {:view (web/view [a] "hi")}}]
    ; TODO