| 1 | import ntpath |
| 2 | from test.test_support import verbose, TestFailed |
| 3 | import os |
| 4 | |
| 5 | errors = 0 |
| 6 | |
| 7 | def tester(fn, wantResult): |
| 8 | global errors |
| 9 | fn = fn.replace("\\", "\\\\") |
| 10 | 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 "" |
| 17 | errors = errors + 1 |
| 18 | |
| 19 | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
| 20 | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
| 21 | tester('ntpath.splitext(".ext")', ('', '.ext')) |
| 22 | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
| 23 | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
| 24 | tester('ntpath.splitext("")', ('', '')) |
| 25 | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
| 26 | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
| 27 | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
| 28 | |
| 29 | tester('ntpath.splitdrive("c:\\foo\\bar")', |
| 30 | ('c:', '\\foo\\bar')) |
| 31 | tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', |
| 32 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 33 | tester('ntpath.splitdrive("c:/foo/bar")', |
| 34 | ('c:', '/foo/bar')) |
| 35 | tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', |
| 36 | ('//conky/mountpoint', '/foo/bar')) |
| 37 | |
| 38 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
| 39 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 40 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
| 41 | |
| 42 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
| 43 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
| 44 | ('\\\\conky\\mountpoint', '')) |
| 45 | |
| 46 | tester('ntpath.split("c:/")', ('c:/', '')) |
| 47 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', '')) |
| 48 | |
| 49 | tester('ntpath.isabs("c:\\")', 1) |
| 50 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 51 | tester('ntpath.isabs("\\foo")', 1) |
| 52 | tester('ntpath.isabs("\\foo\\bar")', 1) |
| 53 | |
| 54 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 55 | "/home/swen") |
| 56 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 57 | "\\home\\swen\\") |
| 58 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 59 | "/home/swen/spam") |
| 60 | |
| 61 | tester('ntpath.join("")', '') |
| 62 | tester('ntpath.join("", "", "")', '') |
| 63 | tester('ntpath.join("a")', 'a') |
| 64 | tester('ntpath.join("/a")', '/a') |
| 65 | tester('ntpath.join("\\a")', '\\a') |
| 66 | tester('ntpath.join("a:")', 'a:') |
| 67 | tester('ntpath.join("a:", "b")', 'a:b') |
| 68 | tester('ntpath.join("a:", "/b")', 'a:/b') |
| 69 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
| 70 | tester('ntpath.join("a", "/b")', '/b') |
| 71 | tester('ntpath.join("a", "\\b")', '\\b') |
| 72 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 73 | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
| 74 | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
| 75 | tester('ntpath.join("a", "b", "\\c")', '\\c') |
| 76 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
| 77 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
| 78 | tester("ntpath.join('c:', '/a')", 'c:/a') |
| 79 | tester("ntpath.join('c:/', '/a')", 'c:/a') |
| 80 | tester("ntpath.join('c:/a', '/b')", '/b') |
| 81 | tester("ntpath.join('c:', 'd:/')", 'd:/') |
| 82 | tester("ntpath.join('c:/', 'd:/')", 'd:/') |
| 83 | tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b') |
| 84 | |
| 85 | tester("ntpath.join('')", '') |
| 86 | tester("ntpath.join('', '', '', '', '')", '') |
| 87 | tester("ntpath.join('a')", 'a') |
| 88 | tester("ntpath.join('', 'a')", 'a') |
| 89 | tester("ntpath.join('', '', '', '', 'a')", 'a') |
| 90 | tester("ntpath.join('a', '')", 'a\\') |
| 91 | tester("ntpath.join('a', '', '', '', '')", 'a\\') |
| 92 | tester("ntpath.join('a\\', '')", 'a\\') |
| 93 | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
| 94 | |
| 95 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 96 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 97 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 98 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 99 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 100 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
| 101 | |
| 102 | tester("ntpath.normpath('C:///A//B')", r'C:\A\B') |
| 103 | tester("ntpath.normpath('D:///A/./B')", r'D:\A\B') |
| 104 | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B') |
| 105 | |
| 106 | tester("ntpath.normpath('..')", r'..') |
| 107 | tester("ntpath.normpath('.')", r'.') |
| 108 | tester("ntpath.normpath('')", r'.') |
| 109 | tester("ntpath.normpath('/')", '\\') |
| 110 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 111 | tester("ntpath.normpath('/../.././..')", '\\') |
| 112 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 113 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 114 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 115 | tester("ntpath.normpath('C:////a/b')", r'C:\a\b') |
| 116 | tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') |
| 117 | |
| 118 | # 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. |
| 123 | try: |
| 124 | import nt |
| 125 | except ImportError: |
| 126 | pass |
| 127 | else: |
| 128 | tester('ntpath.abspath("C:\\")', "C:\\") |
| 129 | |
| 130 | if errors: |
| 131 | raise TestFailed(str(errors) + " errors.") |
| 132 | elif verbose: |
| 133 | print "No errors. Thank your lucky stars." |