Commit e013e059 by Chris Jerdonek

Switched the spec tests to use the AssertStringMixin class.

Also corrected/improved the spec test harness's handling of unicode
since simplejson handles unicode differently from json.
parent 368f0dfd
......@@ -22,13 +22,21 @@ class AssertStringMixin:
"""A unittest.TestCase mixin to check string equality."""
def assertString(self, actual, expected):
def assertString(self, actual, expected, format=None):
"""
Assert that the given strings are equal and have the same type.
Arguments:
format: a format string containing a single conversion specifier %s.
Defaults to "%s".
"""
if format is None:
format = "%s"
# Show both friendly and literal versions.
message = """String mismatch: %%s\
details = """String mismatch: %%s\
Expected: \"""%s\"""
......@@ -37,10 +45,14 @@ class AssertStringMixin:
Expected: %s
Actual: %s""" % (expected, actual, repr(expected), repr(actual))
self.assertEquals(actual, expected, message % "different characters")
def make_message(reason):
description = details % reason
return format % description
self.assertEquals(actual, expected, make_message("different characters"))
details = "types different: %s != %s" % (repr(type(expected)), repr(type(actual)))
self.assertEquals(type(expected), type(actual), message % details)
reason = "types different: %s != %s (actual)" % (repr(type(expected)), repr(type(actual)))
self.assertEquals(type(expected), type(actual), make_message(reason))
class AssertIsMixin:
......
......@@ -7,13 +7,17 @@ Creates a unittest.TestCase for the tests defined in the mustache spec.
# TODO: this module can be cleaned up somewhat.
FILE_ENCODING = 'utf-8' # the encoding of the spec test files.
try:
# We deserialize the json form rather than the yaml form because
# json libraries are available for Python 2.4.
# We use the JSON files rather than the YAML files 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.
# The module json is not available prior to Python 2.6, whereas simplejson is.
# Note that simplejson dropped support for Python 2.4 in simplejson v2.1.0,
# so Python 2.4 requires a simplejson install older than the most recent.
import simplejson as json
import glob
......@@ -21,12 +25,15 @@ import os.path
import unittest
from pystache.renderer import Renderer
from tests.common import AssertStringMixin
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):
# TODO: give this a name better than MustacheSpec.
class MustacheSpec(unittest.TestCase, AssertStringMixin):
pass
def buildTest(testData, spec_filename):
......@@ -52,20 +59,29 @@ def buildTest(testData, spec_filename):
renderer = Renderer(partials=partials)
actual = renderer.render(template, new_data)
actual = actual.encode('utf-8')
# We need to escape the strings that occur in our format string because
# they can contain % symbols, for example (in delimiters.yml)--
#
# "template: '{{=<% %>=}}(<%text%>)'"
#
def escape(s):
return s.replace("%", "%%")
subs = [description, template, json.__version__, str(json)]
subs = tuple([escape(sub) for sub in subs])
# We include the json module version to help in troubleshooting
# json/simplejson issues.
message = """%s
Template: \"""%s\"""
Expected: %s
Actual: %s
%%s
Expected: \"""%s\"""
Actual: \"""%s\"""
""" % (description, template, repr(expected), repr(actual), expected, actual)
(using version %s of %s)
""" % subs
self.assertEquals(actual, expected, message)
self.assertString(actual, expected, format=message)
# The name must begin with "test" for nosetests test discovery to work.
name = 'test: "%s"' % test_name
......@@ -83,10 +99,25 @@ for spec_path in spec_paths:
# We avoid use of the with keyword for Python 2.4 support.
f = open(spec_path, 'r')
try:
spec_data = json.load(f)
s = 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."
#
u = s.decode(FILE_ENCODING)
spec_data = json.loads(u)
tests = spec_data['tests']
for test in 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