|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+declare(strict_types=1);
|
|
|
3
|
+
|
|
|
4
|
+use PHPUnit\Framework\TestCase;
|
|
|
5
|
+
|
|
|
6
|
+require_once __DIR__ . '/../../php/markdown.php';
|
|
|
7
|
+require_once __DIR__ . '/../../php/spreadsheet.php';
|
|
|
8
|
+
|
|
|
9
|
+final class CellValueTests extends TestCase {
|
|
|
10
|
+ public function test_fromCellString_blank() {
|
|
|
11
|
+ $value = CellValue::fromCellString('');
|
|
|
12
|
+ $this->assertSame(CellValue::TYPE_BLANK, $value->type);
|
|
|
13
|
+ $this->assertSame('', $value->formattedValue);
|
|
|
14
|
+ $this->assertSame(null, $value->value);
|
|
|
15
|
+ $value = CellValue::fromCellString(' ');
|
|
|
16
|
+ $this->assertSame(CellValue::TYPE_BLANK, $value->type);
|
|
|
17
|
+ $this->assertSame('', $value->formattedValue);
|
|
|
18
|
+ $this->assertSame(null, $value->value);
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ public function test_fromCellString_number() {
|
|
|
22
|
+ $value = CellValue::fromCellString('123');
|
|
|
23
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
24
|
+ $this->assertSame('123', $value->formattedValue);
|
|
|
25
|
+ $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
|
|
|
26
|
+ $this->assertSame(0, $value->decimals);
|
|
|
27
|
+ $value = CellValue::fromCellString('-0');
|
|
|
28
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
29
|
+ $this->assertSame('0', $value->formattedValue);
|
|
|
30
|
+ $this->assertEqualsWithDelta(0, $value->value, 0.000_001);
|
|
|
31
|
+ $this->assertSame(0, $value->decimals);
|
|
|
32
|
+ $value = CellValue::fromCellString('1,234');
|
|
|
33
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
34
|
+ $this->assertSame('1,234', $value->formattedValue);
|
|
|
35
|
+ $this->assertEqualsWithDelta(1234, $value->value, 0.000_001);
|
|
|
36
|
+ $this->assertSame(0, $value->decimals);
|
|
|
37
|
+ $value = CellValue::fromCellString('-1,234,567.89');
|
|
|
38
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
39
|
+ $this->assertSame('-1,234,567.89', $value->formattedValue);
|
|
|
40
|
+ $this->assertEqualsWithDelta(-1234567.89, $value->value, 0.000_001);
|
|
|
41
|
+ $this->assertSame(2, $value->decimals);
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ public function test_fromCellString_percent() {
|
|
|
45
|
+ $value = CellValue::fromCellString('123%');
|
|
|
46
|
+ $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
|
|
|
47
|
+ $this->assertSame('123%', $value->formattedValue);
|
|
|
48
|
+ $this->assertEqualsWithDelta(1.23, $value->value, 0.000_001);
|
|
|
49
|
+ $this->assertSame(0, $value->decimals);
|
|
|
50
|
+ $value = CellValue::fromCellString('-12.3%');
|
|
|
51
|
+ $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
|
|
|
52
|
+ $this->assertSame('-12.3%', $value->formattedValue);
|
|
|
53
|
+ $this->assertEqualsWithDelta(-0.123, $value->value, 0.000_001);
|
|
|
54
|
+ $this->assertSame(1, $value->decimals);
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ public function test_fromCellString_currency() {
|
|
|
58
|
+ $value = CellValue::fromCellString('$123');
|
|
|
59
|
+ $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
|
|
|
60
|
+ $this->assertSame('$123', $value->formattedValue);
|
|
|
61
|
+ $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
|
|
|
62
|
+ $this->assertSame(0, $value->decimals);
|
|
|
63
|
+ $value = CellValue::fromCellString('-$12.34');
|
|
|
64
|
+ $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
|
|
|
65
|
+ $this->assertSame('-$12.34', $value->formattedValue);
|
|
|
66
|
+ $this->assertEqualsWithDelta(-12.34, $value->value, 0.000_001);
|
|
|
67
|
+ $this->assertSame(2, $value->decimals);
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ public function test_fromCellString_boolean() {
|
|
|
71
|
+ $value = CellValue::fromCellString('true');
|
|
|
72
|
+ $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
|
|
|
73
|
+ $this->assertSame('TRUE', $value->formattedValue);
|
|
|
74
|
+ $this->assertSame(true, $value->value);
|
|
|
75
|
+ $value = CellValue::fromCellString('false');
|
|
|
76
|
+ $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
|
|
|
77
|
+ $this->assertSame('FALSE', $value->formattedValue);
|
|
|
78
|
+ $this->assertSame(false, $value->value);
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ public function test_fromCellString_string() {
|
|
|
82
|
+ $value = CellValue::fromCellString('some text');
|
|
|
83
|
+ $this->assertSame(CellValue::TYPE_STRING, $value->type);
|
|
|
84
|
+ $this->assertSame('some text', $value->formattedValue);
|
|
|
85
|
+ $this->assertSame('some text', $value->value);
|
|
|
86
|
+ $value = CellValue::fromCellString("'0123");
|
|
|
87
|
+ $this->assertSame(CellValue::TYPE_STRING, $value->type);
|
|
|
88
|
+ $this->assertSame('0123', $value->formattedValue);
|
|
|
89
|
+ $this->assertSame('0123', $value->value);
|
|
|
90
|
+ $value = CellValue::fromCellString("'=123");
|
|
|
91
|
+ $this->assertSame(CellValue::TYPE_STRING, $value->type);
|
|
|
92
|
+ $this->assertSame('=123', $value->formattedValue);
|
|
|
93
|
+ $this->assertSame('=123', $value->value);
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ public function test_fromCellString_formula() {
|
|
|
97
|
+ $value = CellValue::fromCellString('=A*B');
|
|
|
98
|
+ $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
|
|
|
99
|
+ $this->assertSame('=A*B', $value->formattedValue);
|
|
|
100
|
+ $this->assertSame('=A*B', $value->value);
|
|
|
101
|
+ $value = CellValue::fromCellString('=MAX(A, 3)');
|
|
|
102
|
+ $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
|
|
|
103
|
+ $this->assertSame('=MAX(A, 3)', $value->formattedValue);
|
|
|
104
|
+ $this->assertSame('=MAX(A, 3)', $value->value);
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ public function test_fromValue_null() {
|
|
|
108
|
+ $value = CellValue::fromValue(null);
|
|
|
109
|
+ $this->assertSame(CellValue::TYPE_BLANK, $value->type);
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ public function test_fromValue_number() {
|
|
|
113
|
+ $value = CellValue::fromValue(123);
|
|
|
114
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
115
|
+ $this->assertSame('123', $value->formattedValue);
|
|
|
116
|
+ $value = CellValue::fromValue(3.141592);
|
|
|
117
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
118
|
+ $this->assertSame('3.141592', $value->formattedValue);
|
|
|
119
|
+ $value = CellValue::fromValue(123456789);
|
|
|
120
|
+ $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
|
|
|
121
|
+ $this->assertSame('123,456,789', $value->formattedValue);
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ public function test_fromValue_boolean() {
|
|
|
125
|
+ $value = CellValue::fromValue(true);
|
|
|
126
|
+ $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
|
|
|
127
|
+ $this->assertSame('TRUE', $value->formattedValue);
|
|
|
128
|
+ $value = CellValue::fromValue(false);
|
|
|
129
|
+ $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
|
|
|
130
|
+ $this->assertSame('FALSE', $value->formattedValue);
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ public function test_fromValue_string() {
|
|
|
134
|
+ $value = CellValue::fromValue('foo');
|
|
|
135
|
+ $this->assertSame(CellValue::TYPE_STRING, $value->type);
|
|
|
136
|
+ $this->assertSame('foo', $value->formattedValue);
|
|
|
137
|
+ $value = CellValue::fromValue('0123456');
|
|
|
138
|
+ $this->assertSame(CellValue::TYPE_STRING, $value->type);
|
|
|
139
|
+ $this->assertSame('0123456', $value->formattedValue);
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ public function test_fromValue_formula() {
|
|
|
143
|
+ $value = CellValue::fromValue('=A*B');
|
|
|
144
|
+ $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
|
|
|
145
|
+ $this->assertSame('=A*B', $value->formattedValue);
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ private function assertSameCellValue(CellValue $expected, CellValue $actual) {
|
|
|
149
|
+ $this->assertSame($expected->formattedValue, $actual->formattedValue);
|
|
|
150
|
+ $this->assertSame($expected->value, $actual->value);
|
|
|
151
|
+ $this->assertSame($expected->type, $actual->type);
|
|
|
152
|
+ $this->assertSame($expected->decimals, $actual->decimals);
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ public function test_operation_add() {
|
|
|
156
|
+ $a = CellValue::fromValue(3);
|
|
|
157
|
+ $b = CellValue::fromValue(4);
|
|
|
158
|
+ $actual = $a->add($b);
|
|
|
159
|
+ $expected = new CellValue('7', 7, CellValue::TYPE_NUMBER, 0);
|
|
|
160
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
161
|
+
|
|
|
162
|
+ $a = CellValue::fromCellString('100%');
|
|
|
163
|
+ $b = CellValue::fromCellString('50%');
|
|
|
164
|
+ $actual = $a->add($b);
|
|
|
165
|
+ $expected = new CellValue('150%', 1.5, CellValue::TYPE_PERCENT, 0);
|
|
|
166
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
167
|
+
|
|
|
168
|
+ $a = CellValue::fromCellString('$123');
|
|
|
169
|
+ $b = CellValue::fromCellString('$321');
|
|
|
170
|
+ $actual = $a->add($b);
|
|
|
171
|
+ $expected = new CellValue('$444.00', 444.0, CellValue::TYPE_CURRENCY, 2);
|
|
|
172
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ public function test_operation_subtract() {
|
|
|
176
|
+ $a = CellValue::fromValue(9);
|
|
|
177
|
+ $b = CellValue::fromValue(4);
|
|
|
178
|
+ $actual = $a->subtract($b);
|
|
|
179
|
+ $expected = new CellValue('5', 5, CellValue::TYPE_NUMBER, 0);
|
|
|
180
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
181
|
+
|
|
|
182
|
+ $a = CellValue::fromCellString('100%');
|
|
|
183
|
+ $b = CellValue::fromCellString('50%');
|
|
|
184
|
+ $actual = $a->subtract($b);
|
|
|
185
|
+ $expected = new CellValue('50%', 0.5, CellValue::TYPE_PERCENT, 0);
|
|
|
186
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
187
|
+
|
|
|
188
|
+ $a = CellValue::fromCellString('$321');
|
|
|
189
|
+ $b = CellValue::fromCellString('$123');
|
|
|
190
|
+ $actual = $a->subtract($b);
|
|
|
191
|
+ $expected = new CellValue('$198.00', 198.0, CellValue::TYPE_CURRENCY, 2);
|
|
|
192
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ public function test_operation_multiply() {
|
|
|
196
|
+ $a = CellValue::fromValue(3);
|
|
|
197
|
+ $b = CellValue::fromValue(4);
|
|
|
198
|
+ $actual = $a->multiply($b);
|
|
|
199
|
+ $expected = new CellValue('12', 12, CellValue::TYPE_NUMBER, 0);
|
|
|
200
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
201
|
+
|
|
|
202
|
+ $a = CellValue::fromCellString('150%');
|
|
|
203
|
+ $b = CellValue::fromCellString('50%');
|
|
|
204
|
+ $actual = $a->multiply($b);
|
|
|
205
|
+ $expected = new CellValue('75%', 0.75, CellValue::TYPE_PERCENT, 0);
|
|
|
206
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
207
|
+
|
|
|
208
|
+ $a = CellValue::fromCellString('$321');
|
|
|
209
|
+ $b = CellValue::fromCellString('50%');
|
|
|
210
|
+ $actual = $a->multiply($b);
|
|
|
211
|
+ $expected = new CellValue('$160.50', 160.50, CellValue::TYPE_CURRENCY, 2);
|
|
|
212
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ public function test_operation_divide() {
|
|
|
216
|
+ $a = CellValue::fromValue(12);
|
|
|
217
|
+ $b = CellValue::fromValue(4);
|
|
|
218
|
+ $actual = $a->divide($b);
|
|
|
219
|
+ $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
|
|
|
220
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
221
|
+
|
|
|
222
|
+ $a = CellValue::fromCellString('150%');
|
|
|
223
|
+ $b = CellValue::fromCellString('50%');
|
|
|
224
|
+ $actual = $a->divide($b);
|
|
|
225
|
+ $expected = new CellValue('300%', 3.0, CellValue::TYPE_PERCENT, 0);
|
|
|
226
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
227
|
+
|
|
|
228
|
+ $a = CellValue::fromCellString('$321');
|
|
|
229
|
+ $b = CellValue::fromCellString('200%');
|
|
|
230
|
+ $actual = $a->divide($b);
|
|
|
231
|
+ $expected = new CellValue('$160.50', 160.50, CellValue::TYPE_CURRENCY, 2);
|
|
|
232
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
233
|
+ }
|
|
|
234
|
+
|
|
|
235
|
+ public function test_operation_modulo() {
|
|
|
236
|
+ $a = CellValue::fromValue(7);
|
|
|
237
|
+ $b = CellValue::fromValue(4);
|
|
|
238
|
+ $actual = $a->modulo($b);
|
|
|
239
|
+ $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
|
|
|
240
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
241
|
+
|
|
|
242
|
+ $a = CellValue::fromCellString('175%');
|
|
|
243
|
+ $b = CellValue::fromCellString('50%');
|
|
|
244
|
+ $actual = $a->modulo($b);
|
|
|
245
|
+ $expected = new CellValue('25%', 0.25, CellValue::TYPE_PERCENT, 0);
|
|
|
246
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
247
|
+
|
|
|
248
|
+ $a = CellValue::fromCellString('$327');
|
|
|
249
|
+ $b = CellValue::fromCellString('$20');
|
|
|
250
|
+ $actual = $a->modulo($b);
|
|
|
251
|
+ $expected = new CellValue('$7.00', 7.00, CellValue::TYPE_CURRENCY, 2);
|
|
|
252
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
253
|
+ }
|
|
|
254
|
+
|
|
|
255
|
+ public function test_operation_unaryNot() {
|
|
|
256
|
+ $this->assertSameCellValue(CellValue::fromValue(false), CellValue::fromValue(true)->not());
|
|
|
257
|
+ $this->assertSameCellValue(CellValue::fromValue(true), CellValue::fromValue(false)->not());
|
|
|
258
|
+ }
|
|
|
259
|
+
|
|
|
260
|
+ public function test_operation_comparators() {
|
|
|
261
|
+ $a = CellValue::fromValue(3);
|
|
|
262
|
+ $b = CellValue::fromValue(4);
|
|
|
263
|
+ $t = CellValue::fromValue(true);
|
|
|
264
|
+ $f = CellValue::fromValue(false);
|
|
|
265
|
+
|
|
|
266
|
+ $this->assertSameCellValue($t, $a->lt($b));
|
|
|
267
|
+ $this->assertSameCellValue($t, $a->lte($b));
|
|
|
268
|
+ $this->assertSameCellValue($f, $a->gt($b));
|
|
|
269
|
+ $this->assertSameCellValue($f, $a->gte($b));
|
|
|
270
|
+ $this->assertSameCellValue($f, $a->eq($b));
|
|
|
271
|
+ $this->assertSameCellValue($t, $a->neq($b));
|
|
|
272
|
+
|
|
|
273
|
+ $this->assertSameCellValue($f, $b->lt($a));
|
|
|
274
|
+ $this->assertSameCellValue($f, $b->lte($a));
|
|
|
275
|
+ $this->assertSameCellValue($t, $b->gt($a));
|
|
|
276
|
+ $this->assertSameCellValue($t, $b->gte($a));
|
|
|
277
|
+ $this->assertSameCellValue($f, $b->eq($a));
|
|
|
278
|
+ $this->assertSameCellValue($t, $b->neq($a));
|
|
|
279
|
+
|
|
|
280
|
+ $this->assertSameCellValue($f, $a->lt($a));
|
|
|
281
|
+ $this->assertSameCellValue($t, $a->lte($a));
|
|
|
282
|
+ $this->assertSameCellValue($f, $a->gt($a));
|
|
|
283
|
+ $this->assertSameCellValue($t, $a->gte($a));
|
|
|
284
|
+ $this->assertSameCellValue($t, $a->eq($a));
|
|
|
285
|
+ $this->assertSameCellValue($f, $a->neq($a));
|
|
|
286
|
+ }
|
|
|
287
|
+
|
|
|
288
|
+ public function test_operation_concatenate() {
|
|
|
289
|
+ $a = CellValue::fromValue('abc');
|
|
|
290
|
+ $b = CellValue::fromValue('xyz');
|
|
|
291
|
+ $actual = $a->concatenate($b);
|
|
|
292
|
+ $expected = CellValue::fromValue('abcxyz');
|
|
|
293
|
+ $this->assertSameCellValue($expected, $actual);
|
|
|
294
|
+ }
|
|
|
295
|
+}
|
|
|
296
|
+?>
|