@@ -452,23 +452,111 @@ def test_percentformatter():
452452
453453def test_EngFormatter_formatting ():
454454 """
455- Create two instances of EngFormatter with default parameters, with and
456- without a unit string ('s' for seconds). Test the formatting in some cases,
457- especially the case when no SI prefix is present, for values in [1, 1000) .
455+ Test instances of EngFormatter for different values of the kwarg `places`,
456+ with and without a unit symbol, as well as with and without a space
457+ separator between the value and the prefix/unit .
458458
459- Should not raise exceptions .
459+ Should not exception .
460460 """
461- unitless = mticker .EngFormatter ()
462- nose .tools .assert_equal (unitless (0.1 ), u'100 m' )
463- nose .tools .assert_equal (unitless (1 ), u'1' )
464- nose .tools .assert_equal (unitless (999.9 ), u'999.9' )
465- nose .tools .assert_equal (unitless (1001 ), u'1.001 k' )
466-
467- with_unit = mticker .EngFormatter (unit = u's' )
468- nose .tools .assert_equal (with_unit (0.1 ), u'100 ms' )
469- nose .tools .assert_equal (with_unit (1 ), u'1 s' )
470- nose .tools .assert_equal (with_unit (999.9 ), u'999.9 s' )
471- nose .tools .assert_equal (with_unit (1001 ), u'1.001 ks' )
461+ UNIT = u's'
462+ DIGITS = set ([u'{0:d}' .format (d ) for d in range (10 )])
463+
464+ # Case A/ With kwarg `places=None` (default)
465+ A_wo_unit_w_sep = ((- 1234.56789 , u'-1.23457 k' ),
466+ (- 1.23456789 , u'-1.23457' ),
467+ (- 0.123456789 , u'-123.457 m' ),
468+ (- 0.00123456789 , u'-1.23457 m' ),
469+ (- 0.0 , u'0' ),
470+ (- 0 , u'0' ),
471+ (0 , u'0' ),
472+ (1.23456789e-6 , u'1.23457 \u03bc ' ),
473+ (0.123456789 , u'123.457 m' ),
474+ (1 , u'1' ),
475+ (1.23456789 , u'1.23457' ),
476+ (999.9 , u'999.9' ),
477+ (1000 , u'1 k' ),
478+ (1001 , u'1.001 k' ),
479+ (100001 , u'100.001 k' ),
480+ (987654.321 , u'987.654 k' ))
481+
482+ # Case B/ With kwarg `places=0`
483+ B_wo_unit_w_sep = ((- 1234.56789 , u'-1 k' ),
484+ (- 1.23456789 , u'-1' ),
485+ (- 0.123456789 , u'-123 m' ),
486+ (- 0.00123456789 , u'-1 m' ),
487+ (- 0.0 , u'0' ),
488+ (- 0 , u'0' ),
489+ (0 , u'0' ),
490+ (1.23456789e-6 , u'1 \u03bc ' ),
491+ (0.123456789 , u'123 m' ),
492+ (1 , u'1' ),
493+ (1.23456789 , u'1' ),
494+ (999.9 , u'999' ),
495+ (1000 , u'1 k' ),
496+ (1001 , u'1 k' ),
497+ (100001 , u'100 k' ),
498+ (987654.321 , u'987 k' ))
499+
500+ # Case C/ With kwarg `places=2`
501+ C_wo_unit_w_sep = ((- 1234.56789 , u'-1.23 k' ),
502+ (- 1.23456789 , u'-1.23' ),
503+ (- 0.123456789 , u'-123.46 m' ),
504+ (- 0.00123456789 , u'-1.23 m' ),
505+ (- 0.0 , u'0.00' ),
506+ (- 0 , u'0.00' ),
507+ (0 , u'0.00' ),
508+ (1.23456789e-6 , u'1.23 \u03bc ' ),
509+ (0.123456789 , u'123.46 m' ),
510+ (1 , u'1.00' ),
511+ (1.23456789 , u'1.23' ),
512+ (999.9 , u'999.90' ),
513+ (1000 , u'1.00 k' ),
514+ (1001 , u'1.00 k' ),
515+ (100001 , u'100.00 k' ),
516+ (987654.321 , u'987.65 k' ))
517+
518+ for ref_cases , places in ((A_wo_unit_w_sep , None ),
519+ (B_wo_unit_w_sep , 0 ),
520+ (C_wo_unit_w_sep , 2 )):
521+
522+ """ Test an EngFormatter instance w/o unit and w/ space separator
523+ (default)
524+ """
525+ eng_formatter = mticker .EngFormatter (places = places )
526+ for value , expected_string in ref_cases :
527+ nose .tools .assert_equal (eng_formatter (value ), expected_string )
528+
529+ """ Test an EngFormatter instance w/ unit and w/ space separator
530+ """
531+ eng_formatter = mticker .EngFormatter (unit = UNIT , places = places )
532+ # Append a unit symbol to the reference cases
533+ # Beware of the case of values in [1, 1000), where there is no prefix!
534+ test_cases = ((val , u"{s:s} {u:s}" .format (s = s , u = UNIT ))
535+ if s [- 1 ] in DIGITS # i.e. if no prefix is present
536+ else (val , u"{s:s}{u:s}" .format (s = s , u = UNIT ))
537+ for val , s in ref_cases )
538+ for value , expected_string in test_cases :
539+ nose .tools .assert_equal (eng_formatter (value ), expected_string )
540+
541+ """ Test an EngFormatter instance w/o unit and w/o space separator
542+ """
543+ eng_formatter = mticker .EngFormatter (places = places , space_sep = False )
544+ # Remove the space separator from the reference cases
545+ test_cases = ((val , u"{s:s}" .format (s = s .replace (" " , "" )))
546+ for val , s in ref_cases )
547+ for value , expected_string in test_cases :
548+ nose .tools .assert_equal (eng_formatter (value ), expected_string )
549+
550+ """ Test an EngFormatter instance w/ unit and w/o space separator
551+ """
552+ eng_formatter = mticker .EngFormatter (unit = UNIT , places = places ,
553+ space_sep = False )
554+ # Remove the space separator from the reference cases
555+ # and append them a unit symbol
556+ test_cases = ((val , u"{s:s}{u:s}" .format (s = s .replace (" " , "" ), u = UNIT ))
557+ for val , s in ref_cases )
558+ for value , expected_string in test_cases :
559+ nose .tools .assert_equal (eng_formatter (value ), expected_string )
472560
473561if __name__ == '__main__' :
474562 import nose
0 commit comments