Debian Codegraph @

tests/test_safe_repr.py

raw symbols SCIP
1 ## #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import unicode_literals
5
6 from six import PY2
7
8 from sure import expect
9 from sure.core import safe_repr
10 from sure.compat import compat_repr
11
12
13 def test_basic_list():
14 "safe_repr should display a simple list"
15 X = ['one', 'yeah']
16 expect(safe_repr(X)).should.equal(compat_repr(
17 "['one', 'yeah']"
18 ))
19
20
21 def test_basic_dict():
22 "safe_repr should return a sorted repr"
23 X = {'b': 'd', 'a': 'c'}
24 expect(safe_repr(X)).should.equal(compat_repr(
25 "{'a': 'c', 'b': 'd'}"
26 ))
27
28
29 def test_nested_dict():
30 "dicts nested inside values should also get sorted"
31 X = {'my::all_users': [{'age': 33, 'name': 'John', 'foo': 'bar'}]}
32 expect(safe_repr(X)).should.equal(compat_repr(
33 '''{'my::all_users': [{'age': 33, 'foo': 'bar', 'name': 'John'}]}'''
34 ))
35
36
37 def test_unicode():
38 "dicts with unicode should work properly"
39 class Y(object):
40 def __init__(self, x):
41 self.x = x
42
43 def __repr__(self):
44 if PY2:
45 # PY2 should return the regular (unicode) string
46 return self.x.encode('utf-8')
47 else:
48 return self.x
49
50 def __eq__(self, other):
51 return self.x == other.x
52
53 y1 = {
54 'a': 2,
55 'b': Y('Gabriel Falcão'),
56 'c': 'Foo',
57 }
58 name = 'Gabriel Falc\xe3o' if PY2 else 'Gabriel Falcão'
59
60 expect(safe_repr(y1)).should.equal(compat_repr(
61 "{'a': 2, 'b': %s, 'c': 'Foo'}" % name
62 ))