| 1 |
|
| 2 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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" |
| 69 |
result = forceunicode(garbage) |
| 70 |
|
| 71 |
def test_forcestr_should_not_fail(self): |
| 72 |
garbage = u"\xef\xff\xbb\xbf\xce\xbb\xff\xff" |
| 73 |
result = forcestr(garbage) |