diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-02 22:58:59 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-02 22:58:59 -0700 |
commit | 09906e25d2df663db3e1d80fc676768e17630f96 (patch) | |
tree | 63ed4c42e999b08be45dfe3b70786bfa2d412ae7 | |
parent | 2e7942cf0652098b067da7eb16b9953754a85af4 (diff) | |
download | pystache-09906e25d2df663db3e1d80fc676768e17630f96.tar.gz |
Spec tests now work with Python 2.4: code now fully works with Python 2.4
-rw-r--r-- | pystache/commands.py | 3 | ||||
-rw-r--r-- | tests/test_spec.py | 60 |
2 files changed, 41 insertions, 22 deletions
diff --git a/pystache/commands.py b/pystache/commands.py index 333e665..1801d40 100644 --- a/pystache/commands.py +++ b/pystache/commands.py @@ -11,7 +11,8 @@ Run this script using the -h option for command-line help. try: import json except: - # For Python 2.5 support: the json module is new in version 2.6. + # The json module is new in Python 2.6, whereas simplejson is + # compatible with earlier versions. import simplejson as json # The optparse module is deprecated in Python 2.7 in favor of argparse. diff --git a/tests/test_spec.py b/tests/test_spec.py index a379f24..02f6080 100644 --- a/tests/test_spec.py +++ b/tests/test_spec.py @@ -3,32 +3,28 @@ """ Creates a unittest.TestCase for the tests defined in the mustache spec. -We did not call this file something like "test_spec.py" to avoid matching -nosetests's default regular expression "(?:^|[\b_\./-])[Tt]est". -This allows us to exclude the spec test cases by default when running -nosetests. To include the spec tests, one can use the following option, -for example-- +""" - nosetests -i spec +# TODO: this module can be cleaned up somewhat. -""" +try: + # We deserialize the json form rather than the yaml form because + # json libraries are available for Python 2.4. + import json +except: + # The json module is new in Python 2.6, whereas simplejson is + # compatible with earlier versions. + import simplejson as json import glob import os.path import unittest -import yaml from pystache.renderer import Renderer -def code_constructor(loader, node): - value = loader.construct_mapping(node) - return eval(value['python'], {}) - -yaml.add_constructor(u'!code', code_constructor) - -specs = os.path.join(os.path.dirname(__file__), '..', 'ext', 'spec', 'specs') -specs = glob.glob(os.path.join(specs, '*.yml')) +root_path = os.path.join(os.path.dirname(__file__), '..', 'ext', 'spec', 'specs') +spec_paths = glob.glob(os.path.join(root_path, '*.json')) class MustacheSpec(unittest.TestCase): pass @@ -46,8 +42,16 @@ def buildTest(testData, spec_filename): expected = testData['expected'] data = testData['data'] + # Convert code strings to functions. + # TODO: make this section of code easier to understand. + new_data = {} + for key, val in data.iteritems(): + if isinstance(val, dict) and val.get('__tag__') == 'code': + val = eval(val['python']) + new_data[key] = val + renderer = Renderer(partials=partials) - actual = renderer.render(template, data) + actual = renderer.render(template, new_data) actual = actual.encode('utf-8') message = """%s @@ -64,14 +68,28 @@ def buildTest(testData, spec_filename): self.assertEquals(actual, expected, message) # The name must begin with "test" for nosetests test discovery to work. - test.__name__ = 'test: "%s"' % test_name + name = 'test: "%s"' % test_name + + # If we don't convert unicode to str, we get the following error: + # "TypeError: __name__ must be set to a string object" + test.__name__ = str(name) return test -for spec in specs: - file_name = os.path.basename(spec) +for spec_path in spec_paths: + + file_name = os.path.basename(spec_path) + + # We avoid use of the with keyword for Python 2.4 support. + f = open(spec_path, 'r') + try: + spec_data = json.load(f) + finally: + f.close() + + tests = spec_data['tests'] - for test in yaml.load(open(spec))['tests']: + for test in tests: test = buildTest(test, file_name) setattr(MustacheSpec, test.__name__, test) # Prevent this variable from being interpreted as another test. |