blob: 336e9e2e7a0d29c197272a8e820fa1f6fb0dfc0e [file] [log] [blame]
Guido van Rossumead9d8d1999-02-03 17:21:21 +00001import ntpath
Tim Petersd4f7f602001-07-19 19:11:41 +00002from 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
Guido van Rossumead9d8d1999-02-03 17:21:21 +000018
Tim Peters3b5e4d12001-07-19 19:02:12 +000019tester('ntpath.splitdrive("c:\\foo\\bar")',
20 ('c:', '\\foo\\bar'))
21tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
22 ('\\\\conky\\mountpoint', '\\foo\\bar'))
23tester('ntpath.splitdrive("c:/foo/bar")',
24 ('c:', '/foo/bar'))
25tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
26 ('//conky/mountpoint', '/foo/bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000027
28tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
Tim Peters3b5e4d12001-07-19 19:02:12 +000029tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
30 ('\\\\conky\\mountpoint\\foo', 'bar'))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000031
32tester('ntpath.split("c:\\")', ('c:\\', ''))
Tim Peters3b5e4d12001-07-19 19:02:12 +000033tester('ntpath.split("\\\\conky\\mountpoint\\")',
34 ('\\\\conky\\mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000035
36tester('ntpath.split("c:/")', ('c:/', ''))
Guido van Rossum630a9a61999-04-06 19:38:18 +000037tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
Guido van Rossumead9d8d1999-02-03 17:21:21 +000038
39tester('ntpath.isabs("c:\\")', 1)
40tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
41tester('ntpath.isabs("\\foo")', 1)
42tester('ntpath.isabs("\\foo\\bar")', 1)
43
Mark Hammond673c6cf2000-08-14 06:21:26 +000044tester('ntpath.abspath("C:\\")', "C:\\")
Mark Hammond673c6cf2000-08-14 06:21:26 +000045
Skip Montanaro877d62e2000-08-23 16:54:27 +000046tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
47 "/home/swen")
48tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
49 "\\home\\swen\\")
50tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
51 "/home/swen/spam")
Mark Hammond673c6cf2000-08-14 06:21:26 +000052
Tim Petersd4f7f602001-07-19 19:11:41 +000053tester('ntpath.join("")', '')
54tester('ntpath.join("", "", "")', '')
55tester('ntpath.join("a")', 'a')
56tester('ntpath.join("/a")', '/a')
57tester('ntpath.join("\\a")', '\\a')
58tester('ntpath.join("a:")', 'a:')
59tester('ntpath.join("a:", "b")', 'a:b')
60tester('ntpath.join("a:", "/b")', 'a:/b')
61tester('ntpath.join("a:", "\\b")', 'a:\\b')
62tester('ntpath.join("a", "/b")', '/b')
63tester('ntpath.join("a", "\\b")', '\\b')
64tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
65tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
66tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
67tester('ntpath.join("a", "b", "\\c")', '\\c')
Tim Peters4223f892001-07-26 21:54:37 +000068tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
Tim Peters33dc0a12001-07-27 08:09:54 +000069tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
70tester("ntpath.join('c:', '/a')", 'c:/a')
71tester("ntpath.join('c:/', '/a')", 'c:/a')
72tester("ntpath.join('c:/a', '/b')", '/b')
73tester("ntpath.join('c:', 'd:/')", 'd:/')
74tester("ntpath.join('c:/', 'd:/')", 'd:/')
75tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Petersd4f7f602001-07-19 19:11:41 +000076
Tim Peters6a3e5f12001-11-05 21:25:02 +000077tester("ntpath.join('')", '')
78tester("ntpath.join('', '', '', '', '')", '')
79tester("ntpath.join('a')", 'a')
80tester("ntpath.join('', 'a')", 'a')
81tester("ntpath.join('', '', '', '', 'a')", 'a')
82tester("ntpath.join('a', '')", 'a\\')
83tester("ntpath.join('a', '', '', '', '')", 'a\\')
Tim Peterse5a611c2001-11-05 21:33:04 +000084tester("ntpath.join('a\\', '')", 'a\\')
85tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters6a3e5f12001-11-05 21:25:02 +000086
Tim Peters54a14a32001-08-30 22:05:26 +000087tester("ntpath.normpath('A//////././//.//B')", r'A\B')
88tester("ntpath.normpath('A/./B')", r'A\B')
89tester("ntpath.normpath('A/foo/../B')", r'A\B')
90tester("ntpath.normpath('C:A//B')", r'C:A\B')
91tester("ntpath.normpath('D:A/./B')", r'D:A\B')
92tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
93
94# Next 3 seem dubious, and especially the 3rd, but normpath is possibly
95# trying to leave UNC paths alone without actually knowing anything about
96# them.
97tester("ntpath.normpath('C:///A//B')", r'C:\\\A\B')
98tester("ntpath.normpath('D:///A/./B')", r'D:\\\A\B')
99tester("ntpath.normpath('e:///A/foo/../B')", r'e:\\\A\B')
100
101tester("ntpath.normpath('..')", r'..')
102tester("ntpath.normpath('.')", r'.')
103tester("ntpath.normpath('')", r'.')
104tester("ntpath.normpath('/')", '\\')
105tester("ntpath.normpath('c:/')", 'c:\\')
106tester("ntpath.normpath('/../.././..')", '\\')
107tester("ntpath.normpath('c:/../../..')", 'c:\\')
108tester("ntpath.normpath('../.././..')", r'..\..\..')
109tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
110
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000111if errors:
Tim Petersd4f7f602001-07-19 19:11:41 +0000112 raise TestFailed(str(errors) + " errors.")
Tim Peters3b5e4d12001-07-19 19:02:12 +0000113elif verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000114 print "No errors. Thank your lucky stars."