@@ -510,13 +510,40 @@ def test_basic(self):
510510 tmp_form = mticker .FormatStrFormatter ('%05d' )
511511 assert '00002' == tmp_form (2 )
512512
513- # test str.format() style formatter
514- tmp_form = mticker .StrMethodFormatter ('{x:05d}' )
515- assert '00002' == tmp_form (2 )
516513
517- # test str.format() style formatter with `pos`
518- tmp_form = mticker .StrMethodFormatter ('{x:03d}-{pos:02d}' )
519- assert '002-01' == tmp_form (2 , 1 )
514+ class TestStrMethodFormatter (object ):
515+ test_data = [
516+ ('{x:05d}' , (2 ,), '00002' ),
517+ ('{x:03d}-{pos:02d}' , (2 , 1 ), '002-01' ),
518+ ]
519+
520+ @pytest .mark .parametrize ('format, input, expected' , test_data )
521+ def test_basic (self , format , input , expected ):
522+ fmt = mticker .StrMethodFormatter (format )
523+ assert fmt (* input ) == expected
524+
525+
526+ class TestEngFormatter (object ):
527+ format_data = [
528+ ('' , 0.1 , u'100 m' ),
529+ ('' , 1 , u'1' ),
530+ ('' , 999.9 , u'999.9' ),
531+ ('' , 1001 , u'1.001 k' ),
532+ (u's' , 0.1 , u'100 ms' ),
533+ (u's' , 1 , u'1 s' ),
534+ (u's' , 999.9 , u'999.9 s' ),
535+ (u's' , 1001 , u'1.001 ks' ),
536+ ]
537+
538+ @pytest .mark .parametrize ('unit, input, expected' , format_data )
539+ def test_formatting (self , unit , input , expected ):
540+ """
541+ Test the formatting of EngFormatter with some inputs, against
542+ instances with and without units. Cases focus on when no SI
543+ prefix is present, for values in [1, 1000).
544+ """
545+ fmt = mticker .EngFormatter (unit )
546+ assert fmt (input ) == expected
520547
521548
522549class TestPercentFormatter (object ):
@@ -525,12 +552,12 @@ class TestPercentFormatter(object):
525552 (100 , 0 , '%' , 120 , 100 , '120%' ),
526553 (100 , 0 , '%' , 100 , 90 , '100%' ),
527554 (100 , 0 , '%' , 90 , 50 , '90%' ),
528- (100 , 0 , '%' , 1.7 , 40 , '2%' ),
555+ (100 , 0 , '%' , - 1.7 , 40 , '- 2%' ),
529556 (100 , 1 , '%' , 90.0 , 100 , '90.0%' ),
530557 (100 , 1 , '%' , 80.1 , 90 , '80.1%' ),
531558 (100 , 1 , '%' , 70.23 , 50 , '70.2%' ),
532559 # 60.554 instead of 60.55: see https://bugs.python.org/issue5118
533- (100 , 1 , '%' , 60.554 , 40 , '60.6%' ),
560+ (100 , 1 , '%' , - 60.554 , 40 , '- 60.6%' ),
534561 # Check auto decimals over different intervals and values
535562 (100 , None , '%' , 95 , 1 , '95.00%' ),
536563 (1.0 , None , '%' , 3 , 6 , '300%' ),
@@ -565,33 +592,24 @@ class TestPercentFormatter(object):
565592 'Custom percent symbol' ,
566593 ]
567594
595+ latex_data = [
596+ (False , False , r'50\{t}%' ),
597+ (False , True , r'50\\\{t\}\%' ),
598+ (True , False , r'50\{t}%' ),
599+ (True , True , r'50\{t}%' ),
600+ ]
601+
568602 @pytest .mark .parametrize (
569603 'xmax, decimals, symbol, x, display_range, expected' ,
570604 percent_data , ids = percent_ids )
571- def test_percentformatter (self , xmax , decimals , symbol ,
572- x , display_range , expected ):
605+ def test_basic (self , xmax , decimals , symbol ,
606+ x , display_range , expected ):
573607 formatter = mticker .PercentFormatter (xmax , decimals , symbol )
574- assert formatter .format_pct (x , display_range ) == expected
575-
576-
577- class TestEngFormatter (object ):
578- format_data = [
579- ('' , 0.1 , u'100 m' ),
580- ('' , 1 , u'1' ),
581- ('' , 999.9 , u'999.9' ),
582- ('' , 1001 , u'1.001 k' ),
583- (u's' , 0.1 , u'100 ms' ),
584- (u's' , 1 , u'1 s' ),
585- (u's' , 999.9 , u'999.9 s' ),
586- (u's' , 1001 , u'1.001 ks' ),
587- ]
588-
589- @pytest .mark .parametrize ('unit, input, expected' , format_data )
590- def test_formatting (self , unit , input , expected ):
591- """
592- Test the formatting of EngFormatter with some inputs, against
593- instances with and without units. Cases focus on when no SI
594- prefix is present, for values in [1, 1000).
595- """
596- fmt = mticker .EngFormatter (unit )
597- assert fmt (input ) == expected
608+ with matplotlib .rc_context (rc = {'text.usetex' : False }):
609+ assert formatter .format_pct (x , display_range ) == expected
610+
611+ @pytest .mark .parametrize ('is_latex, usetex, expected' , latex_data )
612+ def test_latex (self , is_latex , usetex , expected ):
613+ fmt = mticker .PercentFormatter (symbol = '\\ {t}%' , is_latex = is_latex )
614+ with matplotlib .rc_context (rc = {'text.usetex' : usetex }):
615+ assert fmt .format_pct (50 , 100 ) == expected
0 commit comments