2012-01-02 05:45:06 +00:00
|
|
|
suite('should', function() {
|
2012-08-25 02:33:47 +00:00
|
|
|
var should = chai.Should();
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
test('assertion', function(){
|
|
|
|
'test'.should.be.a('string');
|
|
|
|
should.equal('foo', 'foo');
|
2012-03-02 00:28:30 +00:00
|
|
|
should.not.equal('foo', 'bar');
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
2012-01-02 05:48:43 +00:00
|
|
|
test('root exist', function () {
|
|
|
|
var foo = 'foo'
|
|
|
|
, bar = undefined;
|
|
|
|
should.exist(foo);
|
|
|
|
should.not.exist(bar);
|
|
|
|
|
|
|
|
err(function () {
|
2012-09-05 06:09:40 +00:00
|
|
|
should.exist(bar, 'blah');
|
|
|
|
}, "blah: expected undefined to exist");
|
2012-01-02 05:48:43 +00:00
|
|
|
|
|
|
|
err(function () {
|
2012-09-05 06:09:40 +00:00
|
|
|
should.not.exist(foo, 'blah');
|
|
|
|
}, "blah: expected 'foo' to not exist")
|
|
|
|
});
|
|
|
|
|
|
|
|
test('root equal', function () {
|
|
|
|
var value1 = 'value'
|
|
|
|
, value2 = 'value'
|
|
|
|
, foo = 'foo';
|
|
|
|
should.equal(value1, value2);
|
|
|
|
should.not.equal(value1, foo);
|
|
|
|
|
|
|
|
err(function () {
|
|
|
|
should.equal(value1, foo, 'blah');
|
|
|
|
}, "blah: expected 'value' to equal 'foo'");
|
|
|
|
|
|
|
|
err(function () {
|
|
|
|
should.not.equal(value1, value2, 'blah');
|
|
|
|
}, "blah: expected 'value' to not equal 'value'")
|
|
|
|
});
|
|
|
|
|
|
|
|
test('root Throw', function () {
|
|
|
|
should.Throw(function() { throw new Error('error!') }, Error, 'error!');
|
|
|
|
should.not.Throw(function () { });
|
|
|
|
|
|
|
|
err(function () {
|
|
|
|
should.Throw(function () { throw new Error('error!') }, Error, 'needed user!', 'blah');
|
|
|
|
}, "blah: expected [Function] to throw error including 'needed user!' but got 'error!'");
|
|
|
|
|
|
|
|
err(function () {
|
|
|
|
should.not.Throw(function () { throw new Error('error!') }, Error, 'error!', 'blah');
|
|
|
|
}, "blah: expected [Function] to not throw Error but [Error: error!] was thrown");
|
2012-01-02 05:48:43 +00:00
|
|
|
});
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
test('true', function(){
|
2012-03-02 00:28:30 +00:00
|
|
|
(true).should.be.true;
|
2012-01-02 05:45:06 +00:00
|
|
|
false.should.not.be.true;
|
2012-03-02 00:29:55 +00:00
|
|
|
(1).should.not.be.true;false
|
|
|
|
false.should.have.been.false;
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
'test'.should.be.true;
|
|
|
|
}, "expected 'test' to be true")
|
|
|
|
});
|
|
|
|
|
|
|
|
test('ok', function(){
|
|
|
|
true.should.be.ok;
|
|
|
|
false.should.not.be.ok;
|
|
|
|
(1).should.be.ok;
|
|
|
|
(0).should.not.be.ok;
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
''.should.be.ok;
|
|
|
|
}, "expected '' to be truthy");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
'test'.should.not.be.ok;
|
2012-02-27 20:33:24 +00:00
|
|
|
}, "expected 'test' to be falsy");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('false', function(){
|
|
|
|
false.should.be.false;
|
|
|
|
true.should.not.be.false;
|
|
|
|
(0).should.not.be.false;
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
''.should.be.false;
|
|
|
|
}, "expected '' to be false")
|
|
|
|
});
|
|
|
|
|
2012-04-29 06:44:44 +00:00
|
|
|
test('null', function(){
|
|
|
|
(0).should.not.be.null;
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
''.should.be.null;
|
|
|
|
}, "expected '' to be null")
|
|
|
|
});
|
2012-04-29 06:44:57 +00:00
|
|
|
|
|
|
|
test('undefined', function(){
|
|
|
|
(0).should.not.be.undefined;
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
''.should.be.undefined;
|
|
|
|
}, "expected '' to be undefined")
|
|
|
|
});
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
test('arguments', function(){
|
|
|
|
var args = (function(){ return arguments; })(1,2,3);
|
|
|
|
args.should.be.arguments;
|
|
|
|
[].should.not.be.arguments;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.equal()', function(){
|
|
|
|
var foo;
|
|
|
|
should.equal(undefined, foo);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('typeof', function(){
|
|
|
|
'test'.should.be.a('string');
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
'test'.should.not.be.a('string');
|
|
|
|
}, "expected 'test' not to be a string");
|
|
|
|
|
2012-02-25 18:42:49 +00:00
|
|
|
(5).should.be.a('number');
|
|
|
|
(new Number(1)).should.be.a('number');
|
|
|
|
Number(1).should.be.a('number');
|
|
|
|
(true).should.be.a('boolean');
|
|
|
|
(new Array()).should.be.a('array');
|
|
|
|
(new Object()).should.be.a('object');
|
|
|
|
({}).should.be.a('object');
|
|
|
|
([]).should.be.a('array');
|
|
|
|
(function() {}).should.be.a('function');
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
(5).should.be.a('number');
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(5).should.not.be.a('number');
|
|
|
|
}, "expected 5 not to be a number");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('instanceof', function(){
|
|
|
|
function Foo(){}
|
|
|
|
new Foo().should.be.an.instanceof(Foo);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(3).should.an.instanceof(Foo, 'blah');
|
|
|
|
}, "blah: expected 3 to be an instance of Foo");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('within(start, finish)', function(){
|
|
|
|
(5).should.be.within(5, 10);
|
|
|
|
(5).should.be.within(3,6);
|
|
|
|
(5).should.be.within(3,5);
|
|
|
|
(5).should.not.be.within(1,3);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(5).should.not.be.within(4,6, 'blah');
|
|
|
|
}, "blah: expected 5 to not be within 4..6");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(10).should.be.within(50,100, 'blah');
|
|
|
|
}, "blah: expected 10 to be within 50..100");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
({ foo: 1 }).should.have.length.within(50,100, 'blah');
|
|
|
|
}, "blah: expected { foo: 1 } to have a property 'length'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('above(n)', function(){
|
|
|
|
(5).should.be.above(2);
|
|
|
|
(5).should.be.greaterThan(2);
|
|
|
|
(5).should.not.be.above(5);
|
|
|
|
(5).should.not.be.above(6);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(5).should.be.above(6, 'blah');
|
|
|
|
}, "blah: expected 5 to be above 6");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(10).should.not.be.above(6, 'blah');
|
2012-11-27 05:02:47 +00:00
|
|
|
}, "blah: expected 10 to be at most 6");
|
2012-09-05 06:09:40 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({foo: 1}).should.have.length.above(3, 'blah');
|
|
|
|
}, "blah: expected { foo: 1 } to have a property 'length'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
2012-11-27 05:02:47 +00:00
|
|
|
test('least(n)', function(){
|
|
|
|
(5).should.be.at.least(5);
|
|
|
|
(5).should.not.be.at.least(6);
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(5).should.be.at.least(6, 'blah');
|
|
|
|
}, "blah: expected 5 to be at least 6");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(10).should.not.be.at.least(6, 'blah');
|
|
|
|
}, "blah: expected 10 to be below 6");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({foo: 1}).should.have.length.of.at.least(3, 'blah');
|
|
|
|
}, "blah: expected { foo: 1 } to have a property 'length'");
|
|
|
|
});
|
|
|
|
|
2012-03-02 00:28:30 +00:00
|
|
|
test('below(n)', function(){
|
|
|
|
(2).should.be.below(5);
|
|
|
|
(2).should.be.lessThan(5);
|
|
|
|
(2).should.not.be.below(2);
|
|
|
|
(2).should.not.be.below(1);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(6).should.be.below(5, 'blah');
|
|
|
|
}, "blah: expected 6 to be below 5");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(6).should.not.be.below(10, 'blah');
|
2012-11-27 05:02:47 +00:00
|
|
|
}, "blah: expected 6 to be at least 10");
|
2012-03-02 00:28:30 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
({foo: 1}).should.have.length.below(3, 'blah');
|
|
|
|
}, "blah: expected { foo: 1 } to have a property 'length'");
|
2012-03-02 00:28:30 +00:00
|
|
|
});
|
|
|
|
|
2012-11-27 05:02:47 +00:00
|
|
|
test('most(n)', function(){
|
|
|
|
(2).should.be.at.most(2);
|
|
|
|
(2).should.not.be.at.most(1);
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(6).should.be.at.most(5, 'blah');
|
|
|
|
}, "blah: expected 6 to be at most 5");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(6).should.not.be.at.most(10, 'blah');
|
|
|
|
}, "blah: expected 6 to be above 10");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({foo: 1}).should.have.length.of.at.most(3, 'blah');
|
|
|
|
}, "blah: expected { foo: 1 } to have a property 'length'");
|
|
|
|
});
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
test('match(regexp)', function(){
|
|
|
|
'foobar'.should.match(/^foo/)
|
|
|
|
'foobar'.should.not.match(/^bar/)
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'foobar'.should.match(/^bar/i, 'blah')
|
|
|
|
}, "blah: expected 'foobar' to match /^bar/i");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'foobar'.should.not.match(/^foo/i, 'blah')
|
|
|
|
}, "blah: expected 'foobar' not to match /^foo/i");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('length(n)', function(){
|
|
|
|
'test'.should.have.length(4);
|
|
|
|
'test'.should.not.have.length(3);
|
|
|
|
[1,2,3].should.have.length(3);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(4).should.have.length(3, 'blah');
|
|
|
|
}, 'blah: expected 4 to have a property \'length\'');
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'asd'.should.not.have.length(3, 'blah');
|
|
|
|
}, "blah: expected 'asd' to not have a length of 3");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('eql(val)', function(){
|
|
|
|
'test'.should.eql('test');
|
|
|
|
({ foo: 'bar' }).should.eql({ foo: 'bar' });
|
|
|
|
(1).should.eql(1);
|
|
|
|
'4'.should.not.eql(4);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(4).should.eql(3, 'blah');
|
|
|
|
}, 'blah: expected 4 to deeply equal 3');
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('equal(val)', function(){
|
|
|
|
'test'.should.equal('test');
|
|
|
|
(1).should.equal(1);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(4).should.equal(3, 'blah');
|
|
|
|
}, 'blah: expected 4 to equal 3');
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'4'.should.equal(4, 'blah');
|
|
|
|
}, "blah: expected '4' to equal 4");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('empty', function(){
|
2012-02-29 19:51:42 +00:00
|
|
|
function FakeArgs() {};
|
|
|
|
FakeArgs.prototype.length = 0;
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
''.should.be.empty;
|
2012-03-07 16:09:52 +00:00
|
|
|
'foo'.should.not.be.empty;
|
|
|
|
([]).should.be.empty;
|
|
|
|
(['foo']).should.not.be.empty;
|
2012-02-29 19:51:42 +00:00
|
|
|
(new FakeArgs).should.be.empty;
|
2012-03-07 16:09:52 +00:00
|
|
|
({arguments: 0}).should.not.be.empty;
|
2012-02-29 19:51:42 +00:00
|
|
|
({}).should.be.empty;
|
2012-03-07 16:09:52 +00:00
|
|
|
({foo: 'bar'}).should.not.be.empty;
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-03-07 16:09:52 +00:00
|
|
|
''.should.not.be.empty;
|
|
|
|
}, "expected \'\' not to be empty");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-03-07 16:09:52 +00:00
|
|
|
'foo'.should.be.empty;
|
|
|
|
}, "expected \'foo\' to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
([]).should.not.be.empty;
|
|
|
|
}, "expected [] not to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(['foo']).should.be.empty;
|
|
|
|
}, "expected [ \'foo\' ] to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(new FakeArgs).should.not.be.empty;
|
|
|
|
}, "expected {} not to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({arguments: 0}).should.be.empty;
|
|
|
|
}, "expected { arguments: 0 } to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({}).should.not.be.empty;
|
|
|
|
}, "expected {} not to be empty");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({foo: 'bar'}).should.be.empty;
|
|
|
|
}, "expected { foo: \'bar\' } to be empty");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('property(name)', function(){
|
|
|
|
'test'.should.have.property('length');
|
|
|
|
(4).should.not.have.property('length');
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
'asd'.should.have.property('foo');
|
|
|
|
}, "expected 'asd' to have a property 'foo'");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('property(name, val)', function(){
|
|
|
|
'test'.should.have.property('length', 4);
|
|
|
|
'asd'.should.have.property('constructor', String);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'asd'.should.have.property('length', 4, 'blah');
|
|
|
|
}, "blah: expected 'asd' to have a property 'length' of 4, but got 3");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'asd'.should.not.have.property('length', 3, 'blah');
|
|
|
|
}, "blah: expected 'asd' to not have a property 'length' of 3");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'asd'.should.not.have.property('foo', 3, 'blah');
|
|
|
|
}, "blah: 'asd' has no property 'foo'");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'asd'.should.have.property('constructor', Number, 'blah');
|
|
|
|
}, "blah: expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('ownProperty(name)', function(){
|
|
|
|
'test'.should.have.ownProperty('length');
|
|
|
|
'test'.should.haveOwnProperty('length');
|
|
|
|
({ length: 12 }).should.have.ownProperty('length');
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
({ length: 12 }).should.not.have.ownProperty('length', 'blah');
|
|
|
|
}, "blah: expected { length: 12 } to not have own property 'length'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('string()', function(){
|
|
|
|
'foobar'.should.contain.string('bar');
|
|
|
|
'foobar'.should.contain.string('foo');
|
|
|
|
'foobar'.should.not.contain.string('baz');
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(3).should.contain.string('baz', 'blah');
|
|
|
|
}, "blah: expected 3 to be a string");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'foobar'.should.contain.string('baz', 'blah');
|
|
|
|
}, "blah: expected 'foobar' to contain 'baz'");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
'foobar'.should.not.contain.string('bar', 'blah');
|
|
|
|
}, "blah: expected 'foobar' to not contain 'bar'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('include()', function(){
|
|
|
|
['foo', 'bar'].should.include('foo');
|
2012-04-20 17:44:23 +00:00
|
|
|
['foo', 'bar'].should.contain('foo');
|
2012-01-02 05:45:06 +00:00
|
|
|
['foo', 'bar'].should.include('bar');
|
|
|
|
[1,2].should.include(1);
|
|
|
|
['foo', 'bar'].should.not.include('baz');
|
|
|
|
['foo', 'bar'].should.not.include(1);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
['foo'].should.include('bar', 'blah');
|
|
|
|
}, "blah: expected [ 'foo' ] to include 'bar'");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
['bar', 'foo'].should.not.include('foo', 'blah');
|
|
|
|
}, "blah: expected [ 'bar', 'foo' ] to not include 'foo'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('keys(array)', function(){
|
|
|
|
({ foo: 1 }).should.have.keys(['foo']);
|
|
|
|
({ foo: 1, bar: 2 }).should.have.keys(['foo', 'bar']);
|
|
|
|
({ foo: 1, bar: 2 }).should.have.keys('foo', 'bar');
|
2012-04-20 17:44:23 +00:00
|
|
|
({ foo: 1, bar: 2, baz: 3 }).should.include.keys('foo', 'bar');
|
2012-01-02 05:45:06 +00:00
|
|
|
({ foo: 1, bar: 2, baz: 3 }).should.contain.keys('bar', 'foo');
|
|
|
|
({ foo: 1, bar: 2, baz: 3 }).should.contain.keys('baz');
|
|
|
|
|
|
|
|
({ foo: 1, bar: 2 }).should.contain.keys('foo');
|
|
|
|
({ foo: 1, bar: 2 }).should.contain.keys('bar', 'foo');
|
|
|
|
({ foo: 1, bar: 2 }).should.contain.keys(['foo']);
|
|
|
|
({ foo: 1, bar: 2 }).should.contain.keys(['bar']);
|
|
|
|
({ foo: 1, bar: 2 }).should.contain.keys(['bar', 'foo']);
|
|
|
|
|
|
|
|
({ foo: 1, bar: 2 }).should.not.have.keys('baz');
|
|
|
|
({ foo: 1, bar: 2 }).should.not.have.keys('foo', 'baz');
|
|
|
|
({ foo: 1, bar: 2 }).should.not.contain.keys('baz');
|
|
|
|
({ foo: 1, bar: 2 }).should.not.contain.keys('foo', 'baz');
|
|
|
|
({ foo: 1, bar: 2 }).should.not.contain.keys('baz', 'foo');
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.have.keys();
|
|
|
|
}, "keys required");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.have.keys([]);
|
|
|
|
}, "keys required");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.not.have.keys([]);
|
|
|
|
}, "keys required");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.contain.keys([]);
|
|
|
|
}, "keys required");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.have.keys(['bar']);
|
|
|
|
}, "expected { foo: 1 } to have key 'bar'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.have.keys(['bar', 'baz']);
|
|
|
|
}, "expected { foo: 1 } to have keys 'bar', and 'baz'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.have.keys(['foo', 'bar', 'baz']);
|
|
|
|
}, "expected { foo: 1 } to have keys 'foo', 'bar', and 'baz'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.not.have.keys(['foo']);
|
|
|
|
}, "expected { foo: 1 } to not have key 'foo'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.not.have.keys(['foo']);
|
|
|
|
}, "expected { foo: 1 } to not have key 'foo'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1, bar: 2 }).should.not.have.keys(['foo', 'bar']);
|
|
|
|
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.not.contain.keys(['foo']);
|
|
|
|
}, "expected { foo: 1 } to not contain key 'foo'");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
({ foo: 1 }).should.contain.keys('foo', 'bar');
|
|
|
|
}, "expected { foo: 1 } to contain keys 'foo', and 'bar'");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throw', function () {
|
2012-04-06 21:57:54 +00:00
|
|
|
// See GH-45: some poorly-constructed custom errors don't have useful names
|
|
|
|
// on either their constructor or their constructor prototype, but instead
|
|
|
|
// only set the name inside the constructor itself.
|
|
|
|
var PoorlyConstructedError = function () {
|
|
|
|
this.name = 'PoorlyConstructedError';
|
|
|
|
};
|
|
|
|
PoorlyConstructedError.prototype = Object.create(Error.prototype);
|
|
|
|
|
2012-04-06 22:19:28 +00:00
|
|
|
var specificError = new RangeError('boo');
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
var goodFn = function () { 1==1; }
|
|
|
|
, badFn = function () { throw new Error('testing'); }
|
2012-04-06 21:57:54 +00:00
|
|
|
, refErrFn = function () { throw new ReferenceError('hello'); }
|
2012-04-06 22:19:28 +00:00
|
|
|
, ickyErrFn = function () { throw new PoorlyConstructedError(); }
|
|
|
|
, specificErrFn = function () { throw specificError; };
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
(goodFn).should.not.throw();
|
|
|
|
(goodFn).should.not.throw(Error);
|
2012-04-06 22:19:28 +00:00
|
|
|
(goodFn).should.not.throw(specificError);
|
2012-01-02 05:45:06 +00:00
|
|
|
(badFn).should.throw();
|
|
|
|
(badFn).should.throw(Error);
|
|
|
|
(badFn).should.not.throw(ReferenceError);
|
2012-04-06 22:19:28 +00:00
|
|
|
(badFn).should.not.throw(specificError);
|
2012-01-02 05:45:06 +00:00
|
|
|
(refErrFn).should.throw();
|
|
|
|
(refErrFn).should.throw(ReferenceError);
|
2012-04-06 21:39:08 +00:00
|
|
|
(refErrFn).should.throw(Error);
|
2012-01-02 05:45:06 +00:00
|
|
|
(refErrFn).should.not.throw(TypeError);
|
2012-04-06 22:19:28 +00:00
|
|
|
(refErrFn).should.not.throw(specificError);
|
2012-04-06 21:57:54 +00:00
|
|
|
(ickyErrFn).should.throw();
|
|
|
|
(ickyErrFn).should.throw(PoorlyConstructedError);
|
|
|
|
(ickyErrFn).should.throw(Error);
|
2012-04-06 22:19:28 +00:00
|
|
|
(ickyErrFn).should.not.throw(specificError);
|
|
|
|
(specificErrFn).should.throw(specificError);
|
2012-01-02 05:45:06 +00:00
|
|
|
|
2012-02-07 21:39:08 +00:00
|
|
|
(badFn).should.throw(/testing/);
|
2012-03-07 16:38:58 +00:00
|
|
|
(badFn).should.throw('testing');
|
2012-02-07 21:39:08 +00:00
|
|
|
(badFn).should.not.throw(/hello/);
|
2012-03-07 16:38:58 +00:00
|
|
|
(badFn).should.throw(Error, /testing/);
|
|
|
|
(badFn).should.throw(Error, 'testing');
|
2012-02-07 21:39:08 +00:00
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
should.throw(badFn);
|
|
|
|
should.throw(refErrFn, ReferenceError);
|
2012-04-06 21:39:08 +00:00
|
|
|
should.throw(refErrFn, Error);
|
2012-04-06 21:57:54 +00:00
|
|
|
should.throw(ickyErrFn, PoorlyConstructedError);
|
2012-04-06 22:19:28 +00:00
|
|
|
should.throw(specificErrFn, specificError);
|
2012-01-02 05:45:06 +00:00
|
|
|
should.not.throw(goodFn);
|
|
|
|
should.not.throw(badFn, ReferenceError);
|
2012-04-06 22:19:28 +00:00
|
|
|
should.not.throw(badFn, specificError);
|
2012-01-02 05:45:06 +00:00
|
|
|
|
2012-03-07 16:38:58 +00:00
|
|
|
should.throw(badFn, Error, /testing/);
|
|
|
|
should.throw(badFn, Error, 'testing');
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
err(function(){
|
|
|
|
(goodFn).should.throw();
|
|
|
|
}, "expected [Function] to throw an error");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(goodFn).should.throw(ReferenceError);
|
|
|
|
}, "expected [Function] to throw ReferenceError");
|
|
|
|
|
2012-04-06 22:19:28 +00:00
|
|
|
err(function(){
|
|
|
|
(goodFn).should.throw(specificError);
|
|
|
|
}, "expected [Function] to throw [RangeError: boo]");
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
err(function(){
|
|
|
|
(badFn).should.not.throw();
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to not throw an error but [Error: testing] was thrown");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(badFn).should.throw(ReferenceError);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to throw ReferenceError but [Error: testing] was thrown");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
2012-04-06 22:19:28 +00:00
|
|
|
err(function(){
|
|
|
|
(badFn).should.throw(specificError);
|
|
|
|
}, "expected [Function] to throw [RangeError: boo] but [Error: testing] was thrown");
|
|
|
|
|
2012-01-02 05:45:06 +00:00
|
|
|
err(function(){
|
|
|
|
(badFn).should.not.throw(Error);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to not throw Error but [Error: testing] was thrown");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(refErrFn).should.not.throw(ReferenceError);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to not throw ReferenceError but [ReferenceError: hello] was thrown");
|
2012-01-02 05:45:06 +00:00
|
|
|
|
2012-04-06 21:57:54 +00:00
|
|
|
err(function(){
|
|
|
|
(badFn).should.throw(PoorlyConstructedError);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to throw PoorlyConstructedError but [Error: testing] was thrown");
|
2012-04-06 21:57:54 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(ickyErrFn).should.not.throw(PoorlyConstructedError);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to not throw PoorlyConstructedError but { name: 'PoorlyConstructedError' } was thrown");
|
2012-04-06 21:57:54 +00:00
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(ickyErrFn).should.throw(ReferenceError);
|
2012-09-05 06:13:44 +00:00
|
|
|
}, "expected [Function] to throw ReferenceError but { name: 'PoorlyConstructedError' } was thrown");
|
2012-04-06 21:57:54 +00:00
|
|
|
|
2012-04-06 22:19:28 +00:00
|
|
|
err(function(){
|
|
|
|
(specificErrFn).should.throw(new ReferenceError('eek'));
|
|
|
|
}, "expected [Function] to throw [ReferenceError: eek] but [RangeError: boo] was thrown");
|
|
|
|
|
|
|
|
err(function(){
|
|
|
|
(specificErrFn).should.not.throw(specificError);
|
|
|
|
}, "expected [Function] to not throw [RangeError: boo]");
|
|
|
|
|
2012-02-07 21:39:08 +00:00
|
|
|
err(function (){
|
|
|
|
(badFn).should.not.throw(/testing/);
|
|
|
|
}, "expected [Function] to throw error not matching /testing/");
|
|
|
|
|
|
|
|
err(function () {
|
|
|
|
(badFn).should.throw(/hello/);
|
|
|
|
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
|
2012-03-07 16:38:58 +00:00
|
|
|
|
|
|
|
err(function () {
|
2012-09-05 06:09:40 +00:00
|
|
|
(badFn).should.throw(Error, /hello/, 'blah');
|
|
|
|
}, "blah: expected [Function] to throw error matching /hello/ but got 'testing'");
|
2012-03-07 16:38:58 +00:00
|
|
|
|
|
|
|
err(function () {
|
2012-09-05 06:09:40 +00:00
|
|
|
(badFn).should.throw(Error, 'hello', 'blah');
|
|
|
|
}, "blah: expected [Function] to throw error including 'hello' but got 'testing'");
|
2012-01-02 05:45:06 +00:00
|
|
|
});
|
2012-02-24 19:53:33 +00:00
|
|
|
|
|
|
|
test('respondTo', function(){
|
|
|
|
function Foo(){};
|
|
|
|
Foo.prototype.bar = function(){};
|
2012-06-02 16:41:54 +00:00
|
|
|
Foo.func = function(){};
|
2012-02-24 19:53:33 +00:00
|
|
|
|
|
|
|
var bar = {};
|
|
|
|
bar.foo = function(){};
|
|
|
|
|
|
|
|
Foo.should.respondTo('bar');
|
|
|
|
Foo.should.not.respondTo('foo');
|
2012-06-02 16:41:54 +00:00
|
|
|
Foo.should.itself.respondTo('func');
|
|
|
|
Foo.should.itself.not.respondTo('bar');
|
2012-02-24 19:53:33 +00:00
|
|
|
|
|
|
|
bar.should.respondTo('foo');
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
Foo.should.respondTo('baz', 'blah');
|
|
|
|
}, "blah: expected { [Function: Foo] func: [Function] } to respond to \'baz\'");
|
2012-02-24 19:53:33 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
bar.should.respondTo('baz', 'blah');
|
|
|
|
}, "blah: expected { foo: [Function] } to respond to \'baz\'");
|
2012-02-24 19:53:33 +00:00
|
|
|
});
|
2012-02-24 20:30:30 +00:00
|
|
|
|
|
|
|
test('satisfy', function(){
|
|
|
|
var matcher = function(num){
|
|
|
|
return num === 1;
|
|
|
|
};
|
|
|
|
|
2012-02-24 20:46:04 +00:00
|
|
|
(1).should.satisfy(matcher);
|
2012-02-24 20:30:30 +00:00
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(2).should.satisfy(matcher, 'blah');
|
|
|
|
}, "blah: expected 2 to satisfy [Function]");
|
2012-02-24 20:30:30 +00:00
|
|
|
});
|
2012-02-24 22:00:15 +00:00
|
|
|
|
|
|
|
test('closeTo', function(){
|
|
|
|
(1.5).should.be.closeTo(1.0, 0.5);
|
|
|
|
|
|
|
|
err(function(){
|
2012-09-05 06:09:40 +00:00
|
|
|
(2).should.be.closeTo(1.0, 0.5, 'blah');
|
|
|
|
}, "blah: expected 2 to be close to 1 +/- 0.5");
|
2012-02-24 22:00:15 +00:00
|
|
|
});
|
2012-02-07 21:39:08 +00:00
|
|
|
});
|