From 814cefdad769be6db2865b60c405daf6f7699142 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 26 Jan 2012 14:52:41 +0100 Subject: [PATCH] improve test coverage and support negative int for random function --- lib/Twig/Extension/Core.php | 2 +- test/Twig/Tests/Extension/CoreTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index a32946e..312286c 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -299,7 +299,7 @@ function twig_random($values = null) } if (is_int($values) || is_float($values)) { - return mt_rand(0, $values); + return ($values < 0) ? mt_rand($values, 0) : mt_rand(0, $values); } if ($values instanceof Traversable) { diff --git a/test/Twig/Tests/Extension/CoreTest.php b/test/Twig/Tests/Extension/CoreTest.php index 15d6d19..e1936c3 100644 --- a/test/Twig/Tests/Extension/CoreTest.php +++ b/test/Twig/Tests/Extension/CoreTest.php @@ -48,6 +48,10 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase 5.9, range(0, 5, 1), ), + array( // negative + -2, + array(0, -1, -2), + ), ); } @@ -61,6 +65,14 @@ class Twig_Tests_Extension_CoreTest extends PHPUnit_Framework_TestCase } } + public function testRandomFunctionReturnsAsIs() + { + $this->assertSame('', twig_random('')); + + $instance = new stdClass(); + $this->assertSame($instance, twig_random($instance)); + } + /** * @expectedException Twig_Error_Runtime */ -- 1.7.2.5