Passed
Push — nullBounds ( b3e7e4...f3529c )
by no
03:35
created

DecimalParserTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 8
Bugs 0 Features 2
Metric Value
wmc 11
c 8
b 0
f 2
lcom 0
cbo 3
dl 0
loc 160
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getParserClass() 0 3 1
A getInstance() 0 3 1
A testUnlocalization() 0 22 1
B validInputProvider() 0 43 2
A invalidInputProvider() 0 21 2
A splitDecimalExponentProvider() 0 16 1
A testSplitDecimalExponent() 0 7 1
A testApplyDecimalExponent() 0 6 1
A applyDecimalExponentProvider() 0 7 1
1
<?php
2
3
namespace ValueParsers\Test;
4
5
use DataValues\DecimalValue;
6
use ValueParsers\DecimalParser;
7
8
/**
9
 * @covers ValueParsers\DecimalParser
10
 *
11
 * @group DataValue
12
 * @group DataValueExtensions
13
 *
14
 * @license GPL-2.0+
15
 * @author Daniel Kinzler
16
 */
17
class DecimalParserTest extends StringValueParserTest {
18
19
	/**
20
	 * @deprecated since DataValues Common 0.3, just use getInstance.
21
	 */
22
	protected function getParserClass() {
23
		throw new \LogicException( 'Should not be called, use getInstance' );
24
	}
25
26
	/**
27
	 * @see ValueParserTestBase::getInstance
28
	 *
29
	 * @return DecimalParser
30
	 */
31
	protected function getInstance() {
32
		return new DecimalParser();
33
	}
34
35
	/**
36
	 * @see ValueParserTestBase::validInputProvider
37
	 */
38
	public function validInputProvider() {
39
		$argLists = array();
40
41
		$valid = array(
42
			'0' => 0,
43
			'-0' => 0,
44
			'-00.00' => '-0.00',
45
			'+00.00' => '+0.00',
46
			'0001' => 1,
47
			'+42' => 42,
48
			'+01' => 01,
49
			'9001' => 9001,
50
			'-1' => -1,
51
			'-42' => -42,
52
			'.5' => 0.5,
53
			'-.125' => -0.125,
54
			'3.' => 3,
55
			',3,' => 3,
56
			'2.125' => 2.125,
57
			'2.1250' => '+2.1250',
58
			'2.1250e0' => '+2.1250',
59
			'2.1250e3' => '+2125.0',
60
			'2.1250e+3' => '+2125.0',
61
			'2.1250e-2' => '+0.021250',
62
			'123e+3' => '+123000',
63
			'123e-2' => '+1.23',
64
			'-123e-5' => '-0.00123',
65
			' 5 ' => 5,
66
			'100,000' => 100000,
67
			'100 000' => 100000,
68
			'100\'000' => 100000,
69
		);
70
71
		foreach ( $valid as $value => $expected ) {
72
			// Because PHP turns them into ints using black magic
73
			$value = (string)$value;
74
75
			$expected = new DecimalValue( $expected );
76
			$argLists[] = array( $value, $expected );
77
		}
78
79
		return $argLists;
80
	}
81
82
	/**
83
	 * @see StringValueParserTest::invalidInputProvider
84
	 */
85
	public function invalidInputProvider() {
86
		$argLists = parent::invalidInputProvider();
87
88
		$invalid = array(
89
			'foo',
90
			'',
91
			'--1',
92
			'1-',
93
			'one',
94
			'0x20',
95
			'1+1',
96
			'1-1',
97
			'1.2.3',
98
		);
99
100
		foreach ( $invalid as $value ) {
101
			$argLists[] = array( $value );
102
		}
103
104
		return $argLists;
105
	}
106
107
	public function testUnlocalization() {
108
		$unlocalizer = $this->getMock( 'ValueParsers\NumberUnlocalizer' );
109
110
		$unlocalizer->expects( $this->once() )
111
			->method( 'unlocalizeNumber' )
112
			->will( $this->returnCallback( function( $number ) {
113
				return str_replace( '#', '', $number );
114
			} ) );
115
116
		$unlocalizer->expects( $this->never() )
117
			->method( 'getNumberRegex' );
118
119
		$unlocalizer->expects( $this->never() )
120
			->method( 'getUnitRegex' );
121
122
		$parser = new DecimalParser( null, $unlocalizer );
123
124
		$input = '###20#000#000###';
125
		$value = $parser->parse( $input );
126
127
		$this->assertEquals( '20000000', $value->getValue() );
128
	}
129
130
	public function splitDecimalExponentProvider() {
131
		return array(
132
			'trailing newline' => array( "1.2E3\n", '1.2', 3 ),
133
			'whitespace' => array( ' 1.2E3 ', ' 1.2E3 ', 0 ),
134
			'no exponent' => array( '1.2', '1.2', 0 ),
135
			'exponent' => array( '1.2E3', '1.2', 3 ),
136
			'negative exponent' => array( '+1.2e-2', '+1.2', -2 ),
137
			'positive exponent' => array( '-12e+3', '-12', 3 ),
138
			'leading zero' => array( '12e+09', '12', 9 ),
139
			'trailing decimal point' => array( '12.e+3', '12.', 3 ),
140
			'leading decimal point' => array( '.12e+3', '.12', 3 ),
141
			'space' => array( '12 e+3', '12 ', 3 ),
142
			'x10 syntax' => array( '12x10^3', '12', 3 ),
143
			'comma' => array( '12e3,4', '12', 34 ),
144
		);
145
	}
146
147
	/**
148
	 * @dataProvider splitDecimalExponentProvider
149
	 */
150
	public function testSplitDecimalExponent( $valueString, $expectedDecimal, $expectedExponent ) {
151
		$parser = new DecimalParser();
152
		list( $decimal, $exponent ) = $parser->splitDecimalExponent( $valueString );
153
154
		$this->assertSame( $expectedDecimal, $decimal );
155
		$this->assertSame( $expectedExponent, $exponent );
156
	}
157
158
	public function applyDecimalExponentProvider() {
159
		return array(
160
			'no exponent' => array( new DecimalValue( '+1.2' ), 0, new DecimalValue( '+1.2' ) ),
161
			'negative exponent' => array( new DecimalValue( '-1.2' ), -2, new DecimalValue( '-0.012' ) ),
162
			'positive exponent' => array( new DecimalValue( '-12' ), 3, new DecimalValue( '-12000' ) ),
163
		);
164
	}
165
166
	/**
167
	 * @dataProvider applyDecimalExponentProvider
168
	 */
169
	public function testApplyDecimalExponent( DecimalValue $decimal, $exponent, DecimalValue $expectedDecimal ) {
170
		$parser = new DecimalParser();
171
		$actualDecimal = $parser->applyDecimalExponent( $decimal, $exponent );
172
173
		$this->assertSame( $expectedDecimal->getValue(), $actualDecimal->getValue() );
174
	}
175
176
}
177