1 # Copyright (C) 2013-2020 Roland Lutz
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 def throws(fun
, *args
):
22 except Exception as e
:
25 assert throws(xorn
.fixednum
.format
, 42., 0) == TypeError
26 assert throws(xorn
.fixednum
.format
, 42, None) == TypeError
27 assert throws(xorn
.fixednum
.format
, 42, -1) == ValueError
29 assert throws(xorn
.fixednum
.parse
, 42, 0) == TypeError
30 assert throws(xorn
.fixednum
.parse
, 42., 0) == TypeError
31 assert throws(xorn
.fixednum
.parse
, '42', None) == TypeError
32 assert throws(xorn
.fixednum
.parse
, '42', -1) == ValueError
34 assert throws(xorn
.fixednum
.parse
, '', 0) == ValueError
35 assert throws(xorn
.fixednum
.parse
, '.42', 0) == ValueError
36 assert throws(xorn
.fixednum
.parse
, '4.2', 0) == ValueError
37 assert throws(xorn
.fixednum
.parse
, ':42', 0) == ValueError
38 assert throws(xorn
.fixednum
.parse
, 'x', 0) == ValueError
40 assert throws(xorn
.fixednum
.parse
, '', 3) == ValueError
41 assert throws(xorn
.fixednum
.parse
, '.4242', 3) == ValueError
42 assert throws(xorn
.fixednum
.parse
, ':42', 3) == ValueError
43 assert throws(xorn
.fixednum
.parse
, 'x', 3) == ValueError
44 assert throws(xorn
.fixednum
.parse
, '.x', 3) == ValueError
46 assert xorn
.fixednum
.format(0, 0) == '0'
47 assert xorn
.fixednum
.format(0, 3) == '0'
48 assert str(xorn
.fixednum
.parse('0', 0)) == '0'
49 assert str(xorn
.fixednum
.parse('0', 3)) == '0'
50 assert str(xorn
.fixednum
.parse('-0', 0)) == '0'
51 assert str(xorn
.fixednum
.parse('-0', 3)) == '0'
53 for x
, decimal_digits
, s
in [
72 (-12345, 0, '-12345'),
73 (-12345, 1, '-1234.5'),
74 (-12345, 2, '-123.45'),
75 (-12345, 3, '-12.345'),
76 (-12345, 4, '-1.2345'),
77 (-12345, 5, '-.12345'),
80 (-42090, 3, '-42.09'),
82 assert xorn
.fixednum
.format(x
, decimal_digits
) == s
83 assert xorn
.fixednum
.parse(s
, decimal_digits
) == x