Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ParamValidator / TypeDef / FloatDefTest.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\SimpleCallbacks;
6 use Wikimedia\ParamValidator\ValidationException;
7
8 /**
9 * @covers Wikimedia\ParamValidator\TypeDef\FloatDef
10 */
11 class FloatDefTest extends TypeDefTestCase {
12
13 protected static $testClass = FloatDef::class;
14
15 public function provideValidate() {
16 return [
17 [ '123', 123.0 ],
18 [ '123.4', 123.4 ],
19 [ '0.4', 0.4 ],
20 [ '.4', 0.4 ],
21
22 [ '+123', 123.0 ],
23 [ '+123.4', 123.4 ],
24 [ '+0.4', 0.4 ],
25 [ '+.4', 0.4 ],
26
27 [ '-123', -123.0 ],
28 [ '-123.4', -123.4 ],
29 [ '-.4', -0.4 ],
30 [ '-.4', -0.4 ],
31
32 [ '123e5', 12300000.0 ],
33 [ '123E5', 12300000.0 ],
34 [ '123.4e+5', 12340000.0 ],
35 [ '123E5', 12300000.0 ],
36 [ '-123.4e-5', -0.001234 ],
37 [ '.4E-5', 0.000004 ],
38
39 [ '0', 0 ],
40 [ '000000', 0 ],
41 [ '0000.0000', 0 ],
42 [ '000001.0002000000', 1.0002 ],
43 [ '1e0', 1 ],
44 [ '1e-0000', 1 ],
45 [ '1e+00010', 1e10 ],
46
47 'Weird, but ok' => [ '-0', 0 ],
48 'Underflow is ok' => [ '1e-9999', 0 ],
49
50 'Empty decimal part' => [ '1.', new ValidationException( 'test', '1.', [], 'badfloat', [] ) ],
51 'Bad sign' => [ ' 1', new ValidationException( 'test', ' 1', [], 'badfloat', [] ) ],
52 'Comma as decimal separator or thousands grouping?'
53 => [ '1,234', new ValidationException( 'test', '1,234', [], 'badfloat', [] ) ],
54 'U+2212 minus' => [ '−1', new ValidationException( 'test', '−1', [], 'badfloat', [] ) ],
55 'Overflow' => [ '1e9999', new ValidationException( 'test', '1e9999', [], 'notfinite', [] ) ],
56 'Overflow, -INF'
57 => [ '-1e9999', new ValidationException( 'test', '-1e9999', [], 'notfinite', [] ) ],
58 'Bogus value' => [ 'foo', new ValidationException( 'test', 'foo', [], 'badfloat', [] ) ],
59 'Bogus value (2)' => [ '123f4', new ValidationException( 'test', '123f4', [], 'badfloat', [] ) ],
60 'Newline' => [ "123\n", new ValidationException( 'test', "123\n", [], 'badfloat', [] ) ],
61 ];
62 }
63
64 public function provideStringifyValue() {
65 $digits = defined( 'PHP_FLOAT_DIG' ) ? PHP_FLOAT_DIG : 15;
66
67 return [
68 [ 1.2, '1.2' ],
69 [ 10 / 3, '3.' . str_repeat( '3', $digits - 1 ) ],
70 [ 1e100, '1.0e+100' ],
71 [ 6.022e-23, '6.022e-23' ],
72 ];
73 }
74
75 /** @dataProvider provideLocales */
76 public function testStringifyValue_localeWeirdness( $locale ) {
77 static $cats = [ LC_ALL, LC_MONETARY, LC_NUMERIC ];
78
79 $curLocales = [];
80 foreach ( $cats as $c ) {
81 $curLocales[$c] = setlocale( $c, '0' );
82 if ( $curLocales[$c] === false ) {
83 $this->markTestSkipped( 'Locale support is unavailable' );
84 }
85 }
86 try {
87 foreach ( $cats as $c ) {
88 if ( setlocale( $c, $locale ) === false ) {
89 $this->markTestSkipped( "Locale \"$locale\" is unavailable" );
90 }
91 }
92
93 $typeDef = $this->getInstance( new SimpleCallbacks( [] ), [] );
94 $this->assertSame( '123456.789', $typeDef->stringifyValue( 'test', 123456.789, [], [] ) );
95 $this->assertSame( '-123456.789', $typeDef->stringifyValue( 'test', -123456.789, [], [] ) );
96 $this->assertSame( '1.0e+20', $typeDef->stringifyValue( 'test', 1e20, [], [] ) );
97 $this->assertSame( '1.0e-20', $typeDef->stringifyValue( 'test', 1e-20, [], [] ) );
98 } finally {
99 foreach ( $curLocales as $c => $v ) {
100 setlocale( $c, $v );
101 }
102 }
103 }
104
105 public function provideLocales() {
106 return [
107 // May as well test these.
108 [ 'C' ],
109 [ 'C.UTF-8' ],
110
111 // Some hopefullt-common locales with decimal_point = ',' and thousands_sep = '.'
112 [ 'de_DE' ],
113 [ 'de_DE.utf8' ],
114 [ 'es_ES' ],
115 [ 'es_ES.utf8' ],
116
117 // This one, on my system at least, has decimal_point as U+066B.
118 [ 'ps_AF' ],
119 ];
120 }
121
122 }