Ejemplo n.º 1
0
 def test_missing_template(self):
     self.assertEqual(
         formatting.rewrite_templates(
             parsed_file.ParsedFile('abc', path='path', pragmas=()),
             collections.OrderedDict([('foo', match.SpanMatch('',
                                                              (-1, -1))),
                                      ('bar', match.SpanMatch('a',
                                                              (0, 1)))]),
             {
                 # swap foo and bar
                 'foo': formatting.RegexTemplate(r'bar=\g<bar>'),
                 'bar': formatting.RegexTemplate(r'foo=\g<foo>'),
             }),
         # foo is never matched, bar is replaced with foo=<non-match>,
         # which is treated as ''.
         {'bar': 'foo='})
Ejemplo n.º 2
0
 def test_missing(self):
     for template in [r'\1', r'\g<x>']:
         with self.subTest(template=template):
             with self.assertRaises(KeyError):
                 formatting.RegexTemplate(r'\1').substitute_match(
                     parsed_file.ParsedFile('', path='path', pragmas=()),
                     match.SpanMatch('', (0, 0)), {})
Ejemplo n.º 3
0
 def test_named_template(self):
     self.assertEqual(
         formatting.rewrite_templates(
             parsed_file.ParsedFile('abc', path='path', pragmas=()),
             collections.OrderedDict([('foo', match.SpanMatch('b',
                                                              (1, 2)))]),
             {'foo': formatting.RegexTemplate(r'x\g<foo>x')}),
         {'foo': 'xbx'})
Ejemplo n.º 4
0
 def test_labels_nonempty(self):
     self.assertEqual(
         formatting.template_variables(
             {'key': formatting.RegexTemplate(r'\g<template_variable>')}),
         {'key', 'template_variable'})
Ejemplo n.º 5
0
 def test_variables(self, template, expected_variables):
     self.assertEqual(
         formatting.RegexTemplate(template).variables, expected_variables)
Ejemplo n.º 6
0
 def test_present_named(self):
     self.assertEqual(
         formatting.RegexTemplate(r'\g<x>').substitute_match(
             parsed_file.ParsedFile('a', path='path', pragmas=()),
             match.SpanMatch('', (0, 0)),
             {'x': match.SpanMatch('a', (0, 1))}), 'a')
Ejemplo n.º 7
0
 def test_extra(self):
     self.assertEqual(
         formatting.RegexTemplate('').substitute_match(
             parsed_file.ParsedFile('b', path='path', pragmas=()),
             match.SpanMatch('', (0, 0)),
             {'a': match.SpanMatch('b', (0, 1))}), '')