-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDecimalTest.php
244 lines (216 loc) · 8.78 KB
/
DecimalTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace dokuwiki\plugin\struct\test\types;
use dokuwiki\plugin\struct\meta\ValidationException;
use dokuwiki\plugin\struct\meta\Value;
use dokuwiki\plugin\struct\test\mock\Search;
use dokuwiki\plugin\struct\test\StructTest;
use dokuwiki\plugin\struct\types\Decimal;
/**
* Testing the Decimal Type
*
* @group plugin_struct
* @group plugins
*/
class DecimalTest extends StructTest
{
/**
* Provides failing min/max validation data
*
* @return array
*/
public function validateFailProvider()
{
return [
// same as integer:
['foo', '', ''],
['foo222', '', ''],
['-5', '0', ''],
['5', '', '0'],
['500', '100', '200'],
['50', '100', '200'],
// decimal specifics
['5.5', '5.6', ''],
['5,5', '5.6', ''],
['-5.5', '-5.4', ''],
['-5,5', '-5.4', ''],
];
}
/**
* Provides successful min/max validation data
*
* @return array
*/
public function validateSuccessProvider()
{
return [
// same as integer
['0', '', ''],
['-5', '', ''],
['5', '', ''],
['5', '0', ''],
['-5', '', '0'],
['150', '100', '200'],
// decimal specifics
['5.5', '', ''],
['5,5', '', ''],
['-5.5', '', ''],
['-5,5', '', ''],
['5.5', '4.5', ''],
['5,5', '4.5', ''],
['-5.5', '', '4.5'],
['-5,5', '', '4.5'],
['5.5645000', '', ''],
// boundaries
['0', '0', ''],
['0', '', '0'],
['5', '5', ''],
['5', '', '5'],
['0', '0.0', ''],
['0', '', '0.0'],
['5.0', '5.0', ''],
['5.0', '', '5.0'],
];
}
/**
* @dataProvider validateFailProvider
*/
public function test_validate_fail($value, $min, $max)
{
$this->expectException(ValidationException::class);
$decimal = new Decimal(array('min' => $min, 'max' => $max));
$decimal->validate($value);
}
/**
* @dataProvider validateSuccessProvider
*/
public function test_validate_success($value, $min, $max, $decpoint = '.')
{
$decimal = new Decimal(array('min' => $min, 'max' => $max));
$decimal->validate($value);
$this->assertTrue(true); // we simply check that no exceptions are thrown
}
public function valueProvider()
{
return [
// $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix='', $engineering = false, $format = ''
['5000', '5 000,00', '2', ',', ' ', false],
['5000', '5 000', '2', ',', ' ', true],
['5000', '5 000', '0', ',', ' ', false],
['5000', '5 000', '0', ',', ' ', true],
['5000', '5 000', '', ',', ' ', false],
['5000', '5 000', '', ',', ' ', true],
['5000', '5 000', '-1', ',', ' ', false],
['5000', '5 000', '-1', ',', ' ', true],
['777.707', '778', '0', ',', ' ', true],
['777.707', '778', '', ',', ' ', true],
['777.707', '777,71', '2', ',', ' ', true],
['777.707', '777,71', '2', ',', ' ', false],
['-0.55600', '-0,56', '2', ',', ' ', false],
['-0.55600', '-0,55600', '-1', ',', ' ', false],
['-0.55600', '-0,556', '-1', ',', ' ', true],
['-0.55600', '-0,5560', '4', ',', ' ', false],
['-0.55600', '-0,556', '4', ',', ' ', true],
['-0.55600', '$ -0,556', '4', ',', ' ', true, '$ '],
['-0.55600', '-0,556 EUR', '4', ',', ' ', true, '', ' EUR'],
//engineering notation
['1e-18', '1' . "\xE2\x80\xAF" . 'a', '-1', ',', ' ', true, '', '', true],
['1e-15', '1' . "\xE2\x80\xAF" . 'f', '-1', ',', ' ', true, '', '', true],
['1e-12', '1' . "\xE2\x80\xAF" . 'p', '-1', ',', ' ', true, '', '', true],
['1e-9', '1' . "\xE2\x80\xAF" . 'n', '-1', ',', ' ', true, '', '', true],
['1e-6', '1' . "\xE2\x80\xAF" . 'µ', '-1', ',', ' ', true, '', '', true],
['1e-3', '1' . "\xE2\x80\xAF" . 'm', '-1', ',', ' ', true, '', '', true],
['1e3', '1' . "\xE2\x80\xAF" . 'k', '-1', ',', ' ', true, '', '', true],
['1e6', '1' . "\xE2\x80\xAF" . 'M', '-1', ',', ' ', true, '', '', true],
['1e9', '1' . "\xE2\x80\xAF" . 'G', '-1', ',', ' ', true, '', '', true],
['1e12', '1' . "\xE2\x80\xAF" . 'T', '-1', ',', ' ', true, '', '', true],
['1e4', '10' . "\xE2\x80\xAF" . 'k', '-1', ',', ' ', true, '', '', true],
['1e5', '100' . "\xE2\x80\xAF" . 'k', '-1', ',', ' ', true, '', '', true],
['1e-4', '100' . "\xE2\x80\xAF" . 'µ', '-1', ',', ' ', true, '', '', true],
['1e-5', '10' . "\xE2\x80\xAF" . 'µ', '-1', ',', ' ', true, '', '', true],
//test behaviour if number exceeds prefix array
['1e15', '1000' . "\xE2\x80\xAF" . 'T', '-1', ',', ' ', true, '', '', true],
['1e-21', '0.001' . "\xE2\x80\xAF" . 'a', '-1', ',', ' ', true, '', '', true],
// test format string
// invalid or empty format (ignored)
['5000', '5 000', '-1', '.', ' ', true, '', '', false, ''],
['5000', '5 000.00', '2', '.', ' ', false, '', '', false, '%s'],
['5000', '5 000.00', '2', '.', ' ', false, '', '', false, '%1$d'],
['5000', '5 000.00', '2', '.', ' ', false, '', '', false, '%04d%02d%02d'],
// valid format
['1.7', '1.70', '-1', '.', ' ', true, '', '', false, '%01.2f'],
['1.7', '1.70' , '-1', '.', ' ', true, '', '', false, '%01.2F'],
['1.7', '0001' , '-1', '.', ' ', true, '', '', false, "%'.04d"],
['15', '1111' , '-1', '.', ' ', true, '', '', false, '%04b'],
['362525200', '3.625e+8' , '-1', '.', ' ', true, '', '', false, '%.3e'],
['362525200', '3.625E+8' , '-1', '.', ' ', true, '', '', false, '%.3E'],
['1.7', '1' , '2', '.', ' ', false, '', '', false, '%u'],
];
}
/**
* @dataProvider valueProvider
*/
public function test_renderValue(
$value, $expect, $roundto, $decpoint,
$thousands, $trimzeros,
$prefix = '', $postfix = '', $engineering = false,
$format = ''
)
{
$decimal = new Decimal([
'roundto' => $roundto,
'decpoint' => $decpoint,
'thousands' => $thousands,
'trimzeros' => $trimzeros,
'prefix' => $prefix,
'postfix' => $postfix,
'engineering' => $engineering,
'format' => $format
]);
$R = new \Doku_Renderer_xhtml();
$R->doc = '';
$decimal->renderValue($value, $R, 'xhtml');
$this->assertEquals($expect, $R->doc);
}
public function test_sort()
{
$this->loadSchemaJSON('decimal');
$this->waitForTick();
$this->saveData('page1', 'decimal', ['field' => '5000']);
$this->saveData('page2', 'decimal', ['field' => '5000.001']);
$this->saveData('page3', 'decimal', ['field' => '900.5']);
$this->saveData('page4', 'decimal', ['field' => '1.5']);
$search = new Search();
$search->addSchema('decimal');
$search->addColumn('%pageid%');
$search->addColumn('field');
$search->addSort('field', true);
/** @var Value[][] $result */
$result = $search->getRows();
$this->assertEquals(4, count($result));
$this->assertEquals('page4', $result[0][0]->getValue());
$this->assertEquals('page3', $result[1][0]->getValue());
$this->assertEquals('page1', $result[2][0]->getValue());
$this->assertEquals('page2', $result[3][0]->getValue());
}
public function test_filter()
{
$this->loadSchemaJSON('decimal');
$this->waitForTick();
$this->saveData('page1', 'decimal', ['field' => '5000']);
$this->saveData('page2', 'decimal', ['field' => '5000.001']);
$this->saveData('page3', 'decimal', ['field' => '900.5']);
$this->saveData('page4', 'decimal', ['field' => '1.5']);
$search = new Search();
$search->addSchema('decimal');
$search->addColumn('%pageid%');
$search->addColumn('field');
$search->addFilter('field', '800', '>', 'AND');
$search->addSort('field', true);
/** @var Value[][] $result */
$result = $search->getRows();
$this->assertEquals(3, count($result));
$this->assertEquals('page3', $result[0][0]->getValue());
$this->assertEquals('page1', $result[1][0]->getValue());
$this->assertEquals('page2', $result[2][0]->getValue());
}
}