~bulb/bzr/ignore

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_glob.py

  • Committer: Jan Hudec
  • Date: 2006-02-05 21:43:17 UTC
  • Revision ID: bulb@ucw.cz-20060205214317-b5ad5bdd2d6b4586
Added counting non-glob chars and made ** and *** at the end special too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
        expand_glob,
24
24
        translate,
25
25
        SHELL, FNMATCH, NATIVE,
26
 
        _unescape_re,
 
26
        _unescape_glob,
27
27
        )
28
28
from bzrlib.osutils import abspath
29
 
import os.path
30
 
import re
31
29
 
32
30
 
33
31
class TestShellGlobs(TestCase):
120
118
        self.assertMatch(u'x**/x', [u'xaarg/x', u'x/x'], [u'xa/b/x',
121
119
                u'foo/xfoo/x'])
122
120
 
 
121
        self.assertMatch(u'./**', [u'foo/bar/baz/bagoo'], [u'foo/.bar'])
 
122
 
123
123
    def test_triple_asterisk(self):
124
124
        self.assertMatch(u'***/\u8336', [u'\u8336', u'foo/\u8336',
125
125
                u'q/y/z/z/y/\u8336', u'q/y/z/.z/y/\u8336'], [u'that\u8336',
133
133
        self.assertMatch(u'x***/x', [u'xaarg/x', u'x/x'], [u'xa/b/x',
134
134
                u'foo/xfoo/x'])
135
135
 
 
136
        self.assertMatch(u'./***', [u'foo/bar/.baz/bagoo'], [])
 
137
 
136
138
    def test_leading_dotslash(self):
137
139
        self.assertMatch(u'./foo', [u'foo'], [u'\u8336/foo', u'barfoo',
138
140
                u'x/y/foo'])
198
200
 
199
201
class TestUnescape(TestCase):
200
202
 
201
 
    def assertLiteralRe(self, regex, match):
 
203
    def assertLiteral(self, glob, match):
202
204
        try:
203
 
            if _unescape_re(regex) != match:
 
205
            if _unescape_glob(glob) != match:
204
206
                raise AssertionError(repr(
205
 
                    u'Pattern "%s" does not expand to literal "%s"' % (regex,
 
207
                    u'Pattern "%s" does not expand to literal "%s"' % (glob,
206
208
                        match)))
207
209
        except ValueError:
208
210
            raise AssertionError(repr(
209
 
                u'Pattern "%s" does not represent literal"' % regex))
210
 
        if not re.match(regex, match, re.UNICODE):
 
211
                u'Pattern "%s" does not represent literal"' % glob))
 
212
        if not compile(glob).match(match):
211
213
            raise AssertionError(repr(
212
 
                u'Pattern "%s" does not match it\'s expansion "%s"' % (regex,
 
214
                u'Pattern "%s" does not match it\'s expansion "%s"' % (glob,
213
215
                    match)))
214
216
 
215
217
 
216
 
    def assertNotLiteral(self, regex):
 
218
    def assertNotLiteral(self, glob):
217
219
        try:
218
 
            exp = _unescape_re(regex)
 
220
            exp = _unescape_glob(glob)
219
221
            raise AssertionError(repr(
220
222
                u'"%s" should NOT expand to "%s" (nor anything else)'
221
 
                % (regex, exp)))
 
223
                % (glob, exp)))
222
224
        except ValueError:
223
225
            pass
224
226
 
225
227
 
226
228
    def test_literals(self):
227
 
        self.assertLiteralRe(u'foobar', u'foobar')
228
 
        self.assertLiteralRe(u'foo/bar', u'foo/bar')
229
 
        self.assertLiteralRe(u'qyzzy', u'qyzzy')
230
 
        self.assertLiteralRe(u'\u8336', u'\u8336')
 
229
        self.assertLiteral(u'foobar', u'foobar')
 
230
        self.assertLiteral(u'foo/bar', u'foo/bar')
 
231
        self.assertLiteral(u'qyzzy', u'qyzzy')
 
232
        self.assertLiteral(u'\u8336', u'\u8336')
231
233
 
232
234
    def test_chars(self):
233
 
        self.assertLiteralRe(ur'\u8336', u'\u8336')
234
 
        self.assertLiteralRe(ur'\x44\t\r\n', u'\x44\t\r\n')
235
 
        self.assertLiteralRe(ur'\$\#\*', u'$#*')
 
235
        self.assertLiteral(ur'\u8336', u'\u8336')
 
236
        self.assertLiteral(ur'\x44\t\r\n', u'\x44\t\r\n')
 
237
        self.assertLiteral(ur'\$\#\*', u'$#*')
236
238
        self.assertNotLiteral(ur'\b')
237
239
        self.assertNotLiteral(ur'\w')
238
240
 
239
 
    def test_dollar(self):
240
 
        self.assertLiteralRe(u'foobar$', u'foobar')
241
 
        self.assertLiteralRe(u'\u8336$', u'\u8336')
242
 
 
243
241
    def test_specials(self):
244
 
        self.assertNotLiteral(ur'foo(2)bar')
245
242
        self.assertNotLiteral(ur'foo*')
246
 
        self.assertNotLiteral(ur'...')
247
243
        self.assertNotLiteral(ur'qyzz?y')
248
 
        self.assertNotLiteral(ur'a+b')
249
244
        self.assertNotLiteral(ur'a[bcd]')
250
245
        self.assertNotLiteral(ur'\w\d\D')
 
246
        self.assertNotLiteral(ur'RE:anything')
251
247
 
252
248
    def test_globs(self):
253
 
        self.assertLiteralRe(translate(u'foo.bar'), u'foo.bar')
254
 
        self.assertLiteralRe(translate(ur'foo\*bar'), u'foo*bar')
255
 
        self.assertLiteralRe(translate(ur'b{q}z|2'), u'b{q}z|2')
256
 
        self.assertLiteralRe(translate(ur'(foo)'), u'(foo)')
 
249
        self.assertLiteral(u'foo.bar', u'foo.bar')
 
250
        self.assertLiteral(ur'foo\*bar', u'foo*bar')
 
251
        self.assertLiteral(ur'b{q}z|2', u'b{q}z|2')
 
252
        self.assertLiteral(ur'(foo)', u'(foo)')
257
253
 
258
254
 
259
255
class TestExpansion(TestCaseInTempDir):