Selmer: util/set-missing-value-formatter!

This commit is contained in:
Michiel Borkent 2021-05-11 22:13:50 +02:00
parent 00fd7820c3
commit 5554619a9d
3 changed files with 22 additions and 7 deletions

View file

@ -5,7 +5,8 @@
[selmer.filters :as filters]
[selmer.parser]
[selmer.tags :as tags]
[selmer.util :as util]))
[selmer.util :as util]
[selmer.validator :as validator]))
(def spns (sci/create-ns 'selmer.parser nil))
@ -72,8 +73,6 @@
(def sfns (sci/create-ns 'selmer.filters nil))
(def selmer-filters-ns (sci/create-ns 'selmer.filters sfns))
(def selmer-filters-namespace
{'add-filter! (sci/copy-var filters/add-filter! sfns)
'remove-filter! (sci/copy-var filters/remove-filter! sfns)})
@ -97,4 +96,10 @@
'turn-on-escaping! (sci/copy-var turn-on-escaping! suns)
'*escape-variables* escape-variables
'with-escaping (sci/copy-var with-escaping suns)
'without-escaping (sci/copy-var without-escaping suns)})
'without-escaping (sci/copy-var without-escaping suns)
'set-missing-value-formatter! (sci/copy-var util/set-missing-value-formatter! suns)})
(def svns (sci/create-ns 'selmer.validator nil))
(def selmer-validator-namespace
{'validate-off! (sci/copy-var validator/validate-off! svns)})

View file

@ -392,7 +392,9 @@ Use bb run --help to show this help output.
'selmer.filters
@(resolve 'babashka.impl.selmer/selmer-filters-namespace)
'selmer.util
@(resolve 'babashka.impl.selmer/selmer-util-namespace))))
@(resolve 'babashka.impl.selmer/selmer-util-namespace)
'selmer.validator
@(resolve 'babashka.impl.selmer/selmer-validator-namespace))))
(def imports
'{ArithmeticException java.lang.ArithmeticException

View file

@ -2,7 +2,8 @@
"Some additional tests we added ourselves"
(:require [clojure.test :as t :refer [deftest is testing]]
[selmer.parser :as selmer]
[selmer.util :as util]))
[selmer.util :as util]
[selmer.validator :as validator]))
(deftest escaping-test
(testing "escaping by default"
@ -16,5 +17,12 @@
(is (= "&foo" (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(testing "macros"
(is (= "&foo" (util/without-escaping (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))
(is (= "&foo" (util/with-escaping (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2}))))))
(is (= "&foo" (util/with-escaping (selmer/render "{% firstof foo bar %}" {:foo "&foo" :bar 2})))))
(testing "missing value"
(util/set-missing-value-formatter! (constantly "<missing>"))
(is (= "<missing>" (selmer/render "{{ foo }}" {}))))
#_(testing "validator off"
(is (thrown? Exception (selmer/render "{% if foo %} yeah " {:foo true})))
(validator/validate-off!)
(is (selmer/render "{% if foo %} yeah " {:foo true}))))