~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2006 by Canonical Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""\
Test new style globs.

fnmatch is not a very good pattern matcher.
It doesn't handle unicode patterns, nor special
patterns like **.
"""

import re

from bzrlib.tests import TestCase
from bzrlib.glob_matcher import (glob_to_re, glob_to_matcher,
                                 globs_to_re, globs_to_matcher)


class GlobToRe(TestCase):

    def test_no_globs(self):
        self.assertEqual('a$', glob_to_re('a'))
        # fnmatch thinks that an unmatched [ should just
        # be escaped
        self.assertEqual('foo\\[$', glob_to_re('foo['))

    def test_star(self):
        self.assertEqual('a[^/\\\\]*$', glob_to_re('a*'))
        self.assertEqual('[^/\\\\]*a$', glob_to_re('*a'))

    def test_starstar(self):
        self.assertEqual('a.*$', glob_to_re('a**'))
        self.assertEqual('.*a$', glob_to_re('**a'))

    def test_sequence(self):
        self.assertEqual('a[abcd]$', glob_to_re('a[abcd]'))
        self.assertEqual('a[^abcd/\\\\]$', glob_to_re('a[!abcd]'))
        self.assertEqual('a[\\^b]$' , glob_to_re('a[^b]'))
        self.assertEqual('a[^^/\\\\]$', glob_to_re('a[!^]'))


class GlobMatching(TestCase):

    def assertMatching(self, glob, matching, not_matching):
        """Make sure glob matches matching, but not not_matching.

        :param glob: A filename glob
        :param matching: List of matching filenames
        :param not_matching: List on non-matching filenames
        """
        matcher = glob_to_matcher(glob)
        for fname in matching:
            self.failUnless(matcher(fname), 'glob %s did not match %s' % (glob, fname))
        for fname in not_matching:
            self.failIf(matcher(fname), 'glob %s should not match %s' % (glob, fname))

    def test_no_globs(self):
        check = self.assertMatching
        check('a', ['a'], ['b', 'a ', ' a'])
        check('foo[', ['foo['], ['[', 'foo', '[foo'])

    def test_star(self):
        check = self.assertMatching
        check('a*', ['a', 'ab', 'abc', 'a.txt'],
                    ['a/', 'a/a', 'foo/a', 'a\\'])
        # TODO jam 20060107 Some would say '*a' should not match .a
        check('*a', ['a', 'ba', 'bca', '.a', 'c.a'],
                    ['/a', 'a/a', 'foo/a', '\\a', 'a\\a'])

    def test_starstar(self):
        check = self.assertMatching
        check('a**', ['a', 'ab', 'abc', 'a/', 'a/a', 'a\\'],
                     ['foo/a', 'b/a'])
        check('**a', ['a', 'ba', 'bca', '/a', '.a', './.a'],
                     ['booty/ab', 'bca/b'])

    def test_sequence(self):
        check = self.assertMatching
        check('a[abcd]', ['aa', 'ab', 'ac', 'ad'],
                         ['a', 'baa', 'ae', 'a/', 'abc', 'aab'])
        check('a[!abcd]', ['ae', 'af', 'aq'],
                          ['a', 'a/', 'ab', 'ac', 'ad', 'abc'])
        check('a[^b]', ['ab', 'a^'], ['a', 'ac'])
        check('a[!^]', ['ab', 'ac'], ['a', 'a^', 'a/'])