]> Devi Nivas Git - chevron.git/commitdiff
Added unit tests
authornoah morrison <noah@morrison.ph>
Sat, 1 Nov 2014 19:43:19 +0000 (15:43 -0400)
committernoah morrison <noah@morrison.ph>
Sat, 1 Nov 2014 19:43:19 +0000 (15:43 -0400)
.gitmodules [new file with mode: 0644]
spec [new submodule]
test_spec.py [new file with mode: 0755]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..4729540
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "spec"]
+       path = spec
+       url = https://github.com/mustache/spec
diff --git a/spec b/spec
new file mode 160000 (submodule)
index 0000000..934db98
--- /dev/null
+++ b/spec
@@ -0,0 +1 @@
+Subproject commit 934db98d8ba854574d18aa59a01d2c9aee41944d
diff --git a/test_spec.py b/test_spec.py
new file mode 100755 (executable)
index 0000000..661c5ce
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+import unittest
+import os
+import json
+
+from chevron import render
+
+SPECS_PATH = os.path.join('spec', 'specs')
+SPECS = [path for path in os.listdir(SPECS_PATH) if path.endswith('.json')]
+STACHE = render
+
+
+def _test_case_from_path(json_path):
+    class MustacheTestCase(unittest.TestCase):
+        """A simple yaml based test case"""
+        def _test_from_object(obj):
+            """Generate a unit test from a test object"""
+            def test_case(self):
+                """A simple test case"""
+                self.assertEqual(STACHE(obj['template'], obj['data']),
+                                 obj['expected'])
+            return test_case
+        with open(json_path, 'r') as f:
+            yaml = json.load(f)
+        # Generates a unit test for each test object
+        for test in yaml['tests']:
+            vars()['test_'+test['name']] = _test_from_object(test)
+    # Return the built class
+    return MustacheTestCase
+
+# Create TestCase for each json file
+for spec in SPECS:
+    # Ignore optional tests
+    if spec[0] is not '~':
+        globals()[spec] = _test_case_from_path(os.path.join(SPECS_PATH, spec))
+
+# Run unit tests from command line
+if __name__ == "__main__":
+    unittest.main()