Commit c602054f by Chris Jerdonek

Spec test display format: cherry-pick from '4eaa43b5'

Improved display format of spec tests and fixed partials typo.
parent e06bed51
# coding: utf-8
"""
Tests the mustache spec test cases.
"""
import glob import glob
import os.path import os.path
import pystache
from pystache import Loader
import unittest import unittest
import yaml import yaml
import pystache
from pystache import Loader
def code_constructor(loader, node): def code_constructor(loader, node):
value = loader.construct_mapping(node) value = loader.construct_mapping(node)
return eval(value['python'], {}) return eval(value['python'], {})
...@@ -17,7 +26,13 @@ specs = glob.glob(os.path.join(specs, '*.yml')) ...@@ -17,7 +26,13 @@ specs = glob.glob(os.path.join(specs, '*.yml'))
class MustacheSpec(unittest.TestCase): class MustacheSpec(unittest.TestCase):
pass pass
def buildTest(testData, spec): def buildTest(testData, spec_filename):
name = testData['name']
description = testData['desc']
test_name = "%s (%s)" % (name, spec_filename)
def test(self): def test(self):
template = testData['template'] template = testData['template']
partials = testData.has_key('partials') and testData['partials'] or {} partials = testData.has_key('partials') and testData['partials'] or {}
...@@ -32,19 +47,29 @@ def buildTest(testData, spec): ...@@ -32,19 +47,29 @@ def buildTest(testData, spec):
p = open(files[-1], 'w') p = open(files[-1], 'w')
p.write(partials[key]) p.write(partials[key])
p.close() p.close()
self.assertEquals(pystache.render(template, data), expected) actual = pystache.render(template, data).encode('utf-8')
message = """%s
Template: \"""%s\"""
Expected: %s
Actual: %s""" % (description, template, repr(expected), repr(actual))
self.assertEquals(actual, expected, message)
finally: finally:
[os.remove(f) for f in files] [os.remove(f) for f in files]
test.__doc__ = testData['desc'] # The name must begin with "test" for nosetests test discovery to work.
test.__name__ = 'test %s (%s)' % (testData['name'], spec) test.__name__ = 'test: "%s"' % test_name
return test return test
for spec in specs: for spec in specs:
name = os.path.basename(spec).replace('.yml', '') file_name = os.path.basename(spec)
for test in yaml.load(open(spec))['tests']: for test in yaml.load(open(spec))['tests']:
test = buildTest(test, name) test = buildTest(test, file_name)
setattr(MustacheSpec, test.__name__, test) setattr(MustacheSpec, test.__name__, test)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment