def test_hierarchical_partials_with_lambdas(self): view = Lambdas() view.template = '{{>partial_with_partial_and_lambda}}' renderer = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer.render(view) self.assertString(actual, u'nopqrstuvwxyznopqrstuvwxyz')
def test_higher_order_lambda(self): view = Lambdas() view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, u'abcdefghijklmnopqrstuvwxyz')
def test_partials_with_lambda(self): view = Lambdas() view.template = '{{>partial_with_lambda}}' renderer = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer.render(view) self.assertEqual(actual, u'nopqrstuvwxyz')
def test_looping_and_negation_context(self): template = '{{#item}}{{header}}: {{name}} {{/item}}{{^item}} Shouldnt see me{{/item}}' context = Complex() renderer = Renderer() actual = renderer.render(template, context) self.assertEqual(actual, "Colors: red Colors: green Colors: blue ")
def test_higher_order_rot13(self): view = Lambdas() view.template = '{{#rot13}}abcdefghijklm{{/rot13}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, u'nopqrstuvwxyz')
def test_callables(self): view = Lambdas() view.template = '{{#replace_foo_with_bar}}foo != bar. oh, it does!{{/replace_foo_with_bar}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, u'bar != bar. oh, it does!')
def test_nested_context(self): renderer = Renderer() view = NestedContext(renderer) view.template = '{{#foo}}{{thing1}} and {{thing2}} and {{outer_thing}}{{/foo}}{{^foo}}Not foo!{{/foo}}' actual = renderer.render(view) self.assertString(actual, u"one and foo and two")
def test_non_callable_attributes(self): view = Simple() view.thing = 'Chris' renderer = Renderer() actual = renderer.render(view) self.assertEqual(actual, "Hi Chris!")
def test_partial_in_partial_has_access_to_grand_parent_context(self): renderer = Renderer(search_dirs=EXAMPLES_DIR) view = TemplatePartial(renderer=renderer) view.template = '''{{>partial_in_partial}}''' actual = renderer.render(view, {'prop': 'derp'}) self.assertEqual(actual, 'Hi derp!')
def test_nested_context_is_available_in_view(self): renderer = Renderer() view = NestedContext(renderer) view.template = '{{#herp}}{{#derp}}{{nested_context_in_view}}{{/derp}}{{/herp}}' actual = renderer.render(view) self.assertString(actual, u'it works!')
def test_delimiters(self): renderer = Renderer() actual = renderer.render(Delimiters()) self.assertString(actual, u"""\ * It worked the first time. * And it worked the second time. * Then, surprisingly, it worked the third time. """)
def test_render_path(self): """ Test the render_path() method. """ renderer = Renderer() path = get_data_path('say_hello.mustache') actual = renderer.render_path(path, to='foo') self.assertEqual(actual, "Hello, foo")
def test_template_partial_extension(self): renderer = Renderer(search_dirs=EXAMPLES_DIR, file_extension='txt') view = TemplatePartial(renderer=renderer) actual = renderer.render(view) self.assertString(actual, u"""Welcome ------- ## Again, Welcome! ##""")
def test_complex(self): renderer = Renderer() actual = renderer.render(Complex()) self.assertString(actual, u"""\ <h1>Colors</h1> <ul> <li><strong>red</strong></li> <li><a href="#Green">green</a></li> <li><a href="#Blue">blue</a></li> </ul>""")
def test_render__view(self): """ Test rendering a View instance. """ renderer = Renderer() view = Simple() actual = renderer.render(view) self.assertEqual('Hi pizza!', actual)
def test__escape__uses_renderer_unicode(self): """ Test that escape uses the renderer's unicode function. """ renderer = Renderer() renderer.unicode = mock_unicode escape = renderer.escape b = u"foo".encode('ascii') self.assertEqual(escape(b), "FOO")
def test_accessing_properties_on_parent_object_from_child_objects(self): parent = Thing() parent.this = 'derp' parent.children = [Thing()] view = Simple() view.template = "{{#parent}}{{#children}}{{this}}{{/children}}{{/parent}}" renderer = Renderer() actual = renderer.render(view, {'parent': parent}) self.assertString(actual, u'derp')
def test_rendering_partial(self): renderer = Renderer(search_dirs=EXAMPLES_DIR) view = TemplatePartial(renderer=renderer) view.template = '{{>inner_partial}}' actual = renderer.render(view) self.assertString(actual, u'Again, Welcome!') view.template = '{{#looping}}{{>inner_partial}} {{/looping}}' actual = renderer.render(view) self.assertString(actual, u"Again, Welcome! Again, Welcome! Again, Welcome! ")
def test_render__object(self): """ Test rendering an object instance. """ renderer = Renderer() say_hello = SayHello() actual = renderer.render(say_hello) self.assertEqual('Hello, World', actual) actual = renderer.render(say_hello, to='Mars') self.assertEqual('Hello, Mars', actual)
def test_make_load_partial(self): """ Test the _make_load_partial() method. """ renderer = Renderer() renderer.partials = {'foo': 'bar'} load_partial = renderer._make_load_partial() actual = load_partial('foo') self.assertEqual(actual, 'bar') self.assertEqual(type(actual), unicode, "RenderEngine requires that " "load_partial return unicode strings.")
def test_render__template_spec(self): """ Test rendering a TemplateSpec instance. """ renderer = Renderer() class Spec(TemplateSpec): template = "hello, {{to}}" to = 'world' spec = Spec() actual = renderer.render(spec) self.assertString(actual, u'hello, world')
def _assert_render(self, expected, template, *context, **kwargs): """ Test rendering the given template using the given context. """ partials = kwargs.get('partials', None) engine = kwargs.get('engine', None) if not engine: engine = Renderer(partials=partials) context = ContextStack(*context) actual = engine.render(template, context) self.assertString(actual=actual, expected=expected)
def test_template_path_for_partials(self): """ Test that View.template_rel_path is respected for partials. """ spec = TemplateSpec() spec.template = "Partial: {{>tagless}}" renderer1 = Renderer() renderer2 = Renderer(search_dirs=EXAMPLES_DIR) actual = renderer1.render(spec) self.assertString(actual, u"Partial: ") actual = renderer2.render(spec) self.assertEqual(actual, "Partial: No tags...")
def test_make_load_partial__unicode(self): """ Test _make_load_partial(): that load_partial doesn't "double-decode" Unicode. """ renderer = Renderer() renderer.partials = {'partial': 'foo'} load_partial = renderer._make_load_partial() self.assertEqual(load_partial("partial"), "foo") # Now with a value that is already unicode. renderer.partials = {'partial': u'foo'} load_partial = renderer._make_load_partial() # If the next line failed, we would get the following error: # TypeError: decoding Unicode is not supported self.assertEqual(load_partial("partial"), "foo")
def test_template_rel_directory(self): """ Test that View.template_rel_directory is respected. """ class Tagless(TemplateSpec): pass view = Tagless() renderer = Renderer() self.assertRaises(TemplateNotFoundError, renderer.render, view) # TODO: change this test to remove the following brittle line. view.template_rel_directory = "examples" actual = renderer.render(view) self.assertEqual(actual, "No tags...")
def test__load_partial__returns_unicode(self): """ Check that load_partial returns unicode (and not a subclass). """ class MyUnicode(unicode): pass renderer = Renderer(string_encoding='ascii', partials={'str': 'foo', 'subclass': MyUnicode('abc')}) actual = renderer.load_partial('str') self.assertEqual(actual, "foo") self.assertEqual(type(actual), unicode) # Check that unicode subclasses are not preserved. actual = renderer.load_partial('subclass') self.assertEqual(actual, "abc") self.assertEqual(type(actual), unicode)
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. """ renderer = Renderer(search_dirs=EXAMPLES_DIR, file_extension='txt') view = TemplatePartial(renderer=renderer) actual = renderer.render(view) self.assertString(actual, u"""Welcome ------- ## Again, Welcome! ##""")
def test_escape(self): escape = lambda s: "**" + s renderer = Renderer(escape=escape) self.assertEqual(renderer.escape("bar"), "**bar")
def test_higher_order_replace(self): renderer = Renderer() actual = renderer.render(Lambdas()) self.assertEqual(actual, 'bar != bar. oh, it does!')
def test_render__dotted_path(self): renderer = Renderer() template = "{{meh.foo}}" actual = renderer.render(template, NestedObject()) self.assertEqual(actual, 'bar')