@@ -549,26 +549,88 @@ def test_basic(self, format, input, expected):
549
549
550
550
551
551
class 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 , (u'-1.23457 k' , u'-1 k' , u'-1.23 k' )),
556
+ (- 1.23456789 , (u'-1.23457' , u'-1' , u'-1.23' )),
557
+ (- 0.123456789 , (u'-123.457 m' , u'-123 m' , u'-123.46 m' )),
558
+ (- 0.00123456789 , (u'-1.23457 m' , u'-1 m' , u'-1.23 m' )),
559
+ (- 0.0 , (u'0' , u'0' , u'0.00' )),
560
+ (- 0 , (u'0' , u'0' , u'0.00' )),
561
<
10000
/td>+ (0 , (u'0' , u'0' , u'0.00' )),
562
+ (1.23456789e-6 , (u'1.23457 \u03bc ' , u'1 \u03bc ' , u'1.23 \u03bc ' )),
563
+ (0.123456789 , (u'123.457 m' , u'123 m' , u'123.46 m' )),
564
+ (0.1 , (u'100 m' , u'100 m' , u'100.00 m' )),
565
+ (1 , (u'1' , u'1' , u'1.00' )),
566
+ (1.23456789 , (u'1.23457' , u'1' , u'1.23' )),
567
+ (999.9 , (u'999.9' , u'999' , u'999.90' )),
568
+ (1000 , (u'1 k' , u'1 k' , u'1.00 k' )),
569
+ (1001 , (u'1.001 k' , u'1 k' , u'1.00 k' )),
570
+ (100001 , (u'100.001 k' , u'100 k' , u'100.00 k' )),
571
+ (987654.321 , (u'987.654 k' , u'987 k' , u'987.65 k' ))
561
572
]
562
573
563
- @pytest .mark .parametrize ('unit, input, expected' , format_data )
564
- def test_formatting (self , unit , input , expected ):
574
+ @pytest .mark .parametrize ('input, expected' , raw_format_data )
575
+ def test_params (self , input , expected ):
565
576
"""
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).
577
+ Test the formatting of EngFormatter for various values of the 'places'
578
+ argument, in several cases:
579
+ 0. without unit but with a space separator;
580
+ 1. with both a unit and a space separator;
581
+ 2. with a unit but no space separator;
582
+ 3. with neihter a unit nor a space separator.
569
583
"""
570
- fmt = mticker .EngFormatter (unit )
571
- assert fmt (input ) == expected
584
+
585
+ UNIT = u's' # seconds
586
+ DIGITS = u'0123456789' # %timeit showed 10-20% faster search than set
587
+
588
+ # Case 0: unit='' (default) and space_sep=True (default).
589
+ # 'expected' already corresponds to this reference case.
590
+ exp_outputs = (_s for _s in expected ) # simple copy of 'expected'
591
+ formatters = (
592
+ mticker .EngFormatter (), # places=None (default)
593
+ mticker .EngFormatter (places = 0 ),
594
+ mticker .EngFormatter (places = 2 )
595
+ )
596
+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
597
+ assert _formatter (input ) == _exp_output
598
+
599
+ # Case 1: unit=UNIT and space_sep=True (default).
600
+ # Append a unit symbol to the reference case.
601
+ # Beware of the values in [1, 1000), where there is no prefix!
602
+ exp_outputs = (_s + u" " + UNIT if _s [- 1 ] in DIGITS # case w/o prefix
603
+ else _s + UNIT for _s in expected )
604
+ formatters = (
605
+ mticker .EngFormatter (unit = UNIT ), # places=None (default)
606
+ mticker .EngFormatter (unit = UNIT , places = 0 ),
607
+ mticker .EngFormatter (unit = UNIT , places = 2 )
608
+ )
609
+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
610
+ assert _formatter (input ) == _exp_output
611
+
612
+ # Case 2: unit=UNIT and space_sep=False.
613
+ # Remove the space separator from the reference case.
614
+ exp_outputs = (_s .replace (" " , "" ) + UNIT for _s in expected )
615
+ formatters = (
616
+ mticker .EngFormatter (unit = UNIT , space_sep = False ), # places=None
617
+ mticker .EngFormatter (unit = UNIT , places = 0 , space_sep = False ),
618
+ mticker .EngFor
A182
matter (unit = UNIT , places = 2 , space_sep = False )
619
+ )
620
+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
621
+ assert _formatter (input ) == _exp_output
622
+
623
+ # Case 3: unit='' (default) and space_sep=False.
624
+ # Remove the space separator from the reference case and append
625
+ # a unit symbol to it.
626
+ exp_outputs = (_s .replace (" " , "" ) for _s in expected )
627
+ formatters = (
628
+ mticker .EngFormatter (space_sep = False ), # places=None (default)
629
+ mticker .EngFormatter (places = 0 , space_sep = False ),
630
+ mticker .EngFormatter (places = 2 , space_sep = False )
631
+ )
632
+ for _formatter , _exp_output in zip (formatters , exp_outputs ):
633
+ assert _formatter (input ) == _exp_output
572
634
573
635
574
636
class TestPercentFormatter (object ):
0 commit comments