mirror of
https://github.com/python/cpython.git
synced 2025-11-01 22:21:35 +00:00
GH-113528: Move a few misplaced pathlib tests (#113527)
`PurePathBase` does not define `__eq__()`, and so we have no business checking path equality in `test_eq_common` and `test_equivalences`. The tests only pass at the moment because we define the test class's `__eq__()` for use elsewhere. Also move `test_parse_path_common` into the main pathlib test suite. It exercises a private `_parse_path()` method that will be moved to `PurePath` soon. Lastly move a couple more tests concerned with optimisations and path normalisation.
This commit is contained in:
parent
aef375f56e
commit
a9df076d7d
2 changed files with 86 additions and 86 deletions
|
|
@ -45,6 +45,22 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
|
|||
# Make sure any symbolic links in the base test path are resolved.
|
||||
base = os.path.realpath(TESTFN)
|
||||
|
||||
# Keys are canonical paths, values are list of tuples of arguments
|
||||
# supposed to produce equal paths.
|
||||
equivalences = {
|
||||
'a/b': [
|
||||
('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'),
|
||||
('a/b/',), ('a//b',), ('a//b//',),
|
||||
# Empty components get removed.
|
||||
('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''),
|
||||
],
|
||||
'/b/c/d': [
|
||||
('a', '/b/c', 'd'), ('/a', '/b/c', 'd'),
|
||||
# Empty components get removed.
|
||||
('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'),
|
||||
],
|
||||
}
|
||||
|
||||
def test_concrete_class(self):
|
||||
if self.cls is pathlib.PurePath:
|
||||
expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath
|
||||
|
|
@ -95,6 +111,45 @@ def test_constructor_nested(self):
|
|||
self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c")))
|
||||
self.assertEqual(P(P('./a:b')), P('./a:b'))
|
||||
|
||||
def _check_parse_path(self, raw_path, *expected):
|
||||
sep = self.pathmod.sep
|
||||
actual = self.cls._parse_path(raw_path.replace('/', sep))
|
||||
self.assertEqual(actual, expected)
|
||||
if altsep := self.pathmod.altsep:
|
||||
actual = self.cls._parse_path(raw_path.replace('/', altsep))
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
def test_parse_path_common(self):
|
||||
check = self._check_parse_path
|
||||
sep = self.pathmod.sep
|
||||
check('', '', '', [])
|
||||
check('a', '', '', ['a'])
|
||||
check('a/', '', '', ['a'])
|
||||
check('a/b', '', '', ['a', 'b'])
|
||||
check('a/b/', '', '', ['a', 'b'])
|
||||
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
|
||||
check('a/b//c/d', '', '', ['a', 'b', 'c', 'd'])
|
||||
check('a/b/c/d', '', '', ['a', 'b', 'c', 'd'])
|
||||
check('.', '', '', [])
|
||||
check('././b', '', '', ['b'])
|
||||
check('a/./b', '', '', ['a', 'b'])
|
||||
check('a/./.', '', '', ['a'])
|
||||
check('/a/b', '', sep, ['a', 'b'])
|
||||
|
||||
def test_empty_path(self):
|
||||
# The empty path points to '.'
|
||||
p = self.cls('')
|
||||
self.assertEqual(str(p), '.')
|
||||
|
||||
def test_parts_interning(self):
|
||||
P = self.cls
|
||||
p = P('/usr/bin/foo')
|
||||
q = P('/usr/local/bin')
|
||||
# 'usr'
|
||||
self.assertIs(p.parts[1], q.parts[1])
|
||||
# 'bin'
|
||||
self.assertIs(p.parts[2], q.parts[3])
|
||||
|
||||
def test_join_nested(self):
|
||||
P = self.cls
|
||||
p = P('a/b').joinpath(P('c'))
|
||||
|
|
@ -168,6 +223,37 @@ def test_as_bytes_common(self):
|
|||
P = self.cls
|
||||
self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b')
|
||||
|
||||
def test_eq_common(self):
|
||||
P = self.cls
|
||||
self.assertEqual(P('a/b'), P('a/b'))
|
||||
self.assertEqual(P('a/b'), P('a', 'b'))
|
||||
self.assertNotEqual(P('a/b'), P('a'))
|
||||
self.assertNotEqual(P('a/b'), P('/a/b'))
|
||||
self.assertNotEqual(P('a/b'), P())
|
||||
self.assertNotEqual(P('/a/b'), P('/'))
|
||||
self.assertNotEqual(P(), P('/'))
|
||||
self.assertNotEqual(P(), "")
|
||||
self.assertNotEqual(P(), {})
|
||||
self.assertNotEqual(P(), int)
|
||||
|
||||
def test_equivalences(self):
|
||||
for k, tuples in self.equivalences.items():
|
||||
canon = k.replace('/', self.sep)
|
||||
posix = k.replace(self.sep, '/')
|
||||
if canon != posix:
|
||||
tuples = tuples + [
|
||||
tuple(part.replace('/', self.sep) for part in t)
|
||||
for t in tuples
|
||||
]
|
||||
tuples.append((posix, ))
|
||||
pcanon = self.cls(canon)
|
||||
for t in tuples:
|
||||
p = self.cls(*t)
|
||||
self.assertEqual(p, pcanon, "failed with args {}".format(t))
|
||||
self.assertEqual(hash(p), hash(pcanon))
|
||||
self.assertEqual(str(p), canon)
|
||||
self.assertEqual(p.as_posix(), posix)
|
||||
|
||||
def test_ordering_common(self):
|
||||
# Ordering is tuple-alike.
|
||||
def assertLess(a, b):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue