Commits

kumarshantanu  committed 0000d57

add get-new and reset-view! functions
add unit tests for get-new and reset-view!
update CHANGES to reflect new functions
update pom.xml with 0.2-SNAPSHOT version
cleanup runtests.clj script

  • Participants
  • Parent commits dba0861

Comments (0)

Files changed (5)

 
 # Changes and TODO
 
+## 0.2 / 2010-Sep-?? (GMT+5:30)
+
+- Create new view template: empty or with supplied template string
+- Reset view template with supplied template string
+
 ## 0.1 / 2010-Sep-04 (GMT+5:30)
 
 - Load view templates from classpath and directory
   <groupId>org.bituf</groupId>
   <artifactId>clj-stringtemplate</artifactId>
   <packaging>jar</packaging>
-  <version>0.1</version>
+  <version>0.2-SNAPSHOT</version>
   <name>clj-stringtemplate</name>
   <description>Clj-Stringtemplate is a Clojure wrapper for the StringTemplate Java library</description>
   <url>http://code.google.com/p/bitumenframework/</url>

File src/main/clj/org/bituf/clj_stringtemplate.clj

   (:import org.antlr.stringtemplate.StringTemplate)
   (:use org.bituf.clj-stringtemplate.internal))
 
+
+(defn get-new "Return new empty view template - useful as mentioned here:
+  http://hardlikesoftware.com/weblog/2006/12/12/using-json-with-stringtemplate/"
+  ([]
+    (StringTemplate.))
+  ([^String view]
+    (StringTemplate. view)))
+
+
 (defn get-view-from-classpath "Return the view template from classpath"
   [^String view-name]
   (let [st-group (StringTemplateGroup. "default")]
     (.getInstanceOf st-group view-name)))
 
+
 (defn get-view-from-dir "Return the view template from specified directory"
   [^String view-name ^String root-dir]
   (let [st-group (StringTemplateGroup. "default" root-dir)]
     (.getInstanceOf st-group view-name)))
 
+
+(defn reset-view! "Reset view template with supplied content"
+  [^StringTemplate view ^String template]
+  (.setTemplate view template))
+
+
 (defn fill-view! "Fill view template with key/value pairs"
   ;;;
   ;; Fill template with key and value
     (.setAttributes template (kv-to-sv kv-map))
     template))
 
+
 (defn render-view "Return rendered view for the template"
   [^StringTemplate template]
   (.toString template))

File src/test/clj/org/bituf/test_clj_stringtemplate.clj

   `(testing ~label ~@body))
 
 
+(deftest test-get-new
+  (now-testing "Get new view"
+    (fail-on-exception
+      (let [new-empty (get-new)
+            new-templ (get-new "Hello $who$!")
+            _         (fill-view! new-templ "who" "World")
+            newtemp-r (render-view new-templ)]
+        (is (not-nil? new-empty))
+        (is (not-nil? new-templ))
+        (is (= "Hello World!" newtemp-r))))))
+
+
 (deftest test-get-view-from-classpath
   (now-testing "Load view from classpath"
     (fail-on-exception
         (is (not-nil? foo))))))
 
 
+(deftest test-reset-view
+  (now-testing "Reset view"
+    (fail-on-exception
+      (let [template  "Hello $who$!"
+            new-empty (get-new)
+            new!empty (get-new "Some stuff")
+            from-clp  (get-view-from-classpath "st/foo")
+            from-dir  (get-view-from-dir "st/foo" "src/test/clj")
+            allviews  [new-empty new!empty from-clp from-dir]
+            _ (map #(reset-view! % template)     allviews)
+            _ (map #(fill-view! % "who" "World") allviews)]
+        (doseq [each allviews]
+          (= "Hello World!" (render-view each)))))))
+
+
 (deftest test-populate-attr-values-in-view
   (now-testing "Populate attribute key-values in view"
     (fail-on-exception
   (test-get-view-from-classpath)
   (test-get-view-from-dir)
   (test-populate-attr-values-in-view)
-  (test-render-view))
+  (test-render-view)
+  (test-get-new)
+  (test-reset-view))

File src/test/script/runtests.clj

 
 (run-tests
   'org.bituf.test-clj-stringtemplate)
-
-;(binding [org.bituf.sqlrat.entity/*show-sql* true
-;          org.bituf.sqlrat.entity/*show-sql-results* true]
-;  (run-tests
-;    'org.bituf.sqlrat.test.dbblog))
-