From 543e1e27c6efe35fefb90546625f1eec4661a82e Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 5 Oct 2014 15:35:45 -0400 Subject: [PATCH] add support for adding custom template tests --- src/clj_pebble/core.clj | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/clj_pebble/core.clj b/src/clj_pebble/core.clj index 6811056..a1c4e2a 100644 --- a/src/clj_pebble/core.clj +++ b/src/clj_pebble/core.clj @@ -4,7 +4,7 @@ (com.mitchellbosecke.pebble PebbleEngine) (com.mitchellbosecke.pebble.loader ClasspathLoader FileLoader StringLoader) (com.mitchellbosecke.pebble.template PebbleTemplate) - (com.mitchellbosecke.pebble.extension Function Filter)) + (com.mitchellbosecke.pebble.extension Function Filter Test)) (:require [clojure.walk :refer [stringify-keys]])) (defonce classpath-loader (ClasspathLoader.)) @@ -39,6 +39,12 @@ (apply [_ input args] (apply f (concat [input] (get-sorted-args args)))))) +(defn make-test [f] + (reify Test + (getArgumentNames [_] nil) + (apply [_ input args] + (boolean (apply f (concat [input] (get-sorted-args args))))))) + (defmacro defpebblefn [fn-name args & body] `(let [f# (fn ~args ~@body) pebble-fn# (make-function f#)] @@ -49,6 +55,11 @@ pebble-fn# (make-filter f#)] (.put (.getFilters @engine) ~filter-name pebble-fn#))) +(defmacro defpebbletest [test-name args & body] + `(let [f# (fn ~args ~@body) + pebble-fn# (make-test f#)] + (.put (.getTests @engine) ~test-name pebble-fn#))) + (defn- prepare-context-map [context] (if context (stringify-keys context)