From: noah morrison Date: Sun, 16 Nov 2014 20:31:50 +0000 (-0500) Subject: Expanded unittest coverage to 100% X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=2838b06c42d6c9c534ab6b593105fa0a1bd84d89;p=chevron.git Expanded unittest coverage to 100% Using https://pypi.python.org/pypi/coverage --- diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..fe258c6 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[report] +exclude_lines = + if __name__ == .__main__.: diff --git a/entei.py b/entei.py index ff9b944..6c09427 100755 --- 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 index 0000000..437acdf --- /dev/null +++ b/test.rendered @@ -0,0 +1,105 @@ + +variable test +=== +test +=== +test +=== + +comment test +=== +=== +=== + +html escape test (triple brackets) +=== +< > & " +=== +< > & " +=== + +html escape test (ampersand) +=== +< > & " +=== +< > & " +=== + +html escape test (normal) +=== +< > & " +=== +< > & " +=== + +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 +=== diff --git a/test_spec.py b/test_spec.py index d45fd87..1d37a40 100755 --- a/test_spec.py +++ b/test_spec.py @@ -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()