Completed
Pull Request — master (#118)
by no
04:37 queued 02:26
created

DecimalValueTest::getValueProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 1
eloc 42
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
7
/**
8
 * @covers DataValues\DecimalValue
9
 *
10
 * @group DataValue
11
 * @group DataValueExtensions
12
 *
13
 * @license GPL-2.0+
14
 * @author Daniel Kinzler
15
 */
16
class DecimalValueTest extends DataValueTest {
17
18
	/**
19
	 * @see DataValueTest::getClass
20
	 *
21
	 * @return string
22
	 */
23
	public function getClass() {
24
		return DecimalValue::class;
25
	}
26
27
	public function validConstructorArgumentsProvider() {
28
		$argLists = [];
29
30
		$argLists[] = [ 42 ];
31
		$argLists[] = [ -42 ];
32
		$argLists[] = [ '-42' ];
33
		$argLists[] = [ 4.2 ];
34
		$argLists[] = [ -4.2 ];
35
		$argLists[] = [ '+4.2' ];
36
		$argLists[] = [ " +4.2\n" ];
37
		$argLists[] = [ 0 ];
38
		$argLists[] = [ 0.2 ];
39
		$argLists[] = [ '-0.42' ];
40
		$argLists[] = [ '-0.0' ];
41
		$argLists[] = [ '-0' ];
42
		$argLists[] = [ '+0' ];
43
		$argLists[] = [ '+0.0' ];
44
		$argLists[] = [ '+0.000' ];
45
		$argLists[] = [ '+1.' . str_repeat( '0', 124 ) ];
46
		$argLists[] = [ '+1.0' . str_repeat( ' ', 124 ) ];
47
		$argLists[] = [ '4.2' ];
48
		$argLists[] = [ " 4.2\n" ];
49
50
		return $argLists;
51
	}
52
53
	public function invalidConstructorArgumentsProvider() {
54
		$argLists = [];
55
56
		$argLists[] = [ 'foo' ];
57
		$argLists[] = [ '' ];
58
		$argLists[] = [ '++4.2' ];
59
		$argLists[] = [ '--4.2' ];
60
		$argLists[] = [ '-+4.2' ];
61
		$argLists[] = [ '+-4.2' ];
62
		$argLists[] = [ '+/-0' ];
63
		$argLists[] = [ '-.42' ];
64
		$argLists[] = [ '+.42' ];
65
		$argLists[] = [ '.42' ];
66
		$argLists[] = [ '.0' ];
67
		$argLists[] = [ '-00' ];
68
		$argLists[] = [ '−1' ];
69
		$argLists[] = [ '+01.2' ];
70
		$argLists[] = [ 'x2' ];
71
		$argLists[] = [ '2x' ];
72
		$argLists[] = [ '+0100' ];
73
		$argLists[] = [ false ];
74
		$argLists[] = [ true ];
75
		$argLists[] = [ null ];
76
		$argLists[] = [ '0x20' ];
77
		$argLists[] = [ '+1.' . str_repeat( '0', 125 ) ];
78
79
		return $argLists;
80
	}
81
82
	/**
83
	 * @see https://phabricator.wikimedia.org/T110728
84
	 * @see http://www.regular-expressions.info/anchors.html#realend
85
	 */
86
	public function testTrailingNewlineRobustness() {
87
		$value = DecimalValue::newFromArray( "-0.0\n" );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\DecimalValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
88
89
		$this->assertTrue( $value->isZero() );
90
		$this->assertSame( 'C:23:"DataValues\DecimalValue":11:{s:4:"+0.0";}', serialize( $value ) );
91
		$this->assertSame( '+0.0', $value->getValue(), 'getValue' );
92
		$this->assertSame( '+0.0', $value->getArrayValue(), 'getArrayValue' );
93
		$this->assertSame( '+0.0', $value->__toString(), '__toString' );
94
		$this->assertSame( '0', $value->getFractionalPart(), 'getFractionalPart' );
95
	}
96
97
	/**
98
	 * @dataProvider provideFloats
99
	 */
100
	public function testFloatInputs( $float, $expectedPrefix ) {
101
		$originalLocale = setlocale( LC_NUMERIC, '0' );
102
		setlocale( LC_NUMERIC, 'de_DE.utf8' );
103
		$value = DecimalValue::newFromArray( $float );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\DecimalValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
104
		setlocale( LC_NUMERIC, $originalLocale );
105
106
		$this->assertStringStartsWith( $expectedPrefix, $value->getValue(), 'getValue' );
107
	}
108
109
	public function provideFloats() {
110
		return [
111
			[ 0.000000002, '+0.000000002' ],
112
			[ 0.000003, '+0.000003' ],
113
			[ 0.9, '+0.9' ],
114
			[ 1.2, '+1.2' ],
115
			[ 1.5, '+1.5' ],
116
			[ 123E-1, '+12.3' ],
117
			[ 123E+1, '+1230' ],
118
			[ 1234567890123456, '+1234567890123' ],
119
			[ 1234567890123456789, '+1234567890123' ],
120
		];
121
	}
122
123
	/**
124
	 * @dataProvider compareProvider
125
	 */
126
	public function testCompare( DecimalValue $a, DecimalValue $b, $expected ) {
127
		$actual = $a->compare( $b );
0 ignored issues
show
Documentation introduced by
$b is of type object<DataValues\DecimalValue>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
		$this->assertSame( $expected, $actual );
129
130
		$actual = $b->compare( $a );
0 ignored issues
show
Documentation introduced by
$a is of type object<DataValues\DecimalValue>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
		$this->assertSame( -$expected, $actual );
132
	}
133
134
	public function compareProvider() {
135
		return [
136
			'zero/equal' => [ new DecimalValue( 0 ), new DecimalValue( 0 ), 0 ],
137
			'zero-signs/equal' => [ new DecimalValue( '+0' ), new DecimalValue( '-0' ), 0 ],
138
			'zero-digits/equal' => [ new DecimalValue( '+0' ), new DecimalValue( '+0.000' ), 0 ],
139
			'digits/equal' => [ new DecimalValue( '+2.2' ), new DecimalValue( '+2.2000' ), 0 ],
140
			'conversion/equal' => [ new DecimalValue( 2.5 ), new DecimalValue( '+2.50' ), 0 ],
141
			'negative/equal' => [ new DecimalValue( '-1.33' ), new DecimalValue( '-1.33' ), 0 ],
142
143
			'simple/smaller' => [ new DecimalValue( '+1' ), new DecimalValue( '+2' ), -1 ],
144
			'simple/greater' => [ new DecimalValue( '+2' ), new DecimalValue( '+1' ), 1 ],
145
			'negative/greater' => [ new DecimalValue( '-1' ), new DecimalValue( '-2' ), 1 ],
146
			'negative/smaller' => [ new DecimalValue( '-2' ), new DecimalValue( '-1' ), -1 ],
147
			'negative-small/greater' => [ new DecimalValue( '-0.5' ), new DecimalValue( '-0.7' ), 1 ],
148
			'negative-small/smaller' => [ new DecimalValue( '-0.7' ), new DecimalValue( '-0.5' ), -1 ],
149
150
			'digits/greater' => [ new DecimalValue( '+11' ), new DecimalValue( '+8' ), 1 ],
151
			'digits-sub/greater' => [ new DecimalValue( '+11' ), new DecimalValue( '+8.0' ), 1 ],
152
			'negative-digits/greater' => [ new DecimalValue( '-11' ), new DecimalValue( '-80' ), 1 ],
153
			'small/greater' => [ new DecimalValue( '+0.050' ), new DecimalValue( '+0.005' ), 1 ],
154
155
			'signs/greater' => [ new DecimalValue( '+1' ), new DecimalValue( '-8' ), 1 ],
156
			'signs/less' => [ new DecimalValue( '-8' ), new DecimalValue( '+1' ), -1 ],
157
158
			'with-and-without-point' => [ new DecimalValue( '+100' ), new DecimalValue( '+100.01' ), -1 ],
159
		];
160
	}
161
162
	/**
163
	 * @dataProvider getSignProvider
164
	 */
165
	public function testGetSign( DecimalValue $value, $expected ) {
166
		$actual = $value->getSign();
167
		$this->assertSame( $expected, $actual );
168
	}
169
170
	public function getSignProvider() {
171
		return [
172
			'zero is positive' => [ new DecimalValue( 0 ), '+' ],
173
			'zero is always positive' => [ new DecimalValue( '-0' ), '+' ],
174
			'zero is ALWAYS positive' => [ new DecimalValue( '-0.00' ), '+' ],
175
			'+1 is positive' => [ new DecimalValue( '+1' ), '+' ],
176
			'-1 is negative' => [ new DecimalValue( '-1' ), '-' ],
177
			'+0.01 is positive' => [ new DecimalValue( '+0.01' ), '+' ],
178
			'-0.01 is negative' => [ new DecimalValue( '-0.01' ), '-' ],
179
		];
180
	}
181
182
	/**
183
	 * @dataProvider getValueProvider
184
	 */
185
	public function testGetValue( $value, $expected ) {
186
		$precision = ini_set( 'serialize_precision', '2' );
187
		$actual = ( new DecimalValue( $value ) )->getValue();
188
		ini_set( 'serialize_precision', $precision );
189
190
		$this->assertSame( $expected, $actual );
191
	}
192
193
	public function getValueProvider() {
194
		$argLists = [];
195
196
		$argLists[] = [ 42, '+42' ];
197
		$argLists[] = [ -42, '-42' ];
198
		$argLists[] = [ -42.0, '-42' ];
199
		$argLists[] = [ '-42', '-42' ];
200
		$argLists[] = [ 4.5, '+4.5' ];
201
		$argLists[] = [ -4.5, '-4.5' ];
202
		$argLists[] = [ '+4.2', '+4.2' ];
203
		$argLists[] = [ 0, '+0' ];
204
		$argLists[] = [ 0.0, '+0' ];
205
		$argLists[] = [ 1.0, '+1' ];
206
		$argLists[] = [ 0.5, '+0.5' ];
207
		$argLists[] = [ '-0.42', '-0.42' ];
208
		$argLists[] = [ '-0.0', '+0.0' ];
209
		$argLists[] = [ '-0', '+0' ];
210
		$argLists[] = [ '+0.0', '+0.0' ];
211
		$argLists[] = [ '+0', '+0' ];
212
		$argLists[] = [ 2147483649, '+2147483649' ];
213
		$argLists[] = [ 1000000000000000, '+1000000000000000' ];
214
		$argLists[] = [
215
			1 + 1e-12 / 3,
216
			'+1.0000000000003333'
217
		];
218
		$argLists[] = [
219
			1 + 1e-13 / 3,
220
			'+1.0000000000000333'
221
		];
222
		$argLists[] = [
223
			1 + 1e-14 / 3,
224
			'+1.0000000000000033'
225
		];
226
		$argLists[] = [
227
			1 + 1e-15 / 3,
228
			'+1.0000000000000004'
229
		];
230
		$argLists[] = [
231
			1 + 1e-16 / 3,
232
			'+1'
233
		];
234
		$argLists[] = [
235
			1 - 1e-16,
236
			'+0.99999999999999989'
237
		];
238
		$argLists[] = [
239
			1 - 1e-17,
240
			'+1'
241
		];
242
243
		return $argLists;
244
	}
245
246
	/**
247
	 * @dataProvider getValueFloatProvider
248
	 */
249
	public function testGetValueFloat( DecimalValue $value, $expected ) {
250
		$actual = $value->getValueFloat();
251
		$this->assertSame( $expected, $actual );
252
	}
253
254
	public function getValueFloatProvider() {
255
		$argLists = [];
256
257
		$argLists[] = [ new DecimalValue( 42 ), 42.0 ];
258
		$argLists[] = [ new DecimalValue( -42 ), -42.0 ];
259
		$argLists[] = [ new DecimalValue( '-42' ), -42.0 ];
260
		$argLists[] = [ new DecimalValue( 4.5 ), 4.5 ];
261
		$argLists[] = [ new DecimalValue( -4.5 ), -4.5 ];
262
		$argLists[] = [ new DecimalValue( '+4.2' ), 4.2 ];
263
		$argLists[] = [ new DecimalValue( 0 ), 0.0 ];
264
		$argLists[] = [ new DecimalValue( 0.5 ), 0.5 ];
265
		$argLists[] = [ new DecimalValue( '-0.42' ), -0.42 ];
266
		$argLists[] = [ new DecimalValue( '-0.0' ), 0.0 ];
267
		$argLists[] = [ new DecimalValue( '-0' ), 0.0 ];
268
		$argLists[] = [ new DecimalValue( '+0.0' ), 0.0 ];
269
		$argLists[] = [ new DecimalValue( '+0' ), 0.0 ];
270
271
		return $argLists;
272
	}
273
274
	/**
275
	 * @dataProvider getGetIntegerPartProvider
276
	 */
277
	public function testGetIntegerPart( DecimalValue $value, $expected ) {
278
		$actual = $value->getIntegerPart();
279
		$this->assertSame( $expected, $actual );
280
	}
281
282
	public function getGetIntegerPartProvider() {
283
		return [
284
			[ new DecimalValue( '+0' ), '0' ],
285
			[ new DecimalValue( '-0.0' ), '0' ],
286
			[ new DecimalValue( '+10' ), '10' ],
287
			[ new DecimalValue( '-10' ), '10' ],
288
			[ new DecimalValue( '+10.663' ), '10' ],
289
			[ new DecimalValue( '-10.001' ), '10' ],
290
			[ new DecimalValue( '+0.01' ), '0' ],
291
		];
292
	}
293
294
	/**
295
	 * @dataProvider getGetIntegerPartProvider
296
	 */
297
	public function testGetFractionalPart( DecimalValue $value, $expected ) {
298
		$actual = $value->getIntegerPart();
299
		$this->assertSame( $expected, $actual );
300
	}
301
302
	public function getGetFractionalPartProvider() {
303
		return [
304
			[ new DecimalValue( '+0' ), '' ],
305
			[ new DecimalValue( '-0.0' ), '0' ],
306
			[ new DecimalValue( '+10' ), '' ],
307
			[ new DecimalValue( '+10.663' ), '663' ],
308
			[ new DecimalValue( '-10.001' ), '001' ],
309
			[ new DecimalValue( '+0.01' ), '01' ],
310
		];
311
	}
312
313
	/**
314
	 * @dataProvider computeComplementProvider
315
	 */
316
	public function testComputeComplement( DecimalValue $value, $expected ) {
317
		$complement = $value->computeComplement();
318
		$this->assertSame( $expected, $complement->getValue() );
319
320
		$actual = $complement->computeComplement();
321
		$this->assertSame( $value->getValue(), $actual->getValue() );
322
	}
323
324
	public function computeComplementProvider() {
325
		return [
326
			[ new DecimalValue( '+0' ), '+0' ],
327
			[ new DecimalValue( '+0.00' ), '+0.00' ],
328
			[ new DecimalValue( '+1' ), '-1' ],
329
			[ new DecimalValue( '+100.663' ), '-100.663' ],
330
			[ new DecimalValue( '-0.001' ), '+0.001' ],
331
		];
332
	}
333
334
	/**
335
	 * @dataProvider computeComputeAbsolute
336
	 */
337
	public function testComputeAbsolute( DecimalValue $value, $expected ) {
338
		$absolute = $value->computeAbsolute();
339
		$this->assertSame( $expected, $absolute->getValue() );
340
341
		$actual = $absolute->computeAbsolute();
342
		$this->assertSame( $absolute->getValue(), $actual->getValue() );
343
	}
344
345
	public function computeComputeAbsolute() {
346
		return [
347
			[ new DecimalValue( '+0' ), '+0' ],
348
			[ new DecimalValue( '+1' ), '+1' ],
349
			[ new DecimalValue( '-1' ), '+1' ],
350
			[ new DecimalValue( '+100.663' ), '+100.663' ],
351
			[ new DecimalValue( '-100.663' ), '+100.663' ],
352
			[ new DecimalValue( '+0.001' ), '+0.001' ],
353
			[ new DecimalValue( '-0.001' ), '+0.001' ],
354
		];
355
	}
356
357
	/**
358
	 * @dataProvider isZeroProvider
359
	 */
360
	public function testIsZero( DecimalValue $value, $expected ) {
361
		$actual = $value->isZero();
362
		$this->assertSame( $expected, $actual );
363
	}
364
365
	public function isZeroProvider() {
366
		return [
367
			[ new DecimalValue( '+0' ), true ],
368
			[ new DecimalValue( '-0.00' ), true ],
369
370
			[ new DecimalValue( '+1' ),       false ],
371
			[ new DecimalValue( '+100.663' ), false ],
372
			[ new DecimalValue( '-0.001' ),   false ],
373
		];
374
	}
375
376
	/**
377
	 * @dataProvider provideGetTrim
378
	 */
379
	public function testGetTrim( DecimalValue $value, DecimalValue $expected ) {
380
		$actual = $value->getTrimmed();
381
		$this->assertSame( $expected->getValue(), $actual->getValue() );
382
	}
383
384
	public function provideGetTrim() {
385
		return [
386
			[ new DecimalValue( '+8' ),     new DecimalValue( '+8' ) ],
387
			[ new DecimalValue( '+80' ),    new DecimalValue( '+80' ) ],
388
			[ new DecimalValue( '+800' ),   new DecimalValue( '+800' ) ],
389
			[ new DecimalValue( '+0' ),     new DecimalValue( '+0' ) ],
390
			[ new DecimalValue( '+0.0' ),   new DecimalValue( '+0' ) ],
391
			[ new DecimalValue( '+10.00' ), new DecimalValue( '+10' ) ],
392
			[ new DecimalValue( '-0.1' ),   new DecimalValue( '-0.1' ) ],
393
			[ new DecimalValue( '-0.10' ),  new DecimalValue( '-0.1' ) ],
394
			[ new DecimalValue( '-0.010' ), new DecimalValue( '-0.01' ) ],
395
			[ new DecimalValue( '-0.001' ), new DecimalValue( '-0.001' ) ],
396
		];
397
	}
398
399
}
400