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_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_higher_order_lambda(self): view = Lambdas() view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}' renderer = Renderer() actual = renderer.render(view) self.assertString(actual, u'abcdefghijklmnopqrstuvwxyz')
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_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_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_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_render__view(self): """ Test rendering a View instance. """ renderer = Renderer() view = Simple() actual = renderer.render(view) self.assertEqual('Hi pizza!', actual)
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_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_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__escape__uses_renderer_escape(self): """ Test that escape uses the renderer's escape function. """ renderer = Renderer() renderer.escape = lambda s: "**" + s engine = renderer._make_render_engine() escape = engine.escape self.assertEqual(escape("foo"), "**foo")
def test__literal__handles_unicode(self): """ Test that literal doesn't try to "double decode" unicode. """ renderer = Renderer() renderer.string_encoding = 'ascii' engine = renderer._make_render_engine() literal = engine.literal self.assertEqual(literal(u"foo"), "foo")
def test__load_partial__not_found__default(self): """ Check that load_partial provides a nice message when a template is not found. """ renderer = Renderer() engine = renderer._make_render_engine() load_partial = engine.load_partial self.assertException(TemplateNotFoundError, "File 'foo.mustache' not found in dirs: ['.']", load_partial, "foo")
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__escape__uses_renderer_unicode(self): """ Test that escape uses the renderer's unicode function. """ renderer = Renderer() renderer.unicode = mock_unicode engine = renderer._make_render_engine() escape = engine.escape b = u"foo".encode('ascii') self.assertEqual(escape(b), "FOO")
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_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 test__load_partial__not_found__dict(self): """ Check that load_partial provides a nice message when a template is not found. """ renderer = Renderer() renderer.partials = {} engine = renderer._make_render_engine() load_partial = engine.load_partial # Include dict directly since str(dict) is different in Python 2 and 3: # <type 'dict'> versus <class 'dict'>, respectively. self.assertException(TemplateNotFoundError, "Name 'foo' not found in partials: %s" % dict, load_partial, "foo")
def test_string_encoding__default(self): """ Check the default value. """ renderer = Renderer() self.assertEqual(renderer.string_encoding, sys.getdefaultencoding())
def test_file_encoding(self): """ Check that the file_encoding attribute is set correctly. """ renderer = Renderer(file_encoding='foo') self.assertEqual(renderer.file_encoding, 'foo')
def test_string_encoding(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(string_encoding="foo") self.assertEqual(renderer.string_encoding, "foo")
def _make_renderer(): """ Return a default Renderer instance for testing purposes. """ renderer = Renderer(string_encoding='ascii', file_encoding='ascii') return renderer
def test_file_encoding__default(self): """ Check the file_encoding default. """ renderer = Renderer() self.assertEqual(renderer.file_encoding, renderer.string_encoding)
def test_partials__default(self): """ Test the default value. """ renderer = Renderer() self.assertTrue(renderer.partials is None)
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_partials(self): """ Test that the attribute is set correctly. """ renderer = Renderer(partials={'foo': 'bar'}) self.assertEqual(renderer.partials, {'foo': 'bar'})
def test_decode_errors(self): """ Check that the constructor sets the attribute correctly. """ renderer = Renderer(decode_errors="foo") self.assertEqual(renderer.decode_errors, "foo")
def test_decode_errors__default(self): """ Check the default value. """ renderer = Renderer() self.assertEqual(renderer.decode_errors, 'strict')
def test__escape__has_access_to_original_unicode_subclass(self): """ Test that escape receives strings with the unicode subclass intact. """ renderer = Renderer() renderer.escape = lambda s: unicode(type(s).__name__) engine = renderer._make_render_engine() escape = engine.escape class MyUnicode(unicode): pass self.assertEqual(escape(u"foo".encode('ascii')), unicode.__name__) self.assertEqual(escape(u"foo"), unicode.__name__) self.assertEqual(escape(MyUnicode("foo")), MyUnicode.__name__)
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_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_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! ##""")