Skip to content

Commit

Permalink
No longer using eval on assert operator chaijs#386
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Mar 8, 2015
1 parent 4b33fe3 commit 4e16997
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
29 changes: 28 additions & 1 deletion lib/chai/interface/assert.js
Expand Up @@ -1007,7 +1007,34 @@ module.exports = function (chai, util) {
if (!~['==', '===', '>', '>=', '<', '<=', '!=', '!=='].indexOf(operator)) {
throw new Error('Invalid operator "' + operator + '"');
}
var test = new Assertion(eval(val + operator + val2), msg);
var ok;
switch(operator) {
case '==':
ok = val == val2;
break;
case '===':
ok = val === val2;
break;
case '>':
ok = val > val2;
break;
case '>=':
ok = val >= val2;
break;
case '<':
ok = val < val2;
break;
case '<=':
ok = val <= val2;
break;
case '!=':
ok = val != val2;
break;
case '!==':
ok = val !== val2;
break;
}
var test = new Assertion(ok, msg);
test.assert(
true === flag(test, 'object')
, 'expected ' + util.inspect(val) + ' to be ' + operator + ' ' + util.inspect(val2)
Expand Down
13 changes: 9 additions & 4 deletions test/assert.js
Expand Up @@ -563,6 +563,7 @@ describe('assert', function () {
assert.operator(1, '>=', 1);
assert.operator(1, '!=', 2);
assert.operator(1, '!==', 2);
assert.operator(1, '!==', '1');

err(function () {
assert.operator(1, '=', 2);
Expand All @@ -579,6 +580,10 @@ describe('assert', function () {
err(function () {
assert.operator(1, '==', 2);
}, "expected 1 to be == 2");

err(function () {
assert.operator(1, '===', '1');
}, "expected 1 to be === \'1\'");

err(function () {
assert.operator(2, '<=', 1);
Expand All @@ -591,10 +596,10 @@ describe('assert', function () {
err(function () {
assert.operator(1, '!=', 1);
}, "expected 1 to be != 1");

err(function () {
assert.operator(1, '!==', '1');
}, "expected 1 to be !== \'1\'");
err(function () {
assert.operator(1, '!==', 1);
}, "expected 1 to be !== 1");
});

it('closeTo', function(){
Expand Down

0 comments on commit 4e16997

Please sign in to comment.