def test_Templater_open_should_load_template_from_a_raw_file_with_markers(): write_file('test.html', '|||<b>|||</b><u>|||</u>|||') t = Templater.open('test.html', marker='|||') unlink('test.html') result = t._template expected = [None, '<b>', None, '</b><u>', None, '</u>', None] assert expected == result
def test_Templater_open_should_load_template_from_a_raw_file_with_markers(): write_file('test.html', '|||<b>|||</b><u>|||</u>|||') t = Templater.open('test.html', marker='|||') unlink('test.html') result = t._template expected = [None, '<b>', None, '</b><u>', None, '</u>', None] assert expected == result
def test_should_not_have_named_marks_without_nothing_in_the_middle(): write_file('test.html', '{{first}}{{second}}<u>{{text}}</u>{{last}}') try: t = Templater.open('test.html', marker=regexp_marker) except ValueError: pass else: assert "ValueError not raised!" == False
def test_Templater_open_should_remove_leading_linefeed_if_there_is_some(): fp = open('test.html', 'w') fp.write('|||<b>|||</b><u>|||</u>|||\n') fp.close() t = Templater.open('test.html', marker='|||') unlink('test.html') result_1 = t._template expected = [None, '<b>', None, '</b><u>', None, '</u>', None] assert expected == result_1 fp = open('test.html', 'w') fp.write('|||<b>|||</b><u>|||</u>|||\r\n') fp.close() t = Templater.open('test.html', marker='|||') unlink('test.html') result_2 = t._template assert expected == result_2
def test_should_not_have_named_marks_without_nothing_in_the_middle(): write_file('test.html', '{{first}}{{second}}<u>{{text}}</u>{{last}}') try: t = Templater.open('test.html', marker=regexp_marker) except ValueError: pass else: assert "ValueError not raised!" == False
def test_Templater_open_should_remove_leading_linefeed_if_there_is_some(): fp = open('test.html', 'w') fp.write('|||<b>|||</b><u>|||</u>|||\n') fp.close() t = Templater.open('test.html', marker='|||') unlink('test.html') result_1 = t._template expected = [None, '<b>', None, '</b><u>', None, '</u>', None] assert expected == result_1 fp = open('test.html', 'w') fp.write('|||<b>|||</b><u>|||</u>|||\r\n') fp.close() t = Templater.open('test.html', marker='|||') unlink('test.html') result_2 = t._template assert expected == result_2
def test_raise_ValueError_if_there_is_no_named_marker_in_the_end_of_template(): write_file('test.html', '{{start}}<u>{{text}}</u>') try: t = Templater.open('test.html', marker=regexp_marker) except ValueError: unlink('test.html') else: unlink('test.html') assert "ValueError not raised!" == False
def test_raise_ValueError_if_there_is_no_named_marker_in_the_end_of_template(): write_file('test.html', '{{start}}<u>{{text}}</u>') try: t = Templater.open('test.html', marker=regexp_marker) except ValueError: unlink('test.html') else: unlink('test.html') assert "ValueError not raised!" == False
def test_named_markers_should_work(): write_file('test.html', '|||[first]<b>|||[second]</b><u>|||[third]</u>|||[fourth]') t = Templater.open('test.html', marker=re_compile(r'\|\|\|\[([^\]]+)\]')) unlink('test.html') result_1 = t._template expected_1 = [None, '<b>', None, '</b><u>', None, '</u>', None] assert result_1 == expected_1 result_2 = t.parse('<b>hello</b><u>world</u>') expected_2 = {'first': '', 'second': 'hello', 'third': 'world', 'fourth': ''} assert expected_2 == result_2
#!/usr/bin/env python # coding: utf-8 from os import unlink from templater import Templater t = Templater() t.learn('<b>spam</b>') t.learn('<b>eggs</b>') t.learn('<b>ham</b>') t.save('my-little-template.html', marker='|||') # will put a `\n` in the EOF t.dump('my-template.tpl') print(t.parse('<b>parsing using first template object</b>')) t2 = Templater.open('my-little-template.html', marker='|||') # it removes `\n`/`\r\n` in the end of file before creating template definition print(t2.parse('<b>parsing using second template object</b>')) t3 = Templater.load('my-template.tpl') print(t3.parse('<b>parsing using third template object</b>')) # 'my-little-template.html' will have the template string with blanks filled by # '|||' # 'my-template.tpl' will have the pickle of Templater object # Removing files: unlink('my-little-template.html') unlink('my-template.tpl')
#!/usr/bin/env python # coding: utf-8 from os import unlink from templater import Templater t = Templater() t.learn('<b>spam</b>') t.learn('<b>eggs</b>') t.learn('<b>ham</b>') t.save('my-little-template.html', marker='|||') # will put a `\n` in the EOF t.dump('my-template.tpl') print t.parse('<b>parsing using first template object</b>') t2 = Templater.open('my-little-template.html', marker='|||') # it removes `\n`/`\r\n` in the end of file before creating template definition print t2.parse('<b>parsing using second template object</b>') t3 = Templater.load('my-template.tpl') print t3.parse('<b>parsing using third template object</b>') # 'my-little-template.html' will have the template string with blanks filled by # '|||' # 'my-template.tpl' will have the pickle of Templater object # Removing files: unlink('my-little-template.html') unlink('my-template.tpl')