Completed
Pull Request — master (#55)
by no
10:00 queued 07:32
created

QuantityValueTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 27
c 6
b 1
f 1
lcom 0
cbo 3
dl 0
loc 350
rs 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
B validConstructorArgumentsProvider() 0 28 1
B invalidConstructorArgumentsProvider() 0 40 1
B testNewFromNumber() 0 13 5
B newFromNumberProvider() 0 32 1
A testGetValue() 0 3 1
A testGetAmount() 0 3 1
A testGetUnit() 0 3 1
A testGetUpperBound() 0 3 1
A testGetLowerBound() 0 3 1
A testTrailingNewlineRobustness() 0 15 1
A testGetSortKey() 0 3 1
A testGetUncertainty() 0 3 1
A getUncertaintyProvider() 0 16 1
A testGetUncertaintyMargin() 0 7 2
A getUncertaintyMarginProvider() 0 14 1
A testGetOrderOfUncertainty() 0 3 1
A getOrderOfUncertaintyProvider() 0 21 1
A testGetSignificantFigures() 0 3 1
A getSignificantFiguresProvider() 0 19 1
A testTransform() 0 10 1
B transformProvider() 0 35 1
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
use DataValues\QuantityValue;
7
8
/**
9
 * @covers DataValues\QuantityValue
10
 *
11
 * @group DataValue
12
 * @group DataValueExtensions
13
 *
14
 * @license GPL-2.0+
15
 * @author Daniel Kinzler
16
 */
17
class QuantityValueTest extends DataValueTest {
18
19
	/**
20
	 * @see DataValueTest::getClass
21
	 *
22
	 * @return string
23
	 */
24
	public function getClass() {
25
		return 'DataValues\QuantityValue';
26
	}
27
28
	public function validConstructorArgumentsProvider() {
29
		return array(
30
			'Integer' => array(
31
				new DecimalValue( '+42' ),
32
				'1',
33
				new DecimalValue( '+42' ),
34
				new DecimalValue( '+42' )
35
			),
36
			'Float' => array(
37
				new DecimalValue( '+0.01' ),
38
				'1',
39
				new DecimalValue( '+0.02' ),
40
				new DecimalValue( '+0.0001' )
41
			),
42
			'Negative' => array(
43
				new DecimalValue( '-0.5' ),
44
				'1',
45
				new DecimalValue( '+0.02' ),
46
				new DecimalValue( '-0.7' )
47
			),
48
			'Unknown uncertainty' => array(
49
				new DecimalValue( '+1' ),
50
				'1',
51
				null,
52
				null
53
			),
54
		);
55
	}
56
57
	public function invalidConstructorArgumentsProvider() {
58
		return array(
59
			'Empty unit' => array(
60
				new DecimalValue( '+0' ),
61
				'',
62
				new DecimalValue( '+0' ),
63
				new DecimalValue( '+0' )
64
			),
65
			'Integer unit' => array(
66
				new DecimalValue( '+0' ),
67
				1,
68
				new DecimalValue( '+0' ),
69
				new DecimalValue( '+0' )
70
			),
71
			'Upper below amount' => array(
72
				new DecimalValue( '+0' ),
73
				'1',
74
				new DecimalValue( '-0.001' ),
75
				new DecimalValue( '-1' )
76
			),
77
			'Lower above amount' => array(
78
				new DecimalValue( '+0' ),
79
				'1',
80
				new DecimalValue( '+1' ),
81
				new DecimalValue( '+0.001' )
82
			),
83
			'Upper without lower' => array(
84
				new DecimalValue( '+1' ),
85
				'1',
86
				new DecimalValue( '+1' ),
87
				null
88
			),
89
			'Lower without upper' => array(
90
				new DecimalValue( '+1' ),
91
				'1',
92
				null,
93
				new DecimalValue( '+1' )
94
			),
95
		);
96
	}
97
98
	/**
99
	 * @dataProvider instanceProvider
100
	 */
101
	public function testGetValue( QuantityValue $quantity, array $arguments ) {
102
		$this->assertSame( $quantity, $quantity->getValue() );
103
	}
104
105
	/**
106
	 * @dataProvider instanceProvider
107
	 */
108
	public function testGetAmount( QuantityValue $quantity, array $arguments ) {
109
		$this->assertSame( $arguments[0], $quantity->getAmount() );
110
	}
111
112
	/**
113
	 * @dataProvider instanceProvider
114
	 */
115
	public function testGetUnit( QuantityValue $quantity, array $arguments ) {
116
		$this->assertSame( $arguments[1], $quantity->getUnit() );
117
	}
118
119
	/**
120
	 * @dataProvider instanceProvider
121
	 */
122
	public function testGetUpperBound( QuantityValue $quantity, array $arguments ) {
123
		$this->assertSame( $arguments[2], $quantity->getUpperBound() );
124
	}
125
126
	/**
127
	 * @dataProvider instanceProvider
128
	 */
129
	public function testGetLowerBound( QuantityValue $quantity, array $arguments ) {
130
		$this->assertSame( $arguments[3], $quantity->getLowerBound() );
131
	}
132
133
	/**
134
	 * @dataProvider newFromNumberProvider
135
	 */
136
	public function testNewFromNumber( $amount, $unit, $upperBound, $lowerBound, QuantityValue $expected ) {
137
		$quantity = QuantityValue::newFromNumber( $amount, $unit, $upperBound, $lowerBound );
138
139
		$this->assertEquals( $expected->getAmount()->getValue(), $quantity->getAmount()->getValue() );
140
		$this->assertEquals(
141
			$expected->getUpperBound() ? $expected->getUpperBound()->getValue() : null,
142
			$quantity->getUpperBound() ? $quantity->getUpperBound()->getValue() : null
143
		);
144
		$this->assertEquals(
145
			$expected->getLowerBound() ? $expected->getLowerBound()->getValue() : null,
146
			$quantity->getLowerBound() ? $quantity->getLowerBound()->getValue() : null
147
		);
148
	}
149
150
	public function newFromNumberProvider() {
151
		return array(
152
			array(
153
				42, '1', null, null,
154
				new QuantityValue( new DecimalValue( '+42' ), '1' )
155
			),
156
			array(
157
				-0.05, '1', null, null,
158
				new QuantityValue( new DecimalValue( '-0.05' ), '1' )
159
			),
160
			array(
161
				0, 'm', 0.5, -0.5,
162
				new QuantityValue( new DecimalValue( '+0' ), 'm', new DecimalValue( '+0.5' ), new DecimalValue( '-0.5' ) )
163
			),
164
			array(
165
				'+23', '1', null, null,
166
				new QuantityValue( new DecimalValue( '+23' ), '1' )
167
			),
168
			array(
169
				'+42', '1', '+43', '+41',
170
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( '+43' ), new DecimalValue( '+41' ) )
171
			),
172
			array(
173
				'-0.05', 'm', '-0.04', '-0.06',
174
				new QuantityValue( new DecimalValue( '-0.05' ), 'm', new DecimalValue( '-0.04' ), new DecimalValue( '-0.06' ) )
175
			),
176
			array(
177
				new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ),
178
				new QuantityValue( new DecimalValue( '+42' ), '1', new DecimalValue( 43 ), new DecimalValue( 41.0 ) )
179
			),
180
		);
181
	}
182
183
	/**
184
	 * @see https://phabricator.wikimedia.org/T110728
185
	 * @see http://www.regular-expressions.info/anchors.html#realend
186
	 */
187
	public function testTrailingNewlineRobustness() {
188
		$value = QuantityValue::newFromArray( array(
189
			'amount' => "-0.0\n",
190
			'unit' => "1\n",
191
			'upperBound' => "-0.0\n",
192
			'lowerBound' => "-0.0\n",
193
		) );
194
195
		$this->assertSame( array(
196
			'amount' => '+0.0',
197
			'unit' => "1\n",
198
			'upperBound' => '+0.0',
199
			'lowerBound' => '+0.0',
200
		), $value->getArrayValue() );
201
	}
202
203
	/**
204
	 * @dataProvider instanceProvider
205
	 */
206
	public function testGetSortKey( QuantityValue $quantity ) {
207
		$this->assertSame( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
208
	}
209
210
	/**
211
	 * @dataProvider getUncertaintyProvider
212
	 */
213
	public function testGetUncertainty( QuantityValue $quantity, $expected ) {
214
		$this->assertSame( $expected, $quantity->getUncertainty() );
215
	}
216
217
	public function getUncertaintyProvider() {
218
		return array(
219
			array( QuantityValue::newFromNumber( '+0', '1', '+0', '+0' ), 0.0 ),
220
221
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), 2.0 ),
222
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 0.02 ),
223
			array( QuantityValue::newFromNumber( '+100', '1', '+101', '+99' ), 2.0 ),
224
			array( QuantityValue::newFromNumber( '+100.0', '1', '+100.1', '+99.9' ), 0.2 ),
225
			array( QuantityValue::newFromNumber( '+12.34', '1', '+12.35', '+12.33' ), 0.02 ),
226
227
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), 0.8 ),
228
			array( QuantityValue::newFromNumber( '+7.3', '1', '+7.7', '+5.2' ), 2.5 ),
229
230
			'Unknown uncertainty' => array( QuantityValue::newFromNumber( '+0' ), null ),
231
		);
232
	}
233
234
	/**
235
	 * @dataProvider getUncertaintyMarginProvider
236
	 */
237
	public function testGetUncertaintyMargin( QuantityValue $quantity, $expected ) {
238
		if ( $expected === null ) {
239
			$this->assertNull( $quantity->getUncertaintyMargin() );
240
		} else {
241
			$this->assertSame( $expected, $quantity->getUncertaintyMargin()->getValue() );
242
		}
243
	}
244
245
	public function getUncertaintyMarginProvider() {
246
		return array(
247
			array( QuantityValue::newFromNumber( '+0', '1', '+1', '-1' ), '+1' ),
248
			array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), '+0.01' ),
249
250
			array( QuantityValue::newFromNumber( '-1', '1', '-1', '-1' ), '+0' ),
251
252
			array( QuantityValue::newFromNumber( '+0', '1', '+0.2', '-0.6' ), '+0.6' ),
253
			array( QuantityValue::newFromNumber( '+7.5', '1', '+7.5', '+5.5' ), '+2.0' ),
254
			array( QuantityValue::newFromNumber( '+11.5', '1', '+15', '+10.5' ), '+3.5' ),
255
256
			'Unknown uncertainty' => array( QuantityValue::newFromNumber( '+0' ), null ),
257
		);
258
	}
259
260
	/**
261
	 * @dataProvider getOrderOfUncertaintyProvider
262
	 */
263
	public function testGetOrderOfUncertainty( QuantityValue $quantity, $expected ) {
264
		$this->assertSame( $expected, $quantity->getOrderOfUncertainty() );
265
	}
266
267
	public function getOrderOfUncertaintyProvider() {
268
		return array(
269
			0 => array( QuantityValue::newFromNumber( '+0' ), null ),
270
			1 => array( QuantityValue::newFromNumber( '-123' ), null ),
271
			2 => array( QuantityValue::newFromNumber( '-1.23' ), null ),
272
273
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 0 ),
274
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), -2 ),
275
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), -1 ),
276
277
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), -2 ),
278
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), -3 ),
279
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), -3 ),
280
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
281
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 0 ),
282
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 1 ),
283
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
284
285
			'Unknown uncertainty' => array( QuantityValue::newFromNumber( '+0' ), null ),
286
		);
287
	}
288
289
	/**
290
	 * @dataProvider getSignificantFiguresProvider
291
	 */
292
	public function testGetSignificantFigures( QuantityValue $quantity, $expected ) {
293
		$this->assertSame( $expected, $quantity->getSignificantFigures() );
294
	}
295
296
	public function getSignificantFiguresProvider() {
297
		return array(
298
			0 => array( QuantityValue::newFromNumber( '+0' ), 1 ),
299
			1 => array( QuantityValue::newFromNumber( '-123' ), 3 ),
300
			2 => array( QuantityValue::newFromNumber( '-1.23' ), 1 ),
301
302
			10 => array( QuantityValue::newFromNumber( '-100', '1', '-99', '-101' ), 3 ),
303
			11 => array( QuantityValue::newFromNumber( '+0.00', '1', '+0.01', '-0.01' ), 4 ),
304
			12 => array( QuantityValue::newFromNumber( '-117.3', '1', '-117.2', '-117.4' ), 5 ),
305
306
			20 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.97' ), 6 ),
307
			21 => array( QuantityValue::newFromNumber( '-0.002', '1', '-0.001', '-0.004' ), 5 ),
308
			22 => array( QuantityValue::newFromNumber( '-0.002', '1', '+0.001', '-0.06' ), 5 ),
309
			23 => array( QuantityValue::newFromNumber( '-21', '1', '+1.1', '-120' ), 1 ),
310
			24 => array( QuantityValue::newFromNumber( '-2', '1', '+1.1', '-120' ), 1 ),
311
			25 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900.03' ), 3 ),
312
			26 => array( QuantityValue::newFromNumber( '+1000', '1', '+1100', '+900' ), 2 ),
313
		);
314
	}
315
316
	/**
317
	 * @dataProvider transformProvider
318
	 */
319
	public function testTransform( QuantityValue $quantity, $transformation, QuantityValue $expected ) {
320
		$args = func_get_args();
321
		$extraArgs = array_slice( $args, 3 );
322
323
		$call = array( $quantity, 'transform' );
324
		$callArgs = array_merge( array( '?', $transformation ), $extraArgs );
325
		$actual = call_user_func_array( $call, $callArgs );
326
327
		$this->assertTrue( $expected->equals( $actual ), "Expected $expected, got $actual" );
328
	}
329
330
	public function transformProvider() {
331
		$identity = function ( DecimalValue $value ) {
332
			return $value;
333
		};
334
335
		$square = function ( DecimalValue $value ) {
336
			$v = $value->getValueFloat();
337
			return new DecimalValue( $v * $v * $v );
338
		};
339
340
		$scale = function ( DecimalValue $value, $factor ) {
341
			return new DecimalValue( $value->getValueFloat() * $factor );
342
		};
343
344
		return array(
345
			 0 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $identity, QuantityValue::newFromNumber(   '+10',    '?',   '+11',    '+9' ) ),
346
			 1 => array( QuantityValue::newFromNumber(  '-0.5', '1', '-0.4', '-0.6' ), $identity, QuantityValue::newFromNumber(    '-0.5',  '?',    '-0.4',  '-0.6' ) ),
347
			 2 => array( QuantityValue::newFromNumber(  '+0',   '1', '+1',   '-1' ),   $square,   QuantityValue::newFromNumber(    '+0',    '?',    '+1',    '-1' ) ),
348
			 3 => array( QuantityValue::newFromNumber( '+10',   '1', '+11',  '+9' ),   $square,   QuantityValue::newFromNumber( '+1000',    '?', '+1300',  '+700' ) ), // note how rounding applies to bounds
349
			 4 => array( QuantityValue::newFromNumber(  '+0.5', '1', '+0.6', '+0.4' ), $scale,    QuantityValue::newFromNumber(    '+0.25', '?',    '+0.30',  '+0.20' ), 0.5 ),
350
351
			// note: absolutely exact values require conversion with infinite precision!
352
			10 => array( QuantityValue::newFromNumber( '+100', '1', '+100',   '+100' ),    $scale, QuantityValue::newFromNumber( '+12825', '?', '+12825', '+12825' ), 128.25 ),
353
354
			11 => array( QuantityValue::newFromNumber( '+100', '1', '+110',    '+90' ),    $scale, QuantityValue::newFromNumber( '+330',    '?', '+370',    '+300' ), 3.3333 ),
355
			12 => array( QuantityValue::newFromNumber( '+100', '1', '+100.1',  '+99.9' ),  $scale, QuantityValue::newFromNumber( '+333.3',  '?', '+333.7',  '+333.0' ), 3.3333 ),
356
			13 => array( QuantityValue::newFromNumber( '+100', '1', '+100.01', '+99.99' ), $scale, QuantityValue::newFromNumber( '+333.33', '?', '+333.36', '+333.30' ), 3.3333 ),
357
358
			'Unknown uncertainty' => array(
359
				QuantityValue::newFromNumber( '+0' ),
360
				$identity,
361
				QuantityValue::newFromNumber( '+0', '?', null, null )
362
			),
363
		);
364
	}
365
366
}
367