11# encoding: utf-8
2-
2+ import sys
33import pytest
44import doctest
55
99from fractions import Fraction
1010inf , nan = float ('inf' ), float ('nan' )
1111
12+
1213class MyDocTestRunner (doctest .DocTestRunner ):
1314
1415 def __init__ (self ):
@@ -22,13 +23,17 @@ def report_failure(self, out, test, example, got):
2223class TestApprox :
2324
2425 def test_repr_string (self ):
25- # Just make sure the Unicode handling doesn't raise any exceptions.
26- print (approx (1.0 ))
27- print (approx ([1.0 , 2.0 , 3.0 ]))
28- print (approx (inf ))
29- print (approx (1.0 , rel = nan ))
30- print (approx (1.0 , rel = inf ))
31- print (approx (1.0j , rel = inf ))
26+ # for some reason in Python 2.6 it is not displaying the tolerance representation correctly
27+ plus_minus = u'\u00b1 ' if sys .version_info [0 ] > 2 else u'+-'
28+ tol1 , tol2 , infr = '1.0e-06' , '2.0e-06' , 'inf'
29+ if sys .version_info [:2 ] == (2 , 6 ):
30+ tol1 , tol2 , infr = '???' , '???' , '???'
31+ assert repr (approx (1.0 )) == '1.0 {pm} {tol1}' .format (pm = plus_minus , tol1 = tol1 )
32+ assert repr (approx ([1.0 , 2.0 ])) == '1.0 {pm} {tol1}, 2.0 {pm} {tol2}' .format (pm = plus_minus , tol1 = tol1 , tol2 = tol2 )
33+ assert repr (approx (inf )) == 'inf'
34+ assert repr (approx (1.0 , rel = nan )) == '1.0 {pm} ???' .format (pm = plus_minus )
35+ assert repr (approx (1.0 , rel = inf )) == '1.0 {pm} {infr}' .format (pm = plus_minus , infr = infr )
36+ assert repr (approx (1.0j , rel = inf )) == '1j'
3237
3338 def test_operator_overloading (self ):
3439 assert 1 == approx (1 , rel = 1e-6 , abs = 1e-12 )
@@ -285,3 +290,23 @@ def test_doctests(self):
285290 runner = MyDocTestRunner ()
286291 runner .run (test )
287292
293+ def test_unicode_plus_minus (self , testdir ):
294+ """
295+ Comparing approx instances inside lists should not produce an error in the detailed diff.
296+ Integration test for issue #2111.
297+ """
298+ testdir .makepyfile ("""
299+ import pytest
300+ def test_foo():
301+ assert [3] == [pytest.approx(4)]
302+ """ )
303+ expected = '4.0e-06'
304+ # for some reason in Python 2.6 it is not displaying the tolerance representation correctly
305+ if sys .version_info [:2 ] == (2 , 6 ):
306+ expected = '???'
307+ result = testdir .runpytest ()
308+ result .stdout .fnmatch_lines ([
309+ '*At index 0 diff: 3 != 4 * {0}' .format (expected ),
310+ '=* 1 failed in *=' ,
311+ ])
312+
0 commit comments