expect.js tests
node-expect.js-0.3.1+dfsg.orig/test/expect.js 0000664 0000000 0000000 00000036215 12301417000 017517 0 ustar root root
/**
* Module dependencies.
*/
function err (fn, msg) {
try {
fn();
throw new Error('Expected an error');
} catch (err) {
expect(msg).to.be(err.message);
}
}
/**
* Feature detection for `name` support.
*/
var nameSupported;
(function a () {
nameSupported = 'a' == a.name;
})();
/**
* Tests.
*/
describe('expect', function () {
it('should have .version', function () {
expect(expect.version).to.match(/^\d+\.\d+\.\d+$/);
});
it('should work in its basic form', function () {
expect('test').to.be.a('string');
});
it('should 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 equal true")
});
it('should allow not.to', function () {
expect(true).not.to.be(false);
err(function () {
expect(false).not.to.be(false);
}, "expected false to not equal false")
});
it('should 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();
}, "expected 'test' to be falsy");
});
it('should 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 equal false")
});
it('should test functions with arguments', function () {
function itThrowsSometimes (first, second) {
if (first ^ second) {
throw new Error('tell');
}
}
expect(itThrowsSometimes).withArgs(false, false).to.not.throwException();
expect(itThrowsSometimes).withArgs(false, true).to.throwException(/tell/);
expect(itThrowsSometimes).withArgs(true, false).to.throwException(/tell/);
expect(itThrowsSometimes).withArgs(true, true).to.not.throwException();
});
it('should test for exceptions', function () {
function itThrows () {
a.b.c;
}
function itThrowsString () {
throw 'aaa';
}
function itThrowsMessage () {
throw new Error('tobi');
}
var anonItThrows = function () {
a.b.c;
}
function itWorks () {
return
}
var anonItWorks = function () { }
expect(itThrows).to.throwException();
expect(itWorks).to.not.throwException();
var subject;
expect(itThrows).to.throwException(function (e) {
subject = e;
});
expect(subject).to.be.an(Error);
expect(itThrowsMessage).to.throwException(/tobi/);
expect(itThrowsMessage).to.not.throwException(/test/);
err(function () {
expect(itThrowsMessage).to.throwException(/no match/);
}, 'expected \'tobi\' to match /no match/');
var subject2;
expect(itThrowsString).to.throwException(function (str) {
subject2 = str;
});
expect(subject2).to.be('aaa');
expect(itThrowsString).to.throwException(/aaa/);
expect(itThrowsString).to.not.throwException(/bbb/);
err(function () {
expect(itThrowsString).to.throwException(/no match/i);
}, 'expected \'aaa\' to match /no match/i');
var called = false;
expect(itWorks).to.not.throwError(function () {
called = true;
});
expect(called).to.be(false);
err(function () {
expect(5).to.throwException();
}, 'expected 5 to be a function');
err(function () {
expect(anonItThrows).not.to.throwException();
}, 'expected fn not to throw an exception');
err(function () {
expect(anonItWorks).to.throwException();
}, 'expected fn to throw an exception');
if (nameSupported) {
err(function () {
expect(itWorks).to.throwException();
}, 'expected itWorks to throw an exception');
} else {
err(function () {
expect(itWorks).to.throwException();
}, 'expected fn to throw an exception');
}
if (nameSupported) {
err(function () {
expect(itThrows).not.to.throwException();
}, 'expected itThrows not to throw an exception');
} else {
err(function () {
expect(anonItThrows).not.to.throwException();
}, 'expected fn not to throw an exception');
}
});
it('should test arrays', function () {
expect([]).to.be.a('array');
expect([]).to.be.an('array');
err(function () {
expect({}).to.be.an('array');
}, 'expected {} to be an array');
});
it('should test regex', function () {
expect(/a/).to.be.an('regexp');
expect(/a/).to.be.a('regexp');
err(function () {
expect(null).to.be.a('regexp');
}, 'expected null to be a regexp');
});
it('should test objects', function () {
expect({}).to.be.an('object');
err(function () {
expect(null).to.be.an('object');
}, 'expected null to be an object');
});
it('should test .equal()', function () {
var foo;
expect(foo).to.be(undefined);
});
it('should 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');
err(function () {
expect(5).to.not.be.a('number');
}, "expected 5 not to be a number");
});
it('should test instanceof', function () {
function Foo(){}
expect(new Foo()).to.be.a(Foo);
if (nameSupported) {
err(function () {
expect(3).to.be.a(Foo);
}, "expected 3 to be an instance of Foo");
} else {
err(function () {
expect(3).to.be.a(Foo);
}, "expected 3 to be an instance of supplied constructor");
}
});
it('should test within(start, finish)', function () {
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");
});
it('should 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");
});
it('should 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");
});
it('should 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");
});
it('should test eql(val)', function () {
expect('test').to.eql('test');
expect({ foo: 'bar' }).to.eql({ foo: 'bar' });
expect(1).to.eql(1);
expect('4').to.eql(4);
expect(/a/gmi).to.eql(/a/mig);
err(function () {
expect(4).to.eql(3);
}, 'expected 4 to sort of equal 3');
});
it('should 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");
});
it('should test be(val)', function () {
expect('test').to.be('test');
expect(1).to.be(1);
err(function () {
expect(4).to.be(3);
}, 'expected 4 to equal 3');
err(function () {
expect('4').to.be(4);
}, "expected '4' to equal 4");
});
it('should test empty', function () {
expect('').to.be.empty();
expect({}).to.be.empty();
expect([]).to.be.empty();
expect({ length: 0 }).to.be.empty();
err(function () {
expect(null).to.be.empty();
}, 'expected null to be an object');
err(function () {
expect({ a: 'b' }).to.be.empty();
}, 'expected { a: \'b\' } to be empty');
err(function () {
expect({ length: '0' }).to.be.empty();
}, 'expected { length: \'0\' } to be empty');
err(function () {
expect('asd').to.be.empty();
}, "expected 'asd' to be empty");
err(function () {
expect('').to.not.be.empty();
}, "expected '' to not be empty");
err(function () {
expect({}).to.not.be.empty();
}, "expected {} to not be empty");
});
it('should test property(name)', function () {
expect('test').to.have.property('length');
expect(4).to.not.have.property('length');
expect({ length: undefined }).to.have.property('length');
err(function () {
expect('asd').to.have.property('foo');
}, "expected 'asd' to have a property 'foo'");
err(function () {
expect({ length: undefined }).to.not.have.property('length');
}, "expected { length: undefined } to not have a property 'length'");
});
it('should test property(name, val)', function () {
expect('test').to.have.property('length', 4);
expect({ length: undefined }).to.have.property('length', undefined);
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({ length: undefined }).to.not.have.property('length', undefined);
}, "expected { length: undefined } to not have a property 'length'");
});
it('should test own.property(name)', function () {
expect('test').to.have.own.property('length');
expect({ length: 12 }).to.have.own.property('length');
err(function () {
expect({ length: 12 }).to.not.have.own.property('length');
}, "expected { length: 12 } to not have own property 'length'");
});
it('should test string()', function () {
expect('foobar').to.contain('bar');
expect('foobar').to.contain('foo');
expect('foobar').to.include.string('foo');
expect('foobar').to.not.contain('baz');
expect('foobar').to.not.include.string('baz');
err(function () {
expect(3).to.contain('baz');
}, "expected 3 to contain 'baz'");
err(function () {
expect('foobar').to.contain('baz');
}, "expected 'foobar' to contain 'baz'");
err(function () {
expect('foobar').to.not.contain('bar');
}, "expected 'foobar' to not contain 'bar'");
});
it('should test contain()', function () {
expect(['foo', 'bar']).to.contain('foo');
expect(['foo', 'bar']).to.contain('foo');
expect(['foo', 'bar']).to.contain('bar');
expect([1,2]).to.contain(1);
expect(['foo', 'bar']).to.not.contain('baz');
expect(['foo', 'bar']).to.not.contain(1);
err(function () {
expect(['foo']).to.contain('bar');
}, "expected [ 'foo' ] to contain 'bar'");
err(function () {
expect(['bar', 'foo']).to.not.contain('foo');
}, "expected [ 'bar', 'foo' ] to not contain 'foo'");
});
it('should 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.include.keys('foo', 'bar');
expect({ foo: 1, bar: 2, baz: 3 }).to.include.keys('bar', 'foo');
expect({ foo: 1, bar: 2, baz: 3 }).to.include.keys('baz');
expect({ foo: 1, bar: 2 }).to.include.keys('foo');
expect({ foo: 1, bar: 2 }).to.include.keys('bar', 'foo');
expect({ foo: 1, bar: 2 }).to.include.keys(['foo']);
expect({ foo: 1, bar: 2 }).to.include.keys(['bar']);
expect({ foo: 1, bar: 2 }).to.include.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.include.keys('baz');
expect({ foo: 1, bar: 2 }).to.not.include.keys('foo', 'baz');
expect({ foo: 1, bar: 2 }).to.not.include.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.include.keys([]);
}, "keys required");
err(function () {
expect({ foo: 1 }).to.have.keys(['bar']);
}, "expected { foo: 1 } to include key 'bar'");
err(function () {
expect({ foo: 1 }).to.have.keys(['bar', 'baz']);
}, "expected { foo: 1 } to include keys 'bar', and 'baz'");
err(function () {
expect({ foo: 1 }).to.have.keys(['foo', 'bar', 'baz']);
}, "expected { foo: 1 } to include keys 'foo', 'bar', and 'baz'");
err(function () {
expect({ foo: 1 }).to.not.have.keys(['foo']);
}, "expected { foo: 1 } to not include key 'foo'");
err(function () {
expect({ foo: 1 }).to.not.have.keys(['foo']);
}, "expected { foo: 1 } to not include key 'foo'");
err(function () {
expect({ foo: 1, bar: 2 }).to.not.have.keys(['foo', 'bar']);
}, "expected { foo: 1, bar: 2 } to not include keys 'foo', and 'bar'");
err(function () {
expect({ foo: 1 }).to.not.include.keys(['foo']);
}, "expected { foo: 1 } to not include key 'foo'");
err(function () {
expect({ foo: 1 }).to.include.keys('foo', 'bar');
}, "expected { foo: 1 } to include keys 'foo', and 'bar'");
// only
expect({ foo: 1, bar: 1 }).to.only.have.keys('foo', 'bar');
expect({ foo: 1, bar: 1 }).to.only.have.keys(['foo', 'bar']);
err(function () {
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'b', 'c');
}, "expected { a: 'b', c: 'd' } to only have keys 'a', 'b', and 'c'");
err(function () {
expect({ a: 'b', c: 'd' }).to.only.have.keys('a');
}, "expected { a: 'b', c: 'd' } to only have key 'a'");
});
it('should allow chaining with `and`', function () {
expect(5).to.be.a('number').and.be(5);
expect(5).to.be.a('number').and.not.be(6);
expect(5).to.be.a('number').and.not.be(6).and.not.be('5');
err(function () {
expect(5).to.be.a('number').and.not.be(5);
}, "expected 5 to not equal 5");
err(function () {
expect(5).to.be.a('number').and.not.be(6).and.not.be.above(4);
}, "expected 5 to be below 4");
});
it('should fail with `fail`', function () {
err(function () {
expect().fail();
}, "explicit failure");
});
it('should fail with `fail` and custom message', function () {
err(function () {
expect().fail("explicit failure with message");
}, "explicit failure with message");
});
});
node-expect.js-0.3.1+dfsg.orig/test/common.js 0000664 0000000 0000000 00000000217 12301417000 017510 0 ustar root root
/*!
* Common test dependencies.
*/
// expose the globals that are obtained through `
```
## API
**ok**: asserts that the value is _truthy_ or not
```js
expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be.ok();
```
**be** / **equal**: asserts `===` equality
```js
expect(1).to.be(1)
expect(NaN).not.to.equal(NaN);
expect(1).not.to.be(true)
expect('1').to.not.be(1);
```
**eql**: asserts loose equality that works with objects
```js
expect({ a: 'b' }).to.eql({ a: 'b' });
expect(1).to.eql('1');
```
**a**/**an**: asserts `typeof` with support for `array` type and `instanceof`
```js
// typeof with optional `array`
expect(5).to.be.a('number');
expect([]).to.be.an('array'); // works
expect([]).to.be.an('object'); // works too, since it uses `typeof`
// constructors
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
expect(tobi).to.be.a(Ferret);
expect(person).to.be.a(Mammal);
```
**match**: asserts `String` regular expression match
```js
expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
```
**contain**: asserts indexOf for an array or string
```js
expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');
```
**length**: asserts array `.length`
```js
expect([]).to.have.length(0);
expect([1,2,3]).to.have.length(3);
```
**empty**: asserts that an array is empty or not
```js
expect([]).to.be.empty();
expect({}).to.be.empty();
expect({ length: 0, duck: 'typing' }).to.be.empty();
expect({ my: 'object' }).to.not.be.empty();
expect([1,2,3]).to.not.be.empty();
```
**property**: asserts presence of an own property (and value optionally)
```js
expect(window).to.have.property('expect')
expect(window).to.have.property('expect', expect)
expect({a: 'b'}).to.have.property('a');
```
**key**/**keys**: asserts the presence of a key. Supports the `only` modifier
```js
expect({ a: 'b' }).to.have.key('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect({ a: 'b', c: 'd' }).to.not.only.have.key('a');
```
**throwException**/**throwError**: asserts that the `Function` throws or not when called
```js
expect(fn).to.throwError(); // synonym of throwException
expect(fn).to.throwException(function (e) { // get the exception object
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();
```
**withArgs**: creates anonymous function to call fn with arguments
```js
expect(fn).withArgs(invalid, arg).to.throwException();
expect(fn).withArgs(valid, arg).to.not.throwException();
```
**within**: asserts a number within a range
```js
expect(1).to.be.within(0, Infinity);
```
**greaterThan**/**above**: asserts `>`
```js
expect(3).to.be.above(0);
expect(5).to.be.greaterThan(3);
```
**lessThan**/**below**: asserts `<`
```js
expect(0).to.be.below(3);
expect(1).to.be.lessThan(3);
```
**fail**: explicitly forces failure.
```js
expect().fail()
expect().fail("Custom failure message")
```
## Using with a test framework
For example, if you create a test suite with
[mocha](http://github.com/visionmedia/mocha).
Let's say we wanted to test the following program:
**math.js**
```js
function add (a, b) { return a + b; };
```
Our test file would look like this:
```js
describe('test suite', function () {
it('should expose a function', function () {
expect(add).to.be.a('function');
});
it('should do math', function () {
expect(add(1, 3)).to.equal(4);
});
});
```
If a certain expectation fails, an exception will be raised which gets captured
and shown/processed by the test runner.
## Differences with should.js
- No need for static `should` methods like `should.strictEqual`. For example,
`expect(obj).to.be(undefined)` works well.
- Some API simplifications / changes.
- API changes related to browser compatibility.
## Running tests
Clone the repository and install the developer dependencies:
```
git clone git://github.com/LearnBoost/expect.js.git expect
cd expect && npm install
```
### Node
`make test`
### Browser
`make test-browser`
and point your browser(s) to `http://localhost:3000/test/`
## Credits
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
### 3rd-party
Heavily borrows from [should.js](http://github.com/visionmedia/should.js) by TJ
Holowaychuck - MIT.
node-expect.js-0.3.1+dfsg.orig/Makefile 0000664 0000000 0000000 00000000304 12301417000 016340 0 ustar root root
REPORTER = dot
test:
@./node_modules/.bin/mocha \
--require ./test/common \
--reporter $(REPORTER) \
--growl \
test/expect.js
test-browser:
@./node_modules/.bin/serve .
.PHONY: test
node-expect.js-0.3.1+dfsg.orig/History.md 0000664 0000000 0000000 00000003252 12301417000 016670 0 ustar root root
0.3.0 / 2014-02-20
==================
* renmaed to `index.js`
* added repository to package.json
* remove unused variable and merge
* simpify isDate() and remove unnecessary semicolon.
* Add .withArgs() syntax for building scenario
* eql(): fix wrong order of actual vs. expected.
* Added formatting for Error objects
* Add support for 'regexp' type and eql comparison of regular expressions.
* Better to follow the same coding style
* Use 'showDiff' flag
* Add 'actual' & 'expected' property to the thrown error
* Pass .fail() unit test
* Ignore 'script*' global leak in chrome
* Exposed object stringification function
* Use isRegExp in Assertion::throwException. Fix #25
* Cleaned up local variables
0.2.0 / 2012-10-19
==================
* fix isRegExp bug in some edge cases
* add closure to all assertion messages deferring costly inspects
until there is actually a failure
* fix `make test` for recent mochas
* add inspect() case for DOM elements
* relax failure msg null check
* add explicit failure through `expect().fail()`
* clarified all `empty` functionality in README example
* added docs for throwException fn/regexp signatures
0.1.2 / 2012-02-04
==================
* Added regexp matching support for exceptions.
* Added support for throwException callback.
* Added `throwError` synonym to `throwException`.
* Added object support for `.empty`.
* Fixed `.a('object')` with nulls, and english error in error message.
* Fix bug `indexOf` (IE). [hokaccha]
* Fixed object property checking with `undefined` as value. [vovik]
0.1.1 / 2011-12-18
==================
* Fixed typo
0.1.0 / 2011-12-18
==================
* Initial import
node-expect.js-0.3.1+dfsg.orig/.npmignore 0000664 0000000 0000000 00000000026 12301417000 016700 0 ustar root root support
test
Makefile
node-expect.js-0.3.1+dfsg.orig/.gitignore 0000664 0000000 0000000 00000000027 12301417000 016672 0 ustar root root node_modules
.DS_Store