Navigation Menu

Skip to content

Commit

Permalink
[Finder][Comparator] not equal operator
Browse files Browse the repository at this point in the history
  • Loading branch information
gajdaw committed Apr 18, 2012
1 parent 0bfeda6 commit 1b320c8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Symfony/Component/Finder/Comparator/Comparator.php
Expand Up @@ -62,7 +62,7 @@ public function setOperator($operator)
$operator = '==';
}

if (!in_array($operator, array('>', '<', '>=', '<=', '=='))) {
if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) {
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
}

Expand All @@ -85,6 +85,8 @@ public function test($test)
return $test < $this->target;
case '<=':
return $test <= $this->target;
case '!=':
return $test != $this->target;
}

return $test == $this->target;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Comparator/DateComparator.php
Expand Up @@ -28,7 +28,7 @@ class DateComparator extends Comparator
*/
public function __construct($test)
{
if (!preg_match('#^\s*([<>=]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test));
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Finder/Comparator/NumberComparator.php
Expand Up @@ -44,11 +44,14 @@ class NumberComparator extends Comparator
*/
public function __construct($test)
{
if (!preg_match('#^\s*([<>=]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
}

$target = $matches[2];
if (!is_numeric($target)) {
throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
}
if (isset($matches[3])) {
// magnitude
switch (strtolower($matches[3])) {
Expand Down
Expand Up @@ -57,6 +57,7 @@ public function getTestData()
array('> 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
array('after 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
array('since 2005-10-10', array(strtotime('2005-10-15')), array(strtotime('2005-10-09'))),
array('!= 2005-10-10', array(strtotime('2005-10-11')), array(strtotime('2005-10-10'))),
);

}
Expand Down
Expand Up @@ -15,13 +15,23 @@

class NumberComparatorTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()

/**
* @dataProvider getConstructorTestData
*/
public function testConstructor($successes, $failures)
{
try {
new NumberComparator('foobar');
$this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
} catch (\Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
foreach ($successes as $s) {
new NumberComparator($s);
}

foreach ($failures as $f) {
try {
new NumberComparator($f);
$this->fail('__construct() throws an \InvalidArgumentException if the test expression is not valid.');
} catch (\Exception $e) {
$this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException if the test expression is not valid.');
}
}
}

Expand Down Expand Up @@ -66,6 +76,34 @@ public function getTestData()

array('==1g', array('1000000000'), array('999999999', '1000000001')),
array('==1gi', array(1024*1024*1024), array(1024*1024*1024-1, 1024*1024*1024+1)),

array('!= 1000', array('500', '999'), array('1000')),
);
}

public function getConstructorTestData()
{
return array(
array(
array(
'1', '0',
'3.5', '33.55', '123.456', '123456.78',
'.1', '.123',
'.0', '0.0',
'1.', '0.', '123.',
'==1', '!=1', '<1', '>1', '<=1', '>=1',
'==1k', '==1ki', '==1m', '==1mi', '==1g', '==1gi',
'1k', '1ki', '1m', '1mi', '1g', '1gi',
),
array(
false, null, '',
' ', 'foobar',
'=1', '===1',
'0 . 1', '123 .45', '234. 567',
'..', '.0.', '0.1.2',
)
),
);
}

}

0 comments on commit 1b320c8

Please sign in to comment.