Commit 166f7c60 by Chris Jerdonek

Cleaned up test_mustachespec.py somewhat.

parent 17919ab6
...@@ -9,6 +9,7 @@ Creates a unittest.TestCase for the tests defined in the mustache spec. ...@@ -9,6 +9,7 @@ Creates a unittest.TestCase for the tests defined in the mustache spec.
FILE_ENCODING = 'utf-8' # the encoding of the spec test files. FILE_ENCODING = 'utf-8' # the encoding of the spec test files.
yaml = None
try: try:
# We try yaml first since it is more convenient when adding and modifying # We try yaml first since it is more convenient when adding and modifying
...@@ -37,23 +38,22 @@ import os.path ...@@ -37,23 +38,22 @@ import os.path
import unittest import unittest
import pystache import pystache
from pystache import common
from pystache.renderer import Renderer from pystache.renderer import Renderer
from pystache.tests.common import AssertStringMixin, SPEC_TEST_DIR from pystache.tests.common import AssertStringMixin, SPEC_TEST_DIR
spec_paths = glob.glob(os.path.join(SPEC_TEST_DIR, '*.%s' % file_extension)) def parse(u):
def parse(u, file_extension):
""" """
Parse
Arguments: Arguments:
u: a unicode string. u: a unicode string.
""" """
# TODO: find a cleaner mechanism for choosing between the two. # TODO: find a cleaner mechanism for choosing between the two.
if file_extension[0] == 'j': if yaml is None:
# Then json. # Then use json.
# The only way to get the simplejson module to return unicode strings # The only way to get the simplejson module to return unicode strings
# is to pass it unicode. See, for example-- # is to pass it unicode. See, for example--
...@@ -157,38 +157,14 @@ def buildTest(testData, spec_filename, parser): ...@@ -157,38 +157,14 @@ def buildTest(testData, spec_filename, parser):
return test return test
for spec_path in spec_paths: spec_paths = glob.glob(os.path.join(SPEC_TEST_DIR, '*.%s' % file_extension))
for path in spec_paths:
file_name = os.path.basename(spec_path) file_name = os.path.basename(path)
# We use codecs.open() for pre Python 2.6 support and because it ports b = common.read(path)
# correctly to Python 3: u = unicode(b, encoding=FILE_ENCODING)
# spec_data = parse(u)
# "If pre-2.6 compatibility is needed, then you should use codecs.open()
# instead. This will make sure that you get back unicode strings in Python 2."
#
# (from http://docs.python.org/py3k/howto/pyporting.html#text-files )
#
# TODO: share code here with pystache's open() code.
f = codecs.open(spec_path, 'r', encoding=FILE_ENCODING)
try:
u = f.read()
finally:
f.close()
# The only way to get the simplejson module to return unicode strings
# is to pass it unicode. See, for example--
#
# http://code.google.com/p/simplejson/issues/detail?id=40
#
# and the documentation of simplejson.loads():
#
# "If s is a str then decoded JSON strings that contain only ASCII
# characters may be parsed as str for performance and memory reasons.
# If your code expects only unicode the appropriate solution is
# decode s to unicode prior to calling loads."
#
spec_data = parse(u, file_extension)
tests = spec_data['tests'] tests = spec_data['tests']
......
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