@@ -67,8 +67,62 @@ def test_ordinal():
6767 else :
6868 assert False , "ordinal didn't raise a ValueError for non integer value"
6969 assert formatting .ordinal (11 ).endswith ("th" ), "Failed special handling for 11th in ordinal"
70+ assert formatting .ordinal (12 ).endswith ("th" ), "Failed special handling for 12th in ordinal"
71+ assert formatting .ordinal (13 ).endswith ("th" ), "Failed special handling for 13th in ordinal"
7072 assert formatting .ordinal (21 ).endswith ("st" ), "Failed to add st to 21st in ordinal"
73+ assert formatting .ordinal (1 ).endswith ("st" ), "Failed to add st to 1st in ordinal"
74+ assert formatting .ordinal (2 ).endswith ("nd" ), "Failed to add nd to 2nd in ordinal"
75+ assert formatting .ordinal (3 ).endswith ("rd" ), "Failed to add rd to 3rd in ordinal"
76+ assert formatting .ordinal (4 ).endswith ("th" ), "Failed to add th to 4th in ordinal"
77+ assert formatting .ordinal (0 ).endswith ("th" ), "Failed to add th to 0th in ordinal"
78+
79+
80+ def test_quantize ():
81+ assert formatting .quantize (1.23 , 0.1 ) == pytest .approx (1.2 ), "quantize(1.23, 0.1) failed"
82+ assert formatting .quantize (1.26 , 0.1 ) == pytest .approx (1.3 ), "quantize(1.26, 0.1) failed"
83+ assert formatting .quantize (7 , 2 ) == pytest .approx (8 ), "quantize(7, 2) failed"
84+ assert formatting .quantize (6 , 2 ) == pytest .approx (6 ), "quantize(6, 2) failed"
85+
86+
87+ def test_tex_escape ():
88+ assert formatting .tex_escape ("&" ) == r"\&" , "tex_escape & failed"
89+ assert formatting .tex_escape ("%" ) == r"\%" , "tex_escape % failed"
90+ assert formatting .tex_escape ("$" ) == r"\$" , "tex_escape $ failed"
91+ assert formatting .tex_escape ("#" ) == r"\#" , "tex_escape # failed"
92+ assert formatting .tex_escape ("_" ) == r"\_" , "tex_escape _ failed"
93+ assert formatting .tex_escape ("{" ) == r"\{" , "tex_escape { failed"
94+ assert formatting .tex_escape ("}" ) == r"\}" , "tex_escape } failed"
95+ assert formatting .tex_escape ("~" ) == r"\textasciitilde{}" , "tex_escape ~ failed"
96+ assert formatting .tex_escape ("^" ) == r"\^{}" , "tex_escape ^ failed"
97+ assert formatting .tex_escape ("\\ " ) == r"\textbackslash{}" , "tex_escape \\ failed"
98+ assert formatting .tex_escape ("<" ) == r"\textless" , "tex_escape < failed"
99+ assert formatting .tex_escape (">" ) == r"\textgreater" , "tex_escape > failed"
100+ assert formatting .tex_escape ("hello" ) == "hello" , "tex_escape plain text should be unchanged"
101+
102+
103+ def test_format_val_modes ():
104+ value = 1.2345e-6
105+ # eng mode text
106+ result = formatting .format_val (value , fmt = "text" , mode = "eng" )
107+ assert "u" in result or "1" in result , "format_val eng text mode failed"
108+ # sci mode html
109+ result = formatting .format_val (value , fmt = "html" , mode = "sci" )
110+ assert "10" in result , "format_val sci html mode failed"
111+ # sci mode latex
112+ result = formatting .format_val (value , fmt = "latex" , mode = "sci" )
113+ assert r"\times" in result , "format_val sci latex mode failed"
114+ # float mode (default)
115+ result = formatting .format_val (value , fmt = "text" , mode = "float" )
116+ assert "1.2345" in result , "format_val float text mode failed"
117+ # bad mode raises RuntimeError
118+ try :
119+ formatting .format_val (value , fmt = "text" , mode = "bad" )
120+ except RuntimeError :
121+ pass
122+ else :
123+ assert False , "format_val bad mode didn't raise RuntimeError"
71124
72125
73126if __name__ == "__main__" :
74127 pytest .main (["--pdb" , __file__ ])
128+
0 commit comments