diff --git a/scrapy/trunk/scrapy/tests/test_utils_python.py b/scrapy/trunk/scrapy/tests/test_utils_python.py new file mode 100644 index 000000000..92e76781c --- /dev/null +++ b/scrapy/trunk/scrapy/tests/test_utils_python.py @@ -0,0 +1,27 @@ +import unittest + +from scrapy.utils.python import str_to_unicode, unicode_to_str + +class UtilsPythonTestCase(unittest.TestCase): + def test_str_to_unicode(self): + # converting an utf-8 encoded string to unicode + self.assertEqual(str_to_unicode('lel\xc3\xb1e'), u'lel\xf1e') + + # converting a unicode to unicode should return the same object + self.assertEqual(str_to_unicode(u'\xf1e\xf1e\xf1e'), u'\xf1e\xf1e\xf1e') + + # converting a strange object should raise TypeError + self.assertRaises(TypeError, str_to_unicode, 423) + + def test_unicode_to_str(self): + # converting a unicode object to an utf-8 encoded string + self.assertEqual(unicode_to_str(u'\xa3 49'), '\xc2\xa3 49') + + # converting a regular string to string should return the same object + self.assertEqual(unicode_to_str('lel\xf1e'), 'lel\xf1e') + + # converting a strange object should raise TypeError + self.assertRaises(TypeError, unicode_to_str, unittest) + +if __name__ == "__main__": + unittest.main()