testGetUnitRegexMismatch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ValueParsers\Test;
4
5
use ValueParsers\BasicNumberUnlocalizer;
6
7
/**
8
 * @covers ValueParsers\BasicNumberUnlocalizer
9
 *
10
 * @group DataValue
11
 * @group DataValueExtensions
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Daniel Kinzler
15
 */
16
class BasicNumberUnlocalizerTest extends \PHPUnit\Framework\TestCase {
17
18
	public function provideUnlocalizeNumber() {
19
		return [
20
			[ '5', '5' ],
21
			[ '+3', '+3' ],
22
			[ '-15', '-15' ],
23
24
			[ '5.3', '5.3' ],
25
			[ '+3.2', '+3.2' ],
26
			[ '-15.77', '-15.77' ],
27
28
			[ '.3', '.3' ],
29
			[ '+.2', '+.2' ],
30
			[ '-.77', '-.77' ],
31
32
			[ '5.3e4', '5.3e4' ],
33
			[ '+3.2E-4', '+3.2E-4' ],
34
			[ '-15.77e+4.2', '-15.77e+4.2' ],
35
36
			[ '0x20', '0x20' ],
37
			[ '0X20', '0X20' ],
38
39
			[ '1,335.3', '1335.3' ],
40
			[ '+1,333.2', '+1333.2' ],
41
			[ '-1,315.77', '-1315.77' ],
42
43
			[ ' 1,333.3', '1333.3' ],
44
			[ '1,333.3 ', '1333.3' ],
45
		];
46
	}
47
48
	/**
49
	 * @dataProvider provideUnlocalizeNumber
50
	 */
51
	public function testUnlocalizeNumber( $localized, $expected ) {
52
		$unlocalizer = new BasicNumberUnlocalizer();
53
		$unlocalized = $unlocalizer->unlocalizeNumber( $localized );
54
55
		$this->assertSame( $expected, $unlocalized );
56
	}
57
58
	public function provideGetNumberRegexMatch() {
59
		return [
60
			[ '5' ],
61
			[ '+3' ],
62
			[ '-15' ],
63
64
			[ '5.3' ],
65
			[ '+3.2' ],
66
			[ '-15.77' ],
67
68
			[ '.3' ],
69
			[ '+.2' ],
70
			[ '-.77' ],
71
72
			[ '5.3e4' ],
73
			[ '+3.2E-4' ],
74
			[ '-15.77e+2' ],
75
76
			[ '1,335.3' ],
77
			[ '+1,333.2' ],
78
			[ '-1,315.77' ],
79
		];
80
	}
81
82
	/**
83
	 * @dataProvider provideGetNumberRegexMatch
84
	 */
85
	public function testGetNumberRegexMatch( $value ) {
86
		$unlocalizer = new BasicNumberUnlocalizer();
87
		$regex = $unlocalizer->getNumberRegex();
88
89
		$this->assertTrue( (bool)preg_match( "/^($regex)$/u", $value ) );
90
	}
91
92
	public function provideGetNumberRegexMismatch() {
93
		return [
94
			[ '' ],
95
			[ ' ' ],
96
			[ '+' ],
97
			[ 'e' ],
98
99
			[ '+.' ],
100
			[ '.-' ],
101
			[ '...' ],
102
103
			[ '0x20' ],
104
			[ '2x2' ],
105
			[ 'x2' ],
106
			[ '2x' ],
107
108
			[ 'e.' ],
109
			[ '.e' ],
110
			[ '12e' ],
111
			[ 'E17' ],
112
113
			[ '+-3' ],
114
			[ '++7' ],
115
			[ '--5' ],
116
		];
117
	}
118
119
	/**
120
	 * @dataProvider provideGetNumberRegexMismatch
121
	 */
122
	public function testGetNumberRegexMismatch( $value ) {
123
		$unlocalizer = new BasicNumberUnlocalizer();
124
		$regex = $unlocalizer->getNumberRegex();
125
126
		$this->assertFalse( (bool)preg_match( "/^($regex)$/u", $value ) );
127
	}
128
129
	public function provideGetUnitRegexMatch() {
130
		return [
131
			[ '' ],
132
		];
133
	}
134
135
	/**
136
	 * @dataProvider provideGetUnitRegexMatch
137
	 */
138
	public function testGetUnitRegexMatch( $value ) {
139
		$unlocalizer = new BasicNumberUnlocalizer();
140
		$regex = $unlocalizer->getUnitRegex();
141
142
		$this->assertTrue( (bool)preg_match( "/^($regex)$/u", $value ) );
143
	}
144
145
	public function provideGetUnitRegexMismatch() {
146
		return [
147
			[ ' ' ],
148
149
			[ '^' ],
150
			[ '/' ],
151
152
			[ 'x^' ],
153
			[ 'x/' ],
154
155
			[ '2' ],
156
			[ '2b' ],
157
158
			[ '~' ],
159
			[ '#' ],
160
		];
161
	}
162
163
	/**
164
	 * @dataProvider provideGetUnitRegexMismatch
165
	 */
166
	public function testGetUnitRegexMismatch( $value ) {
167
		$unlocalizer = new BasicNumberUnlocalizer();
168
		$regex = $unlocalizer->getUnitRegex();
169
170
		$this->assertFalse( (bool)preg_match( "/^($regex)$/u", $value ) );
171
	}
172
173
}
174