blob: 139aa1f28e06160dc72752c605872d2f37696dda [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Barry Warsaw04f357c2002-07-23 19:04:11 +00002from test.test_support import verbose, TestFailed
Mark Hammond673c6cf2000-08-14 06:21:26 +00003import os
Guido van Rossumead9d8d1999-02-03 17:21:21 +00004
5errors = 0
6
7def tester(fn, wantResult):
Tim Petersd4f7f602001-07-19 19:11:41 +00008 global errors
Eric S. Raymondfc170b12001-02-09 11:51:27 +00009 fn = fn.replace("\\", "\\\\")
Fred Drake004d5e62000-10-23 17:22:08 +000010 gotResult = eval(fn)
11 if wantResult != gotResult:
12 print "error!"
13 print "evaluated: " + str(fn)
14 print "should be: " + str(wantResult)
15 print " returned: " + str(gotResult)
16 print ""
Fred Drake004d5e62000-10-23 17:22:08 +000017 errors = errors + 1
Tim Peters6578dc92002-12-24 18:31:27 +000018
Martin v. Löwisde333792002-12-12 20:30:20 +000019tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
20tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
21tester('ntpath.splitext(".ext")', ('', '.ext'))
22tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
23tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
24tester('ntpath.splitext("")', ('', ''))
25tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
26tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
27tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000028
Tim Peters3b5e4d12001-07-19 19:02:12 +000029tester('ntpath.splitdrive("c:\\foo\\bar")',
30 ('c:', '\\foo\\bar'))
31tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
32 ('\\\\conky\\mountpoint', '\\foo\\bar'))
33tester('ntpath.splitdrive("c:/foo/bar")',
34 ('c:', '/foo/bar'))
35tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
36 ('//conky/mountpoint', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000037
38tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
Tim Peters3b5e4d12001-07-19 19:02:12 +000039tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
40 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000041
42tester('ntpath.split("c:\\")', ('c:\\', ''))
Tim Peters3b5e4d12001-07-19 19:02:12 +000043tester('ntpath.split("\\\\conky\\mountpoint\\")',
44 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000045
46tester('ntpath.split("c:/")', ('c:/', ''))
Guido van Rossum630a9a61999-04-06 19:38:18 +000047tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000048
49tester('ntpath.isabs("c:\\")', 1)
50tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
51tester('ntpath.isabs("\\foo")', 1)
52tester('ntpath.isabs("\\foo\\bar")', 1)
53
Skip Montanaro877d62e2000-08-23 16:54:27 +000054tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
55 "/home/swen")
56tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
57 "\\home\\swen\\")
58tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
59 "/home/swen/spam")
Mark Hammond673c6cf2000-08-14 06:21:26 +000060
Tim Petersd4f7f602001-07-19 19:11:41 +000061tester('ntpath.join("")', '')
62tester('ntpath.join("", "", "")', '')
63tester('ntpath.join("a")', 'a')
64tester('ntpath.join("/a")', '/a')
65tester('ntpath.join("\\a")', '\\a')
66tester('ntpath.join("a:")', 'a:')
67tester('ntpath.join("a:", "b")', 'a:b')
68tester('ntpath.join("a:", "/b")', 'a:/b')
69tester('ntpath.join("a:", "\\b")', 'a:\\b')
70tester('ntpath.join("a", "/b")', '/b')
71tester('ntpath.join("a", "\\b")', '\\b')
72tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
73tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
74tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
75tester('ntpath.join("a", "b", "\\c")', '\\c')
Tim Peters4223f892001-07-26 21:54:37 +000076tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
Tim Peters33dc0a12001-07-27 08:09:54 +000077tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
78tester("ntpath.join('c:', '/a')", 'c:/a')
79tester("ntpath.join('c:/', '/a')", 'c:/a')
80tester("ntpath.join('c:/a', '/b')", '/b')
81tester("ntpath.join('c:', 'd:/')", 'd:/')
82tester("ntpath.join('c:/', 'd:/')", 'd:/')
83tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Petersd4f7f602001-07-19 19:11:41 +000084
Tim Peters6a3e5f12001-11-05 21:25:02 +000085tester("ntpath.join('')", '')
86tester("ntpath.join('', '', '', '', '')", '')
87tester("ntpath.join('a')", 'a')
88tester("ntpath.join('', 'a')", 'a')
89tester("ntpath.join('', '', '', '', 'a')", 'a')
90tester("ntpath.join('a', '')", 'a\\')
91tester("ntpath.join('a', '', '', '', '')", 'a\\')
Tim Peterse5a611c2001-11-05 21:33:04 +000092tester("ntpath.join('a\\', '')", 'a\\')
93tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters6a3e5f12001-11-05 21:25:02 +000094
Tim Peters54a14a32001-08-30 22:05:26 +000095tester("ntpath.normpath('A//////././//.//B')", r'A\B')
96tester("ntpath.normpath('A/./B')", r'A\B')
97tester("ntpath.normpath('A/foo/../B')", r'A\B')
98tester("ntpath.normpath('C:A//B')", r'C:A\B')
99tester("ntpath.normpath('D:A/./B')", r'D:A\B')
100tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
101
Brett Cannonbdc36272004-07-10 20:42:22 +0000102tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
103tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
104tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
Tim Peters54a14a32001-08-30 22:05:26 +0000105
106tester("ntpath.normpath('..')", r'..')
107tester("ntpath.normpath('.')", r'.')
108tester("ntpath.normpath('')", r'.')
109tester("ntpath.normpath('/')", '\\')
110tester("ntpath.normpath('c:/')", 'c:\\')
111tester("ntpath.normpath('/../.././..')", '\\')
112tester("ntpath.normpath('c:/../../..')", 'c:\\')
113tester("ntpath.normpath('../.././..')", r'..\..\..')
114tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
Brett Cannonbdc36272004-07-10 20:42:22 +0000115tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
116tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
Tim Peters54a14a32001-08-30 22:05:26 +0000117
Fred Drake5e997312002-01-15 03:46:43 +0000118# ntpath.abspath() can only be used on a system with the "nt" module
119# (reasonably), so we protect this test with "import nt". This allows
120# the rest of the tests for the ntpath module to be run to completion
121# on any platform, since most of the module is intended to be usable
122# from any platform.
123try:
124 import nt
125except ImportError:
126 pass
127else:
128 tester('ntpath.abspath("C:\\")', "C:\\")
129
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000130if errors:
Tim Petersd4f7f602001-07-19 19:11:41 +0000131 raise TestFailed(str(errors) + " errors.")
Tim Peters3b5e4d12001-07-19 19:02:12 +0000132elif verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000133 print "No errors. Thank your lucky stars."