home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_ntpath.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  4.1 KB  |  115 lines

  1. import ntpath
  2. from 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.splitdrive("c:\\foo\\bar")',
  20.        ('c:', '\\foo\\bar'))
  21. tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")',
  22.        ('\\\\conky\\mountpoint', '\\foo\\bar'))
  23. tester('ntpath.splitdrive("c:/foo/bar")',
  24.        ('c:', '/foo/bar'))
  25. tester('ntpath.splitunc("//conky/mountpoint/foo/bar")',
  26.        ('//conky/mountpoint', '/foo/bar'))
  27.  
  28. tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
  29. tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
  30.        ('\\\\conky\\mountpoint\\foo', 'bar'))
  31.  
  32. tester('ntpath.split("c:\\")', ('c:\\', ''))
  33. tester('ntpath.split("\\\\conky\\mountpoint\\")',
  34.        ('\\\\conky\\mountpoint', ''))
  35.  
  36. tester('ntpath.split("c:/")', ('c:/', ''))
  37. tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', ''))
  38.  
  39. tester('ntpath.isabs("c:\\")', 1)
  40. tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
  41. tester('ntpath.isabs("\\foo")', 1)
  42. tester('ntpath.isabs("\\foo\\bar")', 1)
  43.  
  44. tester('ntpath.abspath("C:\\")', "C:\\")
  45.  
  46. tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
  47.        "/home/swen")
  48. tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
  49.        "\\home\\swen\\")
  50. tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
  51.        "/home/swen/spam")
  52.  
  53. tester('ntpath.join("")', '')
  54. tester('ntpath.join("", "", "")', '')
  55. tester('ntpath.join("a")', 'a')
  56. tester('ntpath.join("/a")', '/a')
  57. tester('ntpath.join("\\a")', '\\a')
  58. tester('ntpath.join("a:")', 'a:')
  59. tester('ntpath.join("a:", "b")', 'a:b')
  60. tester('ntpath.join("a:", "/b")', 'a:/b')
  61. tester('ntpath.join("a:", "\\b")', 'a:\\b')
  62. tester('ntpath.join("a", "/b")', '/b')
  63. tester('ntpath.join("a", "\\b")', '\\b')
  64. tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
  65. tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
  66. tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
  67. tester('ntpath.join("a", "b", "\\c")', '\\c')
  68. tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
  69. tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
  70. tester("ntpath.join('c:', '/a')", 'c:/a')
  71. tester("ntpath.join('c:/', '/a')", 'c:/a')
  72. tester("ntpath.join('c:/a', '/b')", '/b')
  73. tester("ntpath.join('c:', 'd:/')", 'd:/')
  74. tester("ntpath.join('c:/', 'd:/')", 'd:/')
  75. tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b')
  76.  
  77. tester("ntpath.join('')", '')
  78. tester("ntpath.join('', '', '', '', '')", '')
  79. tester("ntpath.join('a')", 'a')
  80. tester("ntpath.join('', 'a')", 'a')
  81. tester("ntpath.join('', '', '', '', 'a')", 'a')
  82. tester("ntpath.join('a', '')", 'a\\')
  83. tester("ntpath.join('a', '', '', '', '')", 'a\\')
  84. tester("ntpath.join('a\\', '')", 'a\\')
  85. tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
  86.  
  87. tester("ntpath.normpath('A//////././//.//B')", r'A\B')
  88. tester("ntpath.normpath('A/./B')", r'A\B')
  89. tester("ntpath.normpath('A/foo/../B')", r'A\B')
  90. tester("ntpath.normpath('C:A//B')", r'C:A\B')
  91. tester("ntpath.normpath('D:A/./B')", r'D:A\B')
  92. tester("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.
  97. tester("ntpath.normpath('C:///A//B')", r'C:\\\A\B')
  98. tester("ntpath.normpath('D:///A/./B')", r'D:\\\A\B')
  99. tester("ntpath.normpath('e:///A/foo/../B')", r'e:\\\A\B')
  100.  
  101. tester("ntpath.normpath('..')", r'..')
  102. tester("ntpath.normpath('.')", r'.')
  103. tester("ntpath.normpath('')", r'.')
  104. tester("ntpath.normpath('/')", '\\')
  105. tester("ntpath.normpath('c:/')", 'c:\\')
  106. tester("ntpath.normpath('/../.././..')", '\\')
  107. tester("ntpath.normpath('c:/../../..')", 'c:\\')
  108. tester("ntpath.normpath('../.././..')", r'..\..\..')
  109. tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
  110.  
  111. if errors:
  112.     raise TestFailed(str(errors) + " errors.")
  113. elif verbose:
  114.     print "No errors.  Thank your lucky stars."
  115.