Fix toBool() and add more test cases

This commit is contained in:
Kijin Sung 2017-04-08 22:02:03 +09:00
parent f73580945d
commit 8147044802
2 changed files with 10 additions and 2 deletions

View file

@ -508,8 +508,8 @@ function tobool($input)
{
if (is_scalar($input))
{
if (preg_match('/^(1|[ty].*|on|ok.*oui|si|vrai|aye)$/i', $input)) return true;
if (preg_match('/^(0|[fn].*|off)$/i', $input)) return false;
if (preg_match('/^(?:1|[ty].*|on|ok.*|oui|si|vrai|aye)$/i', $input)) return true;
if (preg_match('/^(?:0|[fn].*|off|out)$/i', $input)) return false;
}
return (bool)$input;
}

View file

@ -123,18 +123,26 @@ class FunctionsTest extends \Codeception\TestCase\Test
$this->assertTrue(tobool('on'));
$this->assertTrue(tobool('ok'));
$this->assertTrue(tobool('okay'));
$this->assertTrue(tobool('oui'));
$this->assertTrue(tobool('si'));
$this->assertTrue(tobool('vrai'));
$this->assertTrue(tobool('true'));
$this->assertTrue(tobool(1));
$this->assertTrue(tobool(-1));
$this->assertTrue(tobool(true));
$this->assertTrue(tobool(array(1, 2, 3)));
$this->assertTrue(tobool(new stdClass));
$this->assertFalse(tobool('N'));
$this->assertFalse(tobool('no'));
$this->assertFalse(tobool('none'));
$this->assertFalse(tobool('Nobody'));
$this->assertFalse(tobool('false'));
$this->assertFalse(tobool('off'));
$this->assertFalse(tobool('OUT'));
$this->assertFalse(tobool('Fuck you!'));
$this->assertFalse(tobool(0));
$this->assertFalse(tobool(0.00000000));
$this->assertFalse(tobool(''));
$this->assertFalse(tobool(false));
$this->assertFalse(tobool(null));