Exemplo n.º 1
0
 def test_from_diff_multiple(self):
     self.assertEqual(
         matcher.MatchInfo.from_diff('metavar', 'a\nb\nc\n', '1\nb\n3\n'),
         matcher.MatchInfo(
             match.Match(),
             bindings={
                 'metavar.0':
                 matcher.BoundValue(
                     match.SpanMatch(
                         string='a\n',
                         span=(0, 2),
                     )),
                 'metavar.1':
                 matcher.BoundValue(
                     match.SpanMatch(
                         string='c\n',
                         span=(4, 6),
                     )),
             },
             replacements={
                 'metavar.0': formatting.LiteralTemplate('1\n'),
                 'metavar.1': formatting.LiteralTemplate('3\n'),
             },
         ),
     )
Exemplo n.º 2
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='})
Exemplo n.º 3
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)), {})
Exemplo n.º 4
0
 def test_string_match(self):
     self.assertEqual(
         formatting.rewrite_templates(
             parsed_file.ParsedFile('abc', path='path', pragmas=()),
             collections.OrderedDict([
                 ('foo', match.SpanMatch('abc', (0, 3))),
                 ('bar', match.StringMatch('xyz'))
             ]), {'foo': formatting.ShTemplate(r'$bar')}), {'foo': 'xyz'})
Exemplo n.º 5
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'})
Exemplo n.º 6
0
 def test_from_diff_change(self):
     self.assertEqual(
         matcher.MatchInfo.from_diff('metavar', 'a\nb\n', 'a\nc\n'),
         matcher.MatchInfo(
             match.Match(),
             bindings={
                 'metavar.0':
                 matcher.BoundValue(
                     match.SpanMatch(
                         string='b\n',
                         span=(2, 4),
                     ))
             },
             replacements={'metavar.0': formatting.LiteralTemplate('c\n')},
         ),
     )
Exemplo n.º 7
0
 def test_matches(self):
     parsed = matcher.parse_ast('xy = 2', '<string>')
     matches = list(
         matcher.find_iter(
             base_matchers.MatchesRegex(r'^(?P<name>.)(.)$',
                                        base_matchers.Bind('inner')),
             parsed))
     # There is only one AST node of length >= 2 (which the regex requires): xy.
     self.assertEqual(matches, [
         matcher.MatchInfo(
             mock.ANY, {
                 'inner': mock.ANY,
                 'name': matcher.BoundValue(match.SpanMatch('x', (0, 1))),
             })
     ])
     [matchinfo] = matches
     self.assertEqual(matchinfo.match.span, (0, 2))
     self.assertEqual(matchinfo.match, matchinfo.bindings['inner'].value)
Exemplo n.º 8
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')
Exemplo n.º 9
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))}), '')