Commit 08c4032b by Chris Jerdonek

Removed View dependency from more examples and test cases.

parent de8aed53
import pystache
from examples.lambdas import rot
class PartialsWithLambdas(pystache.View):
template_path = 'examples'
class PartialsWithLambdas(object):
def rot(self):
return rot
\ No newline at end of file
import pystache
from pystache import TemplateSpec
class Simple(pystache.View):
template_path = 'examples'
class Simple(TemplateSpec):
def thing(self):
return "pizza"
......
import pystache
from pystache import TemplateSpec
class TemplatePartial(pystache.View):
template_path = 'examples'
class TemplatePartial(TemplateSpec):
def __init__(self, renderer):
self.renderer = renderer
def _context_get(self, key):
return self.renderer.context.get(key)
def title(self):
return "Welcome"
......@@ -13,4 +18,4 @@ class TemplatePartial(pystache.View):
return [{'item': 'one'}, {'item': 'two'}, {'item': 'three'}]
def thing(self):
return self.context.get('prop')
\ No newline at end of file
return self._context_get('prop')
\ No newline at end of file
......@@ -7,8 +7,12 @@ Provides test-related code that can be used by all tests.
import os
import examples
DATA_DIR = 'tests/data'
EXAMPLES_DIR = os.path.dirname(examples.__file__)
def get_data_path(file_name):
return os.path.join(DATA_DIR, file_name)
......
......@@ -12,7 +12,8 @@ from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext
from pystache import Renderer
from tests.common import AssertStringMixin
from .common import EXAMPLES_DIR
from .common import AssertStringMixin
class TestView(unittest.TestCase, AssertStringMixin):
......@@ -42,13 +43,19 @@ class TestView(unittest.TestCase, AssertStringMixin):
self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>")
def test_template_partial(self):
self.assertEquals(TemplatePartial().render(), """<h1>Welcome</h1>
renderer = Renderer(search_dirs=EXAMPLES_DIR)
actual = renderer.render(TemplatePartial(renderer=renderer))
self.assertString(actual, u"""<h1>Welcome</h1>
Again, Welcome!""")
def test_template_partial_extension(self):
view = TemplatePartial()
view.template_extension = 'txt'
self.assertString(view.render(), u"""Welcome
renderer = Renderer(search_dirs=EXAMPLES_DIR, file_extension='txt')
view = TemplatePartial(renderer=renderer)
actual = renderer.render(view)
self.assertString(actual, u"""Welcome
-------
## Again, Welcome! ##""")
......@@ -77,9 +84,13 @@ Again, Welcome!""")
self.assertString(actual, u'it works!')
def test_partial_in_partial_has_access_to_grand_parent_context(self):
view = TemplatePartial(context = {'prop': 'derp'})
renderer = Renderer(search_dirs=EXAMPLES_DIR)
view = TemplatePartial(renderer=renderer)
view.template = '''{{>partial_in_partial}}'''
self.assertEquals(view.render(), 'Hi derp!')
actual = renderer.render(view, {'prop': 'derp'})
self.assertEquals(actual, 'Hi derp!')
if __name__ == '__main__':
unittest.main()
......@@ -8,7 +8,8 @@ from examples.lambdas import Lambdas
from examples.template_partial import TemplatePartial
from examples.simple import Simple
from tests.common import AssertStringMixin
from .common import EXAMPLES_DIR
from .common import AssertStringMixin
class TestSimple(unittest.TestCase, AssertStringMixin):
......@@ -26,8 +27,8 @@ class TestSimple(unittest.TestCase, AssertStringMixin):
context = Complex()
renderer = Renderer()
expected = renderer.render(template, context)
self.assertEquals(expected, "Colors: red Colors: green Colors: blue ")
actual = renderer.render(template, context)
self.assertEquals(actual, "Colors: red Colors: green Colors: blue ")
def test_empty_context(self):
template = '{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}'
......@@ -38,16 +39,21 @@ class TestSimple(unittest.TestCase, AssertStringMixin):
view.template = '{{#replace_foo_with_bar}}foo != bar. oh, it does!{{/replace_foo_with_bar}}'
renderer = Renderer()
expected = renderer.render(view)
self.assertString(expected, u'bar != bar. oh, it does!')
actual = renderer.render(view)
self.assertString(actual, u'bar != bar. oh, it does!')
def test_rendering_partial(self):
view = TemplatePartial()
renderer = Renderer(search_dirs=EXAMPLES_DIR)
view = TemplatePartial(renderer=renderer)
view.template = '{{>inner_partial}}'
self.assertEquals(view.render(), 'Again, Welcome!')
actual = renderer.render(view)
self.assertString(actual, u'Again, Welcome!')
view.template = '{{#looping}}{{>inner_partial}} {{/looping}}'
self.assertEquals(view.render(), '''Again, Welcome! Again, Welcome! Again, Welcome! ''')
actual = renderer.render(view)
self.assertString(actual, u"Again, Welcome! Again, Welcome! Again, Welcome! ")
def test_non_existent_value_renders_blank(self):
view = Simple()
......@@ -66,9 +72,12 @@ class TestSimple(unittest.TestCase, AssertStringMixin):
In particular, this means that trailing newlines should be removed.
"""
view = TemplatePartial()
view.template_extension = 'txt'
self.assertString(view.render(), u"""Welcome
renderer = Renderer(search_dirs=EXAMPLES_DIR, file_extension='txt')
view = TemplatePartial(renderer=renderer)
actual = renderer.render(view)
self.assertString(actual, u"""Welcome
-------
## Again, Welcome! ##""")
......@@ -20,16 +20,14 @@ from pystache import View
from pystache.template_spec import SpecLoader
from pystache.locator import Locator
from pystache.loader import Loader
from .common import DATA_DIR
from .common import EXAMPLES_DIR
from .common import AssertIsMixin
from .common import AssertStringMixin
from .common import DATA_DIR
from .data.views import SampleView
from .data.views import NonAscii
EXAMPLES_DIR = os.path.dirname(examples.__file__)
class Thing(object):
pass
......@@ -78,13 +76,18 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin):
self.assertEquals(view.render(), "Partial: No tags...")
def test_basic_method_calls(self):
view = Simple()
self.assertEquals(view.render(), "Hi pizza!")
renderer = Renderer()
actual = renderer.render(Simple())
self.assertString(actual, u"Hi pizza!")
def test_non_callable_attributes(self):
view = Simple()
view.thing = 'Chris'
self.assertEquals(view.render(), "Hi Chris!")
renderer = Renderer()
actual = renderer.render(view)
self.assertEquals(actual, "Hi Chris!")
def test_complex(self):
renderer = Renderer()
......@@ -143,10 +146,13 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin):
parent = Thing()
parent.this = 'derp'
parent.children = [Thing()]
view = Simple(context={'parent': parent})
view = Simple()
view.template = "{{#parent}}{{#children}}{{this}}{{/children}}{{/parent}}"
self.assertEquals(view.render(), 'derp')
renderer = Renderer()
actual = renderer.render(view, {'parent': parent})
self.assertString(actual, u'derp')
def test_inverted_lists(self):
renderer = Renderer()
......
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