def test_load__template__correct_loader(self): """ Test that reader.unicode() is called correctly. This test tests that the correct reader is called with the correct arguments. This is a catch-all test to supplement the other test cases. It tests SpecLoader.load() independent of reader.unicode() being implemented correctly (and tested). """ class MockLoader(Loader): def __init__(self): self.s = None self.encoding = None # Overrides the existing method. def unicode(self, s, encoding=None): self.s = s self.encoding = encoding return u"foo" loader = MockLoader() custom_loader = SpecLoader() custom_loader.loader = loader view = TemplateSpec() view.template = "template-foo" view.template_encoding = "encoding-foo" # Check that our unicode() above was called. self._assert_template(custom_loader, view, u'foo') self.assertEquals(loader.s, "template-foo") self.assertEquals(loader.encoding, "encoding-foo")
def _render_object(self, obj, *context, **kwargs): """ Render the template associated with the given object. """ loader = self._make_loader() # TODO: consider an approach that does not require using an if # block here. For example, perhaps this class's loader can be # a SpecLoader in all cases, and the SpecLoader instance can # check the object's type. Or perhaps Loader and SpecLoader # can be refactored to implement the same interface. if isinstance(obj, TemplateSpec): loader = SpecLoader(loader) template = loader.load(obj) else: template = loader.load_object(obj) context = [obj] + list(context) return self._render_string(template, *context, **kwargs)