3 namespace Wikimedia\ParamValidator\TypeDef
;
5 use Wikimedia\ParamValidator\SimpleCallbacks
;
6 use Wikimedia\ParamValidator\ValidationException
;
9 * @covers Wikimedia\ParamValidator\TypeDef\FloatDef
11 class FloatDefTest
extends TypeDefTestCase
{
13 protected static $testClass = FloatDef
::class;
15 public function provideValidate() {
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 ],
42 [ '000001.0002000000', 1.0002 ],
47 'Weird, but ok' => [ '-0', 0 ],
48 'Underflow is ok' => [ '1e-9999', 0 ],
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', [] ) ],
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', [] ) ],
64 public function provideStringifyValue() {
65 $digits = defined( 'PHP_FLOAT_DIG' ) ? PHP_FLOAT_DIG
: 15;
69 [ 10 / 3, '3.' . str_repeat( '3', $digits - 1 ) ],
70 [ 1e100
, '1.0e+100' ],
71 [ 6.022e-23, '6.022e-23' ],
75 /** @dataProvider provideLocales */
76 public function testStringifyValue_localeWeirdness( $locale ) {
77 static $cats = [ LC_ALL
, LC_MONETARY
, LC_NUMERIC
];
80 foreach ( $cats as $c ) {
81 $curLocales[$c] = setlocale( $c, '0' );
82 if ( $curLocales[$c] === false ) {
83 $this->markTestSkipped( 'Locale support is unavailable' );
87 foreach ( $cats as $c ) {
88 if ( setlocale( $c, $locale ) === false ) {
89 $this->markTestSkipped( "Locale \"$locale\" is unavailable" );
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, [], [] ) );
99 foreach ( $curLocales as $c => $v ) {
105 public function provideLocales() {
107 // May as well test these.
111 // Some hopefullt-common locales with decimal_point = ',' and thousands_sep = '.'
117 // This one, on my system at least, has decimal_point as U+066B.