Use the plus/minus unicode symbol in the repr string.
This was a challenge because it had to work in python2 and python3, which have almost opposite unicode models, and I couldn't use the six library. I'm also not sure the solution I found would work in python3 before python3.3, because I use the u'' string prefix which I think was initially not part of python3.
This commit is contained in:
parent
dd28e28b34
commit
bf97d5b817
|
@ -1339,7 +1339,7 @@ class RaisesContext(object):
|
||||||
|
|
||||||
# builtin pytest.approx helper
|
# builtin pytest.approx helper
|
||||||
|
|
||||||
class approx:
|
class approx(object):
|
||||||
""" assert that two numbers (or two sets of numbers) are equal to each
|
""" assert that two numbers (or two sets of numbers) are equal to each
|
||||||
other within some margin.
|
other within some margin.
|
||||||
|
|
||||||
|
@ -1410,10 +1410,11 @@ class approx:
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
from collections import Iterable
|
from collections import Iterable
|
||||||
plus_minus = lambda x: '{} \u00B1 {:.1e}'.format(x, self._get_margin(x))
|
utf_8 = lambda s: s.encode('utf-8') if sys.version_info.major == 2 else s
|
||||||
|
plus_minus = lambda x: utf_8(u'{} \u00b1 {:.1e}'.format(x, self._get_margin(x)))
|
||||||
|
|
||||||
if isinstance(self.expected, Iterable):
|
if isinstance(self.expected, Iterable):
|
||||||
return str([plus_minus(x) for x in self.expected])
|
return ', '.join([plus_minus(x) for x in self.expected])
|
||||||
else:
|
else:
|
||||||
return plus_minus(self.expected)
|
return plus_minus(self.expected)
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ class TestApprox:
|
||||||
runner.run(test)
|
runner.run(test)
|
||||||
|
|
||||||
def test_repr_string(self):
|
def test_repr_string(self):
|
||||||
|
# Just make sure the Unicode handling doesn't raise any exceptions.
|
||||||
print(pytest.approx(1.0))
|
print(pytest.approx(1.0))
|
||||||
assert repr(pytest.approx(1.0)) == '1.0 ± 1.0e-06'
|
print(pytest.approx([1.0, 2.0, 3.0]))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue