#1 英文str与unicode 内容相同 但是类型不同
test_type
#2 中文str与unicode 内容与类型都不同
test_type
#3 open只能按照str读
test_open_str_file
test_open_unicode_file
#4 codecs只能按照unicode读
test_codecs_str_file
test_codecs_unicode_file
#5 open只能写str内容 (英文两种皆可)
test_file_can_not_write_unicode
#6 codecs只能写unicode内容 (英文两种皆可)
test_codecs_can_not_write_str
# -*- encoding: utf-8 -*-
import codecs
import unittest
import os
FAILED_CODECS = "test_codecs_can_not_write_str.txt"
FAILED_FILE = "test_file_can_not_write_unicode.txt"
STR_FILE = "str.txt"
UNICODE_FILE = "unicode.txt"
def to_unicode(unicode_or_str):
if isinstance(unicode_or_str, str):
value = unicode_or_str.decode('utf-8')
else:
value = unicode_or_str
return value
def to_str(unicode_or_str):
if isinstance(unicode_or_str, unicode):
value = unicode_or_str.encode('utf-8')
else:
value = unicode_or_str
return value
class Unicode(unittest.TestCase):
@classmethod
def setUpClass(cls):
with open(STR_FILE, "w") as outfile:
outfile.write("你好")
with codecs.open(UNICODE_FILE, "w", "utf-8") as outfile:
outfile.write(u"你好")
@classmethod
def tearDownClass(cls):
os.remove(FAILED_CODECS)
os.remove(FAILED_FILE)
os.remove(STR_FILE)
os.remove(UNICODE_FILE)
def test_open_str_file(self):
with open(STR_FILE, 'r') as infile:
line = infile.read()
self.assertIsInstance(line, str)
self.assertEqual("你好", line)
def test_open_unicode_file(self):
with open(UNICODE_FILE, 'r') as infile:
line = infile.read()
self.assertIsInstance(line, str)
self.assertEqual("你好", line)
def test_codecs_str_file(self):
with codecs.open(STR_FILE, "r", "utf-8") as infile:
line = infile.read()
self.assertIsInstance(line, unicode)
self.assertEqual(u"你好", line)
def test_codecs_unicode_file(self):
with codecs.open(UNICODE_FILE, "r", "utf-8") as infile:
line = infile.read()
self.assertIsInstance(line, unicode)
self.assertEqual(u"你好", line)
def test_file_can_not_write_unicode(self):
outfile = open(FAILED_FILE, "w")
outfile.write(u"hello")
with self.assertRaises(UnicodeEncodeError):
outfile.write(u"你好")
def test_codecs_can_not_write_str(self):
outfile = codecs.open(FAILED_CODECS, "w", "utf-8")
outfile.write("hello")
with self.assertRaises(UnicodeDecodeError):
outfile.write("你好")
def test_type(self):
self.assertIsInstance("你好", str)
self.assertIsInstance(u"你好", unicode)
self.assertIsInstance(to_unicode("你好"), unicode)
self.assertIsInstance(to_str(u"你好"), str)
self.assertIsInstance(to_str("你好"), str)
self.assertIsInstance(to_unicode(u"你好"), unicode)
self.assertIsInstance("hello", str)
self.assertIsInstance(u"hello", unicode)
self.assertIsInstance(to_unicode("hello"), unicode)
self.assertIsInstance(to_str(u"hello"), str)
self.assertIsInstance(to_str("hello"), str)
self.assertIsInstance(to_unicode(u"hello"), unicode)
def test_str_VS_str(self):
self.assertEqual("你好", "你好")
def test_unicode_VS_unicode(self):
self.assertEqual(u"你好", u"你好")
def test_str_VS_unicode1(self):
with self.assertRaises(UnicodeDecodeError):
print "你好" in u"你好"
def test_str_VS_unicode2(self):
self.assertEqual("hello", u"hello")
if __name__ == '__main__':
unittest.main()