@@ -549,26 +549,97 @@ def test_basic(self, format, input, expected):
549549
550550
551551class TestEngFormatter (object ):
552- format_data = [
553- ('' , 0.1 , u'100 m' ),
554- ('' , 1 , u'1' ),
555- ('' , 999.9 , u'999.9' ),
556- ('' , 1001 , u'1.001 k' ),
557- (u's' , 0.1 , u'100 ms' ),
558- (u's' , 1 , u'1 s' ),
559- (u's' , 999.9 , u'999.9 s' ),
560- (u's' , 1001 , u'1.001 ks' ),
552+ # (input, expected) where ''expected'' corresponds to the outputs
553+ # respectively returned when (places=None, places=0, places=2)
554+ raw_format_data = [
555+ (- 1234.56789 , ('-1.23457 k' , '-1 k' , '-1.23 k' )),
556+ (- 1.23456789 , ('-1.23457' , '-1' , '-1.23' )),
557+ (- 0.123456789 , ('-123.457 m' , '-123 m' , '-123.46 m' )),
558+ (- 0.00123456789 , ('-1.23457 m' , '-1 m' , '-1.23 m' )),
559+ (- 0.0 , ('0' , '0' , '0.00' )),
560+ (- 0 , ('0' , '0' , '0.00' )),
561+ (0 , ('0' , '0' , '0.00' )),
562+ (1.23456789e-6 , ('1.23457 \u03bc ' , '1 \u03bc ' , '1.23 \u03bc ' )),
563+ (0.123456789 , ('123.457 m' , '123 m' , '123.46 m' )),
564+ (0.1 , ('100 m' , '100 m' , '100.00 m' )),
565+ (1 , ('1' , '1' , '1.00' )),
566+ (1.23456789 , ('1.23457' , '1' , '1.23' )),
567+ (999.9 , ('999.9' , '1 k' , '999.90' )), # places=0: corner-case rounding
568+ (999.9999 , ('1 k' , '1 k' , '1.00 k' )), # corner-case roudning for all
569+ (1000 , ('1 k' , '1 k' , '1.00 k' )),
570+ (1001 , ('1.001 k' , '1 k' , '1.00 k' )),
571+ (100001 , ('100.001 k' , '100 k' , '100.00 k' )),
572+ (987654.321 , ('987.654 k' , '988 k' , '987.65 k' )),
573+ (1.23e27 , ('1230 Y' , '1230 Y' , '1230.00 Y' )) # OoR value (> 1000 Y)
561574 ]
562575
563- @pytest .mark .parametrize ('unit, input, expected' , format_data )
564- def test_formatting (self , unit , input , expected ):
576+ @pytest .mark .parametrize ('input, expected' , raw_format_data )
577+ def test_params (self , input , expected ):
565578 """
566- Test the formatting of EngFormatter with some inputs, against
567- instances with and without units. Cases focus on when no SI
568- prefix is present, for values in [1, 1000).
579+ Test the formatting of EngFormatter for various values of the 'places'
580+ argument, in several cases:
581+ 0. without a unit symbol but with a (default) space separator;
582+ 1. with both a unit symbol and a (default) space separator;
583+ 2. with both a unit symbol and some non default separators;
584+ 3. without a unit symbol but with some non default separators.
585+ Note that cases 2. and 3. are looped over several separator strings.
569586 """
570- fmt = mticker .EngFormatter (unit )
571- assert fmt (input ) == expected
587+
588+ UNIT = 's' # seconds
589+ DIGITS = '0123456789' # %timeit showed 10-20% faster search than set
590+
591+ # Case 0: unit='' (default) and sep=' ' (default).
592+ # 'expected' already corresponds to this reference case.
593+ exp_outputs = expected
594+ formatters = (
595+ mticker .EngFormatter (), # places=None (default)
596+ mticker .EngFormatter (places = 0 ),
597+ mticker .EngFormatter (places = 2 )
598+ )
599+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
600+ assert _formatter (input ) == _exp_output
601+
602+ # Case 1: unit=UNIT and sep=' ' (default).
603+ # Append a unit symbol to the reference case.
604+ # Beware of the values in [1, 1000), where there is no prefix!
605+ exp_outputs = (_s + " " + UNIT if _s [- 1 ] in DIGITS # case w/o prefix
606+ else _s + UNIT for _s in expected )
607+ formatters = (
608+ mticker .EngFormatter (unit = UNIT ), # places=None (default)
609+ mticker .EngFormatter (unit = UNIT , places = 0 ),
610+ mticker .EngFormatter (unit = UNIT , places = 2 )
611+ )
612+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
613+ assert _formatter (input ) == _exp_output
614+
615+ # Test several non default separators: no separator, a narrow
616+ # no-break space (unicode character) and an extravagant string.
617+ for _sep in ("" , "\N{NARROW NO-BREAK SPACE} " , "@_@" ):
618+ # Case 2: unit=UNIT and sep=_sep.
619+ # Replace the default space separator from the reference case
620+ # with the tested one `_sep` and append a unit symbol to it.
621+ exp_outputs = (_s + _sep + UNIT if _s [- 1 ] in DIGITS # no prefix
622+ else _s .replace (" " , _sep ) + UNIT
623+ for _s in expected )
624+ formatters = (
625+ mticker .EngFormatter (unit = UNIT , sep = _sep ), # places=None
626+ mticker .EngFormatter (unit = UNIT , places = 0 , sep = _sep ),
627+ mticker .EngFormatter (unit = UNIT , places = 2 , sep = _sep )
628+ )
629+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
630+ assert _formatter (input ) == _exp_output
631+
632+ # Case 3: unit='' (default) and sep=_sep.
633+ # Replace the default space separator from the reference case
634+ # with the tested one `_sep`. Reference case is already unitless.
635+ exp_outputs = (_s .replace (" " , _sep ) for _s in expected )
636+ formatters = (
637+ mticker .EngFormatter (sep = _sep ), # places=None (default)
638+ mticker .EngFormatter (places = 0 , sep = _sep ),
639+ mticker .EngFormatter (places = 2 , sep = _sep )
640+ )
641+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
642+ assert _formatter (input ) == _exp_output
572643
573644
574645class TestPercentFormatter (object ):
0 commit comments