Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 53d05e1f

Von Moritz Bunkus vor mehr als 9 Jahren hinzugefügt

  • ID 53d05e1f806b560e2ed3bed2cc540310aaed7d47
  • Vorgänger 1b415045
  • Nachfolger 10ed12ee

Form::parse_amount: Parsen als Oktalzahlen verhindern

Unterschiede anzeigen:

SL/Form.pm
947 947

  
948 948
  # Make sure no code wich is not a math expression ends up in eval().
949 949
  return 0 unless $amount =~ /^ [\s \d \( \) \- \+ \* \/ \. ]* $/x;
950

  
951
  # Prevent numbers from being parsed as octals;
952
  $amount =~ s{ (?<! [\d.] ) 0+ (?= [1-9] ) }{}gx;
953

  
950 954
  return scalar(eval($amount)) * 1 ;
951 955
}
952 956

  
t/form/parse_amount.t
1
use strict;
2
use Test::More;
3

  
4
use lib 't';
5
use Support::TestSetup;
6

  
7
Support::TestSetup::login();
8

  
9
my $config = {};
10

  
11
# Positive numbers
12
$config->{numberformat} = '1.000,00';
13

  
14
is($::form->parse_amount($config, '12345'),        12345,     '12345 (numberformat: 1.000,00)');
15
is($::form->parse_amount($config, '1.234,5'),      1234.5,    '1.234,5 (numberformat: 1.000,00)');
16
is($::form->parse_amount($config, '9.871.234,5'),  9871234.5, '9.871.234,5 (numberformat: 1.000,00)');
17
is($::form->parse_amount($config, '1234,5'),       1234.5,    '1234,5 (numberformat: 1.000,00)');
18
is($::form->parse_amount($config, '012345'),       12345,     '012345 (numberformat: 1.000,00)');
19
is($::form->parse_amount($config, '01.234,5'),     1234.5,    '01.234,5 (numberformat: 1.000,00)');
20
is($::form->parse_amount($config, '01234,5'),      1234.5,    '01234,5 (numberformat: 1.000,00)');
21
is($::form->parse_amount($config, '09.871.234,5'), 9871234.5, '09.871.234,5 (numberformat: 1.000,00)');
22

  
23
$config->{numberformat} = '1000,00';
24

  
25
is($::form->parse_amount($config, '12345'),        12345,     '12345 (numberformat: 1000,00)');
26
is($::form->parse_amount($config, '1.234,5'),      1234.5,    '1.234,5 (numberformat: 1000,00)');
27
is($::form->parse_amount($config, '9.871.234,5'),  9871234.5, '9.871.234,5 (numberformat: 1000,00)');
28
is($::form->parse_amount($config, '1234,5'),       1234.5,    '1234,5 (numberformat: 1000,00)');
29
is($::form->parse_amount($config, '012345'),       12345,     '012345 (numberformat: 1000,00)');
30
is($::form->parse_amount($config, '01.234,5'),     1234.5,    '01.234,5 (numberformat: 1000,00)');
31
is($::form->parse_amount($config, '01234,5'),      1234.5,    '01234,5 (numberformat: 1000,00)');
32
is($::form->parse_amount($config, '09.871.234,5'), 9871234.5, '09.871.234,5 (numberformat: 1000,00)');
33

  
34
$config->{numberformat} = '1,000.00';
35

  
36
is($::form->parse_amount($config, '12345'),        12345,     '12345 (numberformat: 1,000.00)');
37
is($::form->parse_amount($config, '1,234.5'),      1234.5,    '1,234.5 (numberformat: 1,000.00)');
38
is($::form->parse_amount($config, '9,871,234.5'),  9871234.5, '9,871,234,5 (numberformat: 1,000.00)');
39
is($::form->parse_amount($config, '1234.5'),       1234.5,    '1234.5 (numberformat: 1,000.00)');
40
is($::form->parse_amount($config, '012345'),       12345,     '012345 (numberformat: 1,000.00)');
41
is($::form->parse_amount($config, '01,234.5'),     1234.5,    '01,234.5 (numberformat: 1,000.00)');
42
is($::form->parse_amount($config, '01234.5'),      1234.5,    '01234.5 (numberformat: 1,000.00)');
43
is($::form->parse_amount($config, '09,871,234.5'), 9871234.5, '09,871,234,5 (numberformat: 1,000.00)');
44

  
45
$config->{numberformat} = '1000.00';
46

  
47
is($::form->parse_amount($config, '12345'),        12345,     '12345 (numberformat: 1000.00)');
48
is($::form->parse_amount($config, '1,234.5'),      1234.5,    '1,234.5 (numberformat: 1000.00)');
49
is($::form->parse_amount($config, '9,871,234.5'),  9871234.5, '9,871,234,5 (numberformat: 1000.00)');
50
is($::form->parse_amount($config, '1234.5'),       1234.5,    '1234.5 (numberformat: 1000.00)');
51
is($::form->parse_amount($config, '012345'),       12345,     '012345 (numberformat: 1000.00)');
52
is($::form->parse_amount($config, '01,234.5'),     1234.5,    '01,234.5 (numberformat: 1000.00)');
53
is($::form->parse_amount($config, '01234.5'),      1234.5,    '01234.5 (numberformat: 1000.00)');
54
is($::form->parse_amount($config, '09,871,234.5'), 9871234.5, '09,871,234,5 (numberformat: 1000.00)');
55

  
56
# Negative numbers
57
$config->{numberformat} = '1.000,00';
58

  
59
is($::form->parse_amount($config, '-12345'),        -12345,     '-12345 (numberformat: 1.000,00)');
60
is($::form->parse_amount($config, '-1.234,5'),      -1234.5,    '-1.234,5 (numberformat: 1.000,00)');
61
is($::form->parse_amount($config, '-9.871.234,5'),  -9871234.5, '-9.871.234,5 (numberformat: 1.000,00)');
62
is($::form->parse_amount($config, '-1234,5'),       -1234.5,    '-1234,5 (numberformat: 1.000,00)');
63
is($::form->parse_amount($config, '-012345'),       -12345,     '-012345 (numberformat: 1.000,00)');
64
is($::form->parse_amount($config, '-01.234,5'),     -1234.5,    '-01.234,5 (numberformat: 1.000,00)');
65
is($::form->parse_amount($config, '-01234,5'),      -1234.5,    '-01234,5 (numberformat: 1.000,00)');
66
is($::form->parse_amount($config, '-09.871.234,5'), -9871234.5, '-09.871.234,5 (numberformat: 1.000,00)');
67

  
68
$config->{numberformat} = '1000,00';
69

  
70
is($::form->parse_amount($config, '-12345'),        -12345,     '-12345 (numberformat: 1000,00)');
71
is($::form->parse_amount($config, '-1.234,5'),      -1234.5,    '-1.234,5 (numberformat: 1000,00)');
72
is($::form->parse_amount($config, '-9.871.234,5'),  -9871234.5, '-9.871.234,5 (numberformat: 1000,00)');
73
is($::form->parse_amount($config, '-1234,5'),       -1234.5,    '-1234,5 (numberformat: 1000,00)');
74
is($::form->parse_amount($config, '-012345'),       -12345,     '-012345 (numberformat: 1000,00)');
75
is($::form->parse_amount($config, '-01.234,5'),     -1234.5,    '-01.234,5 (numberformat: 1000,00)');
76
is($::form->parse_amount($config, '-01234,5'),      -1234.5,    '-01234,5 (numberformat: 1000,00)');
77
is($::form->parse_amount($config, '-09.871.234,5'), -9871234.5, '-09.871.234,5 (numberformat: 1000,00)');
78

  
79
$config->{numberformat} = '1,000.00';
80

  
81
is($::form->parse_amount($config, '-12345'),        -12345,     '-12345 (numberformat: 1,000.00)');
82
is($::form->parse_amount($config, '-1,234.5'),      -1234.5,    '-1,234.5 (numberformat: 1,000.00)');
83
is($::form->parse_amount($config, '-9,871,234.5'),  -9871234.5, '-9,871,234,5 (numberformat: 1,000.00)');
84
is($::form->parse_amount($config, '-1234.5'),       -1234.5,    '-1234.5 (numberformat: 1,000.00)');
85
is($::form->parse_amount($config, '-012345'),       -12345,     '-012345 (numberformat: 1,000.00)');
86
is($::form->parse_amount($config, '-01,234.5'),     -1234.5,    '-01,234.5 (numberformat: 1,000.00)');
87
is($::form->parse_amount($config, '-01234.5'),      -1234.5,    '-01234.5 (numberformat: 1,000.00)');
88
is($::form->parse_amount($config, '-09,871,234.5'), -9871234.5, '-09,871,234,5 (numberformat: 1,000.00)');
89

  
90
$config->{numberformat} = '1000.00';
91

  
92
is($::form->parse_amount($config, '-12345'),        -12345,     '-12345 (numberformat: 1000.00)');
93
is($::form->parse_amount($config, '-1,234.5'),      -1234.5,    '-1,234.5 (numberformat: 1000.00)');
94
is($::form->parse_amount($config, '-9,871,234.5'),  -9871234.5, '-9,871,234,5 (numberformat: 1000.00)');
95
is($::form->parse_amount($config, '-1234.5'),       -1234.5,    '-1234.5 (numberformat: 1000.00)');
96
is($::form->parse_amount($config, '-012345'),       -12345,     '-012345 (numberformat: 1000.00)');
97
is($::form->parse_amount($config, '-01,234.5'),     -1234.5,    '-01,234.5 (numberformat: 1000.00)');
98
is($::form->parse_amount($config, '-01234.5'),      -1234.5,    '-01234.5 (numberformat: 1000.00)');
99
is($::form->parse_amount($config, '-09,871,234.5'), -9871234.5, '-09,871,234,5 (numberformat: 1000.00)');
100

  
101
# Calculations
102
$config->{numberformat} = '1.000,00';
103

  
104
is($::form->parse_amount($config, '47/2+3,5*(4+5)'),                  55,    '47/2+3,5*(4+5) (numberformat: 1.000,00)');
105
is($::form->parse_amount($config, '047/002+003,05*(04+000005)'),      50.95, '047/002+003,05*(04+000005) (numberformat: 1.000,00)');
106
is($::form->parse_amount($config, '47 / 2+       3,5*( 4 + 5)'),      55,    '47 / 2+       3.,*( 4 + 5) (numberformat: 1.000,00)');
107
is($::form->parse_amount($config, '047/ 002+ 003,05 * (04 +000005)'), 50.95, '047/ 002+ 003,05 * (04 +000005) (numberformat: 1.000,00)');
108

  
109
$config->{numberformat} = '1,000.00';
110

  
111
is($::form->parse_amount($config, '47/2+3.5*(4+5)'),                  55,    '47/2+3.5*(4+5) (numberformat: 1,000.00)');
112
is($::form->parse_amount($config, '047/002+003.05*(04+000005)'),      50.95, '047/002+003.05*(04+000005) (numberformat: 1,000.00)');
113
is($::form->parse_amount($config, '47 / 2+       3.5*( 4 + 5)'),      55,    '47 / 2+       3.5*( 4 + 5) (numberformat: 1,000.00)');
114
is($::form->parse_amount($config, '047/ 002+ 003.05 * (04 +000005)'), 50.95, '047/ 002+ 003.05 * (04 +000005) (numberformat: 1,000.00)');
115

  
116
# Weird edge cases
117

  
118
$config->{numberformat} = '1.000,00';
119

  
120
is($::form->parse_amount($config, '-0+1'), 1, '-0+1 (numberformat: 1.000,00)');
121
is($::form->parse_amount($config, '-0+9'), 9, '-0+9 (numberformat: 1.000,00)');
122
is($::form->parse_amount($config, '20*0'), 0, '20*0 (numberformat: 1.000,00)');
123
is($::form->parse_amount($config, '20*0123'), 2460, '20*0123 (numberformat: 1.000,00)');
124
is($::form->parse_amount($config, '010+010'), 20, '010+010 (numberformat: 1.000,00)');
125
is($::form->parse_amount($config, '+(010*2)'), 20, '+(010*2) (numberformat: 1.000,00)');
126

  
127
done_testing;
128

  
129
1;

Auch abrufbar als: Unified diff