From 0aeae17824352bb4b80fd9c5374f90b46f7a9ed5 Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 19 Jun 2014 08:26:19 -0400 Subject: [PATCH] update tests --- test/clj_jtwig/convert_test.clj | 16 +++++++------- test/clj_jtwig/core_test.clj | 12 +++++------ test/clj_jtwig/functions_test.clj | 35 +++++++++++++------------------ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/test/clj_jtwig/convert_test.clj b/test/clj_jtwig/convert_test.clj index 8df0738..230c5d8 100644 --- a/test/clj_jtwig/convert_test.clj +++ b/test/clj_jtwig/convert_test.clj @@ -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!)))) diff --git a/test/clj_jtwig/core_test.clj b/test/clj_jtwig/core_test.clj index 63fc29d..d9f0d64 100644 --- a/test/clj_jtwig/core_test.clj +++ b/test/clj_jtwig/core_test.clj @@ -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"}) diff --git a/test/clj_jtwig/functions_test.clj b/test/clj_jtwig/functions_test.clj index c878ded..40380e5 100644 --- a/test/clj_jtwig/functions_test.clj +++ b/test/clj_jtwig/functions_test.clj @@ -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")))