Skip to content

Commit 6c7d319

Browse files
authored
Merge pull request #168 from e1himself/fix/lime-test-assertion-bug
Fix a bug in the lime unit-testing lib
2 parents ca84921 + a53e3cf commit 6c7d319

15 files changed

+111
-47
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ services:
3030
# cache vendor dirs
3131
cache:
3232
directories:
33-
- lib/vendor
33+
- lib/vendor/swiftmailer
3434
- $HOME/.composer/cache
3535

3636
install:

lib/vendor/lime/lime.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ public function ok($exp, $message = '')
190190
return $result;
191191
}
192192

193+
/**
194+
* Compares two values and returns true if they are equal
195+
*
196+
* @param mixed $exp1 left value
197+
* @param mixed $exp2 right value
198+
* @return bool
199+
*/
200+
private function equals($exp1, $exp2)
201+
{
202+
if (is_object($exp1) || is_object($exp2)) {
203+
return $exp1 === $exp2;
204+
} else if (is_float($exp1) && is_float($exp2)) {
205+
return abs($exp1 - $exp2) < self::EPSILON;
206+
} else if (is_string($exp1) && is_numeric($exp1) || is_string($exp2) && is_numeric($exp2)) {
207+
return $exp1 == $exp2;
208+
} else if (is_string($exp1) || is_string($exp2)) {
209+
return (string) $exp1 === (string) $exp2;
210+
}
211+
return $exp1 == $exp2;
212+
}
213+
193214
/**
194215
* Compares two values and passes if they are equal (==)
195216
*
@@ -201,18 +222,7 @@ public function ok($exp, $message = '')
201222
*/
202223
public function is($exp1, $exp2, $message = '')
203224
{
204-
if (is_object($exp1) || is_object($exp2))
205-
{
206-
$value = $exp1 === $exp2;
207-
}
208-
else if (is_float($exp1) && is_float($exp2))
209-
{
210-
$value = abs($exp1 - $exp2) < self::EPSILON;
211-
}
212-
else
213-
{
214-
$value = $exp1 == $exp2;
215-
}
225+
$value = $this->equals($exp1, $exp2);
216226

217227
if (!$result = $this->ok($value, $message))
218228
{
@@ -233,9 +243,11 @@ public function is($exp1, $exp2, $message = '')
233243
*/
234244
public function isnt($exp1, $exp2, $message = '')
235245
{
236-
if (!$result = $this->ok($exp1 != $exp2, $message))
246+
$value = $this->equals($exp1, $exp2);
247+
248+
if (!$result = $this->ok(!$value, $message))
237249
{
238-
$this->set_last_test_errors(array(sprintf(" %s", var_export($exp2, true)), ' ne', sprintf(" %s", var_export($exp2, true))));
250+
$this->set_last_test_errors(array(sprintf(" %s", var_export($exp1, true)), ' ne', sprintf(" %s", var_export($exp2, true))));
239251
}
240252

241253
return $result;

test/unit/i18n/sfChoiceFormatTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
@@ -107,7 +107,7 @@
107107
$t->is($n->format($string, 0), 'are even numbers', '->format() can takes a set notation in the format string');
108108
$t->is($n->format($string, 2), 'are even numbers', '->format() can takes a set notation in the format string');
109109
$t->is($n->format($string, 4), 'are even numbers', '->format() can takes a set notation in the format string');
110-
$t->is(!$n->format($string, 1), 'are even numbers', '->format() can takes a set notation in the format string');
110+
$t->isnt($n->format($string, 1), 'are even numbers', '->format() can takes a set notation in the format string');
111111
$t->is($n->format($string, 5), 'are not even and greater than or equal to 5', '->format() can takes a set notation in the format string');
112112

113113
$t->diag('set notation for polish');

test/unit/log/sfLoggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
@@ -78,7 +78,7 @@ class notaLogger
7878
$logger->log = '';
7979
$logger->log('foo', constant($levelConstant));
8080

81-
$t->is($logger->log, constant($logLevelConstant) >= constant($levelConstant), sprintf('->log() only logs if the level is >= to the defined log level (%s >= %s)', $logLevelConstant, $levelConstant));
81+
$t->is($logger->log, constant($logLevelConstant) >= constant($levelConstant) ? 'foo' : '', sprintf('->log() only logs if the level is >= to the defined log level (%s >= %s)', $logLevelConstant, $levelConstant));
8282
}
8383
}
8484

test/unit/test/limeTestTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
require_once(__DIR__.'/../../bootstrap/unit.php');
4+
5+
$t = new lime_test(28);
6+
7+
$t->comment('A. Equal numbers');
8+
$t->is(1, 1);
9+
$t->is(2, 2);
10+
$t->is(-100, -100);
11+
$t->is(0, 0);
12+
13+
$t->comment('B. Equal strings');
14+
$t->is('', '');
15+
$t->is('A', 'A');
16+
$t->is('aaa', 'aaa');
17+
$t->is("\0", "\0");
18+
19+
$t->comment('C. Equivalent number <-> numeric string');
20+
$t->is('0', 0);
21+
$t->is('1', 1);
22+
$t->is('-1', -1);
23+
$t->is('10000000.0', 10000000.0);
24+
25+
$t->comment('D. Not equal numbers');
26+
$t->isnt(10, 1);
27+
$t->isnt(-2, 2);
28+
$t->isnt(100, 100.1);
29+
$t->isnt(0, -1);
30+
$t->isnt(-2, 'Hello');
31+
$t->isnt(100, array(100));
32+
33+
$t->comment('E. Both falsy');
34+
$t->is(0, false);
35+
$t->is(0, null);
36+
$t->is('', false);
37+
$t->is(false, null);
38+
$t->is(array(), null);
39+
40+
$t->comment('F. Values that should not be equal');
41+
$t->isnt(true, 'Hello');
42+
$t->isnt('Hello', true);
43+
$t->isnt('Hello', 0);
44+
$t->isnt(0, 'Hello');
45+
$t->isnt('', 0);

test/unit/util/sfBrowserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function getDefaultServerArray($name)
342342
list($method, $uri, $parameters) = $b->click('submit', array('myfile'=>$unexistentFilename));
343343
$files = $b->getFiles();
344344
$t->is($method, 'post', 'file upload is using right method');
345-
$t->is(!isset($parameters['myfile']), 'file upload key is removed from the main request');
345+
$t->ok(!isset($parameters['myfile']), 'file upload key is removed from the main request');
346346
$t->is(isset($files['myfile'])&&is_array($files['myfile']), true, 'file upload set up a _FILE entry for our test file');
347347
$t->is(array_keys($files['myfile']), array('name','type','tmp_name','error','size'), 'file upload returns correctly formatted array');
348348
$t->is($files['myfile']['error'], UPLOAD_ERR_NO_FILE, 'unexistent file does not exists (UPLOAD_ERR_NO_FILE)');
@@ -363,4 +363,4 @@ public function getDefaultServerArray($name)
363363
$t->diag('bug #106');
364364
list($method, $uri, $parameters) = $b->click('submit7');
365365
$t->is(isset($parameters['']), false, 'submit without name is not submitted');
366-
$t->is($parameters['text_default_value'], 'default', 'input field with name is still submitted');
366+
$t->is($parameters['text_default_value'], 'default', 'input field with name is still submitted');

test/unit/util/sfParameterHolderTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/*
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6-
*
6+
*
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
1010

1111
require_once(__DIR__.'/../../bootstrap/unit.php');
1212

13-
$t = new lime_test(24);
13+
$t = new lime_test(28);
1414

1515
// ->clear()
1616
$t->diag('->clear()');
@@ -30,12 +30,20 @@
3030
$t->is($ph->get('bar'), null, '->get() returns null if the key does not exist');
3131

3232
// checks that get returns reference
33-
$ref = 'foobar';
34-
$ph->set('ref', $ref);
33+
$ph->set('ref', 'foobar');
34+
35+
$ref1 = null;
36+
$ref1 = & $ph->get('ref');
37+
$t->is($ref1, 'foobar');
38+
3539
$ref2 = null;
36-
$ref2 &= $ph->get('ref'); // obtain the very same reference and modify it
37-
$ref2 &= 'barfoo';
38-
$t->is($ref2 , $ref, '->get() returns a reference for the given key');
40+
$ref2 = & $ph->get('ref'); // obtain the very same reference and modify it
41+
$ref2 = 'barfoo';
42+
43+
$t->is($ref1, 'barfoo');
44+
$t->is($ref2, 'barfoo');
45+
$t->is($ph->get('ref'), 'barfoo');
46+
$t->is($ref2 , $ref1, '->get() returns a reference for the given key');
3947

4048
$ph = new sfParameterHolder();
4149
$t->is('default_value', $ph->get('foo1', 'default_value'), '->get() takes the default value as its second argument');

test/unit/util/sfToolkitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
// ::isUTF8()
3939
$t->diag('::isUTF8()');
40-
$t->is('été', true, '::isUTF8() returns true if the parameter is an UTF-8 encoded string');
40+
$t->is(sfToolkit::isUTF8('été'), true, '::isUTF8() returns true if the parameter is an UTF-8 encoded string');
4141
$t->is(sfToolkit::isUTF8('AZERTYazerty1234-_'), true, '::isUTF8() returns true if the parameter is an UTF-8 encoded string');
4242
$t->is(sfToolkit::isUTF8('AZERTYazerty1234-_'.chr(254)), false, '::isUTF8() returns false if the parameter is not an UTF-8 encoded string');
4343
// check a very long string

test/unit/validator/sfValidatorAndTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
{
7171
$t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
7272
$t->is($e[0]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
73-
$t->is($e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorSchemaError');
73+
$t->ok($e instanceof sfValidatorErrorSchema, '->clean() throws a sfValidatorSchemaError');
7474
}
7575

7676
$v1->setOption('max_length', 2);
@@ -86,7 +86,7 @@
8686
$t->is(count($e), 2, '->clean() throws an error for every error');
8787
$t->is($e[0]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
8888
$t->is($e[1]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
89-
$t->is($e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorSchemaError');
89+
$t->ok($e instanceof sfValidatorErrorSchema, '->clean() throws a sfValidatorSchemaError');
9090
}
9191

9292
$v->setOption('halt_on_error', true);
@@ -101,7 +101,7 @@
101101
$t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
102102
$t->is(count($e), 1, '->clean() only returns the first error if halt_on_error option is true');
103103
$t->is($e[0]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
104-
$t->is($e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorSchemaError');
104+
$t->ok($e instanceof sfValidatorErrorSchema, '->clean() throws a sfValidatorSchemaError');
105105
}
106106

107107
try
@@ -115,7 +115,7 @@
115115
{
116116
$t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
117117
$t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError if invalid message is not empty');
118-
$t->is(!$e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorError if invalid message is not empty');
118+
$t->ok(!$e instanceof sfValidatorErrorSchema, '->clean() throws a sfValidatorError if invalid message is not empty');
119119
}
120120

121121
// ->asString()
@@ -127,5 +127,5 @@
127127
, '->asString() returns a string representation of the validator');
128128

129129
$v = new sfValidatorAnd(array($v1, $v2), array(), array('required' => 'This is required.'));
130-
$t->is($v->asString(), "(\n String({ max_length: 3 })\n and({}, { required: 'This is required.' })\n String({ min_length: 3 })\n)"
130+
$t->is($v->asString(), "(\n String({ max_length: 3 })\n and({}, { required: 'This is required.' })\n String({ min_length: 3 })\n)"
131131
, '->asString() returns a string representation of the validator');

test/unit/validator/sfValidatorDateRangeTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
require_once(__DIR__.'/../../bootstrap/unit.php');
1212

13-
$t = new lime_test(7);
13+
$t = new lime_test(6);
1414

1515
try
1616
{
@@ -20,8 +20,7 @@
2020
}
2121
catch (RuntimeException $e)
2222
{
23-
$t->pass('__construct() throws a sfValidatorError if you don\'t pass a from_date and a to_date option');
24-
$t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
23+
$t->pass('__construct() throws a RuntimeException if you don\'t pass a from_date and a to_date option');
2524
}
2625

2726
$v = new sfValidatorDateRange(array(

0 commit comments

Comments
 (0)