]> granicus.if.org Git - python/commitdiff
#5609 - test_urllib coverage for url2pathname and pathname2url. Patch
authorSenthil Kumaran <senthil@uthcode.com>
Thu, 11 Apr 2013 03:51:19 +0000 (20:51 -0700)
committerSenthil Kumaran <senthil@uthcode.com>
Thu, 11 Apr 2013 03:51:19 +0000 (20:51 -0700)
contribution by Thomas Fenzl & Maksim Kozyarchuk

Lib/test/test_urllib.py

index 4f71b24a0877dfcaffc71cc30ea2f48020ffb189..140e5c8ab19966e18cb5fd0cff0ab95a8a423eaa 100644 (file)
@@ -11,6 +11,7 @@ from test import support
 import os
 import sys
 import tempfile
+from nturl2path import url2pathname, pathname2url
 
 from base64 import b64encode
 import collections
@@ -24,6 +25,8 @@ def hexescape(char):
 
 # Shortcut for testing FancyURLopener
 _urlopener = None
+
+
 def urlopen(url, data=None, proxies=None):
     """urlopen(url [, data]) -> open file-like object"""
     global _urlopener
@@ -1312,24 +1315,60 @@ class RequestTests(unittest.TestCase):
         self.assertEqual(request.get_method(), 'HEAD')
 
 
-def test_main():
-    support.run_unittest(
-        urlopen_FileTests,
-        urlopen_HttpTests,
-        urlretrieve_FileTests,
-        urlretrieve_HttpTests,
-        ProxyTests,
-        QuotingTests,
-        UnquotingTests,
-        urlencode_Tests,
-        Pathname_Tests,
-        Utility_Tests,
-        URLopener_Tests,
-        #FTPWrapperTests,
-        RequestTests,
-    )
+class URL2PathNameTests(unittest.TestCase):
+
+    def test_converting_drive_letter(self):
+        self.assertEqual(url2pathname("///C|"), 'C:')
+        self.assertEqual(url2pathname("///C:"), 'C:')
+        self.assertEqual(url2pathname("///C|/"), 'C:\\')
+
+    def test_converting_when_no_drive_letter(self):
+        # cannot end a raw string in \
+        self.assertEqual(url2pathname("///C/test/"), r'\\\C\test' '\\')
+        self.assertEqual(url2pathname("////C/test/"), r'\\C\test' '\\')
+
+    def test_simple_compare(self):
+        self.assertEqual(url2pathname("///C|/foo/bar/spam.foo"),
+                         r'C:\foo\bar\spam.foo')
+
+    def test_non_ascii_drive_letter(self):
+        self.assertRaises(IOError, url2pathname, "///\u00e8|/")
+
+    def test_roundtrip_url2pathname(self):
+        list_of_paths = ['C:',
+                         r'\\\C\test\\',
+                         r'C:\foo\bar\spam.foo'
+                         ]
+        for path in list_of_paths:
+                self.assertEqual(url2pathname(pathname2url(path)), path)
+
+class PathName2URLTests(unittest.TestCase):
+
+    def test_converting_drive_letter(self):
+        self.assertEqual(pathname2url("C:"), '///C:')
+        self.assertEqual(pathname2url("C:\\"), '///C:')
+
+    def test_converting_when_no_drive_letter(self):
+        self.assertEqual(pathname2url(r"\\\folder\test" "\\"),
+                         '/////folder/test/')
+        self.assertEqual(pathname2url(r"\\folder\test" "\\"),
+                         '////folder/test/')
+        self.assertEqual(pathname2url(r"\folder\test" "\\"),
+                         '/folder/test/')
+
+    def test_simple_compare(self):
+        self.assertEqual(pathname2url(r'C:\foo\bar\spam.foo'),
+                         "///C:/foo/bar/spam.foo" )
 
+    def test_long_drive_letter(self):
+        self.assertRaises(IOError, pathname2url, "XX:\\")
 
+    def test_roundtrip_pathname2url(self):
+        list_of_paths = ['///C:',
+                         '/////folder/test/',
+                         '///C:/foo/bar/spam.foo']
+        for path in list_of_paths:
+                self.assertEqual(pathname2url(url2pathname(path)), path)
 
 if __name__ == '__main__':
-    test_main()
+    unittest.main()