]> Devi Nivas Git - chevron.git/commitdiff
Expanded unittest coverage to 100%
authornoah morrison <noah@morrison.ph>
Sun, 16 Nov 2014 20:31:50 +0000 (15:31 -0500)
committernoah morrison <noah@morrison.ph>
Sun, 16 Nov 2014 20:31:50 +0000 (15:31 -0500)
Using https://pypi.python.org/pypi/coverage

.coveragerc [new file with mode: 0644]
entei.py
test.rendered [new file with mode: 0644]
test_spec.py

diff --git a/.coveragerc b/.coveragerc
new file mode 100644 (file)
index 0000000..fe258c6
--- /dev/null
@@ -0,0 +1,3 @@
+[report]
+exclude_lines =
+    if __name__ == .__main__.:
index ff9b944b44bb623f6e21de86af19c293d1883348..6c09427c51ae4eeac97c41528657c72e80841657 100755 (executable)
--- a/entei.py
+++ b/entei.py
@@ -258,7 +258,9 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
             try:
                 # Maybe it's in the file system
                 path = partials_path + '/' + name + '.' + partials_ext
-                return open(path, 'r')
+                with open(path, 'r') as partial:
+                    return partial.read()
+
             except IOError:
                 # Alright I give up on you
                 return ''
@@ -376,11 +378,18 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
 
     return output
 
-if __name__ == '__main__':
-    args = {
-        'template': open(argv[2], 'r'),
-        'data': json.load(open(argv[1], 'r')),
-    }
+def main(data, template):
+    data = data
+    template = template
 
-    output = render(**args)
-    print(output)
+    with open(template, 'r') as template_file:
+        with open(data, 'r') as data_file:
+            args = {
+                'template': template_file,
+                'data': json.load(data_file)
+            }
+
+            return render(**args)
+
+if __name__ == '__main__':
+   print(main(argv[1], argv[2]))
diff --git a/test.rendered b/test.rendered
new file mode 100644 (file)
index 0000000..437acdf
--- /dev/null
@@ -0,0 +1,105 @@
+
+variable test
+===
+test
+===
+test
+===
+
+comment test
+===
+===
+===
+
+html escape test (triple brackets)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (ampersand)
+===
+< > & "
+===
+< > & "
+===
+
+html escape test (normal)
+===
+&lt; &gt; &amp; &quot;
+===
+&lt; &gt; &amp; &quot;
+===
+
+section test (truthy)
+===
+true
+===
+true
+===
+
+section test (falsy)
+===
+===
+===
+
+section test (list)
+===
+number: 1
+name: one
+---
+number: 2
+name: two
+---
+number: 3
+name: three
+---
+===
+number: 1
+name: one
+---
+number: 2
+name: two
+---
+number: 3
+name: three
+---
+===
+
+section test (scope)
+===
+test
+new test
+===
+test
+new test
+===
+
+inverted section test (truthy)
+===
+===
+===
+
+inverted section test (falsy)
+===
+false
+===
+false
+===
+
+partial test
+===
+this is a partial
+===
+this is a partial
+===
+
+delimiter test
+===
+test
+test
+===
+test
+test
+===
index d45fd87723e764ac5aad38b78fbec5a00bf841cf..1d37a403cb0f5d617cb3f129eb0d24f07e52210d 100755 (executable)
@@ -3,11 +3,11 @@ import unittest
 import os
 import json
 
-from entei import render
+import entei
 
 SPECS_PATH = os.path.join('spec', 'specs')
 SPECS = [path for path in os.listdir(SPECS_PATH) if path.endswith('.json')]
-STACHE = render
+STACHE = entei.render
 
 
 def _test_case_from_path(json_path):
@@ -46,6 +46,29 @@ for spec in SPECS:
         spec = spec.split('.')[0]
         globals()[spec] = _test_case_from_path(os.path.join(SPECS_PATH, spec))
 
+
+class ExpandedCoverage(unittest.TestCase):
+
+    def test_unclosed_sections(self):
+        test1 = {
+            'template': '{{# section }} oops {{/ wrong_section }}'
+        }
+
+        test2 = {
+            'template': '{{# section }} end of file'
+        }
+
+        self.assertRaises(entei.UnclosedSection, entei.render, **test1)
+        self.assertRaises(entei.UnclosedSection, entei.render, **test2)
+
+    def test_main(self):
+        result = entei.main('data.json', 'test.mustache')
+        with open('test.rendered', 'r') as f:
+            expected = f.read()
+
+        self.assertEqual(result, expected)
+
+
 # Run unit tests from command line
 if __name__ == "__main__":
     unittest.main()