Source

Clj-StringTemplate / src / test / clj / org / bituf / test_clj_stringtemplate.clj

Full commit
(ns org.bituf.test-clj-stringtemplate
  (:use org.bituf.clj-stringtemplate)
  (:use clojure.test))


(def who "Miguel")
(def names ["Eugenia" "Matt" "Derek"])

(defn not-nil? "Tells whether an object is not-nil" [obj] (not (nil? obj)))

(defn fail "Fails a test, with message" [msg] (println msg) (is false))

(defmacro fail-on-exception [& body]
  `(try ~@body
    (catch Exception ~'nee
      (.printStackTrace ~'nee)
      (fail (.getMessage ~'nee)))))

(defmacro now-testing [label & body]
  (println "Now testing: " label)
  `(testing ~label ~@body))


(deftest test-get-view-from-classpath
  (now-testing "Load view from classpath"
    (fail-on-exception
      (let [hello (get-view-from-classpath "hello")
            foo   (get-view-from-classpath "st/foo")]
        (is (not-nil? hello))
        (is (not-nil? foo))))))


(deftest test-get-view-from-dir
  (now-testing "Load view from directory"
    (fail-on-exception
      (let [hello (get-view-from-dir "hello"  "src/test/clj")
            foo   (get-view-from-dir "st/foo" "src/test/clj")]
        (is (not-nil? hello))
        (is (not-nil? foo))))))


(deftest test-populate-attr-values-in-view
  (now-testing "Populate attribute key-values in view"
    (fail-on-exception
      (let [hello (get-view-from-classpath "hello")
            _ (fill-view! hello "who" who)
            foo   (get-view-from-classpath "st/foo")
            _ (fill-view! foo "names" names)]
        (is (not-nil? hello))
        (is (not-nil? foo)))))
  (now-testing "Populate attribute key/value map in view"
    (fail-on-exception
      (let [hello (get-view-from-classpath "hello")
            _ (fill-view! hello {"who" who})
            foo   (get-view-from-classpath "st/foo")
            _ (fill-view! foo {"names" names})]
        (is (not-nil? hello))
        (is (not-nil? foo))))))


(deftest test-render-view
  (now-testing "Render view"
    (fail-on-exception
      (let [hello (get-view-from-classpath "hello")
            _ (fill-view! hello "who" who)
            hello-r (render-view hello)
            foo   (get-view-from-classpath "st/foo")
            _ (fill-view! foo "names" names)
            foo-r (render-view foo)]
        (is (= hello-r (str "hello " who "!")))
        (is (= foo-r (apply str "foobar "
                       (interpose "\n" (map #(str "baz " % "\n") names)))))))))

(deftest test-kv-to-sv
  (now-testing "Transform Keyword-key/Value map to String-key/value map"
    (is (= {"a" 10 "b" 20 "c" 30}
          (kv-to-sv {:a 10 :b 20 :c 30})))
    (is (= {"a" 10 "b" 20 "c" 30}
          (kv-to-sv {:a 10 "b" 20 :c 30})))))

(defn test-ns-hook []
  (test-get-view-from-classpath)
  (test-get-view-from-dir)
  (test-populate-attr-values-in-view)
  (test-render-view)
  (test-kv-to-sv))