Commit 64cbf129 by Chris Jerdonek

Merge 'development-spec' into 'development': spec-compliance. Happy New Year! :)

This commit merges pvande's spec-compliant branch `e06bed51`
into the development branch with all tests (including v1.0.3 spec tests) passing.
Also, speed did not slow considerably.  Further refactoring and clean-up are
still needed.

Here are results of the benchmark script before and after (using
`python tests/benchmark.py 10000`):

development (before):

    Benchmarking: 10000x

    0.931457042694
    3.08717703819

development (after):

    Benchmarking: 10000x

    1.56453585625
    4.69798803329

And here were the test results before the commit (now all 280 are passing):

(using `nosetests --with-doctest --doctest-extension=rst -i spec`)

    Ran 280 tests in 0.535s

    FAILED (errors=5, failures=39)
parents 8b92c96f 4e04f610
## Again, {{title}}! ## ## Again, {{title}}! ##
\ No newline at end of file
Subproject commit 62871926ab5789ab6c55f5a1deda359ba5f7b2fa Subproject commit 48c933b0bb780875acbfd15816297e263c53d6f7
...@@ -13,3 +13,17 @@ DATA_DIR = 'tests/data' ...@@ -13,3 +13,17 @@ DATA_DIR = 'tests/data'
def get_data_path(file_name): def get_data_path(file_name):
return os.path.join(DATA_DIR, file_name) return os.path.join(DATA_DIR, file_name)
def assert_strings(test_case, actual, expected):
# Show both friendly and literal versions.
message = """\
Expected: \"""%s\"""
Actual: \"""%s\"""
Expected: %s
Actual: %s""" % (expected, actual, repr(expected), repr(actual))
test_case.assertEquals(actual, expected, message)
...@@ -55,7 +55,11 @@ def buildTest(testData, spec_filename): ...@@ -55,7 +55,11 @@ def buildTest(testData, spec_filename):
Template: \"""%s\""" Template: \"""%s\"""
Expected: %s Expected: %s
Actual: %s""" % (description, template, repr(expected), repr(actual)) Actual: %s
Expected: \"""%s\"""
Actual: \"""%s\"""
""" % (description, template, repr(expected), repr(actual), expected, actual)
self.assertEquals(actual, expected, message) self.assertEquals(actual, expected, message)
......
...@@ -13,6 +13,9 @@ from examples.unicode_output import UnicodeOutput ...@@ -13,6 +13,9 @@ from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext from examples.nested_context import NestedContext
from tests.common import assert_strings
class TestView(unittest.TestCase): class TestView(unittest.TestCase):
def test_comments(self): def test_comments(self):
self.assertEquals(Comments().render(), """<h1>A Comedy of Errors</h1> self.assertEquals(Comments().render(), """<h1>A Comedy of Errors</h1>
...@@ -47,20 +50,14 @@ Again, Welcome!""") ...@@ -47,20 +50,14 @@ Again, Welcome!""")
def test_template_partial_extension(self): def test_template_partial_extension(self):
view = TemplatePartial() view = TemplatePartial()
view.template_extension = 'txt' view.template_extension = 'txt'
self.assertEquals(view.render(), """Welcome assert_strings(self, view.render(), u"""Welcome
------- -------
## Again, Welcome! ## ## Again, Welcome! ##""")
""")
def test_delimiters(self): def test_delimiters(self):
self.assertEquals(Delimiters().render(), """ assert_strings(self, Delimiters().render(), """* It worked the first time.
* It worked the first time.
* And it worked the second time. * And it worked the second time.
* Then, surprisingly, it worked the third time. * Then, surprisingly, it worked the third time.
""") """)
......
...@@ -68,7 +68,7 @@ class RenderEngineEnderTestCase(unittest.TestCase): ...@@ -68,7 +68,7 @@ class RenderEngineEnderTestCase(unittest.TestCase):
""" """
engine = self._engine() engine = self._engine()
partials = {'partial': "{{person}}"} partials = {'partial': u"{{person}}"}
engine.load_partial = lambda key: partials[key] engine.load_partial = lambda key: partials[key]
self._assert_render('Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, engine=engine) self._assert_render('Hi Mom', 'Hi {{>partial}}', {'person': 'Mom'}, engine=engine)
...@@ -243,12 +243,20 @@ class RenderEngineEnderTestCase(unittest.TestCase): ...@@ -243,12 +243,20 @@ class RenderEngineEnderTestCase(unittest.TestCase):
def test_render__section__lambda__tag_in_output(self): def test_render__section__lambda__tag_in_output(self):
""" """
Check that callable output isn't treated as a template string (issue #46). Check that callable output is treated as a template string (issue #46).
The spec says--
When used as the data value for a Section tag, the lambda MUST
be treatable as an arity 1 function, and invoked as such (passing
a String containing the unprocessed section contents). The
returned value MUST be rendered against the current delimiters,
then interpolated in place of the section.
""" """
template = '{{#test}}Mom{{/test}}' template = '{{#test}}Hi {{person}}{{/test}}'
context = {'test': (lambda text: '{{hi}} %s' % text)} context = {'person': 'Mom', 'test': (lambda text: text + " :)")}
self._assert_render('{{hi}} Mom', template, context) self._assert_render('Hi Mom :)', template, context)
def test_render__section__comment__multiline(self): def test_render__section__comment__multiline(self):
""" """
......
import unittest import unittest
import pystache import pystache
from pystache import Renderer from pystache import Renderer
from examples.nested_context import NestedContext from examples.nested_context import NestedContext
...@@ -7,6 +8,9 @@ from examples.lambdas import Lambdas ...@@ -7,6 +8,9 @@ from examples.lambdas import Lambdas
from examples.template_partial import TemplatePartial from examples.template_partial import TemplatePartial
from examples.simple import Simple from examples.simple import Simple
from tests.common import assert_strings
class TestSimple(unittest.TestCase): class TestSimple(unittest.TestCase):
def test_nested_context(self): def test_nested_context(self):
...@@ -44,11 +48,19 @@ class TestSimple(unittest.TestCase): ...@@ -44,11 +48,19 @@ class TestSimple(unittest.TestCase):
def test_template_partial_extension(self): def test_template_partial_extension(self):
"""
Side note:
From the spec--
Partial tags SHOULD be treated as standalone when appropriate.
In particular, this means that trailing newlines should be removed.
"""
view = TemplatePartial() view = TemplatePartial()
view.template_extension = 'txt' view.template_extension = 'txt'
self.assertEquals(view.render(), """Welcome assert_strings(self, view.render(), u"""Welcome
------- -------
## Again, Welcome! ## ## Again, Welcome! ##""")
""")
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