~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_urlutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-02 22:43:51 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060502224351-2aab4b966d21acb5
Added bzrlib.urlutils.split and basename + dirname

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
class TestUrlToPath(TestCase):
29
29
    
 
30
    def test_basename(self):
 
31
        # bzrlib.urlutils.basename
 
32
        # Test bzrlib.urlutils.split()
 
33
        basename = urlutils.basename
 
34
        if sys.platform == 'win32':
 
35
            self.assertRaises(InvalidURL, basename, 'file:///path/to/foo')
 
36
            self.assertEqual('foo', basename('file:///C|/foo'))
 
37
            self.assertEqual('', basename('file:///C|/'))
 
38
        else:
 
39
            self.assertEqual('foo', basename('file:///foo'))
 
40
            self.assertEqual('', basename('file:///'))
 
41
 
 
42
        self.assertEqual('foo', basename('http://host/path/to/foo'))
 
43
        self.assertEqual('foo', basename('http://host/path/to/foo/'))
 
44
        self.assertEqual('',
 
45
            basename('http://host/path/to/foo/', exclude_trailing_slash=False))
 
46
        self.assertEqual('path', basename('http://host/path'))
 
47
        self.assertEqual('', basename('http://host/'))
 
48
        self.assertEqual('', basename('http://host'))
 
49
        self.assertEqual('path', basename('http:///nohost/path'))
 
50
 
 
51
        self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path'))
 
52
        self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path/'))
 
53
        self.assertEqual('', basename('random+scheme://user:pass@ahost:port/'))
 
54
 
 
55
        # relative paths
 
56
        self.assertEqual('foo', basename('path/to/foo'))
 
57
        self.assertEqual('foo', basename('path/to/foo/'))
 
58
        self.assertEqual('', basename('path/to/foo/',
 
59
            exclude_trailing_slash=False))
 
60
        self.assertEqual('foo', basename('path/../foo'))
 
61
        self.assertEqual('foo', basename('../path/foo'))
 
62
 
 
63
    def test_dirname(self):
 
64
        # Test bzrlib.urlutils.dirname()
 
65
        dirname = urlutils.dirname
 
66
        if sys.platform == 'win32':
 
67
            self.assertRaises(InvalidURL, dirname, 'file:///path/to/foo')
 
68
            self.assertEqual('file:///C|/', dirname('file:///C|/foo'))
 
69
            self.assertEqual('file:///C|/', dirname('file:///C|/'))
 
70
        else:
 
71
            self.assertEqual('file:///', dirname('file:///foo'))
 
72
            self.assertEqual('file:///', dirname('file:///'))
 
73
 
 
74
        self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo'))
 
75
        self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo/'))
 
76
        self.assertEqual('http://host/path/to/foo',
 
77
            dirname('http://host/path/to/foo/', exclude_trailing_slash=False))
 
78
        self.assertEqual('http://host/', dirname('http://host/path'))
 
79
        self.assertEqual('http://host/', dirname('http://host/'))
 
80
        self.assertEqual('http://host', dirname('http://host'))
 
81
        self.assertEqual('http:///nohost', dirname('http:///nohost/path'))
 
82
 
 
83
        self.assertEqual('random+scheme://user:pass@ahost:port/',
 
84
            dirname('random+scheme://user:pass@ahost:port/path'))
 
85
        self.assertEqual('random+scheme://user:pass@ahost:port/',
 
86
            dirname('random+scheme://user:pass@ahost:port/path/'))
 
87
        self.assertEqual('random+scheme://user:pass@ahost:port/',
 
88
            dirname('random+scheme://user:pass@ahost:port/'))
 
89
 
 
90
        # relative paths
 
91
        self.assertEqual('path/to', dirname('path/to/foo'))
 
92
        self.assertEqual('path/to', dirname('path/to/foo/'))
 
93
        self.assertEqual('path/to/foo',
 
94
            dirname('path/to/foo/', exclude_trailing_slash=False))
 
95
        self.assertEqual('path/..', dirname('path/../foo'))
 
96
        self.assertEqual('../path', dirname('../path/foo'))
 
97
 
30
98
    def test_function_type(self):
31
99
        if sys.platform == 'win32':
32
100
            self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
57
125
        to_url = urlutils._win32_local_path_to_url
58
126
        self.assertEqual('file:///C|/path/to/foo',
59
127
            to_url('C:/path/to/foo'))
60
 
        self.assertEqual('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s',
 
128
        self.assertEqual('file:///D|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s',
61
129
            to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s'))
62
130
 
63
131
    def test_win32_local_path_from_url(self):
64
132
        from_url = urlutils._win32_local_path_from_url
65
133
        self.assertEqual('C:/path/to/foo',
66
134
            from_url('file:///C|/path/to/foo'))
67
 
        self.assertEqual(u'd:/path/to/r\xe4ksm\xf6rg\xe5s',
 
135
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
68
136
            from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
69
 
        self.assertEqual(u'd:/path/to/r\xe4ksm\xf6rg\xe5s',
 
137
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
70
138
            from_url('file:///d|/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
71
139
 
72
140
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
73
141
        # Not a valid _win32 url, no drive letter
74
142
        self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
75
143
 
 
144
    def test_split(self):
 
145
        # Test bzrlib.urlutils.split()
 
146
        split = urlutils.split
 
147
        if sys.platform == 'win32':
 
148
            self.assertRaises(InvalidURL, split, 'file:///path/to/foo')
 
149
            self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
 
150
            self.assertEqual(('file:///C|/', ''), split('file:///C|/'))
 
151
        else:
 
152
            self.assertEqual(('file:///', 'foo'), split('file:///foo'))
 
153
            self.assertEqual(('file:///', ''), split('file:///'))
 
154
 
 
155
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo'))
 
156
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo/'))
 
157
        self.assertEqual(('http://host/path/to/foo', ''),
 
158
            split('http://host/path/to/foo/', exclude_trailing_slash=False))
 
159
        self.assertEqual(('http://host/', 'path'), split('http://host/path'))
 
160
        self.assertEqual(('http://host/', ''), split('http://host/'))
 
161
        self.assertEqual(('http://host', ''), split('http://host'))
 
162
        self.assertEqual(('http:///nohost', 'path'), split('http:///nohost/path'))
 
163
 
 
164
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
 
165
            split('random+scheme://user:pass@ahost:port/path'))
 
166
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
 
167
            split('random+scheme://user:pass@ahost:port/path/'))
 
168
        self.assertEqual(('random+scheme://user:pass@ahost:port/', ''),
 
169
            split('random+scheme://user:pass@ahost:port/'))
 
170
 
 
171
        # relative paths
 
172
        self.assertEqual(('path/to', 'foo'), split('path/to/foo'))
 
173
        self.assertEqual(('path/to', 'foo'), split('path/to/foo/'))
 
174
        self.assertEqual(('path/to/foo', ''),
 
175
            split('path/to/foo/', exclude_trailing_slash=False))
 
176
        self.assertEqual(('path/..', 'foo'), split('path/../foo'))
 
177
        self.assertEqual(('../path', 'foo'), split('../path/foo'))
 
178
 
76
179
    def test_strip_trailing_slash(self):
77
180
        sts = urlutils.strip_trailing_slash
78
181
        if sys.platform == 'win32':