2013-09-17 21:22:36 +00:00
|
|
|
describe('utilities', function () {
|
2012-11-29 08:18:48 +00:00
|
|
|
var expect = chai.expect;
|
2012-04-22 19:26:45 +00:00
|
|
|
|
2013-11-28 21:45:10 +00:00
|
|
|
after(function() {
|
|
|
|
// Some clean-up so we can run tests in a --watch
|
|
|
|
delete chai.Assertion.prototype.eqqqual;
|
|
|
|
delete chai.Assertion.prototype.result;
|
|
|
|
delete chai.Assertion.prototype.doesnotexist;
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('_obj', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
var foo = 'bar'
|
|
|
|
, test = expect(foo);
|
|
|
|
|
|
|
|
expect(test).to.have.property('_obj', foo);
|
|
|
|
|
|
|
|
var bar = 'baz';
|
|
|
|
test._obj = bar;
|
|
|
|
|
|
|
|
expect(test).to.have.property('_obj', bar);
|
|
|
|
test.equal(bar);
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('transferFlags', function () {
|
2012-05-22 18:51:04 +00:00
|
|
|
var foo = 'bar'
|
|
|
|
, test = expect(foo).not;
|
|
|
|
|
|
|
|
chai.use(function (_chai, utils) {
|
2012-05-27 01:38:10 +00:00
|
|
|
var obj = {};
|
|
|
|
utils.transferFlags(test, obj);
|
|
|
|
expect(utils.flag(obj, 'object')).to.equal(foo);
|
|
|
|
expect(utils.flag(obj, 'negate')).to.equal(true);
|
2012-05-22 18:51:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('transferFlags, includeAll = false', function () {
|
2012-05-27 01:38:10 +00:00
|
|
|
var foo = 'bar';
|
|
|
|
|
|
|
|
chai.use(function (_chai, utils) {
|
|
|
|
var obj = {};
|
2013-09-17 21:22:36 +00:00
|
|
|
var test = function() {};
|
2012-05-27 01:38:10 +00:00
|
|
|
|
|
|
|
var assertion = new chai.Assertion({}, "message", test);
|
|
|
|
var flag = {};
|
|
|
|
utils.flag(obj, 'flagMe', flag);
|
|
|
|
utils.flag(obj, 'negate', true);
|
|
|
|
utils.transferFlags(test, obj, false);
|
|
|
|
|
|
|
|
expect(utils.flag(obj, 'object')).to.equal(undefined);
|
|
|
|
expect(utils.flag(obj, 'message')).to.equal(undefined);
|
|
|
|
expect(utils.flag(obj, 'ssfi')).to.equal(undefined);
|
|
|
|
expect(utils.flag(obj, 'negate')).to.equal(true);
|
|
|
|
expect(utils.flag(obj, 'flagMe')).to.equal(flag);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('getPathValue', function () {
|
2012-04-22 19:26:45 +00:00
|
|
|
var object = {
|
|
|
|
hello: 'universe'
|
|
|
|
, universe: {
|
|
|
|
hello: 'world'
|
|
|
|
}
|
|
|
|
, world: [ 'hello', 'universe' ]
|
|
|
|
, complex: [
|
|
|
|
{ hello: 'universe' }
|
|
|
|
, { universe: 'world' }
|
2012-06-24 12:22:12 +00:00
|
|
|
, [ { hello: 'world' } ]
|
2012-04-22 19:26:45 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2012-06-24 12:22:12 +00:00
|
|
|
var arr = [ [ true ] ];
|
|
|
|
|
2012-04-22 19:26:45 +00:00
|
|
|
chai.use(function (_chai, utils) {
|
|
|
|
var gpv = utils.getPathValue;
|
|
|
|
expect(gpv('hello', object)).to.equal('universe');
|
|
|
|
expect(gpv('universe.hello', object)).to.equal('world');
|
|
|
|
expect(gpv('world[1]', object)).to.equal('universe');
|
|
|
|
expect(gpv('complex[1].universe', object)).to.equal('world');
|
2012-06-24 12:22:12 +00:00
|
|
|
expect(gpv('complex[2][0].hello', object)).to.equal('world');
|
|
|
|
expect(gpv('[0][0]', arr)).to.be.true;
|
2012-04-22 19:26:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-20 23:48:56 +00:00
|
|
|
describe('getPathInfo', function() {
|
2014-11-21 19:27:26 +00:00
|
|
|
var gpi,
|
|
|
|
obj = {
|
|
|
|
id: '10702S300W',
|
|
|
|
primes: [2, 3, 5, 7, 11],
|
|
|
|
dimensions: {
|
|
|
|
units: 'mm',
|
|
|
|
lengths: [[1.2, 3.5], [2.2, 1.5], [5, 7]]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-20 23:48:56 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
chai.use(function (_chai, utils) {
|
2014-11-21 19:27:26 +00:00
|
|
|
gpi = utils.getPathInfo;
|
2014-11-20 23:48:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 19:27:26 +00:00
|
|
|
it('should handle simple property', function() {
|
|
|
|
var info = gpi('dimensions.units', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.dimensions);
|
|
|
|
info.value.should.equal(obj.dimensions.units);
|
|
|
|
info.name.should.equal('units');
|
|
|
|
info.exists.should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle non-existent property', function() {
|
|
|
|
var info = gpi('dimensions.size', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.dimensions);
|
|
|
|
expect(info.value).to.be.undefined;
|
|
|
|
info.name.should.equal('size');
|
|
|
|
info.exists.should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle array index', function() {
|
|
|
|
var info = gpi('primes[2]', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.primes);
|
|
|
|
info.value.should.equal(obj.primes[2]);
|
|
|
|
info.name.should.equal(2);
|
|
|
|
info.exists.should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle dimensional array', function() {
|
|
|
|
var info = gpi('dimensions.lengths[2][1]', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.dimensions.lengths[2]);
|
|
|
|
info.value.should.equal(obj.dimensions.lengths[2][1]);
|
|
|
|
info.name.should.equal(1);
|
|
|
|
info.exists.should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle out of bounds array index', function() {
|
|
|
|
var info = gpi('dimensions.lengths[3]', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.dimensions.lengths);
|
|
|
|
expect(info.value).to.be.undefined;
|
|
|
|
info.name.should.equal(3);
|
|
|
|
info.exists.should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle out of bounds dimensional array index', function() {
|
|
|
|
var info = gpi('dimensions.lengths[2][5]', obj);
|
|
|
|
|
|
|
|
info.parent.should.equal(obj.dimensions.lengths[2]);
|
|
|
|
expect(info.value).to.be.undefined;
|
|
|
|
info.name.should.equal(5);
|
|
|
|
info.exists.should.be.false;
|
2014-11-20 23:48:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasProperty', function() {
|
|
|
|
var hp;
|
|
|
|
beforeEach(function() {
|
|
|
|
chai.use(function (_chai, utils) {
|
|
|
|
hp = utils.hasProperty;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle array index', function() {
|
|
|
|
var arr = [1, 2, 'cheeseburger'];
|
|
|
|
|
|
|
|
hp(1, arr).should.be.true;
|
|
|
|
hp(3, arr).should.be.false;
|
|
|
|
});
|
|
|
|
|
2014-11-21 19:27:26 +00:00
|
|
|
it('should handle literal types', function() {
|
2014-11-20 23:48:56 +00:00
|
|
|
var s = 'string literal';
|
2014-11-21 19:27:26 +00:00
|
|
|
hp('length', s).should.be.true;
|
|
|
|
hp(3, s).should.be.true;
|
|
|
|
hp(14, s).should.be.false;
|
|
|
|
|
|
|
|
hp('foo', 1).should.be.false;
|
2014-11-20 23:48:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle undefined', function() {
|
|
|
|
var o = {
|
|
|
|
foo: 'bar'
|
|
|
|
};
|
|
|
|
|
|
|
|
hp('foo', o).should.be.true;
|
|
|
|
hp('baz', o).should.be.false;
|
|
|
|
hp(0, o).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle undefined', function() {
|
|
|
|
hp('foo', undefined).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle null', function() {
|
|
|
|
hp('foo', null).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('addMethod', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
chai.use(function(_chai, utils) {
|
|
|
|
expect(_chai.Assertion).to.not.respondTo('eqqqual');
|
2012-05-09 22:57:14 +00:00
|
|
|
_chai.Assertion.addMethod('eqqqual', function (str) {
|
2012-05-06 10:54:32 +00:00
|
|
|
var object = utils.flag(this, 'object');
|
|
|
|
new _chai.Assertion(object).to.be.eql(str);
|
|
|
|
});
|
|
|
|
expect(_chai.Assertion).to.respondTo('eqqqual');
|
|
|
|
});
|
|
|
|
|
|
|
|
expect('spec').to.eqqqual('spec');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('addMethod returning result', function () {
|
2012-05-10 02:18:58 +00:00
|
|
|
chai.use(function(_chai, utils) {
|
|
|
|
_chai.Assertion.addMethod('result', function () {
|
|
|
|
return 'result';
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(expect('foo').result()).to.equal('result');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('overwriteMethod', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
expect(_chai.Assertion).to.respondTo('eqqqual');
|
2012-05-09 22:57:14 +00:00
|
|
|
_chai.Assertion.overwriteMethod('eqqqual', function (_super) {
|
2012-04-29 07:21:53 +00:00
|
|
|
return function (str) {
|
2012-05-06 10:54:32 +00:00
|
|
|
var object = _.flag(this, 'object');
|
2012-04-29 07:21:53 +00:00
|
|
|
if (object == 'cucumber' && str == 'cuke') {
|
2012-05-06 10:54:32 +00:00
|
|
|
_.flag(this, 'cucumber', true);
|
2012-04-29 07:21:53 +00:00
|
|
|
} else {
|
2012-05-06 10:54:32 +00:00
|
|
|
_super.apply(this, arguments);
|
2012-04-29 07:21:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2012-05-06 10:54:32 +00:00
|
|
|
|
2012-04-29 07:21:53 +00:00
|
|
|
});
|
|
|
|
|
2012-05-06 10:54:32 +00:00
|
|
|
var vege = expect('cucumber').to.eqqqual('cucumber');
|
|
|
|
expect(vege.__flags).to.not.have.property('cucumber');
|
|
|
|
var cuke = expect('cucumber').to.eqqqual('cuke');
|
|
|
|
expect(cuke.__flags).to.have.property('cucumber');
|
|
|
|
|
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
expect(_chai.Assertion).to.not.respondTo('doesnotexist');
|
2012-05-09 22:57:14 +00:00
|
|
|
_chai.Assertion.overwriteMethod('doesnotexist', function (_super) {
|
2012-05-06 10:54:32 +00:00
|
|
|
expect(_super).to.be.a('function');
|
|
|
|
return function () {
|
|
|
|
_.flag(this, 'doesnt', true);
|
|
|
|
_super.apply(this, arguments);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var dne = expect('something').to.doesnotexist();
|
|
|
|
expect(dne.__flags).to.have.property('doesnt');
|
2012-04-29 07:21:53 +00:00
|
|
|
});
|
2012-04-29 17:51:28 +00:00
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('overwriteMethod returning result', function () {
|
2012-05-10 02:18:58 +00:00
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
_chai.Assertion.overwriteMethod('result', function (_super) {
|
|
|
|
return function () {
|
|
|
|
return 'result';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(expect('foo').result()).to.equal('result');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('addProperty', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
chai.use(function (_chai, _) {
|
2012-05-09 22:57:14 +00:00
|
|
|
_chai.Assertion.addProperty('tea', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
_.flag(this, 'tea', 'chai');
|
2012-04-29 17:51:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-05-06 10:54:32 +00:00
|
|
|
var assert = expect('chai').to.be.tea;
|
|
|
|
expect(assert.__flags.tea).to.equal('chai');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('addProperty returning result', function () {
|
2012-05-10 02:18:58 +00:00
|
|
|
chai.use(function(_chai, _) {
|
|
|
|
_chai.Assertion.addProperty('result', function () {
|
|
|
|
return 'result';
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(expect('foo').result).to.equal('result');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('overwriteProperty', function () {
|
2012-05-06 10:54:32 +00:00
|
|
|
chai.use(function (_chai, _) {
|
2012-10-01 20:13:57 +00:00
|
|
|
expect(new chai.Assertion()).to.have.property('tea');
|
2012-05-09 22:57:14 +00:00
|
|
|
_chai.Assertion.overwriteProperty('tea', function (_super) {
|
2012-05-06 10:54:32 +00:00
|
|
|
return function () {
|
|
|
|
var act = _.flag(this, 'object');
|
|
|
|
if (act === 'matcha') {
|
|
|
|
_.flag(this, 'tea', 'matcha');
|
|
|
|
} else {
|
|
|
|
_super.call(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var matcha = expect('matcha').to.be.tea;
|
|
|
|
expect(matcha.__flags.tea).to.equal('matcha');
|
|
|
|
var assert = expect('something').to.be.tea;
|
|
|
|
expect(assert.__flags.tea).to.equal('chai');
|
2012-04-29 17:51:28 +00:00
|
|
|
});
|
2012-05-07 21:49:38 +00:00
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('overwriteProperty returning result', function () {
|
2012-05-10 02:18:58 +00:00
|
|
|
chai.use(function(_chai, _) {
|
|
|
|
_chai.Assertion.overwriteProperty('result', function (_super) {
|
|
|
|
return function () {
|
|
|
|
return 'result';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(expect('foo').result).to.equal('result');
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('getMessage', function () {
|
2012-05-07 21:49:38 +00:00
|
|
|
chai.use(function (_chai, _) {
|
2012-05-07 22:03:50 +00:00
|
|
|
expect(_.getMessage({}, [])).to.equal('');
|
|
|
|
expect(_.getMessage({}, [null, null, null])).to.equal('');
|
|
|
|
|
|
|
|
var obj = {};
|
|
|
|
_.flag(obj, 'message', 'foo');
|
|
|
|
expect(_.getMessage(obj, [])).to.contain('foo');
|
2014-06-08 21:00:42 +00:00
|
|
|
|
|
|
|
var obj = {};
|
2014-11-06 11:09:55 +00:00
|
|
|
var msg = function() { return "expected a to eql b"; }
|
|
|
|
var negateMsg = function() { return "expected a not to eql b"; }
|
2014-06-08 21:00:42 +00:00
|
|
|
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a to eql b");
|
|
|
|
_.flag(obj, 'negate', true);
|
|
|
|
expect(_.getMessage(obj, [null, msg, negateMsg])).to.equal("expected a not to eql b");
|
2012-05-07 21:49:38 +00:00
|
|
|
});
|
|
|
|
});
|
2012-05-27 02:34:55 +00:00
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('inspect with custom object-returning inspect()s', function () {
|
2013-06-14 03:12:58 +00:00
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
var obj = {
|
|
|
|
outer: {
|
|
|
|
inspect: function () {
|
|
|
|
return { foo: 'bar' };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(_.inspect(obj)).to.equal('{ outer: { foo: \'bar\' } }');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-01 19:19:27 +00:00
|
|
|
it('inspect negative zero', function () {
|
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
expect(_.inspect(-0)).to.equal('-0');
|
2014-11-01 19:24:59 +00:00
|
|
|
expect(_.inspect([-0])).to.equal('[ -0 ]');
|
|
|
|
expect(_.inspect({ hp: -0 })).to.equal('{ hp: -0 }');
|
2014-11-01 19:19:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-17 21:22:36 +00:00
|
|
|
it('addChainableMethod', function () {
|
2012-05-27 02:34:55 +00:00
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
_chai.Assertion.addChainableMethod('x',
|
|
|
|
function () {
|
|
|
|
new chai.Assertion(this._obj).to.be.equal('x');
|
|
|
|
}
|
|
|
|
, function () {
|
2013-02-13 11:41:22 +00:00
|
|
|
this._obj = this._obj || {};
|
2012-05-27 02:34:55 +00:00
|
|
|
this._obj.__x = 'X!'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect("foo").x.to.equal("foo");
|
|
|
|
expect("x").x();
|
|
|
|
|
|
|
|
expect(function () {
|
|
|
|
expect("foo").x();
|
2012-06-24 11:43:50 +00:00
|
|
|
}).to.throw(_chai.AssertionError);
|
2012-05-27 02:34:55 +00:00
|
|
|
|
2013-02-13 11:41:22 +00:00
|
|
|
// Verify whether the original Function properties are present.
|
|
|
|
// see https://github.com/chaijs/chai/commit/514dd6ce4#commitcomment-2593383
|
|
|
|
var propertyDescriptor = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, "x");
|
|
|
|
expect(propertyDescriptor.get).to.have.property("call", Function.prototype.call);
|
|
|
|
expect(propertyDescriptor.get).to.have.property("apply", Function.prototype.apply);
|
|
|
|
expect(propertyDescriptor.get()).to.have.property("call", Function.prototype.call);
|
|
|
|
expect(propertyDescriptor.get()).to.have.property("apply", Function.prototype.apply);
|
|
|
|
|
2012-05-27 02:34:55 +00:00
|
|
|
var obj = {};
|
|
|
|
expect(obj).x.to.be.ok;
|
|
|
|
expect(obj).to.have.property('__x', 'X!');
|
|
|
|
})
|
2013-11-30 17:17:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('overwriteChainableMethod', function () {
|
|
|
|
chai.use(function (_chai, _) {
|
|
|
|
_chai.Assertion.overwriteChainableMethod('x',
|
|
|
|
function(_super) {
|
|
|
|
return function() {
|
|
|
|
if (_.flag(this, 'marked')) {
|
|
|
|
new chai.Assertion(this._obj).to.be.equal('spot');
|
|
|
|
} else {
|
|
|
|
_super.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
, function(_super) {
|
|
|
|
return function() {
|
|
|
|
_.flag(this, 'message', 'x marks the spot');
|
|
|
|
_super.apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure the original behavior of 'x' remains the same
|
|
|
|
expect('foo').x.to.equal("foo");
|
|
|
|
expect("x").x();
|
|
|
|
expect(function () {
|
|
|
|
expect("foo").x();
|
|
|
|
}).to.throw(_chai.AssertionError);
|
|
|
|
var obj = {};
|
|
|
|
expect(obj).x.to.be.ok;
|
|
|
|
expect(obj).to.have.property('__x', 'X!');
|
|
|
|
|
|
|
|
// Test the new behavior of 'x'
|
|
|
|
var assertion = expect('foo').x.to.be.ok;
|
|
|
|
expect(_.flag(assertion, 'message')).to.equal('x marks the spot');
|
|
|
|
expect(function () {
|
|
|
|
var assertion = expect('x');
|
|
|
|
_.flag(assertion, 'marked', true);
|
|
|
|
assertion.x()
|
|
|
|
}).to.throw(_chai.AssertionError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-04-22 19:26:45 +00:00
|
|
|
});
|