def test_multiline_arg(): t = Template('{{text|\na=\nb\nc\n}}') t.set_arg('d', 'e') assert '{{text|\na=\nb\nc|\nd=\ne\n}}' == t.string t = Template('{{text\n\n | a = b\n\n}}') t.set_arg('c', 'd') assert '{{text\n\n | a = b\n\n | c = d\n\n}}' == t.string
def test_multiline_arg(self): t = Template('{{text|\na=\nb\nc\n}}') t.set_arg('d', 'e') self.assertEqual('{{text|\na=\nb\nc|\nd=\ne\n}}', t.string) t = Template('{{text\n\n | a = b\n\n}}') t.set_arg('c', 'd') self.assertEqual('{{text\n\n | a = b\n\n | c = d\n\n}}', t.string)
def update_arguments(template: Template, args: dict, overwrite=False): """ Updates the arguments of the given template with the values of the given dict (args) Args: template: Template that should be updated args(dict): Dict containing the arguments that should be set. key=argument name, value=argument value overwrite(bool): If True existing values will be overwritten Returns: Nothing """ for key, value in args.items(): if template.has_arg(key): # update argument if overwrite: template.del_arg(key) template.set_arg(key, value) else: pass else: if value is not None: template.set_arg(key, value, preserve_spacing=False)
def test_new_dont_preserve_space(self): t = Template('{{t\n | a = v \n}}') t.set_arg('b', 'w', preserve_spacing=False) self.assertEqual( '{{t\n | a = v \n|b=w}}', t.string )
def test_set_arg(self): # Template with no args, keyword t = Template('{{t}}') t.set_arg('a', 'b') self.assertEqual('{{t|a=b}}', t.string) # Template with no args, auto positional t = Template('{{t}}') t.set_arg('1', 'b') self.assertEqual('{{t|1=b}}', t.string) # Force keyword t = Template('{{t}}') t.set_arg('1', 'b', positional=False) self.assertEqual('{{t|1=b}}', t.string) # Arg already exist, positional t = Template('{{t|a}}') t.set_arg('1', 'b') self.assertEqual('{{t|b}}', t.string) # Append new keyword when there is more than one arg t = Template('{{t|a}}') t.set_arg('z', 'z') self.assertEqual('{{t|a|z=z}}', t.string) # Preserve spacing t = Template('{{t\n | p1 = v1\n | p22 = v2\n}}') t.set_arg('z', 'z') self.assertEqual( '{{t\n | p1 = v1\n | p22 = v2\n | z = z\n}}', t.string ) # Preserve spacing, only one argument t = Template('{{t\n | afadfaf = value \n}}') t.set_arg('z', 'z') self.assertEqual( '{{t\n | afadfaf = value\n | z = z\n}}', t.string )
def test_preserve_spacing_with_only_one_arg(self): t = Template('{{t\n | afadfaf = value \n}}') t.set_arg('z', 'z') self.assertEqual('{{t\n | afadfaf = value\n | z = z\n}}', t.string)
def test_set_arg(): # Template with no args, keyword t = Template('{{t}}') t.set_arg('a', 'b') assert '{{t|a=b}}' == t.string # Template with no args, auto positional t = Template('{{t}}') t.set_arg('1', 'b') assert '{{t|1=b}}' == t.string # Force keyword t = Template('{{t}}') t.set_arg('1', 'b', positional=False) assert '{{t|1=b}}' == t.string # Arg already exist, positional t = Template('{{t|a}}') t.set_arg('1', 'b') assert '{{t|b}}' == t.string # Append new keyword when there is more than one arg t = Template('{{t|a}}') t.set_arg('z', 'z') assert '{{t|a|z=z}}' == t.string # Preserve spacing t = Template('{{t\n | p1 = v1\n | p22 = v2\n}}') t.set_arg('z', 'z') assert '{{t\n | p1 = v1\n | p22 = v2\n | z = z\n}}' == t.string
def test_force_new_to_positional_when_old_is_keyword(self): t = Template('{{t|1=v}}') t.set_arg('1', 'v', positional=True) self.assertEqual('{{t|v}}', t.string)
def test_multi_set_positional_args(self): t = Template('{{t}}') t.set_arg('1', 'p', positional=True) t.set_arg('2', 'q', positional=True) self.assertEqual('{{t|p|q}}', t.string)
def test_multi_set_positional_args(): t = Template('{{t}}') t.set_arg('1', 'p', positional=True) t.set_arg('2', 'q', positional=True) assert '{{t|p|q}}' == t.string
def test_after(): t = Template('{{t|a|b|c=c|d}}') t.set_arg('e', 'e', after='c') assert '{{t|a|b|c=c|e=e|d}}' == t.string
def test_before(): t = Template('{{t|a|b|c=c|d}}') t.set_arg('e', 'e', before='c') assert '{{t|a|b|e=e|c=c|d}}' == t.string
def test_new_dont_preserve_space(): t = Template('{{t\n | a = v \n}}') t.set_arg('b', 'w', preserve_spacing=False) assert '{{t\n | a = v \n|b=w}}' == t.string
def test_preserve_spacing_with_only_one_arg(): t = Template('{{t\n | afadfaf = value \n}}') t.set_arg('z', 'z') assert '{{t\n | afadfaf = value\n | z = z\n}}' == t.string
def test_before(self): t = Template('{{t|a|b|c=c|d}}') t.set_arg('e', 'e', before='c') self.assertEqual('{{t|a|b|e=e|c=c|d}}', t.string)
def test_after(self): t = Template('{{t|a|b|c=c|d}}') t.set_arg('e', 'e', after='c') self.assertEqual('{{t|a|b|c=c|e=e|d}}', t.string)
def test_invalid_position(): t = Template('{{t}}') t.set_arg('2', 'a', positional=True) assert '{{t|2=a}}' == t.string
def test_invalid_position(self): t = Template('{{t}}') t.set_arg('2', 'a', positional=True) self.assertEqual('{{t|2=a}}', t.string)
def test_force_new_to_positional_when_old_is_keyword(): t = Template('{{t|1=v}}') t.set_arg('1', 'v', positional=True) assert '{{t|v}}' == t.string
def test_not_name_and_positional_is_none(self): t = Template('{{t}}') t.set_arg(None, 'v') self.assertEqual('{{t|v}}', t.string)
def test_not_name_and_positional_is_none(): t = Template('{{t}}') t.set_arg(None, 'v') assert '{{t|v}}' == t.string