update tests

This commit is contained in:
Gered 2014-06-19 08:26:19 -04:00
parent 941c0ad2e2
commit 0aeae17824
3 changed files with 28 additions and 35 deletions

View file

@ -167,15 +167,15 @@
(set-options! :auto-convert-map-keywords true)
(is (= (render "{{keys_are_all_keywords(x)}}" {:x {:a "foo" :b "bar" :c "baz"}})
"true"))
"1"))
(is (= (render "{{keys_are_all_keywords(x)}}" {:x {"a" "foo" "b" "bar" "c" "baz"}})
"true"))
"1"))
(set-options! :auto-convert-map-keywords false)
(is (= (render "{{keys_are_all_keywords(x)}}" {"x" {:a "foo" :b "bar" :c "baz"}})
"true"))
"1"))
(is (= (render "{{keys_are_all_strings(x)}}" {"x" {"a" "foo" "b" "bar" "c" "baz"}})
"true"))
"1"))
(set-options! :auto-convert-map-keywords true)
(reset-functions!))))
@ -196,15 +196,15 @@
(set-options! :auto-convert-map-keywords true)
(is (= (render "{{keys_are_all_keywords(get_map_with_keywords(null))}}" {})
"true"))
"1"))
(is (= (render "{{keys_are_all_keywords(get_map_with_strings(null))}}" {})
"true"))
"1"))
(set-options! :auto-convert-map-keywords false)
(is (= (render "{{keys_are_all_keywords(get_map_with_keywords(null))}}" {})
"true"))
"1"))
(is (= (render "{{keys_are_all_strings(get_map_with_strings(null))}}" {})
"true"))
"1"))
(set-options! :auto-convert-map-keywords true)
(reset-functions!))))

View file

@ -20,9 +20,8 @@
{:name "Bob"})
"Hello Bob!")
"passing a model-map")
(is (= (render "Hello {{ name }}!"
nil)
"Hello null!")
(is (= (render "Hello {{ name }}!")
"Hello !")
"not passing a model-map")
(is (= (render "Hello {{ name }}!"
{"name" "Bob"})
@ -60,7 +59,7 @@
"passing an integer")
(is (= (render "null {{ x }}"
{:x nil})
"null null")
"null ")
"passing a nil value")
(is (= (render "char {{ x }}"
{:x \a})
@ -108,9 +107,8 @@
(render-file invalid-filename
{:name "Bob"}))
"trying to render a file that doesn't exist")
(is (= (render-file test-filename
nil)
"Hello null from a file!")
(is (= (render-file test-filename)
"Hello from a file!")
"not passing a model-map")
(is (= (render-file test-filename
{"name" "Bob"})

View file

@ -1,15 +1,12 @@
(ns clj-jtwig.functions-test
(:import (com.lyncode.jtwig.functions.repository CallableFunction))
(:require [clojure.test :refer :all]
[clj-jtwig.core :refer :all]
[clj-jtwig.functions :refer :all]))
; TODO: is there a better way to test that something is an instance of some object generated by reify?
(defn valid-function-handler? [x]
(and (not (nil? x))
(-> x
(class)
(.getName)
(.startsWith "clj_jtwig.functions$make_function_handler"))))
(instance? CallableFunction x)))
(deftest template-functions
(testing "Adding custom template functions"
@ -80,15 +77,14 @@
(is (= (render "{{add2(1, 2)}}" nil)
"3")
"fixed number of arguments (correct amount)")
(is (thrown-with-msg?
(is (thrown?
Exception
#"clojure\.lang\.ArityException: Wrong number of args"
(render "{{add2(1)}}" nil)))
(is (= (render "{{addAll(1, 2, 3, 4, 5)}}" nil)
"15")
"variable number of arguments (non-zero)")
(is (= (render "{{addAll}}" nil)
"null")
"")
"variable number of arguments (zero)")
(reset-functions!)))
@ -177,7 +173,7 @@
"a")
"char via model-map")
(is (= (render "{{identity(x)}}" {:x true})
"true")
"1")
"boolean via model-map")
(is (= (render "{{identity(x)}}" {:x '(1 2 3 4 5)})
"[1, 2, 3, 4, 5]")
@ -209,7 +205,7 @@
"a")
"char via constant value embedded in the template")
(is (= (render "{{identity(true)}}" nil)
"true")
"1")
"boolean via constant value embedded in the template")
(is (= (render "{{identity([1, 2, 3, 4, 5])}}" nil)
"[1, 2, 3, 4, 5]")
@ -232,7 +228,7 @@
"vector (iterating over a model-map var passed to a function and returned from it)")
; TODO: order of iteration through a map is undefined, the string being tested may not always be the same (wrt. order)
(is (= (render "{% for k, v in identity(x) %}{{k}}: {{v}} {% endfor %}" {:x {:a 1 :b "foo" :c nil}})
"b: foo c: null a: 1 ")
"b: foo c: a: 1 ")
"map (iterating over a model-map var passed to a function and returned from it)")
(is (= (render "{% for i in identity(x) %}{{i}} {% endfor %}" {:x #{1 2 3 4 5}})
"1 4 3 2 5 ")
@ -248,7 +244,7 @@
"list by comprehension (iterating over a model-map var passed to a function and returned from it)")
; TODO: order of iteration through a map is undefined, the string being tested may not always be the same (wrt. order)
(is (= (render "{% for k, v in identity({a: 1, b: 'foo', c: null}) %}{{k}}: {{v}} {% endfor %}" nil)
"b: foo c: null a: 1 ")
"b: foo c: a: 1 ")
"map (iterating over a model-map var passed to a function and returned from it)")
(reset-functions!))))
@ -295,17 +291,17 @@
(testing "contains"
(is (= (render "{{ {a: 1, b: 2, c: 3}|contains(\"b\") }}" nil)
"true"))
"1"))
(is (= (render "{{ {a: 1, b: 2, c: 3}|contains(\"d\") }}" nil)
"false"))
"0"))
(is (= (render "{{ [1, 2, 3, 4]|contains(2) }}" nil)
"true"))
"1"))
(is (= (render "{{ [1, 2, 3, 4]|contains(5) }}" nil)
"false"))
"0"))
(is (= (render "{{ \"abcdef\"|contains(\"abc\") }}" nil)
"true"))
"1"))
(is (= (render "{{ \"abcdef\"|contains(\"xyz\") }}" nil)
"false")))
"0")))
(testing "dump"
(is (= (render "{{ a|dump }}" {:a [{:foo "bar"} [1, 2, 3] "hello"]})
@ -354,9 +350,8 @@
(testing "nth"
(is (= (render "{{ [1, 2, 3, 4, 5]|nth(2) }}" nil)
"3"))
(is (thrown-with-msg?
(is (thrown?
Exception
#"java.lang.IndexOutOfBoundsException"
(render "{{ [1, 2, 3, 4, 5]|nth(6) }}" nil)))
(is (= (render "{{ [1, 2, 3, 4, 5]|nth(6, \"not found\") }}" nil)
"not found")))