def test_register_without_priority(self): """ Registering a tag without specifying a priority: default to 0 """ tag_objs = [Tag("dummy"), Tag("dumdum"), Tag("mudmud")] tag.register(*tag_objs) tags = tag.get_manager()._tag_list self.assertTrue(0 in tags.keys()) self.assertEqual(len(tags[0]), 3) self.assertListEqual(tags[0], tag_objs)
def test_register_Tag_sublass(self): """ Registering a Tag subclass """ class DummyTag(Tag): pass t = DummyTag("dummy") tag.register(t) self.assertTrue(isinstance(tag.get_manager()._tag_list[0], DummyTag))
def test_global_cache_clear(self): """ Clearing all registered tags' cache """ tag.register(tag.Tag("dummy", cached=True), tag.Tag("dumdum", cached=True), tag.Tag("mudmud", cached=True)) m = tag.get_manager() for t in m: t.process("dummydumdummudmud", **{"dummy": "dummy", "dumdum": "dumdum", "mudmud": "mudmud"}) m.clear_cache() for t in m: self.assertEqual({}, t.cache)
def test_several_tags(self): """ Rendering a template containing several different tags """ tag.register("%dummytag%", "{muddytag}", "--dumdum--") tmpl = template.Template("%dummytag%, {muddytag} & --dumdum--") args = { "%dummytag%" : "dummyval", "{muddytag}" : "foo", "--dumdum--" : "bar" } self.assertEqual("dummyval, foo & bar", self.render(tmpl, **args))
def test_basic_rendering_filetemplate(self): """ Basic rendering of a file template """ tag.register(r"{{ (\w+) }}") t = template.Template.from_file(self.tmplpath) rt = t(title="TIE Is Evil", name="Raphi", age=26) self.assertEqual("{{ include testinclude.txt }}\n" "TIE Is Evil\n" "Hello, my name is Raphi\n" "I'm 26 years old and my name is Raphi !", rt)
def test_custom_manager_registration(self): """ Registration with a custom tag manager """ class dummy_manager(object): patterns = [] def add(self, t): self.patterns.append(t) dm = dummy_manager() tag.set_manager(dm) tag.register(*self.tags) self.assertListEqual(self.tags, dm.patterns)
def test_rendering_filetemplate_with_customtag(self): """ Rendering a file template with a custom tag defined """ def include_tag(match, **context): fname = match.group(1) return "Contents of file %s" % fname tag.register(r"{{ (\w+) }}") tag.register(tag.Tag(r"{{ include (\w.+) }}", processor=include_tag)) t = template.Template.from_file(self.tmplpath) rt = t(title="TIE Is Evil", name="Bobby", age=32) self.assertEqual("Contents of file testinclude.txt\n" "TIE Is Evil\n" "Hello, my name is Bobby\n" "I'm 32 years old and my name is Bobby !", rt)
def test_repeated_tags(self): """ Rendering a template containing repeated tags """ tag.register("--dumdum--") tmpl = template.Template("--dumdum--, --dumdum-- & --dumdum--") self.assertEqual("foo, foo & foo", self.render(tmpl, **{"--dumdum--": "foo"}))
def test_single_tag(self): """ Rendering a single tag template """ tag.register("%dummytag%") tmpl = template.Template("dum dummy %dummytag% dumdum") self.assertEqual("dum dummy dummyval dumdum", self.render(tmpl, **{"%dummytag%": "dummyval"}))
def test_no_tag_matched(self): """ Template default renderer should return the template string unchanged if no tag matched """ tag.register('foo') tmpl = template.Template("dummydumdum") self.assertEqual(tmpl.template, self.render(tmpl))
def test_register_with_priority(self): """ Registering tag with a PriorityTagManager """ try: tag.register(("dummy", 0), ("dumdum", 7), ("mudmud", 4)) except Exception: self.fail("Couldnt register Priority Tags")
def test_manager_iterator(self): """ Iterating directly over a TagManager """ tags = TestRegistration.tags tag.register(*tags) for i, t in enumerate(tag._manager): self.assertEqual(t, tags[i])
def test_register_string(self): """ Registering string instead of Tag instances """ tag.register("dummy", "dumdum", "mudmud") self.assertTrue(all(map(lambda t: isinstance(t, tag.Tag), tag._manager)))
def test_basic_registration(self): """ Basic tag registration and retrieval """ tag.register(*self.tags) self.assertEqual(len(tag._manager._tag_list), 3) self.assertListEqual(self.tags, tag._manager._tag_list)
def test_check_tags_priority(self): """ Checking Tags priority """ tag.register(("dummy", 4), ("dumdum", 7), ("mudmud", 0)) tag_patterns = [t.regexp.pattern for t in tag.get_manager()] self.assertListEqual(tag_patterns, ["mudmud", "dummy", "dumdum"])