[Zope3-checkins] CVS: Zope3/src/zope/app/traversing/tests - test_conveniencefunctions.py:1.15

Albertas Agejevas alga@codeworks.lt
Tue, 8 Apr 2003 14:26:35 -0400


Update of /cvs-repository/Zope3/src/zope/app/traversing/tests
In directory cvs.zope.org:/tmp/cvs-serv3531/tests

Modified Files:
	test_conveniencefunctions.py 
Log Message:
Enhanced joinPath to normalize . and .. path elements, added a
more verbose docstring and more tests.


=== Zope3/src/zope/app/traversing/tests/test_conveniencefunctions.py 1.14 => 1.15 ===
--- Zope3/src/zope/app/traversing/tests/test_conveniencefunctions.py:1.14	Mon Mar 31 08:32:06 2003
+++ Zope3/src/zope/app/traversing/tests/test_conveniencefunctions.py	Tue Apr  8 14:26:34 2003
@@ -233,76 +233,152 @@
             self.root
             )
 
-    _bad_locations = (
-        (UnicodeError, '\xa323'),
-        (ValueError, ''),
-        (ValueError, '//'),
-        (ValueError, '/foo//bar'),
-
-        # regarding the next two errors:
-        # having a trailing slash on a location is undefined.
-        # we might want to give it a particular meaning for zope3 later
-        # for now, it is an invalid location identifier
-        (ValueError, '/foo/bar/'),
-        (ValueError, 'foo/bar/'),
+    def testCanonicalPath(self):
 
-        (IndexError, '/a/../..'),
-        (ValueError, '/a//v'),
+        _bad_locations = (
+            (UnicodeError, '\xa323'),
+            (ValueError, ''),
+            (ValueError, '//'),
+            (ValueError, '/foo//bar'),
+
+            # regarding the next two errors:
+            # having a trailing slash on a location is undefined.
+            # we might want to give it a particular meaning for zope3 later
+            # for now, it is an invalid location identifier
+            (ValueError, '/foo/bar/'),
+            (ValueError, 'foo/bar/'),
+
+            (IndexError, '/a/../..'),
+            (ValueError, '/a//v'),
+            )
+
+        # sequence of N-tuples:
+        #   (loc_returned_as_string, input, input, ...)
+        # The string and tuple are tested as input as well as being the
+        # specification for output.
+
+        _good_locations = (
+            # location returned as string
+            ( u'/xx/yy/zz',
+                # arguments to try in addition to the above
+                '/xx/yy/zz',
+                '/xx/./yy/ww/../zz',
+            ),
+            ( u'/xx/yy/zz',
+                '/xx/yy/zz',
+            ),
+            ( u'/xx',
+                '/xx',
+            ),
+            ( u'/',
+                '/',
+            ),
         )
 
-    # sequence of N-tuples:
-    #   (loc_returned_as_string, loc_returned_as_tuple, input, input, ...)
-    # The string and tuple are tested as input as well as being the
-    # specification for output.
-
-    _good_locations = (
-        # location returned as string
-        ( u'/xx/yy/zz',
-            # arguments to try in addition to the above
-            '/xx/yy/zz',
-            '/xx/./yy/ww/../zz',
-        ),
-        ( u'/xx/yy/zz',
-            '/xx/yy/zz',
-        ),
-        ( u'/xx',
-            '/xx',
-        ),
-        ( u'/',
-            '/',
-        ),
-    )
-
-    def testCanonicalPath(self):
         from zope.app.traversing import canonicalPath
 
-        for error_type, value in self._bad_locations:
+        for error_type, value in _bad_locations:
             self.assertRaises(error_type, canonicalPath, value)
 
-        for spec in self._good_locations:
+        for spec in _good_locations:
             correct_answer = spec[0]
             for argument in spec:
                 self.assertEqual(canonicalPath(argument), correct_answer,
                                  "failure on %s" % argument)
 
 
-    def testJoinPathDoubleSlash(self):
+    def test_normalizePath(self):
+
+        _bad_locations = (
+            (ValueError, '//'),
+            (ValueError, '/foo//bar'),
+            (IndexError, '/a/../..'),
+            (IndexError, '/a/./../..'),
+            )
+
+        # sequence of N-tuples:
+        #   (loc_returned_as_string, input, input, ...)
+        # The string and tuple are tested as input as well as being the
+        # specification for output.
+
+        _good_locations = (
+            # location returned as string
+            ( '/xx/yy/zz',
+              # arguments to try in addition to the above
+              '/xx/yy/zz',
+              '/xx/./yy/ww/../zz',
+              '/xx/./yy/ww/./../zz',
+            ),
+            ( 'xx/yy/zz',
+              # arguments to try in addition to the above
+              'xx/yy/zz',
+              'xx/./yy/ww/../zz',
+              'xx/./yy/ww/./../zz',
+            ),
+            ( '/xx/yy/zz',
+              '/xx/yy/zz',
+            ),
+            ( '/xx',
+              '/xx',
+            ),
+            ( '/',
+              '/',
+            ),
+        )
+
+
+        from zope.app.traversing import _normalizePath
+
+        for error_type, value in _bad_locations:
+            self.assertRaises(error_type, _normalizePath, value)
+
+        for spec in _good_locations:
+            correct_answer = spec[0]
+            for argument in spec:
+                self.assertEqual(_normalizePath(argument), correct_answer,
+                                 "failure on %s" % argument)
+
+    def test_joinPath_slashes(self):
         from zope.app.traversing import joinPath
         path = u'/'
-        args = ('/test', 'bla', '/foo/', '/bar')
-        self.assertEqual(joinPath(path, *args), u'/test/bla/foo/bar')
-        
-    def testJoinPathMultipleArgs(self):
+        args = ('/test', 'bla', '/foo', 'bar')
+        self.assertRaises(ValueError, joinPath, path, *args)
+
+        args = ('/test', 'bla', 'foo/', '/bar')
+        self.assertRaises(ValueError, joinPath, path, *args)
+
+    def test_joinPath(self):
         from zope.app.traversing import joinPath
-        path = u'/bla/'
+        path = u'/bla'
         args = ('foo', 'bar', 'baz', 'bone')
         self.assertEqual(joinPath(path, *args), u'/bla/foo/bar/baz/bone')
 
-    def testJoinPathNoSlash(self):
-        from zope.app.traversing import joinPath
         path = u'bla'
         args = ('foo', 'bar', 'baz', 'bone')
         self.assertEqual(joinPath(path, *args), u'bla/foo/bar/baz/bone')
+
+        path = u'bla'
+        args = ('foo', 'bar/baz', 'bone')
+        self.assertEqual(joinPath(path, *args), u'bla/foo/bar/baz/bone')
+
+        path = u'bla/'
+        args = ('foo', 'bar', 'baz', 'bone')
+        self.assertRaises(ValueError, joinPath, path, *args)
+
+    def test_joinPath_normalize(self):
+        from zope.app.traversing import joinPath
+        path = u'/bla'
+        args = ('foo', 'bar', '..', 'baz', 'bone')
+        self.assertEqual(joinPath(path, *args), u'/bla/foo/baz/bone')
+
+        path = u'bla'
+        args = ('foo', 'bar', '.', 'baz', 'bone')
+        self.assertEqual(joinPath(path, *args), u'bla/foo/bar/baz/bone')
+
+        path = u'/'
+        args = ('foo', 'bar', '.', 'baz', 'bone')
+        self.assertEqual(joinPath(path, *args), u'/foo/bar/baz/bone')
+
 
 def test_suite():
     return makeSuite(Test)