-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.py
64 lines (49 loc) · 1.64 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import doctest
from nose.tools import assert_equal
from nose.plugins.skip import SkipTest
from nose_parameterized import parameterized
from autorepr import autostr, autounicode, autorepr, text, to_text, to_bytes, PY2
if PY2:
to_dunderstr = to_bytes
else:
to_dunderstr = to_text
class Person(object):
def __init__(self, name):
self.name = name
ascii = Person("Alex")
uni = Person(u"☃")
bin = Person("\x00\xff")
@parameterized([
(1, ascii.name, autostr, ascii),
(2, text(ascii.name), autounicode, ascii),
(3, to_dunderstr(uni.name), autostr, uni),
(4, uni.name, autounicode, uni),
(5, bin.name, autostr, bin),
(6, u"\x00\xff", autounicode, bin),
])
def test_encodings(num, expected, func, input):
f = func("{self.name}")
res = f(input)
assert_equal(res, expected)
assert_equal(type(res), type(expected))
@parameterized([
("'Alex'", autorepr("{self.name!r}")),
("name='Alex'", autorepr(["name"])),
("42", autorepr("{foo}", foo=lambda self: 42)),
("name='Alex' foo=42", autorepr(["name", "foo"], foo=lambda self: 42)),
])
def test_autorepr(expected, func):
res = func(ascii)
assert_equal("<%s.Person %s at 0x%x>" %(__name__, expected, id(ascii)), res)
def test_with_function_as_input():
f = autounicode(autostr("{self.name} {foo}", foo=lambda x: 42))
assert_equal(f(ascii), "Alex 42")
def test_readme_doctests():
if not PY2:
raise SkipTest
res = doctest.testfile("README.rst", optionflags=doctest.ELLIPSIS, encoding="utf-8")
assert_equal(res.failed, 0)
if __name__ == '__main__':
import nose
nose.run(argv=[__file__])