blob: 40d785f04ec54b5433e0e312f2b6ab69078baf9a [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
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
Skip Montanaro877d62e2000-08-23 16:54:27 +000044tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
45 "/home/swen")
46tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
47 "\\home\\swen\\")
48tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
49 "/home/swen/spam")
Mark Hammond673c6cf2000-08-14 06:21:26 +000050
Tim Petersd4f7f602001-07-19 19:11:41 +000051tester('ntpath.join("")', '')
52tester('ntpath.join("", "", "")', '')
53tester('ntpath.join("a")', 'a')
54tester('ntpath.join("/a")', '/a')
55tester('ntpath.join("\\a")', '\\a')
56tester('ntpath.join("a:")', 'a:')
57tester('ntpath.join("a:", "b")', 'a:b')
58tester('ntpath.join("a:", "/b")', 'a:/b')
59tester('ntpath.join("a:", "\\b")', 'a:\\b')
60tester('ntpath.join("a", "/b")', '/b')
61tester('ntpath.join("a", "\\b")', '\\b')
62tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
63tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
64tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
65tester('ntpath.join("a", "b", "\\c")', '\\c')
Tim Peters4223f892001-07-26 21:54:37 +000066tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
Tim Peters33dc0a12001-07-27 08:09:54 +000067tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
68tester("ntpath.join('c:', '/a')", 'c:/a')
69tester("ntpath.join('c:/', '/a')", 'c:/a')
70tester("ntpath.join('c:/a', '/b')", '/b')
71tester("ntpath.join('c:', 'd:/')", 'd:/')
72tester("ntpath.join('c:/', 'd:/')", 'd:/')
73tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
Tim Petersd4f7f602001-07-19 19:11:41 +000074
Tim Peters6a3e5f12001-11-05 21:25:02 +000075tester("ntpath.join('')", '')
76tester("ntpath.join('', '', '', '', '')", '')
77tester("ntpath.join('a')", 'a')
78tester("ntpath.join('', 'a')", 'a')
79tester("ntpath.join('', '', '', '', 'a')", 'a')
80tester("ntpath.join('a', '')", 'a\\')
81tester("ntpath.join('a', '', '', '', '')", 'a\\')
Tim Peterse5a611c2001-11-05 21:33:04 +000082tester("ntpath.join('a\\', '')", 'a\\')
83tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
Tim Peters6a3e5f12001-11-05 21:25:02 +000084
Tim Peters54a14a32001-08-30 22:05:26 +000085tester("ntpath.normpath('A//////././//.//B')", r'A\B')
86tester("ntpath.normpath('A/./B')", r'A\B')
87tester("ntpath.normpath('A/foo/../B')", r'A\B')
88tester("ntpath.normpath('C:A//B')", r'C:A\B')
89tester("ntpath.normpath('D:A/./B')", r'D:A\B')
90tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
91
92# Next 3 seem dubious, and especially the 3rd, but normpath is possibly
93# trying to leave UNC paths alone without actually knowing anything about
94# them.
95tester("ntpath.normpath('C:///A//B')", r'C:\\\A\B')
96tester("ntpath.normpath('D:///A/./B')", r'D:\\\A\B')
97tester("ntpath.normpath('e:///A/foo/../B')", r'e:\\\A\B')
98
99tester("ntpath.normpath('..')", r'..')
100tester("ntpath.normpath('.')", r'.')
101tester("ntpath.normpath('')", r'.')
102tester("ntpath.normpath('/')", '\\')
103tester("ntpath.normpath('c:/')", 'c:\\')
104tester("ntpath.normpath('/../.././..')", '\\')
105tester("ntpath.normpath('c:/../../..')", 'c:\\')
106tester("ntpath.normpath('../.././..')", r'..\..\..')
107tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
108
Fred Drake5e997312002-01-15 03:46:43 +0000109# ntpath.abspath() can only be used on a system with the "nt" module
110# (reasonably), so we protect this test with "import nt". This allows
111# the rest of the tests for the ntpath module to be run to completion
112# on any platform, since most of the module is intended to be usable
113# from any platform.
114try:
115 import nt
116except ImportError:
117 pass
118else:
119 tester('ntpath.abspath("C:\\")', "C:\\")
120
Guido van Rossumead9d8d1999-02-03 17:21:21 +0000121if errors:
Tim Petersd4f7f602001-07-19 19:11:41 +0000122 raise TestFailed(str(errors) + " errors.")
Tim Peters3b5e4d12001-07-19 19:02:12 +0000123elif verbose:
Fred Drake004d5e62000-10-23 17:22:08 +0000124 print "No errors. Thank your lucky stars."