chai/test/expect.js

479 lines
13 KiB
JavaScript
Raw Normal View History

2011-12-18 12:30:59 +00:00
/*!
2011-12-15 10:54:37 +00:00
* Module dependencies.
2011-12-18 12:30:59 +00:00
*
* Chai is automatically included in the browser.
2011-12-15 10:54:37 +00:00
*/
2011-12-15 12:03:47 +00:00
if (!chai) {
2011-12-18 12:30:59 +00:00
var chai = require('..');
2011-12-15 12:03:47 +00:00
}
2011-12-15 10:54:37 +00:00
2011-12-15 18:19:11 +00:00
var expect = chai.expect;
2011-12-15 10:54:37 +00:00
function err(fn, msg) {
try {
fn();
chai.fail('expected an error');
} catch (err) {
expect(msg).to.equal(err.message);
}
}
2012-01-02 05:45:06 +00:00
suite('expect', function () {
test('=.version', function() {
expect(chai.version).to.match(/^\d+\.\d+\.\d+$/);
});
test('=double require', function(){
//require('chai').expect().to.equal(expect);
});
test('assertion', function(){
expect('test').to.be.a('string');
expect('foo').to.equal('foo');
});
test('true', function(){
expect(true).to.be.true;
expect(false).to.not.be.true;
expect(1).to.not.be.true;
err(function(){
expect('test').to.be.true;
}, "expected 'test' to be true")
});
test('ok', function(){
expect(true).to.be.ok;
expect(false).to.not.be.ok;
expect(1).to.be.ok;
expect(0).to.not.be.ok;
err(function(){
expect('').to.be.ok;
}, "expected '' to be truthy");
err(function(){
expect('test').to.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(){
expect(false).to.be.false;
expect(true).to.not.be.false;
expect(0).to.not.be.false;
err(function(){
expect('').to.be.false;
}, "expected '' to be false")
});
test('exist', function(){
var foo = 'bar'
, bar;
expect(foo).to.exist;
expect(bar).to.not.exist;
});
test('arguments', function(){
var args = (function(){ return arguments; })(1,2,3);
expect(args).to.be.arguments;
expect([]).to.not.be.arguments;
});
test('.equal()', function(){
var foo;
expect(undefined).to.equal(foo);
});
test('typeof', function(){
expect('test').to.be.a('string');
err(function(){
expect('test').to.not.be.a('string');
}, "expected 'test' not to be a string");
expect(5).to.be.a('number');
2012-02-25 18:28:40 +00:00
expect(new Number(1)).to.be.a('number');
2012-02-25 18:42:49 +00:00
expect(Number(1)).to.be.a('number');
2012-02-25 18:33:05 +00:00
expect(true).to.be.a('boolean');
expect(new Array()).to.be.a('array');
expect(new Object()).to.be.a('object');
expect({}).to.be.a('object');
expect([]).to.be.a('array');
2012-02-25 18:38:43 +00:00
expect(function() {}).to.be.a('function');
2012-01-02 05:45:06 +00:00
err(function(){
expect(5).to.not.be.a('number');
}, "expected 5 not to be a number");
});
test('instanceof', function(){
function Foo(){}
expect(new Foo()).to.be.an.instanceof(Foo);
err(function(){
expect(3).to.an.instanceof(Foo);
}, "expected 3 to be an instance of Foo");
});
test('within(start, finish)', function(){
expect(5).to.be.within(5, 10);
expect(5).to.be.within(3,6);
expect(5).to.be.within(3,5);
expect(5).to.not.be.within(1,3);
err(function(){
expect(5).to.not.be.within(4,6);
}, "expected 5 to not be within 4..6");
err(function(){
expect(10).to.be.within(50,100);
}, "expected 10 to be within 50..100");
});
test('above(n)', function(){
expect(5).to.be.above(2);
expect(5).to.be.greaterThan(2);
expect(5).to.not.be.above(5);
expect(5).to.not.be.above(6);
err(function(){
expect(5).to.be.above(6);
}, "expected 5 to be above 6");
err(function(){
expect(10).to.not.be.above(6);
}, "expected 10 to be below 6");
});
test('match(regexp)', function(){
expect('foobar').to.match(/^foo/)
expect('foobar').to.not.match(/^bar/)
err(function(){
expect('foobar').to.match(/^bar/i)
}, "expected 'foobar' to match /^bar/i");
err(function(){
expect('foobar').to.not.match(/^foo/i)
}, "expected 'foobar' not to match /^foo/i");
});
test('length(n)', function(){
expect('test').to.have.length(4);
expect('test').to.not.have.length(3);
expect([1,2,3]).to.have.length(3);
err(function(){
expect(4).to.have.length(3);
}, 'expected 4 to have a property \'length\'');
err(function(){
expect('asd').to.not.have.length(3);
}, "expected 'asd' to not have a length of 3");
});
test('eql(val)', function(){
expect('test').to.eql('test');
expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
expect(1).to.eql(1);
expect('4').to.not.eql(4);
err(function(){
expect(4).to.eql(3);
}, 'expected 4 to equal 3');
});
test('equal(val)', function(){
expect('test').to.equal('test');
expect(1).to.equal(1);
err(function(){
expect(4).to.equal(3);
}, 'expected 4 to equal 3');
err(function(){
expect('4').to.equal(4);
}, "expected '4' to equal 4");
});
test('empty', function(){
expect('').to.be.empty;
expect([]).to.be.empty;
expect({ length: 0 }).to.be.empty;
err(function(){
expect({}).to.be.empty;
}, 'expected {} to have a property \'length\'');
err(function(){
expect([ 'hello', 'world' ]).to.be.empty;
}, "expected [ \'hello\', \'world\' ] to be empty");
err(function(){
expect([ { hello: 'world' } ]).to.be.empty;
}, "expected [ { hello: \'world\' } ] to be empty");
err(function(){
expect('asd').to.be.empty;
}, "expected 'asd' to be empty");
err(function(){
expect('').to.not.be.empty;
}, "expected '' not to be empty");
});
test('property(name)', function(){
expect('test').to.have.property('length');
expect(4).to.not.have.property('length');
err(function(){
expect('asd').to.have.property('foo');
}, "expected 'asd' to have a property 'foo'");
});
test('property(name, val)', function(){
expect('test').to.have.property('length', 4);
expect('asd').to.have.property('constructor', String);
err(function(){
expect('asd').to.have.property('length', 4);
}, "expected 'asd' to have a property 'length' of 4, but got 3");
err(function(){
expect('asd').to.not.have.property('length', 3);
}, "expected 'asd' to not have a property 'length' of 3");
err(function(){
expect('asd').to.not.have.property('foo', 3);
}, "'asd' has no property 'foo'");
err(function(){
expect('asd').to.have.property('constructor', Number);
}, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
});
test('ownProperty(name)', function(){
expect('test').to.have.ownProperty('length');
expect('test').to.haveOwnProperty('length');
expect({ length: 12 }).to.have.ownProperty('length');
err(function(){
expect({ length: 12 }).to.not.have.ownProperty('length');
}, "expected { length: 12 } to not have own property 'length'");
});
test('string()', function(){
expect('foobar').to.have.string('bar');
expect('foobar').to.have.string('foo');
expect('foobar').to.not.have.string('baz');
err(function(){
expect(3).to.have.string('baz');
}, "expected 3 to be a string");
err(function(){
expect('foobar').to.have.string('baz');
}, "expected 'foobar' to contain 'baz'");
err(function(){
expect('foobar').to.not.have.string('bar');
}, "expected 'foobar' to not contain 'bar'");
});
test('include()', function(){
expect(['foo', 'bar']).to.include('foo');
expect(['foo', 'bar']).to.include('foo');
expect(['foo', 'bar']).to.include('bar');
expect([1,2]).to.include(1);
expect(['foo', 'bar']).to.not.include('baz');
expect(['foo', 'bar']).to.not.include(1);
err(function(){
expect(['foo']).to.include('bar');
}, "expected [ 'foo' ] to include 'bar'");
err(function(){
expect(['bar', 'foo']).to.not.include('foo');
}, "expected [ 'bar', 'foo' ] to not include 'foo'");
});
test('keys(array)', function(){
expect({ foo: 1 }).to.have.keys(['foo']);
expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']);
expect({ foo: 1, bar: 2 }).to.have.keys('foo', 'bar');
expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('foo', 'bar');
expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('bar', 'foo');
expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('baz');
expect({ foo: 1, bar: 2 }).to.contain.keys('foo');
expect({ foo: 1, bar: 2 }).to.contain.keys('bar', 'foo');
expect({ foo: 1, bar: 2 }).to.contain.keys(['foo']);
expect({ foo: 1, bar: 2 }).to.contain.keys(['bar']);
expect({ foo: 1, bar: 2 }).to.contain.keys(['bar', 'foo']);
expect({ foo: 1, bar: 2 }).to.not.have.keys('baz');
expect({ foo: 1, bar: 2 }).to.not.have.keys('foo', 'baz');
expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz');
expect({ foo: 1, bar: 2 }).to.not.contain.keys('foo', 'baz');
expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz', 'foo');
err(function(){
expect({ foo: 1 }).to.have.keys();
}, "keys required");
err(function(){
expect({ foo: 1 }).to.have.keys([]);
}, "keys required");
err(function(){
expect({ foo: 1 }).to.not.have.keys([]);
}, "keys required");
err(function(){
expect({ foo: 1 }).to.contain.keys([]);
}, "keys required");
err(function(){
expect({ foo: 1 }).to.have.keys(['bar']);
}, "expected { foo: 1 } to have key 'bar'");
err(function(){
expect({ foo: 1 }).to.have.keys(['bar', 'baz']);
}, "expected { foo: 1 } to have keys 'bar', and 'baz'");
err(function(){
expect({ foo: 1 }).to.have.keys(['foo', 'bar', 'baz']);
}, "expected { foo: 1 } to have keys 'foo', 'bar', and 'baz'");
err(function(){
expect({ foo: 1 }).to.not.have.keys(['foo']);
}, "expected { foo: 1 } to not have key 'foo'");
err(function(){
expect({ foo: 1 }).to.not.have.keys(['foo']);
}, "expected { foo: 1 } to not have key 'foo'");
err(function(){
expect({ foo: 1, bar: 2 }).to.not.have.keys(['foo', 'bar']);
}, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'");
err(function(){
expect({ foo: 1 }).to.not.contain.keys(['foo']);
}, "expected { foo: 1 } to not contain key 'foo'");
err(function(){
expect({ foo: 1 }).to.contain.keys('foo', 'bar');
}, "expected { foo: 1 } to contain keys 'foo', and 'bar'");
});
test('chaining', function(){
var tea = { name: 'chai', extras: ['milk', 'sugar', 'smile'] };
expect(tea).to.have.property('extras').with.lengthOf(3);
err(function(){
expect(tea).to.have.property('extras').with.lengthOf(4);
}, "expected [ 'milk', 'sugar', 'smile' ] to have a length of 4 but got 3");
expect(tea).to.be.a('object').and.have.property('name', 'chai');
});
test('throw', function () {
var goodFn = function () { 1==1; }
, badFn = function () { throw new Error('testing'); }
, refErrFn = function () { throw new ReferenceError(); };
expect(goodFn).to.not.throw();
expect(goodFn).to.not.throw(Error);
expect(badFn).to.throw();
expect(badFn).to.throw(Error);
expect(badFn).to.not.throw(ReferenceError);
expect(refErrFn).to.throw();
expect(refErrFn).to.throw(ReferenceError);
expect(refErrFn).to.not.throw(Error);
expect(refErrFn).to.not.throw(TypeError);
2012-02-07 21:39:08 +00:00
expect(badFn).to.throw(/testing/);
expect(badFn).to.not.throw(/hello/);
2012-01-02 05:45:06 +00:00
err(function(){
expect(goodFn).to.throw();
}, "expected [Function] to throw an error");
err(function(){
expect(goodFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError");
err(function(){
expect(badFn).to.not.throw();
}, "expected [Function] to not throw an error");
err(function(){
expect(badFn).to.throw(ReferenceError);
}, "expected [Function] to throw ReferenceError but a Error was thrown");
err(function(){
expect(badFn).to.not.throw(Error);
}, "expected [Function] to not throw Error");
err(function(){
expect(refErrFn).to.not.throw(ReferenceError);
}, "expected [Function] to not throw ReferenceError");
err(function(){
expect(refErrFn).to.throw(Error);
}, "expected [Function] to throw Error but a ReferenceError was thrown");
2012-02-07 21:39:08 +00:00
err(function (){
expect(badFn).to.not.throw(/testing/);
}, "expected [Function] to throw error not matching /testing/");
err(function () {
expect(badFn).to.throw(/hello/);
}, "expected [Function] to throw error matching /hello/ but got \'testing\'");
2012-01-02 05:45:06 +00:00
});
2012-02-24 19:53:15 +00:00
test('respondTo', function(){
function Foo(){};
Foo.prototype.bar = function(){};
var bar = {};
bar.foo = function(){};
expect(Foo).to.respondTo('bar');
expect(Foo).to.not.respondTo('foo');
expect(bar).to.respondTo('foo');
err(function(){
expect(Foo).to.respondTo('baz');
}, "expected [Function: Foo] to respond to \'baz\'");
err(function(){
expect(bar).to.respondTo('baz');
}, "expected { foo: [Function] } to respond to \'baz\'");
});
2012-02-24 20:30:20 +00:00
test('satisfy', function(){
var matcher = function(num){
return num === 1;
};
expect(1).to.satisfy(matcher);
err(function(){
expect(2).to.satisfy(matcher);
}, "expected 2 to satisfy [Function]");
});
2012-02-24 22:00:03 +00:00
test('closeTo', function(){
expect(1.5).to.be.closeTo(1.0, 0.5);
err(function(){
expect(2).to.be.closeTo(1.0, 0.5);
}, "expected 2 to be close to 1 +/- 0.5");
});
2012-01-21 23:38:18 +00:00
});