3 # Copyright (C) 2010 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This file contains tests for functions in code/wfpl/format.php
21 # See code/wfpl/test.php for details on how to run or create tests
24 require_once('code/wfpl/format.php');
26 function _test_format_money($arg, $dollars_correct, $money_correct) {
27 unit_test_func('format_dollars', $arg, $dollars_correct);
28 unit_test_func('format_money', $arg, $money_correct);
31 function test_format_money() {
32 _test_format_money('1', '$1', '$1.00');
33 _test_format_money('0', '$0', '$0.00');
34 _test_format_money('0.0', '$0', '$0.00');
35 _test_format_money('0.00', '$0', '$0.00');
36 _test_format_money('00.00', '$0', '$0.00');
37 _test_format_money('0.000', '$0', '$0.00');
38 _test_format_money('0000.00000', '$0', '$0.00');
39 _test_format_money('0.01', '$0', '$0.01');
40 _test_format_money('0.51', '$1', '$0.51');
41 _test_format_money('2.05', '$2', '$2.05');
42 _test_format_money('2.10000500', '$2', '$2.10');
43 _test_format_money('2.005', '$2', '$2.01');
44 _test_format_money('2.004', '$2', '$2.00');
45 _test_format_money('2.1', '$2', '$2.10');
46 _test_format_money('2.0', '$2', '$2.00');
47 _test_format_money('1000', '$1,000', '$1,000.00');
48 _test_format_money('10000000', '$10,000,000', '$10,000,000.00');
49 _test_format_money('100000000000', '$100,000,000,000', '$100,000,000,000.00');
50 _test_format_money('1.99', '$2', '$1.99');
51 _test_format_money('1.555', '$2', '$1.56');
52 _test_format_money('19.555', '$20', '$19.56');
53 _test_format_money('9.499', '$9', '$9.50');
54 _test_format_money('9.989', '$10', '$9.99');
55 _test_format_money('9.999', '$10', '$10.00');
56 _test_format_money('19999999999.555', '$20,000,000,000', '$19,999,999,999.56');
60 # run tests where int, int_0, decimal, positive_decimal should return the same result
61 function _test_format_int($arg, $correct) {
65 $correct_0 = $correct;
67 unit_test_func('format_int', $arg, $correct);
68 unit_test_func('format_int_0', $arg, $correct_0);
69 unit_test_func('format_positive_decimal', $arg, $correct);
70 unit_test_func('format_decimal', $arg, $correct);
73 function test_format_misc() {
74 unit_test_func('format_caption', 'something', 'Something');
75 unit_test_func('format_caption', 'two_words', 'Two Words');
76 unit_test_func('format_caption', 'email_special_case', 'E-mail Special Case');
77 unit_test_func('format_caption', 'BIG', 'BIG');
78 unit_test_func('format_caption', '1,2.3%^&', '1,2.3%^&');
80 # test int, int_0, positive_decimal, decimal
81 _test_format_int('', '');
82 _test_format_int('abcdef', '');
83 _test_format_int('0', '0');
84 _test_format_int('00', '0');
85 _test_format_int('0000', '0');
86 _test_format_int('1', '1');
87 _test_format_int('100000000000000000000000000', '100000000000000000000000000');
88 _test_format_int('10000 0000000000000000000000', '100000000000000000000000000');
89 _test_format_int(' 1a2b3c%^&', '123');
91 unit_test_func('format_int', '-1', '1');
92 unit_test_func('format_int', '+1', '1');
93 unit_test_func('format_int_0', '-1', '1');
94 unit_test_func('format_int_0', '+1', '1');
95 unit_test_func('format_positive_decimal', '-1', '1');
96 unit_test_func('format_positive_decimal', '+1', '1');
97 unit_test_func('format_decimal', '-1', '-1');
98 unit_test_func('format_decimal', '+1', '1');
100 unit_test_func('format_int', '-1.0', '1');
101 unit_test_func('format_int', '+1.0', '1');
102 unit_test_func('format_int_0', '-1.0', '1');
103 unit_test_func('format_int_0', '+1.0', '1');
104 unit_test_func('format_positive_decimal', '-1.0', '1.0');
105 unit_test_func('format_positive_decimal', '+1.0', '1.0');
106 unit_test_func('format_decimal', '-1.0', '-1.0');
107 unit_test_func('format_decimal', '+1.0', '1.0');
109 unit_test_func('format_positive_decimal', '1.0', '1.0');
110 unit_test_func('format_decimal', '1.0', '1.0');
112 # extra zeros after the decimal point
113 unit_test_func('format_positive_decimal', '-1.00', '1.0');
114 unit_test_func('format_positive_decimal', '+1.00', '1.0');
115 unit_test_func('format_decimal', '-1.00', '-1.0');
116 unit_test_func('format_decimal', '+1.00', '1.0');
117 unit_test_func('format_positive_decimal', '-1.000', '1.0');
118 unit_test_func('format_positive_decimal', '+1.000', '1.0');
119 unit_test_func('format_decimal', '-1.000', '-1.0');
120 unit_test_func('format_decimal', '+1.000', '1.0');
121 # important zeros after the decimal point
122 unit_test_func('format_positive_decimal', '-1.020', '1.02');
123 unit_test_func('format_positive_decimal', '+1.020', '1.02');
124 unit_test_func('format_decimal', '-1.020', '-1.02');
125 unit_test_func('format_decimal', '+1.020', '1.02');
126 unit_test_func('format_positive_decimal', '-1.0020', '1.002');
127 unit_test_func('format_positive_decimal', '+1.0020', '1.002');
128 unit_test_func('format_decimal', '-1.0020', '-1.002');
129 unit_test_func('format_decimal', '+1.0020', '1.002');
130 # signs in weird places
131 unit_test_func('format_positive_decimal', '-1.-0020', '1.002');
132 unit_test_func('format_positive_decimal', '+1.-0020', '1.002');
133 unit_test_func('format_decimal', '-1.-0020', '-1.002');
134 unit_test_func('format_decimal', '+1.-0020', '1.002');
135 unit_test_func('format_positive_decimal', '-1.0-020', '1.002');
136 unit_test_func('format_positive_decimal', '+1.0-020', '1.002');
137 unit_test_func('format_decimal', '-1.0-020', '-1.002');
138 unit_test_func('format_decimal', '+1.0-020', '1.002');
139 unit_test_func('format_positive_decimal', '-1-.0020', '1.002');
140 unit_test_func('format_positive_decimal', '+1-.0020', '1.002');
141 unit_test_func('format_decimal', '-1-.0020', '-1.002');
142 unit_test_func('format_decimal', '+1-.0020', '1.002');
143 unit_test_func('format_positive_decimal', '--1.0020', '1.002');
144 unit_test_func('format_positive_decimal', '+-1.0020', '1.002');
145 unit_test_func('format_decimal', '--1.0020', '-1.002');
146 unit_test_func('format_decimal', '+-1.0020', '-1.002');
149 unit_test_func('format_zip', '', '');
150 unit_test_func('format_zip', '12345', '12345');
151 unit_test_func('format_zip', 'snt3nvwm4vm2zvw0%^&3-', '34203');
152 unit_test_func('format_zip', '123456', '12345-6');
153 unit_test_func('format_zip', '1234567890', '12345-67890');
155 unit_test_func('format_filename', '1234567890', '1234567890');
156 unit_test_func('format_filename', 'A*b#C:D/E?f<g&H!I', 'a_b_c_d_e_f_g_h_i');
157 unit_test_func('format_filename', '-?', '__');
158 unit_test_func('format_filename', '--help', '_-help');
159 unit_test_func('format_filename', '.secret', '_secret');
161 unit_test_func('format_filename', 'A*b#C:D/E?f<g&H!I', true, 'A_b_C_D_E_f_g_H_I');
164 format_path($str, $allow_uppercase = false)
165 client_path_to_filename($path)
166 format_image_w_h($str)
167 format_image_w_h_thumb_w_h($str)
176 format_money($str, $display_cents = true)
178 format_mdy_to_ymd($str)
180 format_hours_minutes($str)
182 format_12hr_to_hours($str)
187 function format_unit_tests_main() {