Debian Codegraph @

dotviewer/test/test_unicode_util.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 import py
5 import codecs
6 from dotviewer.strunicode import RAW_ENCODING, forcestr, forceunicode, tryencode
7
8 SOURCE1 = u"""digraph G{
9 λ -> b
10 b -> μ
11 }
12 """
13
14 FILENAME = 'test.dot'
15
16 class TestUnicodeUtil(object):
17
18 def test_idempotent(self):
19 x = u"a"
20 assert forceunicode(forcestr(x)) == x
21
22 x = u"λ"
23 assert forceunicode(forcestr(x)) == x
24
25 assert forceunicode(forcestr(SOURCE1)) == SOURCE1
26
27 x = "a"
28 assert forcestr(forceunicode(x)) == x
29
30 # utf-8 encoded.
31 # fragile, does not consider RAW_ENCODING
32 # x = "\xef\xbb\xbf\xce\xbb"
33 # assert forcestr(forceunicode(x)) == x
34
35 def test_does_not_double_encode(self):
36 x = u"λ"
37 x_e = forcestr(x)
38 assert forcestr(x_e) == x_e
39
40 x_u = forceunicode(x_e)
41 assert forceunicode(x_u) == x_u
42
43 def test_file(self):
44 udir = py.path.local.make_numbered_dir(prefix='usession-dot-', keep=3)
45 full_filename = str(udir.join(FILENAME))
46 f = codecs.open(full_filename, 'wb', RAW_ENCODING)
47 f.write(SOURCE1)
48 f.close()
49
50 with open(full_filename) as f1:
51 assert forceunicode(f1.read()) == SOURCE1
52
53 f3 = codecs.open(full_filename, 'r', RAW_ENCODING)
54 c = f3.read()
55 f3.close()
56 result = (c == SOURCE1)
57 assert result
58
59 def test_only_unicode_encode(self):
60 sut = [1, u"a", "miau", u"λ"]
61 expected = [int, str, str, str ]
62
63 results = map(tryencode, sut)
64 for result, expected_type in zip(results, expected):
65 assert isinstance(result, expected_type)
66
67 def test_forceunicode_should_not_fail(self):
68 garbage = "\xef\xff\xbb\xbf\xce\xbb\xff\xff" # garbage with a lambda
69 result = forceunicode(garbage) # should not raise
70
71 def test_forcestr_should_not_fail(self):
72 garbage = u"\xef\xff\xbb\xbf\xce\xbb\xff\xff" # garbage
73 result = forcestr(garbage) # should not raise