diff --git a/History.md b/History.md index 4339f4b..f723b48 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,17 @@ +3.0.0 / 2014-01- +================== + + * Split everything to smaller files + * Added huge extension to .match and .matchEach. Thanks @alsotang for initial code and idea. + * Added .containDeep and .containEql + * Separate build for browser and node.js + * Basic plugin system + * Breaking change: .Object now do not fail on arrays + * Breaking change: Additional messages now replaces generated and do not added + * Breaking change: .keys now check as is - no keys in args means no keys in object + * Deprecated: assert extension + * Deprecated: .include and .includeEql + 2.1.1 / 2013-12-05 ================== diff --git a/Makefile b/Makefile index 1849c71..73a910b 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,6 @@ test: @./node_modules/.bin/mocha --ui exports --recursive $(TESTS) browser: - @browserify lib/should.js -s should --dg false -o should.js + @browserify lib/browser.js -s Should --dg false -o should.js .PHONY: test browser \ No newline at end of file diff --git a/Readme.md b/Readme.md index 73cab13..894f739 100644 --- a/Readme.md +++ b/Readme.md @@ -1,8 +1,8 @@ # should.js -_should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org). +_should_ is an expressive, readable, test framework agnostic, assertion library. Main goals of this library __to be expressive__ and __to be helpful__. It means test code should be clean, and error messages enough helpfull to understand error. -It extends the Object prototype with a single non-enumerable getter that allows you to express how that object should behave. +It extends the `Object.prototype` with a single non-enumerable getter that allows you to express how that object should behave, also it returns itself when required with `require`. ## Example ```javascript @@ -29,13 +29,23 @@ someAsyncTask(foo, function(err, result){ result.bar.should.equal(foo); }); ``` -## Installation +## To begin + 1. Install it: + $ npm install should --save-dev + + 2. Require it and use: + +```javascript +var should = require('should'); + +(5).should.be.exactly(5).and.be.a.Number; +``` ## In browser -If you want to use _should_ in browser, use version that is in root of repository. It is build with browserify (see [Makefile](https://github.com/visionmedia/should.js/blob/master/Makefile) about how it is build). To build fresh version: +If you want to use _should_ in browser, use version that is in root of repository (or build it yourself). It is builded with browserify (see [Makefile](https://github.com/visionmedia/should.js/blob/master/Makefile)). To build fresh version: ```bash # you should have browserify @@ -43,38 +53,37 @@ npm install -g browserify make browser ``` -## chaining assertions - -Every assertion will return a should.js-wraped Object, so assertions can be chained. For example: +This script exported to `window.Should`. It is the same as use `should` statically: ```js -user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); -user.pets.should.be.instanceof(Array).and.have.lengthOf(4); +Should(5).be.exactly(5) ``` -All assertions return themselves but not `property` and `ownProperty`. They two would return the property value. +Also as for node.js case `Object.prototype` extended with `should` (that is why `window.Should` used): -for example if a property is volatile we can first assert property existence: -```javascript -// `user.should.have.property('pets')` returned `should(user.pets)` but not `should(user)` -user.should.have.property('pets').with.lengthOf(4) -``` -which is essentially equivalent to below, however the property may not exist: -```javascript -user.pets.should.have.lengthOf(4) +```js +window.should.be.exactly(window); +// the same +// window is host object +should.be.exactly(window); +// you should not really care about it + +(5).should.be.exactly(5); ``` -our dummy getters such as _a_, _an_, _be_, _of_ and _have_ make tests more readable while the getters _and_ and _with_ helps express chaining: -```javascript -user.should.be.an.instanceOf(Object).and.have.property('name', 'tj') -user.should.have.property('pets').with.a.lengthOf(4) +*should.js* uses EcmaScript 5 very extensively so any browser that support ES5 is supported. (IE <=8 not supported). + +You can easy install it with again npm or bower: +``` +npm install should --save-dev +# or +bower install visionmedia/should.js ``` -Those getters don't have any actual effect, just for readability. -## static +## Static should and assert module For some rare cases should can be used statically, without `Object.prototype`. -It can be replacement for node assert module (and it uses the same `AssertionError`): +It can be replacement for node assert module: ```javascript assert.fail(actual, expected, message, operator) // just write wrong should assertion @@ -89,227 +98,381 @@ assert.throws(block, [error], [message]) // should(block).throw([error]) assert.doesNotThrow(block, [message]) // should(block).not.throw([error]) assert.ifError(value) // should(value).Error (to check if it is error) or should(value).not.ok (to check that it is falsy) ``` + +## .not -## ok +`.not` negate current assertion. + +# Assertions +## chaining assertions + +Every assertion will return a `should.js`-wrapped Object, so assertions can be chained. +You can use this helpers to just chain: `.an`, `.of`, `.a`, `.and`, `.be`, `.have`, `.with`, `.is`, `.which`. Use them for better readability, they do nothing at all. +For example: +```js +user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); +user.pets.should.be.instanceof(Array).and.have.lengthOf(4); +``` +Almost all assertions return the same object - so you can easy chain them. But some move assertion object to property value. See feather, it will be mention if object chainged. + +## .ok + +Assert if asseted object is truthy in javascript meaning of truthy ('', null, undefined, 0 , NaN, Infinity - is falsy, so all others are truthy). Assert truthfulness: + ```javascript -true.should.be.ok -'yay'.should.be.ok -(1).should.be.ok +true.should.be.ok; +'yay'.should.be.ok; +(1).should.be.ok; +({}).should.be.ok; ``` + or negated: + ```javascript -false.should.not.be.ok -''.should.not.be.ok -(0).should.not.be.ok +false.should.not.be.ok; +''.should.not.be.ok; +(0).should.not.be.ok; ``` -## true -Assert === true: +## .true + +Assert if asseted object === true: + ```javascript -true.should.be.true -'1'.should.not.be.true +true.should.be.true; +'1'.should.not.be.true; ``` -## false -Assert === false: +## .false + +Assert if asseted object === false: + ```javascript -false.should.be.false -(0).should.not.be.false +false.should.be.false; +(0).should.not.be.false; ``` -## arguments -Assert `Arguments`: +## .eql(otherValue) + +Assert if asserted object is *equal* to otherValue. This means that object compared by its actual content, not just reference equality. + ```javascript -var args = (function(){ return arguments; })(1,2,3); -args.should.be.arguments; -[].should.not.be.arguments; +({ foo: 'bar' }).should.eql({ foo: 'bar' }); +[1,2,3].should.eql([1,2,3]); +// see next example it is correct, even if it is different types, but actual content the same +[1, 2, 3].should.eql({ '0': 1, '1': 2, '2': 3 }); ``` -## empty +## .equal(otherValue) and .exactly(otherValue) + +Assert if asserted object strictly equal to `otherValue` (using `===` - no type conversion for primitive types and reference equivalence for reference types). -Asserts that given object is empty ```javascript -[].should.be.empty -''.should.be.empty -({}).should.be.empty -(function() { - arguments.should.be.empty; -})() +(4).should.equal(4); +'test'.should.equal('test'); +[1,2,3].should.not.equal([1,2,3]); +(4).should.be.exactly(4); ``` -## eql -equality: +## .startWith(str) + +Assert that string starts with `str`. + ```javascript -({ foo: 'bar' }).should.eql({ foo: 'bar' }) -[1,2,3].should.eql([1,2,3]) +'foobar'.should.startWith('foo'); +'foobar'.should.not.startWith('bar'); ``` -## equal and exactly +## .endWith(str) + +Assert that string ends with `str`. -strict equality: ```javascript -should.strictEqual(undefined, value) -should.strictEqual(false, value) -(4).should.equal(4) -'test'.should.equal('test') -[1,2,3].should.not.equal([1,2,3]) -(4).should.be.exactly(4) +'foobar'.should.endWith('bar'); +'foobar'.should.not.endWith('foo'); ``` -## within -Assert inclusive numeric range: +## .within(from, to) + +Assert inclusive numeric range (`<= to` and `>= from`): ```javascript -user.age.should.be.within(5, 50) +user.age.should.be.within(5, 50); +(5).should.be.within(5, 10).and.within(5, 5); ``` -## approximately -Assert floating point number: +## .approximately(num, delta) + +Assert floating point number near `num` within `delta` margin: + ```javascript (99.99).should.be.approximately(100, 0.1); ``` -## type -Assert __typeof__: +## .above(num) and .greaterThan(num) + +Assert numeric value above the given value (`> num`): + ```javascript -user.should.be.type('object') -'test'.should.be.type('string') +user.age.should.be.above(5); +user.age.should.not.be.above(100); +(5).should.be.above(0); +(5).should.not.be.above(5); ``` -## instanceof and instanceOf -Assert __instanceof__ or __instanceOf__: +## .below(num) and .lessThan(num) + +Assert numeric value below the given value (`< num`): + ```javascript -user.should.be.an.instanceof(User) -[].should.be.an.instanceOf(Array) +user.age.should.be.below(100); +user.age.should.not.be.below(5); +(5).should.be.below(6); +(5).should.not.be.below(5); ``` -## above -Assert numeric value above the given value: +## .NaN + +Assert numeric value is NaN: + ```javascript -user.age.should.be.above(5) -user.age.should.not.be.above(100) +(undefined + 0).should.be.NaN; ``` -## below -Assert numeric value below the given value: +## .Infinity + +Assert numeric value is Infinity: + ```javascript -user.age.should.be.below(100) -user.age.should.not.be.below(5) +(1/0).should.be.Infinity; ``` -## NaN -Assert numeric valus is NaN: +## .type(str) + +Assert given value have such type (using __typeof__ operator): ```javascript -(undefined + 0).should.be.NaN; +user.should.be.type('object'); +'test'.should.be.type('string'); ``` -## Infinity -Assert numeric valus is Infinity: +## .instanceof(constructor) and .instanceOf(constructor) + +Assert given value is instance of `constructor` (using __instanceof__ operator): + ```javascript -(1/0).should.be.Infinity; +user.should.be.an.instanceof(User); +[].should.be.an.instanceOf(Array); ``` -## match -Assert regexp match: +## .arguments + +Assert given object is an `Arguments`: + ```javascript -username.should.match(/^\w+$/) +var args = (function(){ return arguments; })(1,2,3); +args.should.be.arguments; +[].should.not.be.arguments; ``` -## length -Assert _length_ property exists and has a value of the given number: +## .Object, .Number, .Array, .Boolean, .Function, .String, .Error + +Assert given object is instance of such constructor (shortcut for `.instanceof` assertion). + ```javascript -user.pets.should.have.length(5) -user.pets.should.have.a.lengthOf(5) -({ length: 10}).should.have.length(10); +({}).should.be.an.Object; +(1).should.be.a.Number; +[].should.be.an.Array.and.an.Object; +(true).should.be.a.Boolean; +''.should.be.a.String; ``` -Aliases: _lengthOf_ -## property +## .property(name[, value]) -Assert property exists and has optional value(compare using `===`): +Assert property exists and has optional value(compare using `.eql`): ```javascript -user.should.have.property('name') -user.should.have.property('age', 15) -user.should.not.have.property('rawr') -user.should.not.have.property('age', 0) +user.should.have.property('name'); +user.should.have.property('age', 15); +user.should.not.have.property('rawr'); +user.should.not.have.property('age', 0); +[1, 2].should.have.property('0', 1); ``` -## properties +__NB__ `.property` change object to actual property value! + +## .properties(propName1, propName2, ...) or .properties([propName1, propName2, ...]) or .properties(obj) + +`obj` it is object that map properties to their actual values. + +Assert all given properties exists and have given values (compare using `.eql`): -Assert given properties exists: ```javascript user.should.have.properties('name', 'age'); user.should.have.properties(['name', 'age']); +user.should.have.properties({ + name: 'denis', + age: 24 +}); ``` -## ownProperty -Assert own property (on the immediate object): +## .length(number) and .lengthOf(number) + +Assert _length_ property exists and has a value of the given number (shortcut for `.property('length', number)`): ```javascript -({ foo: 'bar' }).should.have.ownProperty('foo') +user.pets.should.have.length(5); +user.pets.should.have.a.lengthOf(5); +({ length: 10}).should.have.length(10); ``` -## status(code) - Asserts that `.statusCode` is `code`: +__NB__ `.length` change object to actual property value! + +## .ownProperty(str) and .hasOwnProperty(str) + +Assert given object has own property (using `.hasOwnProperty`): ```javascript -res.should.have.status(200); +({ foo: 'bar' }).should.have.ownProperty('foo').equal('bar'); ``` -## header(field[, value]) - Asserts that a `.headers` object with `field` and optional `value` are present: +__NB__ `.length` change object to actual property value! + +## .empty + +Assert given value is empty. It means for strings, arrays, arguments length == 0 and for object do not have own properties. + ```javascript -res.should.have.header('content-length'); -res.should.have.header('Content-Length', '123'); -res.should.have.header('content-length', '123'); +[].should.be.empty; +''.should.be.empty; +({}).should.be.empty; +(function() { + arguments.should.be.empty; +})(); ``` -## json - Assert that Content-Type is "application/json; charset=utf-8" +## .keys([key1, key2, ...]) and .keys(key1, key2, ...) and .key(key) + +Assert own object keys, which must match _exactly_, +and will fail if you omit a key or two: + ```javascript -res.should.be.json +var obj = { foo: 'bar', baz: 'raz' }; +obj.should.have.keys('foo', 'baz'); +obj.should.have.keys(['foo', 'baz']); +({}).should.have.keys(); +({}).should.have.keys('key'); //fail AssertionError: expected {} to have key 'key'missing keys: 'key' ``` -## html - Assert that Content-Type is "text/html; charset=utf-8" +## .containEql(otherValue) + +Assert given value to contain something *.eql* to otherValue. See examples to understand better: + ```javascript -res.should.be.html +'hello boy'.should.containEql('boy'); +[1,2,3].should.containEql(3); +[[1],[2],[3]].should.containEql([3]); +[[1],[2],[3, 4]].should.not.containEql([3]); + +({ b: 10 }).should.containEql({ b: 10 }); +[1, 2, 3].should.containEql(1); +([1, 2, { a: 10 }]).should.containEql({ a: 10 }); +[1, 2, 3].should.not.containEql({ a: 1 }); + +[{a: 'a'}, {b: 'b', c: 'c'}].should.containEql({a: 'a'}); +[{a: 'a'}, {b: 'b', c: 'c'}].should.not.containEql({b: 'b'}); +``` + +## .containDeep(otherValue) + +Assert given value to contain something *.eql* to otherValue within depth. Again see examples: + +```javascript +'hello boy'.should.containDeep('boy'); +[1,2,3].should.containDeep(3); + +({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should + .containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}); + +[1, 2, 3, { a: { b: { d: 12 }}}].should.containDeep({ a: { b: {d: 12}}}); + +[[1],[2],[3]].should.containDeep([3]); +[[1],[2],[3, 4]].should.containDeep([3]); +[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep({a: 'a'}); +[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep({b: 'b'}); ``` -## include(obj) or contain(obj) +It does not search somewhere in depth it check all pattern in depth. It is important meaning. -Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf. Also it can assert if given object will have some sub-object. +## .match(otherValue) + +Assert given object to match `otherValue`. + +Given: String, otherValue: regexp. Uses `RegExp#exec(str)`: +```javascript +username.should.match(/^\w+$/) +``` + +Given: Array, otherValue: regexp - assert each value match to regexp. +```javascript +['a', 'b', 'c'].should.match(/[a-z]/); +['a', 'b', 'c'].should.not.match(/[d-z]/); +``` -Assert array value: +Given: Object, otherValue: regexp - assert own property's values to match regexp. ```javascript -[1,2,3].should.include(3) -[1,2,3].should.include(2) -[1,2,3].should.not.include(4) +({ a: 'foo', c: 'barfoo' }).should.match(/foo$/); +({ a: 'a' }).should.not.match(/^http/); ``` -Assert substring: + +Given: Anything, otherValue: function - assert if given value matched to function. + +Function can use .should inside or return 'true' or 'false', in all other cases it do nothing. If you return value that return assertion, you will receive better error messages. + +```javascript +(5).should.match(function(n) { return n > 0; }); +(5).should.not.match(function(n) { return n < 0; }); +(5).should.not.match(function(it) { it.should.be.an.Array; }); +(5).should.match(function(it) { return it.should.be.a.Number; }); +``` + +Now compare messages: +```javascript +(5).should.not.match(function(it) { it.should.be.a.Number; }); +//AssertionError: expected 5 not to match [Function] +(5).should.not.match(function(it) { return it.should.be.a.Number; }); +//AssertionError: expected 5 not to match [Function] +// expected 5 to be a number +``` + +Given: object, otherValue: another object - assert that object properties match to properties of another object in meaning that describe above cases. See examples: + ```javascript -'foo bar baz'.should.include('foo') -'foo bar baz'.should.include('bar') -'foo bar baz'.should.include('baz') -'foo bar baz'.should.not.include('FOO') +({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should + .match({ a: 10, b: /c$/, c: function(it) { return it.should.have.property('d', 10); }}); + +[10, 'abc', { d: 10 }, 0].should + .match({ '0': 10, '1': /c$/, '2': function(it) { return it.should.have.property('d', 10); } }); + +[10, 'abc', { d: 10 }, 0].should + .match([10, /c$/, function(it) { return it.should.have.property('d', 10); }]); ``` -Assert object includes another object: + +## .matchEach(otherValue) + +Assert given property keys and values each match given check object. + +If `otherValue` is RegExp, then each property value checked to match it: ```javascript -var tobi = { name: 'Tobi', age: 1 }; -var jane = { name: 'Jane', age: 5 }; -var user = { name: 'TJ', pet: tobi }; +(['a', 'b', 'c']).should.matchEach(/[a-c]/); +``` -user.should.include({ pet: tobi }); -user.should.include({ pet: tobi, name: 'TJ' }); -user.should.not.include({ pet: jane }); -user.should.not.include({ name: 'Someone' }); +If `otherValue` is Function, then check each property value and key matched it: +```javascript +[10, 11, 12].should.matchEach(function(it) { return it >= 10; }); +[10, 11, 12].should.matchEach(function(it) { return it >= 10; }); ``` -## includeEql(obj) -Assert that an object equal to the given `obj` is present in an Array: +In other cases it check that each property value is `.eql` to `otherValue`: ```javascript -[[1],[2],[3]].should.includeEql([3]) -[[1],[2],[3]].should.includeEql([2]) -[[1],[2],[3]].should.not.includeEql([4]) +[10, 10].should.matchEach(10); ``` -## throw() +## .throw() and throwError() Assert an exception is thrown: @@ -342,64 +505,93 @@ Assert exepection message matches regexp: }).should.throw(/^fail/); ``` -## throwError() +If you need to pass arguments and/or context to execute function use `Function#bind(context, arg1, ...)`: +```js +function isPositive(n) { + if(n <= 0) throw new Error('Given number is not positive') +} -An alias of `throw`, its purpose is to be an option for those who run -[jshint](https://github.com/jshint/node-jshint/) in strict mode. +isPositive.bind(null, 10).should.not.throw(); +isPositive.bind(null, -10).should.throw(); +``` + +If you need to check something in asynchronous function it is required to do in 2 steps: ```js -(function(){ - throw new Error('failed to baz'); -}).should.throwError(/^fail.*/); +// first we need to check that function is called +var called = false; +collection.findOne({ _id: 10 }, function(err, res) { + called = true; + + //second we test what you want + res.should.be.... +}); + +called.should.be.true; +``` + +In case you are using something like `Mocha`, you should use asynchronous test and call `done()` in proper place to make sure that you asynchronous function is called before test is finished. +```js +collection.findOne({ _id: 10 }, function(err, res) { + if(err) return done(err); + //second we test what you want + res.should.be.... + + done(); +}); ``` -## startWith(str) +In general case if you need to check that something is executed you need such thing as `spies`, good example is an [sinon](http://sinonjs.org/). -Assert that string starts with `str`. +## .status(code) +Asserts that `.statusCode` is `code`: ```javascript -'foobar'.should.startWith('foo') -'foobar'.should.not.startWith('bar') +res.should.have.status(200); ``` -## endWith(str) -Assert that string ends with `str`. +Not included in browser build. + +## .header(field[, value]) +Asserts that a `.headers` object with `field` and optional `value` are present: ```javascript -'foobar'.should.endWith('bar') -'foobar'.should.not.endWith('foo') +res.should.have.header('content-length'); +res.should.have.header('Content-Length', '123'); ``` -## keys +Not included in browser build. -Assert own object keys, which must match _exactly_, -and will fail if you omit a key or two: +## .json - var obj = { foo: 'bar', baz: 'raz' }; - obj.should.have.keys('foo', 'baz'); - obj.should.have.keys(['foo', 'baz']); +Assert that Content-Type is "application/json; charset=utf-8" -## type assertions +```javascript +res.should.be.json +``` + +Not included in browser build. + +## .html +Assert that Content-Type is "text/html; charset=utf-8" ```javascript -({}).should.be.an.Object; -(1).should.be.an.Number; -[].should.be.an.Array; -(true).should.be.a.Boolean; -''.should.be.a.String; +res.should.be.html ``` +Not included in browser build. + ## Optional Error description As it can often be difficult to ascertain exactly where failed assertions are coming from in your tests, an optional description parameter can be passed to several should matchers. The description will follow the failed assertion in the error: (1).should.eql(0, 'some useful description') - AssertionError: expected 1 to equal 0 | some useful description + AssertionError: some useful description at Object.eql (/Users/swift/code/should.js/node_modules/should/lib/should.js:280:10) ... -The methods that support this optional description are: `eql`, `equal`, `within`, `a`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`, `include`, and `includeEql`. +The methods that support this optional description are: `eql`, `equal`, `within`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`. ## Mocha example @@ -417,11 +609,23 @@ describe('mylib', function () { }); ``` -## Running tests +## Contributions + +[Actual list of contributors](https://github.com/visionmedia/should.js/graphs/contributors) if you want to show it your friends. To run the tests for _should_ simply run: $ make test + +Before contribute something: + +1. Your code looks like all other code - all project should look like it was written by one man, always. +2. If you want propose something - just create issue and describe your question with much description as you can. +3. Please never send issues or pull requests about code style, jshint violations etc - i do not accept it (and you will spend your time for free). +4. If you think you have some general receipt, consider create PR with it. +5. If you not sure if you receipt enough general just create your own plugin for should.js. (see should.use and Assertion.add usage). +6. If you add new code it should be covered by tests, no tests - no code. +7. If you find bug (or at least you think it is a bug), create issue with library version and test case that I can run and see what are you talking about, or at least full steps that I can reproduce it. ## OMG IT EXTENDS OBJECT???!?!@ @@ -429,27 +633,4 @@ Yes, yes it does, with a single getter _should_, and no it won't break your code ## License -(The MIT License) - -Copyright (c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> - -Copyright (c) 2011 Aseem Kishore <aseem.kishore@gmail.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. +MIT diff --git a/index.js b/index.js index 6dbdbde..2bb5c33 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,2 @@ -module.exports = require('./lib/should'); \ No newline at end of file +module.exports = require('./lib/node.js'); \ No newline at end of file diff --git a/lib/browser.js b/lib/browser.js new file mode 100644 index 0000000..a68bb36 --- /dev/null +++ b/lib/browser.js @@ -0,0 +1,22 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +var should = require('./should'); + +should + .use(require('./ext/assert')) + .use(require('./ext/chain')) + .use(require('./ext/bool')) + .use(require('./ext/number')) + .use(require('./ext/eql')) + .use(require('./ext/type')) + .use(require('./ext/string')) + .use(require('./ext/property')) + .use(require('./ext/error')) + .use(require('./ext/match')) + .use(require('./ext/deprecated')); + + module.exports = should; \ No newline at end of file diff --git a/lib/eql.js b/lib/eql.js index 65b3370..17bd0fd 100644 --- a/lib/eql.js +++ b/lib/eql.js @@ -1,3 +1,9 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + // Taken from node's assert module, because it sucks // and exposes next to nothing useful. var util = require('./util'); diff --git a/lib/ext/assert.js b/lib/ext/assert.js index 6c61872..91a3cc0 100644 --- a/lib/ext/assert.js +++ b/lib/ext/assert.js @@ -1,5 +1,11 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + var util = require('../util') - , assert = util.assert + , assert = require('assert') , AssertionError = assert.AssertionError; module.exports = function(should) { diff --git a/lib/ext/bool.js b/lib/ext/bool.js index 84b121c..42c73a9 100644 --- a/lib/ext/bool.js +++ b/lib/ext/bool.js @@ -1,3 +1,9 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + module.exports = function(should, Assertion) { Assertion.add('true', function() { this.is.exactly(true) @@ -6,4 +12,10 @@ module.exports = function(should, Assertion) { Assertion.add('false', function() { this.is.exactly(false) }, true); + + Assertion.add('ok', function() { + this.params = { operator: 'to be truthy' }; + + this.assert(this.obj); + }, true); }; \ No newline at end of file diff --git a/lib/ext/chain.js b/lib/ext/chain.js index 9c50f38..301c38e 100644 --- a/lib/ext/chain.js +++ b/lib/ext/chain.js @@ -1,3 +1,9 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + module.exports = function(should, Assertion) { function addLink(name) { @@ -8,5 +14,5 @@ module.exports = function(should, Assertion) { }); } - ['an', 'of', 'a', 'and', 'be', 'have', 'with', 'is'].forEach(addLink); + ['an', 'of', 'a', 'and', 'be', 'have', 'with', 'is', 'which'].forEach(addLink); }; \ No newline at end of file diff --git a/lib/ext/deprecated.js b/lib/ext/deprecated.js new file mode 100644 index 0000000..3f512fc --- /dev/null +++ b/lib/ext/deprecated.js @@ -0,0 +1,31 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +var util = require('../util'), + eql = require('../eql'); + +module.exports = function (should, Assertion) { + var i = should.format; + + Assertion.add('include', function(obj, description){ + if (!Array.isArray(this.obj) && !util.isString(this.obj)){ + this.params = { operator: 'to include an object equal to ' + i(obj), message: description }; + var cmp = {}; + for (var key in obj) cmp[key] = this.obj[key]; + this.assert(eql(cmp, obj)); + } else { + this.params = { operator: 'to include ' + i(obj), message: description }; + + this.assert(~this.obj.indexOf(obj)); + } + }) + + Assertion.add('includeEql', function(obj, description) { + this.params = { operator: 'to include an object equal to ' + i(obj), message: description }; + + this.assert(this.obj.some(function(item) { return eql(obj, item); })); + }); +} \ No newline at end of file diff --git a/lib/ext/eql.js b/lib/ext/eql.js index 3486f94..e3b03e5 100644 --- a/lib/ext/eql.js +++ b/lib/ext/eql.js @@ -1,16 +1,22 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + var eql = require('../eql'); module.exports = function(should, Assertion) { Assertion.add('eql', function(val, description) { this.params = { operator: 'to equal', expected: val, showDiff: true, message: description }; - this.assert2(eql(val, this.obj)); + this.assert(eql(val, this.obj)); }); Assertion.add('equal', function(val, description) { this.params = { operator: 'to be', expected: val, showDiff: true, message: description }; - this.assert2(val === this.obj); + this.assert(val === this.obj); }); Assertion.alias('equal', 'exactly'); diff --git a/lib/ext/error.js b/lib/ext/error.js new file mode 100644 index 0000000..93a934a --- /dev/null +++ b/lib/ext/error.js @@ -0,0 +1,51 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +module.exports = function(should, Assertion) { + var i = should.format; + + Assertion.add('throw', function(message) { + var fn = this.obj + , err = {} + , errorInfo = '' + , ok = true; + + try { + fn(); + ok = false; + } catch (e) { + err = e; + } + + if (ok) { + if ('string' == typeof message) { + ok = message == err.message; + } else if (message instanceof RegExp) { + ok = message.test(err.message); + } else if ('function' == typeof message) { + ok = err instanceof message; + } + + if (message && !ok) { + if ('string' == typeof message) { + errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'"; + } else if (message instanceof RegExp) { + errorInfo = " with a message matching " + message + ", but got '" + err.message + "'"; + } else if ('function' == typeof message) { + errorInfo = " of type " + message.name + ", but got " + err.constructor.name; + } + } else { + errorInfo = " (got "+i(err)+")"; + } + } + + this.params = { operator: 'to throw exception' + errorInfo }; + + this.assert(ok); + }); + + Assertion.alias('throw', 'throwError'); +} \ No newline at end of file diff --git a/lib/ext/http.js b/lib/ext/http.js new file mode 100644 index 0000000..47a814b --- /dev/null +++ b/lib/ext/http.js @@ -0,0 +1,34 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +//var statusCodes = require('http').STATUS_CODES; + +module.exports = function(should, Assertion) { + var i = should.format; + + Assertion.add('header', function(field, val){ + this + .have.property('headers') + .and.have.property(field.toLowerCase(), val); + }); + + Assertion.add('status', function(code) { + //this.params = { operator: 'to have response code ' + code + ' ' + i(statusCodes[code]) + // + ', but got ' + this.obj.statusCode + ' ' + i(statusCodes[this.obj.statusCode]) } + + this.have.property('statusCode', code); + }) + + Assertion.add('json', function() { + this.have.property('headers') + .and.have.property('content-type').include('application/json'); + }, true) + + Assertion.add('html', function() { + this.have.property('headers') + .and.have.property('content-type').include('text/html'); + }, true) +} \ No newline at end of file diff --git a/lib/ext/match.js b/lib/ext/match.js new file mode 100644 index 0000000..45f0e81 --- /dev/null +++ b/lib/ext/match.js @@ -0,0 +1,107 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +var util = require('../util'), + eql = require('../eql'); + +module.exports = function(should, Assertion) { + var i = should.format; + + Assertion.add('match', function(other, description) { + this.params = { operator: 'to match ' + i(other), message: description }; + + if(!eql(this.obj, other)) { + if(util.isRegExp(other)) { // something - regex + + if(util.isString(this.obj)) { + + this.assert(other.exec(this.obj)); + } else if(Array.isArray(this.obj)) { + + this.obj.forEach(function(item) { + this.assert(other.exec(item));// should we try to convert to String and exec? + }, this); + } else if(util.isObject(this.obj)) { + + var notMatchedProps = [], matchedProps = []; + util.forOwn(this.obj, function(value, name) { + if(other.exec(value)) matchedProps.push(i(name)); + else notMatchedProps.push(i(name)); + }, this); + + if(notMatchedProps.length) + this.params.operator += '\n\tnot matched properties: ' + notMatchedProps.join(', '); + if(matchedProps.length) + this.params.operator += '\n\tmatched properties: ' + matchedProps.join(', '); + + this.assert(notMatchedProps.length == 0); + } // should we try to convert to String and exec? + } else if(util.isFunction(other)) { + try { + var res = other(this.obj); + } catch(e) { + if(e instanceof should.AssertionError) { + this.params.operator += '\n\t' + e.message; + } + throw e; + } + + if(res instanceof Assertion) { + this.params.operator += '\n\t' + res.getMessage(); + } + + //if we throw exception ok - it is used .should inside + if(util.isBoolean(res)) { + this.assert(res); // if it is just boolean function assert on it + } + } else if(util.isObject(other)) { // try to match properties (for Object and Array) + var notMatchedProps = [], matchedProps = []; + + util.forOwn(other, function(value, key) { + try { + this.obj[key].should.match(value); + matchedProps.push(key); + } catch(e) { + if(e instanceof should.AssertionError) { + notMatchedProps.push(key); + } else { + throw e; + } + } + }, this); + + if(notMatchedProps.length) + this.params.operator += '\n\tnot matched properties: ' + notMatchedProps.join(', '); + if(matchedProps.length) + this.params.operator += '\n\tmatched properties: ' + matchedProps.join(', '); + + this.assert(notMatchedProps.length == 0); + } else { + this.assert(false); + } + } + }); + + Assertion.add('matchEach', function(other, description) { + this.params = { operator: 'to match each ' + i(other), message: description }; + + var f = other; + + if(util.isRegExp(other)) + f = function(it) { return !!other.exec(it); }; + else if(!util.isFunction(other)) + f = function(it) { return eql(it, other); }; + + util.forOwn(this.obj, function(value, key) { + var res = f(value, key); + + //if we throw exception ok - it is used .should inside + if(util.isBoolean(res)) { + this.assert(res); // if it is just boolean function assert on it + } + }, this); + }); +} \ No newline at end of file diff --git a/lib/ext/number.js b/lib/ext/number.js index f53c15d..ad80c4e 100644 --- a/lib/ext/number.js +++ b/lib/ext/number.js @@ -1,9 +1,15 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + module.exports = function(should, Assertion) { Assertion.add('NaN', function() { this.params = { operator: 'to be NaN' }; this.is.a.Number - .and.assert2(isNaN(this.obj)); + .and.assert(isNaN(this.obj)); }, true); Assertion.add('Infinity', function() { @@ -11,19 +17,34 @@ module.exports = function(should, Assertion) { this.is.a.Number .and.not.a.NaN - .and.assert2(!isFinite(this.obj)); + .and.assert(!isFinite(this.obj)); }, true); Assertion.add('within', function(start, finish, description){ this.params = { operator: 'to be within '+ start + '..' + finish, message: description }; - this.assert2(this.obj >= start && this.obj <= finish); + this.assert(this.obj >= start && this.obj <= finish); }); Assertion.add('approximately', function(value, delta, description) { this.params = { operator: 'to be approximately ' + value + " ±" + delta, message: description }; - this.assert2(Math.abs(this.obj - value) <= delta); + this.assert(Math.abs(this.obj - value) <= delta); + }); + + Assertion.add('above', function(n, description){ + this.params = { operator: 'to be above ' + n, message: description }; + + this.assert(this.obj > n); }); + Assertion.add('below', function(n, description){ + this.params = { operator: 'to be below ' + n, message: description }; + + this.assert(this.obj < n); + }); + + Assertion.alias('above', 'greaterThan'); + Assertion.alias('below', 'lessThan'); + }; \ No newline at end of file diff --git a/lib/ext/property.js b/lib/ext/property.js new file mode 100644 index 0000000..873fb5b --- /dev/null +++ b/lib/ext/property.js @@ -0,0 +1,215 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +var util = require('../util'), + eql = require('../eql'); + +var aSlice = Array.prototype.slice; + +module.exports = function(should, Assertion) { + var i = should.format; + + Assertion.add('property', function(name, val) { + if(arguments.length > 1) { + var p = {}; + p[name] = val; + this.have.properties(p); + } else { + this.have.properties(name); + } + this.obj = this.obj[name]; + }); + + Assertion.add('properties', function(names) { + var values = {}; + if(arguments.length > 1) { + names = aSlice.call(arguments); + } else if(!Array.isArray(names)) { + if(util.isString(names)) { + names = [names]; + } else { + values = names; + names = Object.keys(names); + } + } + + var obj = Object(this.obj), missingProperties = []; + + //just enumerate properties and check if they all present + names.forEach(function(name) { + if(!(name in obj)) missingProperties.push(i(name)); + }); + + var props = missingProperties; + if(props.length === 0) { + props = names.map(i); + } + + var operator = (props.length === 1 ? + 'to have property ' : 'to have properties ') + props.join(', '); + + this.params = { operator: operator }; + + this.assert(missingProperties.length === 0); + + // check if values in object matched expected + var valueCheckNames = Object.keys(values); + if(valueCheckNames.length) { + var wrongValues = []; + props = []; + + // now check values, as there we have all properties + valueCheckNames.forEach(function(name) { + var value = values[name]; + if(!eql(obj[name], value)) { + wrongValues.push(i(name) + ' of ' + i(value) + ' (got ' + i(obj[name]) +')'); + } else { + props.push(i(name) + ' of ' + i(value)); + } + }); + + if(wrongValues.length > 0) { + props = wrongValues; + } + + operator = (props.length === 1 ? + 'to have property ' : 'to have properties ') + props.join(', '); + + this.params = { operator: operator }; + + this.assert(wrongValues.length === 0); + } + }); + + Assertion.add('length', function(n, description){ + this.have.property('length', n, description); + }); + + Assertion.alias('lengthOf', 'length'); + + var hasOwnProperty = Object.prototype.hasOwnProperty; + + Assertion.add('ownProperty', function(name, description){ + this.params = { operator: 'to have own property ' + i(name), message: description }; + + this.assert(hasOwnProperty.call(this.obj, name)); + + this.obj = this.obj[name]; + }); + + Assertion.alias('hasOwnProperty', 'ownProperty'); + + Assertion.add('empty', function() { + this.params = { operator: 'to be empty' }; + + if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) { + this.have.property('length', 0); + } else { + var obj = Object(this.obj); // wrap to reference for booleans and numbers + for (var prop in obj) { + this.have.not.ownProperty(prop); + } + } + }, true); + + Assertion.add('keys', function(keys) { + if(arguments.length > 1) keys = aSlice.call(arguments); + else if(arguments.length === 1 && util.isString(keys)) keys = [ keys ]; + else if(arguments.length === 0) keys = []; + + var obj = Object(this.obj); + + // first check if some keys are missing + var missingKeys = []; + keys.forEach(function(key) { + if(!hasOwnProperty.call(this.obj, key)) + missingKeys.push(i(key)); + }, this); + + // second check for extra keys + var extraKeys = []; + Object.keys(obj).forEach(function(key) { + if(keys.indexOf(key) < 0) { + extraKeys.push(i(key)); + } + }); + + var verb = keys.length === 0 ? 'to be empty' : + 'to have '+(keys.length === 1 ? 'key ' : 'keys ') + + this.params = { operator: verb + keys.map(i).join(', ')}; + + if(missingKeys.length > 0) + this.params.operator += '\n\tmissing keys: ' + missingKeys.join(', '); + + if(extraKeys.length > 0) + this.params.operator += '\n\textra keys: ' + extraKeys.join(', '); + + this.assert(missingKeys.length === 0 && extraKeys.length === 0); + }); + + Assertion.alias("keys", "key"); + + Assertion.add('containEql', function(other) { + this.params = { operator: 'to contain ' + i(other) }; + var obj = this.obj; + if(Array.isArray(obj)) { + this.assert(obj.some(function(item) { + return eql(item, other); + })); + } else if(util.isString(obj)) { + // expect obj to be string + this.assert(obj.indexOf(String(other)) >= 0); + } else if(util.isObject(obj)) { + // object contains object case + Object.keys(other).forEach(function(key) { + Object(obj[key]).should.eql(other[key]); + }, this); + } else { + //other uncovered cases + this.assert(false); + } + }); + + Assertion.add('containDeep', function(other) { + this.params = { operator: 'to contain ' + i(other) }; + var obj = this.obj; + if(Array.isArray(obj)) { + if(Array.isArray(other)) { + this.assert(other.every(function(item) { + try { + obj.should.containDeep(item); + return true; + } catch(e) { + return false; + } + })); + } else { + this.assert(obj.some(function(item) { + try { + item.should.containDeep(other); + return true; + } catch (e) { + return false; + } + })); + } + } else if(util.isString(obj)) { + // expect obj to be string + this.assert(obj.indexOf(String(other)) >= 0); + } else if(util.isObject(obj)) { + // object contains object case + Object.keys(other).forEach(function(key) { + Object(obj[key]).should.containDeep(other[key]); + }, this); + } else { + //other uncovered cases + this.eql(other); + } + }); + + +} \ No newline at end of file diff --git a/lib/ext/string.js b/lib/ext/string.js new file mode 100644 index 0000000..e89eb37 --- /dev/null +++ b/lib/ext/string.js @@ -0,0 +1,19 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +module.exports = function(should, Assertion) { + Assertion.add('startWith', function(str, description) { + this.params = { operator: 'to start with ' + should.format(str), message: description }; + + this.assert(0 === this.obj.indexOf(str)); + }); + + Assertion.add('endWith', function(str, description) { + this.params = { operator: 'to end with ' + should.format(str), message: description }; + + this.assert(this.obj.indexOf(str, this.obj.length - str.length) >= 0); + }); +} \ No newline at end of file diff --git a/lib/ext/type.js b/lib/ext/type.js index 8d92917..72739eb 100644 --- a/lib/ext/type.js +++ b/lib/ext/type.js @@ -1,9 +1,72 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + var util = require('../util'); module.exports = function(should, Assertion) { Assertion.add('Number', function() { this.params = { operator: 'to be a number' }; - this.assert2(util.isNumber(this.obj)); + this.assert(util.isNumber(this.obj)); + }, true); + + Assertion.add('arguments', function() { + this.params = { operator: 'to be arguments' }; + + this.assert(util.isArguments(this.obj)); + }, true); + + Assertion.add('type', function(type, description) { + this.params = { operator: 'to have type ' + type, message: description }; + + (typeof this.obj).should.be.exactly(type, description); + }); + + Assertion.add('instanceof', function(constructor, description){ + this.params = { operator: 'to be an instance of ' + constructor.name, message: description }; + + this.assert(Object(this.obj) instanceof constructor); + }); + + Assertion.add('Function', function() { + this.params = { operator: 'to be a function' }; + + this.assert(util.isFunction(this.obj)); + }, true); + + Assertion.add('Object', function() { + this.params = { operator: 'to be an object' }; + + this.assert(util.isObject(this.obj)); }, true); + + Assertion.add('String', function() { + this.params = { operator: 'to be a string' }; + + this.assert(util.isString(this.obj)); + }, true); + + Assertion.add('Array', function() { + this.params = { operator: 'to be an array' }; + + this.assert(Array.isArray(this.obj)); + }, true); + + Assertion.add('Boolean', function() { + this.params = { operator: 'to be a boolean' }; + + this.assert(util.isBoolean(this.obj)); + }, true); + + Assertion.add('Error', function() { + this.params = { operator: 'to be an error' }; + + this.assert(util.isError(this.obj)); + }, true); + + + Assertion.alias('instanceof', 'instanceOf'); }; \ No newline at end of file diff --git a/lib/http.js b/lib/http.js deleted file mode 100644 index e7d3ee0..0000000 --- a/lib/http.js +++ /dev/null @@ -1,63 +0,0 @@ -//copy of node http module status codes -//https://github.com/joyent/node/blob/master/lib/_http_server.js - -var STATUS_CODES = exports.STATUS_CODES = { - 100 : 'Continue', - 101 : 'Switching Protocols', - 102 : 'Processing', // RFC 2518, obsoleted by RFC 4918 - 200 : 'OK', - 201 : 'Created', - 202 : 'Accepted', - 203 : 'Non-Authoritative Information', - 204 : 'No Content', - 205 : 'Reset Content', - 206 : 'Partial Content', - 207 : 'Multi-Status', // RFC 4918 - 300 : 'Multiple Choices', - 301 : 'Moved Permanently', - 302 : 'Moved Temporarily', - 303 : 'See Other', - 304 : 'Not Modified', - 305 : 'Use Proxy', - 307 : 'Temporary Redirect', - 400 : 'Bad Request', - 401 : 'Unauthorized', - 402 : 'Payment Required', - 403 : 'Forbidden', - 404 : 'Not Found', - 405 : 'Method Not Allowed', - 406 : 'Not Acceptable', - 407 : 'Proxy Authentication Required', - 408 : 'Request Time-out', - 409 : 'Conflict', - 410 : 'Gone', - 411 : 'Length Required', - 412 : 'Precondition Failed', - 413 : 'Request Entity Too Large', - 414 : 'Request-URI Too Large', - 415 : 'Unsupported Media Type', - 416 : 'Requested Range Not Satisfiable', - 417 : 'Expectation Failed', - 418 : 'I\'m a teapot', // RFC 2324 - 422 : 'Unprocessable Entity', // RFC 4918 - 423 : 'Locked', // RFC 4918 - 424 : 'Failed Dependency', // RFC 4918 - 425 : 'Unordered Collection', // RFC 4918 - 426 : 'Upgrade Required', // RFC 2817 - 428 : 'Precondition Required', // RFC 6585 - 429 : 'Too Many Requests', // RFC 6585 - 431 : 'Request Header Fields Too Large',// RFC 6585 - 500 : 'Internal Server Error', - 501 : 'Not Implemented', - 502 : 'Bad Gateway', - 503 : 'Service Unavailable', - 504 : 'Gateway Time-out', - 505 : 'HTTP Version Not Supported', - 506 : 'Variant Also Negotiates', // RFC 2295 - 507 : 'Insufficient Storage', // RFC 4918 - 509 : 'Bandwidth Limit Exceeded', - 510 : 'Not Extended', // RFC 2774 - 511 : 'Network Authentication Required' // RFC 6585 -}; - -module.exports.STATUS_CODES = STATUS_CODES; \ No newline at end of file diff --git a/lib/node.js b/lib/node.js new file mode 100644 index 0000000..be2b5fb --- /dev/null +++ b/lib/node.js @@ -0,0 +1,23 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + +var should = require('./should'); + +should + .use(require('./ext/assert')) + .use(require('./ext/chain')) + .use(require('./ext/bool')) + .use(require('./ext/number')) + .use(require('./ext/eql')) + .use(require('./ext/type')) + .use(require('./ext/string')) + .use(require('./ext/property')) + .use(require('./ext/http')) + .use(require('./ext/error')) + .use(require('./ext/match')) + .use(require('./ext/deprecated')); + + module.exports = should; \ No newline at end of file diff --git a/lib/should.js b/lib/should.js index 58371b1..3bbc0c5 100644 --- a/lib/should.js +++ b/lib/should.js @@ -1,18 +1,12 @@ /*! * Should - * Copyright(c) 2010-2012 TJ Holowaychuk + * Copyright(c) 2010-2014 TJ Holowaychuk * MIT Licensed */ -/** - * Module dependencies. - */ var util = require('./util') - , assert = util.assert - , AssertionError = assert.AssertionError - , statusCodes = require('./http').STATUS_CODES - , eql = require('./eql') + , AssertionError = util.AssertionError , inspect = util.inspect; /** @@ -35,49 +29,63 @@ var Assertion = should.Assertion = function Assertion(obj) { this.obj = obj; }; + +/** + Way to extend Assertion function. It uses some logic + to define only positive assertions and itself rule with negative assertion. + + All actions happen in subcontext and this method take care about negation. + Potentially we can add some more modifiers that does not depends from state of assertion. +*/ Assertion.add = function(name, f, isGetter) { var prop = {}; - prop[isGetter ? 'get' : 'value'] = function assertion() { + prop[isGetter ? 'get' : 'value'] = function() { var context = new Assertion(this.obj); + context.copy = context.copyIfMissing; + try { f.apply(context, arguments); } catch(e) { + //copy data from sub context to this this.copy(context); + + //check for fail if(e instanceof should.AssertionError) { + //negative fail if(this.negate) { + this.obj = context.obj; + this.negate = false; return this; } - this.assert2(false); + this.assert(false); } // throw if it is another exception throw e; } + //copy data from sub context to this this.copy(context); if(this.negate) { - this.assert2(false); + this.assert(false); } + this.obj = context.obj; + this.negate = false; return this; }; + Object.defineProperty(Assertion.prototype, name, prop); -}; +} Assertion.alias = function(from, to) { Assertion.prototype[to] = Assertion.prototype[from] }; -/*Assertion.prototype = { - constructor: Assertion -}*/ - -should.inspect = inspect; should.AssertionError = AssertionError; var i = should.format = function i(value) { if(util.isDate(value) && typeof value.inspect !== 'function') return value.toISOString(); //show millis in dates - return should.inspect(value); + return inspect(value, { depth: null }); }; - should.use = function(f) { f(this, Assertion); return this; @@ -105,55 +113,17 @@ Object.defineProperty(Object.prototype, 'should', { }); -var hasOwnProperty = Object.prototype.hasOwnProperty; - -/** - * Prototype. - */ - Assertion.prototype = { + constructor: Assertion, - /** - * Assert _expr_ with the given _msg_ and _negatedMsg_. - * - * @param {Boolean} expr - * @param {function} msg - * @param {function} negatedMsg - * @param {Object} [expected] - * @param {Boolean} [showDiff] - * @param {String} [description] - * @api private - */ - - assert: function(expr, msg, negatedMsg, expected, showDiff, description){ - msg = this.negate ? negatedMsg : msg - - var ok = this.negate ? !expr : expr; - - if (ok) return; - - var err = new AssertionError({ - message: msg.call(this) - , actual: this.obj - , expected: expected - , stackStartFunction: this.assert - }); - - err.showDiff = showDiff; - err.description = description - - throw err; - }, - - assert2: function(expr) { + assert: function(expr) { if(expr) return; var params = this.params; var msg = params.message, generatedMessage = false; if(!msg) { - msg = 'expected ' + this.inspect + (this.negate ? ' not ': ' ') + - params.operator + ('expected' in params ? ' ' + i(params.expected) : ''); + msg = this.getMessage(); generatedMessage = true; } @@ -161,665 +131,39 @@ Assertion.prototype = { message: msg , actual: this.obj , expected: params.expected - , stackStartFunction: this.assert2 + , stackStartFunction: this.assert }); err.showDiff = params.showDiff; err.operator = params.operator; err.generatedMessage = generatedMessage; - //delete this.negate; - throw err; }, - copy: function(other) { - if(!this.params) - this.params = other.params; - }, - - - /** - * Negation modifier. - * - * @api public - */ - - get not() { - this.negate = !this.negate; - return this; - }, - - /** - * Get object inspection string. - * - * @return {String} - * @api private - */ - - get inspect() { - return i(this.obj); - }, - - /** - * Assert instanceof `Arguments`. - * - * @api public - */ - - get arguments() { - this.assert( - util.isArguments(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be arguments' } - , function(){ return 'expected ' + this.inspect + ' to not be arguments' }); - return this; - }, - - /** - * Assert that object is empty. - * - * @api public - */ - - get empty() { - var length = this.obj.length; - - if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) { - this.assert( - 0 === length - , function(){ return 'expected ' + this.inspect + ' to be empty' } - , function(){ return 'expected ' + this.inspect + ' not to be empty' }); - } else { - var ok = true; - for (var prop in this.obj) { - if(hasOwnProperty.call(this.obj, prop)) { - ok = false; - break; - } - } - - this.assert( - ok - , function(){ return 'expected ' + this.inspect + ' to be empty' } - , function(){ return 'expected ' + this.inspect + ' not to be empty' }); - - } - return this; - }, - - /** - * Assert ok. - * - * @api public - */ - - get ok() { - this.assert( - this.obj - , function(){ return 'expected ' + this.inspect + ' to be truthy' } - , function(){ return 'expected ' + this.inspect + ' to be falsey' }); - return this; - }, - - /** - * Assert typeof. - * - * @param {*} type - * @param {String} description - * @api public - */ - type: function(type, description){ - this.assert( - type == typeof this.obj - , function(){ return 'expected ' + this.inspect + ' to have type ' + type + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' not to have type ' + type + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert instanceof. - * - * @param {Function} constructor - * @param {String} description - * @api public - */ - - instanceof: function(constructor, description){ - var name = constructor.name; - this.assert( - Object(this.obj) instanceof constructor - , function(){ return 'expected ' + this.inspect + ' to be an instance of ' + name + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' not to be an instance of ' + name + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert if given object is a function. - */ - get Function(){ - this.assert( - util.isFunction(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be a function' } - , function(){ return 'expected ' + this.inspect + ' not to be a function' }); - return this; - }, - - /** - * Assert given object is an object. - */ - get Object(){ - this.assert( - util.isObject(this.obj) && !Array.isArray(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be an object' } - , function(){ return 'expected ' + this.inspect + ' not to be an object' }); - return this; - }, - - /** - * Assert given object is a string - */ - get String(){ - this.assert( - util.isString(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be a string' } - , function(){ return 'expected ' + this.inspect + ' not to be a string' }); - return this; - }, - - /** - * Assert given object is an array - */ - get Array(){ - this.assert( - Array.isArray(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be an array' } - , function(){ return 'expected ' + this.inspect + ' not to be an array' }); - return this; - }, - - /** - * Assert given object is a number. NaN and Infinity are not numbers. - */ - - - /** - * Assert given object is a boolean - */ - get Boolean(){ - this.assert( - util.isBoolean(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be a boolean' } - , function(){ return 'expected ' + this.inspect + ' not to be a boolean' }); - return this; - }, - - /** - * Assert given object is an error - */ - get Error() { - this.assert( - util.isError(this.obj) - , function(){ return 'expected ' + this.inspect + ' to be an error' } - , function(){ return 'expected ' + this.inspect + ' not to be an error' }); - return this; - }, - /** - * Assert numeric value above _n_. - * - * @param {Number} n - * @param {String} description - * @api public - */ - - above: function(n, description){ - this.assert( - this.obj > n - , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; + getMessage: function() { + return 'expected ' + i(this.obj) + (this.negate ? ' not ': ' ') + + this.params.operator + ('expected' in this.params ? ' ' + i(this.params.expected) : ''); }, - /** - * Assert numeric value below _n_. - * - * @param {Number} n - * @param {String} description - * @api public - */ - - below: function(n, description){ - this.assert( - this.obj < n - , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert string value matches _regexp_. - * - * @param {RegExp} regexp - * @param {String} description - * @api public - */ - - match: function(regexp, description){ - this.assert( - regexp.exec(this.obj) - , function(){ return 'expected ' + this.inspect + ' to match ' + regexp + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' not to match ' + regexp + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert property "length" exists and has value of _n_. - * - * @param {Number} n - * @param {String} description - * @api public - */ - - length: function(n, description){ - this.obj.should.have.property('length'); - var len = this.obj.length; - this.assert( - n == len - , function(){ return 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not have a length of ' + len + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert property _name_ exists, with optional _val_. - * - * @param {String} name - * @param {*} [val] - * @param {String} description - * @api public - */ - - property: function(name, val, description){ - if (this.negate && undefined !== val) { - if (undefined === this.obj[name]) { - throw new Error(this.inspect + ' has no property ' + i(name) + (description ? " | " + description : "")); - } - } else { - this.assert( - undefined !== this.obj[name] - , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - } - - if (undefined !== val) { - this.assert( - val === this.obj[name] - , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) - + ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - } - - this.obj = this.obj[name]; - return this; - }, - /** - * Asset have given properties - * @param {Array|String ...} names - * @api public - */ - properties: function(names) { - var str - , ok = true; - - names = names instanceof Array - ? names - : Array.prototype.slice.call(arguments); - - var len = names.length; - - if (!len) throw new Error('names required'); - - // make sure they're all present - ok = names.every(function(name){ - return this.obj[name] !== undefined; - }, this); - - // key string - if (len > 1) { - names = names.map(function(name){ - return i(name); - }); - var last = names.pop(); - str = names.join(', ') + ', and ' + last; - } else { - str = i(names[0]); - } - - // message - str = 'have ' + (len > 1 ? 'properties ' : 'a property ') + str; - - this.assert( - ok - , function(){ return 'expected ' + this.inspect + ' to ' + str } - , function(){ return 'expected ' + this.inspect + ' to not ' + str }); - - return this; - }, - - /** - * Assert own property _name_ exists. - * - * @param {String} name - * @param {String} description - * @api public - */ - - ownProperty: function(name, description){ - this.assert( - hasOwnProperty.call(this.obj, name) - , function(){ return 'expected ' + this.inspect + ' to have own property ' + i(name) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not have own property ' + i(name) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - this.obj = this.obj[name]; - return this; - }, - - /** - * Assert that string starts with `str`. - * @param {String} str - * @param {String} description - * @api public - */ - - startWith: function(str, description) { - this.assert(0 === this.obj.indexOf(str) - , function() { return 'expected ' + this.inspect + ' to start with ' + i(str) + (description ? " | " + description : "") } - , function() { return 'expected ' + this.inspect + ' to not start with ' + i(str) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert that string ends with `str`. - * @param {String} str - * @param {String} description - * @api public - */ - - endWith: function(str, description) { - this.assert(-1 !== this.obj.indexOf(str, this.obj.length - str.length) - , function() { return 'expected ' + this.inspect + ' to end with ' + i(str) + (description ? " | " + description : "") } - , function() { return 'expected ' + this.inspect + ' to not end with ' + i(str) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert that `obj` is present via `.indexOf()` or that `obj` contains some sub-object. - * - * @param {*} obj - * @param {String} description - * @api public - */ - - include: function(obj, description){ - if (!Array.isArray(this.obj) && !util.isString(this.obj)){ - var cmp = {}; - for (var key in obj) cmp[key] = this.obj[key]; - this.assert( - eql(cmp, obj) - , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - } else { - this.assert( - ~this.obj.indexOf(obj) - , function(){ return 'expected ' + this.inspect + ' to include ' + i(obj) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not include ' + i(obj) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - } - return this; - }, - - /** - * Assert that an object equal to `obj` is present. - * - * @param {Array} obj - * @param {String} description - * @api public - */ - - includeEql: function(obj, description){ - this.assert( - this.obj.some(function(item) { return eql(obj, item); }) - , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } - , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } - , void 0 - , void 0 - , description); - return this; - }, - - /** - * Assert exact keys or inclusion of keys by using - * the `.include` modifier. - * - * @param {Array|String ...} keys - * @api public - */ - - keys: function(keys){ - var str - , ok = true; - - keys = keys instanceof Array - ? keys - : Array.prototype.slice.call(arguments); - - if (!keys.length) throw new Error('keys required'); - - var actual = Object.keys(this.obj) - , len = keys.length; - - // make sure they're all present - ok = keys.every(function(key){ - return ~actual.indexOf(key); - }); - - // matching length - ok = ok && keys.length == actual.length; - - // key string - if (len > 1) { - keys = keys.map(function(key){ - return i(key); - }); - var last = keys.pop(); - str = keys.join(', ') + ', and ' + last; - } else { - str = i(keys[0]); - } - - // message - str = 'have ' + (len > 1 ? 'keys ' : 'key ') + str; - - this.assert( - ok - , function(){ return 'expected ' + this.inspect + ' to ' + str } - , function(){ return 'expected ' + this.inspect + ' to not ' + str }); - - return this; - }, - - /** - * Assert that header `field` has the given `val`. - * - * @param {String} field - * @param {String} val - * @return {Assertion} for chaining - * @api public - */ - - header: function(field, val){ - this.obj.should - .have.property('headers').and - .have.property(field.toLowerCase(), val); - return this; - }, - - /** - * Assert `.statusCode` of `code`. - * - * @param {Number} code - * @return {Assertion} for chaining - * @api public - */ - - status: function(code){ - this.obj.should.have.property('statusCode'); - var status = this.obj.statusCode; - - this.assert( - code == status - , function(){ return 'expected response code of ' + code + ' ' + i(statusCodes[code]) - + ', but got ' + status + ' ' + i(statusCodes[status]) } - , function(){ return 'expected to not respond with ' + code + ' ' + i(statusCodes[code]) }); - - return this; + copy: function(other) { + this.params = other.params; }, - /** - * Assert that this response has content-type: application/json. - * - * @return {Assertion} for chaining - * @api public - */ - - get json() { - this.obj.should.have.property('headers'); - this.obj.headers.should.have.property('content-type'); - this.obj.headers['content-type'].should.include('application/json'); - return this; + copyIfMissing: function(other) { + if(!this.params) this.params = other.params; }, - /** - * Assert that this response has content-type: text/html. - * - * @return {Assertion} for chaining - * @api public - */ - - get html() { - this.obj.should.have.property('headers'); - this.obj.headers.should.have.property('content-type'); - this.obj.headers['content-type'].should.include('text/html'); - return this; - }, /** - * Assert that this function will or will not - * throw an exception. + * Negation modifier. * - * @return {Assertion} for chaining * @api public */ - throw: function(message){ - var fn = this.obj - , err = {} - , errorInfo = '' - , ok = true; - - try { - fn(); - ok = false; - } catch (e) { - err = e; - } - - if (ok) { - if ('string' == typeof message) { - ok = message == err.message; - } else if (message instanceof RegExp) { - ok = message.test(err.message); - } else if ('function' == typeof message) { - ok = err instanceof message; - } - - if (message && !ok) { - if ('string' == typeof message) { - errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'"; - } else if (message instanceof RegExp) { - errorInfo = " with a message matching " + message + ", but got '" + err.message + "'"; - } else if ('function' == typeof message) { - errorInfo = " of type " + message.name + ", but got " + err.constructor.name; - } - } - } - - this.assert( - ok - , function(){ return 'expected an exception to be thrown' + errorInfo } - , function(){ return 'expected no exception to be thrown, got "' + err.message + '"' }); - + get not() { + this.negate = !this.negate; return this; } }; -/** - * Aliases. - */ - -(function alias(name, as){ - Assertion.prototype[as] = Assertion.prototype[name]; - return alias; -}) -('instanceof', 'instanceOf') -('throw', 'throwError') -('length', 'lengthOf') -('keys', 'key') -('ownProperty', 'haveOwnProperty') -('above', 'greaterThan') -('below', 'lessThan') -('include', 'contain'); - -should - .use(require('./ext/assert')) - .use(require('./ext/chain')) - .use(require('./ext/bool')) - .use(require('./ext/number')) - .use(require('./ext/eql')) - .use(require('./ext/type')) \ No newline at end of file diff --git a/lib/util.js b/lib/util.js index 6b825cc..9b8d7a3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,3 +1,9 @@ +/*! + * Should + * Copyright(c) 2010-2014 TJ Holowaychuk + * MIT Licensed + */ + /** * Check if given obj just a primitive type wrapper * @param {Object} obj @@ -100,4 +106,14 @@ exports.isError = isError; exports.inspect = require('util').inspect; -exports.assert = require('assert'); \ No newline at end of file +exports.AssertionError = require('assert').AssertionError; + +var hasOwnProperty = Object.prototype.hasOwnProperty; + +exports.forOwn = function(obj, f, context) { + for(var prop in obj) { + if(hasOwnProperty.call(obj, prop)) { + f.call(context, obj[prop], prop); + } + } +}; \ No newline at end of file diff --git a/package.json b/package.json index 73c5057..325d0e9 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,14 @@ { "name": "should" , "description": "test framework agnostic BDD-style assertions" - , "version": "2.1.1" + , "version": "3.0.0" , "author": "TJ Holowaychuk " , "repository": { "type": "git", "url": "git://github.com/visionmedia/should.js.git" } , "homepage": "https://github.com/visionmedia/should.js" - , "contributors": [ "Aseem Kishore " ] , "scripts": { "test": "make test" } , "devDependencies": { "mocha": "*" } , "keywords": ["test", "bdd", "assert"] - , "main": "./lib/should.js" + , "main": "./lib/node.js" , "engines": { "node": ">= 0.2.0" } } diff --git a/test/ext/bool.test.js b/test/ext/bool.test.js index 1ef5a51..9230756 100644 --- a/test/ext/bool.test.js +++ b/test/ext/bool.test.js @@ -28,5 +28,20 @@ module.exports = { err(function(){ false.should.not.be.false; }, "expected false not to be false") - } + }, + + '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; + }, "expected 'test' not to be truthy"); + }, } \ No newline at end of file diff --git a/test/ext/deprecated.test.js b/test/ext/deprecated.test.js new file mode 100644 index 0000000..c8436c4 --- /dev/null +++ b/test/ext/deprecated.test.js @@ -0,0 +1,79 @@ +var err = require('../util').err; +var should = require('../../'); + +module.exports = { + 'test include() with string': function(){ + 'foobar'.should.include('bar'); + 'foobar'.should.include('foo'); + 'foobar'.should.not.include('baz'); + + err(function(){ + 'foobar'.should.include('baz'); + }, "expected 'foobar' to include 'baz'"); + + err(function(){ + 'foobar'.should.not.include('bar'); + }, "expected 'foobar' not to include 'bar'"); + + err(function(){ + 'foobar'.should.include('baz', 'foo'); + }, "foo"); + }, + + 'test include() with array': function(){ + ['foo', 'bar'].should.include('foo'); + ['foo', 'bar'].should.include('foo'); + ['foo', 'bar'].should.include('bar'); + [1,2].should.include(1); + ['foo', 'bar'].should.not.include('baz'); + ['foo', 'bar'].should.not.include(1); + + err(function(){ + ['foo'].should.include('bar'); + }, "expected [ 'foo' ] to include 'bar'"); + + err(function(){ + ['bar', 'foo'].should.not.include('foo'); + }, "expected [ 'bar', 'foo' ] not to include 'foo'"); + + err(function(){ + ['bar', 'foo'].should.not.include('foo', 'foo'); + }, "foo"); + }, + + 'test include() with object': function(){ + var tobi = { name: 'Tobi', age: 2 }; + var jane = { name: 'Jane', age: 2 }; + + var user = { name: 'TJ', pet: tobi, age: 24 }; + + user.should.include({ pet: tobi }); + user.should.include({ pet: tobi, name: 'TJ' }); + user.should.not.include({ pet: tobi, name: 'Someone else' }); + user.should.not.include({ pet: jane }); + user.should.not.include({ pet: jane, name: 'TJ' }); + + err(function(){ + user.should.include({ pet: { name: 'Luna' } }); + }, "expected { name: 'TJ', pet: { name: 'Tobi', age: 2 }, age: 24 } to include an object equal to { pet: { name: 'Luna' } }"); + }, + + 'test includeEql() with array': function(){ + [['foo'], ['bar']].should.includeEql(['foo']); + [['foo'], ['bar']].should.includeEql(['bar']); + [['foo'], ['bar']].should.not.includeEql(['baz']); + [].should.not.includeEql(['baz']); + + err(function(){ + [['foo']].should.includeEql(['bar']); + }, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ]"); + + err(function(){ + [['foo']].should.not.includeEql(['foo']); + }, "expected [ [ 'foo' ] ] not to include an object equal to [ 'foo' ]"); + + err(function(){ + [['foo']].should.not.includeEql(['foo'], 'foo'); + }, "foo"); + }, +} \ No newline at end of file diff --git a/test/ext/eql.test.js b/test/ext/eql.test.js new file mode 100644 index 0000000..ce992d1 --- /dev/null +++ b/test/ext/eql.test.js @@ -0,0 +1,43 @@ +var err = require('../util').err, + should = require('../../'); + +module.exports = { + 'test eql(val)': function(){ + 'test'.should.eql('test'); + ({ foo: 'bar' }).should.eql({ foo: 'bar' }); + (1).should.eql(1); + '4'.should.eql(4); + var memo = []; + function memorize() { + memo.push(arguments); + } + memorize('a', [1, 2]); + memorize('a', [1, 2]); + memo[0].should.eql(memo[1]); + + err(function(){ + (4).should.eql(3); + }, 'expected 4 to equal 3'); + }, + + 'test equal(val)': function(){ + 'test'.should.equal('test'); + (1).should.equal(1); + + err(function(){ + (4).should.equal(3); + }, 'expected 4 to be 3'); + + err(function(){ + '4'.should.equal(4); + }, "expected '4' to be 4"); + + var date = new Date; + date.should.equal(date); + }, + + 'test .equal()': function(){ + var foo; + should.equal(undefined, foo); + } +} \ No newline at end of file diff --git a/test/ext/error.test.js b/test/ext/error.test.js new file mode 100644 index 0000000..deed208 --- /dev/null +++ b/test/ext/error.test.js @@ -0,0 +1,106 @@ +var err = require('../util').err, + should = require('../../'); + +module.exports = { + 'test throw()': function(){ + (function(){}).should.not.throw(); + (function(){ throw new Error('fail') }).should.throw(); + + err(function(){ + (function(){}).should.throw(); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ + throw new Error('fail'); + }).should.not.throw(); + }, 'expected [Function] not to throw exception (got [Error: fail])'); + }, + + 'test throw() with regex message': function(){ + (function(){ throw new Error('fail'); }).should.throw(/fail/); + + err(function(){ + (function(){}).should.throw(/fail/); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw new Error('error'); }).should.throw(/fail/); + }, "expected [Function] to throw exception with a message matching /fail/, but got 'error'"); + }, + + 'test throw() with string message': function(){ + (function(){ throw new Error('fail'); }).should.throw('fail'); + + err(function(){ + (function(){}).should.throw('fail'); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw new Error('error'); }).should.throw('fail'); + }, "expected [Function] to throw exception with a message matching 'fail', but got 'error'"); + }, + + 'test throw() with type': function(){ + (function(){ throw new Error('fail'); }).should.throw(Error); + + err(function(){ + (function(){}).should.throw(Error); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw 'error'; }).should.throw(Error); + }, "expected [Function] to throw exception of type Error, but got String"); + }, + + 'test throwError()': function(){ + (function(){}).should.not.throwError(); + (function(){ throw new Error('fail') }).should.throwError(); + + err(function(){ + (function(){}).should.throwError(); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ + throw new Error('fail'); + }).should.not.throwError(); + }, 'expected [Function] not to throw exception (got [Error: fail])'); + }, + + 'test throwError() with regex message': function(){ + (function(){ throw new Error('fail'); }).should.throwError(/fail/); + + err(function(){ + (function(){}).should.throwError(/fail/); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw new Error('error'); }).should.throwError(/fail/); + }, "expected [Function] to throw exception with a message matching /fail/, but got 'error'"); + }, + + 'test throwError() with string message': function(){ + (function(){ throw new Error('fail'); }).should.throwError('fail'); + + err(function(){ + (function(){}).should.throwError('fail'); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw new Error('error'); }).should.throwError('fail'); + }, "expected [Function] to throw exception with a message matching 'fail', but got 'error'"); + }, + + 'test throwError() with type': function(){ + (function(){ throw new Error('fail'); }).should.throw(Error); + + err(function(){ + (function(){}).should.throw(Error); + }, 'expected [Function] to throw exception'); + + err(function(){ + (function(){ throw 'error'; }).should.throw(Error); + }, "expected [Function] to throw exception of type Error, but got String"); + }, +} \ No newline at end of file diff --git a/test/ext/http.test.js b/test/ext/http.test.js new file mode 100644 index 0000000..0d3670a --- /dev/null +++ b/test/ext/http.test.js @@ -0,0 +1,38 @@ +var err = require('../util').err; +var should = require('../../'); + +module.exports = { + 'test .json': function(){ + var req = { + headers: { + 'content-type': 'application/json' + } + }; + + req.should.be.json; + + req = { + headers: { + 'content-type': 'application/json; charset=utf-8' + } + }; + + req.should.be.json; + + req = { + headers: { + 'content-type': 'text/html' + } + }; + + req.should.not.be.json; + + ({}).should.not.be.json; + }, + + 'test .status': function() { + ({ statusCode: 300 }).should.have.not.status(200); + + ({ statusCode: 200 }).should.have.status(200); + } +} \ No newline at end of file diff --git a/test/ext/match.test.js b/test/ext/match.test.js new file mode 100644 index 0000000..b155cbd --- /dev/null +++ b/test/ext/match.test.js @@ -0,0 +1,120 @@ +var err = require('../util').err; +var should = require('../../'); + +module.exports = { + + 'test string match(regexp)': function(){ + 'foobar'.should.match(/^foo/) + 'foobar'.should.not.match(/^bar/) + + err(function(){ + 'foobar'.should.match(/^bar/i) + }, "expected 'foobar' to match /^bar/i"); + + err(function(){ + 'foobar'.should.not.match(/^foo/i) + }, "expected 'foobar' not to match /^foo/i"); + + err(function(){ + 'foobar'.should.match(/^bar/i, 'foo') + }, "foo"); + + err(function(){ + 'foobar'.should.not.match(/^foo/i, 'foo') + }, "foo"); + }, + + 'test object match(regexp)': function() { + ({ a: 'foo', c: 'barfoo' }).should.match(/foo$/); + + ({ a: 'a' }).should.not.match(/^http/); + + // positive false + err(function() { + ({ a: 'foo', c: 'barfoo' }).should.not.match(/foo$/); + }, "expected { a: 'foo', c: 'barfoo' } not to match /foo$/\n\tmatched properties: 'a', 'c'"); + + // negative true + err(function() { + ({ a: 'foo', c: 'barfoo' }).should.match(/^foo$/); + }, "expected { a: 'foo', c: 'barfoo' } to match /^foo$/\n\tnot matched properties: 'c'\n\tmatched properties: 'a'"); + }, + + 'test array match(regexp)': function() { + ['a', 'b', 'c'].should.match(/[a-z]/); + ['a', 'b', 'c'].should.not.match(/[d-z]/); + + err(function() { + ['a', 'b', 'c'].should.not.match(/[a-z]/); + }, "expected [ 'a', 'b', 'c' ] not to match /[a-z]/"); + + err(function() { + ['a', 'b', 'c'].should.match(/[d-z]/); + }, "expected [ 'a', 'b', 'c' ] to match /[d-z]/"); + }, + + 'test match(function)': function() { + (5).should.match(function(n) { return n > 0; }); + + (5).should.not.match(function(n) { return n < 0; }); + + (5).should.not.match(function(it) { it.should.be.an.Array; }); + + (5).should.match(function(it) { it.should.be.a.Number; }); + + err(function() { + (5).should.match(function(n) { return n < 0; }); + }, "expected 5 to match [Function]"); + + err(function() { + (5).should.match(function(it) { it.should.be.an.Array; }); + }, "expected 5 to match [Function]\n\texpected 5 to be an array"); + + err(function() { + (5).should.not.match(function(it) { return it.should.be.a.Number; }); + }, "expected 5 not to match [Function]\n\texpected 5 to be a number"); + + err(function() { + (5).should.not.match(function(n) { return n > 0; }); + }, "expected 5 not to match [Function]"); + }, + + 'test match(object)': function() { + ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should + .match({ a: 10, b: /c$/, c: function(it) { return it.should.have.property('d', 10); }}); + + [10, 'abc', { d: 10 }, 0].should + .match({ '0': 10, '1': /c$/, '2': function(it) { return it.should.have.property('d', 10); } }); + + [10, 'abc', { d: 10 }, 0].should + .match([10, /c$/, function(it) { return it.should.have.property('d', 10); }]); + + err(function() { + ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should + .match({ a: 11, b: /c$/, c: function(it) { return it.should.have.property('d', 10); }}); + }, "expected { a: 10, b: 'abc', c: { d: 10 }, d: 0 } to match { a: 11, b: /c$/, c: [Function] }\n\tnot matched properties: a\n\tmatched properties: b, c"); + + err(function() { + ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should.not + .match({ a: 10, b: /c$/, c: function(it) { return it.should.have.property('d', 10); }}); + }, "expected { a: 10, b: 'abc', c: { d: 10 }, d: 0 } not to match { a: 10, b: /c$/, c: [Function] }\n\tmatched properties: a, b, c"); + }, + + 'test each property match(function)': function() { + [10, 11, 12].should.matchEach(function(it) { return it >= 10; }); + + [10, 10].should.matchEach(10); + + ({ a: 10, b: 11, c: 12}).should.matchEach(function(value, key) { value.should.be.a.Number; }); + + (['a', 'b', 'c']).should.matchEach(/[a-c]/); + + err(function() { + (['a', 'b', 'c']).should.not.matchEach(/[a-c]/); + }, "expected [ 'a', 'b', 'c' ] not to match each /[a-c]/"); + + err(function() { + [10, 11].should.matchEach(10); + }, "expected [ 10, 11 ] to match each 10"); + } +} \ No newline at end of file diff --git a/test/ext/number.test.js b/test/ext/number.test.js index ed4ef50..3eae3d6 100644 --- a/test/ext/number.test.js +++ b/test/ext/number.test.js @@ -91,15 +91,15 @@ module.exports = { err(function(){ (10).should.not.be.above(6); - }, "expected 10 to be below 6"); + }, "expected 10 not to be above 6"); err(function(){ (5).should.be.above(6, 'foo'); - }, "expected 5 to be above 6 | foo"); + }, "foo"); err(function(){ (10).should.not.be.above(6, 'foo'); - }, "expected 10 to be below 6 | foo"); + }, "foo"); }, 'test below(n)': function(){ @@ -114,14 +114,14 @@ module.exports = { err(function(){ (6).should.not.be.below(10); - }, "expected 6 to be above 10"); + }, "expected 6 not to be below 10"); err(function(){ (6).should.be.below(5, 'foo'); - }, "expected 6 to be below 5 | foo"); + }, "foo"); err(function(){ (6).should.not.be.below(10, 'foo'); - }, "expected 6 to be above 10 | foo"); + }, "foo"); } } \ No newline at end of file diff --git a/test/ext/property.test.js b/test/ext/property.test.js new file mode 100644 index 0000000..e01fa29 --- /dev/null +++ b/test/ext/property.test.js @@ -0,0 +1,189 @@ +var err = require('../util').err; +var should = require('../../'); + +module.exports = { + '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 property 'foo'"); + }, + + 'test property(name, val)': function(){ + 'test'.should.have.property('length', 4); + 'asd'.should.have.property('constructor', String); + + err(function(){ + 'asd'.should.have.property('length', 4); + }, "expected 'asd' to have property 'length' of 4 (got 3)"); + + err(function(){ + 'asd'.should.not.have.property('length', 3); + }, "expected 'asd' not to have property 'length' of 3"); + + err(function(){ + 'asd'.should.have.property('constructor', Number); + }, "expected 'asd' to have property 'constructor' of [Function: Number] (got [Function: String])"); + }, + + 'test length(n)': function(){ + 'test'.should.have.length(4); + 'test'.should.not.have.length(3); + [1,2,3].should.have.length(3); + ({ length: 10}).should.have.length(10); + + err(function(){ + (4).should.have.length(3); + }, "expected 4 to have property 'length'"); + + err(function(){ + 'asd'.should.not.have.length(3); + }, "expected 'asd' not to have property 'length' of 3"); + + }, + + 'test ownProperty(name)': function(){ + 'test'.should.have.ownProperty('length'); + ({ length: 12 }).should.have.ownProperty('length'); + + err(function(){ + ({ length: 12 }).should.not.have.ownProperty('length'); + }, "expected { length: 12 } not to have own property 'length'"); + + err(function(){ + ({ length: 12 }).should.not.have.ownProperty('length', 'foo'); + }, "foo"); + + err(function(){ + ({ length: 12 }).should.have.ownProperty('foo', 'foo'); + }, "foo"); + }, + + 'test ownProperty(name).equal(val)': function() { + ({length: 10}).should.have.ownProperty('length').equal(10); + }, + + 'test properties(name1, name2, ...)': function(){ + 'test'.should.have.properties('length', 'indexOf'); + (4).should.not.have.properties('length'); + + err(function(){ + 'asd'.should.have.properties('foo'); + }, "expected 'asd' to have property 'foo'"); + + err(function(){ + 'asd'.should.not.have.properties('length', 'indexOf'); + }, "expected 'asd' not to have properties 'length', 'indexOf'"); + }, + + 'test properties([names])': function(){ + 'test'.should.have.properties(['length', 'indexOf']); + (4).should.not.have.properties(['length']); + + err(function(){ + 'asd'.should.have.properties(['foo']); + }, "expected 'asd' to have property 'foo'"); + }, + + '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'); + ({}).should.have.keys(); + ({}).should.have.keys([]); + + err(function(){ + ({ foo: 1 }).should.have.keys(['bar']); + }, "expected { foo: 1 } to have key 'bar'\n\tmissing keys: 'bar'\n\textra keys: 'foo'"); + + err(function(){ + ({ foo: 1 }).should.have.keys(['bar', 'baz']); + }, "expected { foo: 1 } to have keys 'bar', 'baz'\n\tmissing keys: 'bar', 'baz'\n\textra keys: 'foo'"); + + err(function(){ + ({ foo: 1 }).should.not.have.keys('foo'); + }, "expected { foo: 1 } not to have key 'foo'"); + + err(function(){ + ({ foo: 1 }).should.not.have.keys(['foo']); + }, "expected { foo: 1 } not to have key 'foo'"); + + err(function(){ + ({ foo: 1, bar: 2 }).should.not.have.keys(['foo', 'bar']); + }, "expected { foo: 1, bar: 2 } not to have keys 'foo', 'bar'"); + }, + + 'test empty': function(){ + ''.should.be.empty; + [].should.be.empty; + ({}).should.be.empty; + ({ length: 10 }).should.not.be.empty; + + (function() { + arguments.should.be.empty; + })(); + + err(function(){ + ({}).should.not.be.empty; + }, 'expected {} not to be empty'); + + err(function(){ + ({ length: 10 }).should.be.empty; + }, 'expected { length: 10 } to be empty'); + + err(function(){ + 'asd'.should.be.empty; + }, "expected 'asd' to be empty"); + + err(function(){ + ''.should.not.be.empty; + }, "expected '' not to be empty"); + }, + + 'test containEql': function() { + 'hello boy'.should.containEql('boy'); + [1,2,3].should.containEql(3); + [[1],[2],[3]].should.containEql([3]); + [[1],[2],[3, 4]].should.not.containEql([3]); + [{a: 'a'}, {b: 'b', c: 'c'}].should.containEql({a: 'a'}); + [{a: 'a'}, {b: 'b', c: 'c'}].should.not.containEql({b: 'b'}); + + ({ b: 10 }).should.containEql({ b: 10 }); + [1, 2, 3].should.containEql(1); + ([1, 2, { a: 10 }]).should.containEql({ a: 10 }); + [1, 2, 3].should.not.containEql({ a: 1 }); + + err(function() { + [1,2,3].should.not.containEql(3); + }, "expected [ 1, 2, 3 ] not to contain 3"); + + err(function() { + [1,2,3].should.containEql(4); + }, "expected [ 1, 2, 3 ] to contain 4"); + }, + + 'test containDeep': function() { + 'hello boy'.should.containDeep('boy'); + [1,2,3].should.containDeep(3); + + ({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should + .containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}); + + [1, 2, 3, { a: { b: { d: 12 }}}].should.containDeep({ a: { b: {d: 12}}}); + + [[1],[2],[3]].should.containDeep([3]); + [[1],[2],[3, 4]].should.containDeep([3]); + [{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep({a: 'a'}); + [{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep({b: 'b'}); + + err(function() { + 'hello boy'.should.not.containDeep('boy'); + }, "expected 'hello boy' not to contain 'boy'"); + + err(function() { + [{a: 'a'}, {b: 'b', c: 'c'}].should.not.containDeep({b: 'b'}); + }, "expected [ { a: 'a' }, { b: 'b', c: 'c' } ] not to contain { b: 'b' }"); + } +} \ No newline at end of file diff --git a/test/ext/string.test.js b/test/ext/string.test.js new file mode 100644 index 0000000..72635c2 --- /dev/null +++ b/test/ext/string.test.js @@ -0,0 +1,47 @@ +var err = require('../util').err; +var should = require('../../'); + +module.exports = { + 'test startWith()': function() { + 'foobar'.should.startWith('foo'); + 'foobar'.should.not.startWith('bar'); + + err(function() { + 'foobar'.should.startWith('bar'); + }, "expected 'foobar' to start with 'bar'"); + + err(function() { + 'foobar'.should.not.startWith('foo'); + }, "expected 'foobar' not to start with 'foo'"); + + err(function() { + 'foobar'.should.startWith('bar', 'baz'); + }, "baz"); + + err(function() { + 'foobar'.should.not.startWith('foo', 'baz'); + }, "baz"); + }, + + 'test endWith()': function() { + 'foobar'.should.endWith('bar'); + 'foobar'.should.not.endWith('foo'); + + err(function() { + 'foobar'.should.endWith('foo'); + }, "expected 'foobar' to end with 'foo'"); + + err(function() { + 'foobar'.should.not.endWith('bar'); + }, "expected 'foobar' not to end with 'bar'"); + + err(function() { + 'foobar'.should.endWith('foo', 'baz'); + }, "baz"); + + err(function() { + 'foobar'.should.not.endWith('bar', 'baz'); + }, "baz"); + }, + +} \ No newline at end of file diff --git a/test/ext/type.test.js b/test/ext/type.test.js new file mode 100644 index 0000000..4987e01 --- /dev/null +++ b/test/ext/type.test.js @@ -0,0 +1,192 @@ +var err = require('../util').err, + should = require('../../'); + +module.exports = { + 'test arguments': function(){ + var args = (function(){ return arguments; })(1,2,3); + args.should.be.arguments; + [].should.not.be.arguments; + + err(function() { + ((function(){ return arguments; })(1,2,3)).should.not.be.arguments; + }, "expected { '0': 1, '1': 2, '2': 3 } not to be arguments"); + + err(function() { + ({}).should.be.arguments; + }, "expected {} to be arguments"); + }, + + 'test typeof': function(){ + 'test'.should.have.type('string'); + + err(function(){ + 'test'.should.not.have.type('string'); + }, "expected 'test' not to have type string"); + + err(function(){ + 'test'.should.not.have.type('string', 'foo'); + }, "foo"); + + err(function(){ + (10).should.have.type('string'); + }, "expected 10 to have type string"); + + (5).should.have.type('number'); + + err(function(){ + (5).should.not.have.type('number'); + }, "expected 5 not to have type number"); + + err(function(){ + (5).should.not.have.type('number', 'foo'); + }, "foo"); + }, + + 'test instanceof': function(){ + function Foo(){} + new Foo().should.be.an.instanceof(Foo); + + new Date().should.be.an.instanceof(Date); + + var tobi = { name: 'Tobi', age: 2 }; + tobi.should.be.an.instanceof(Object); + + var getSomething = function() {return "something"}; + getSomething.should.be.an.instanceof(Function); + + var number = Object(5); + (number instanceof Number).should.be.true; + number.should.be.an.instanceof(Number); + + var boolean = Object(true); + (boolean instanceof Boolean).should.be.true; + boolean.should.be.an.instanceof(Boolean); + + var string = Object('string'); + (string instanceof String).should.be.true; + string.should.be.an.instanceof(String); + + err(function(){ + (3).should.an.instanceof(Foo); + }, "expected 3 to be an instance of Foo"); + + err(function(){ + (3).should.an.instanceof(Foo, 'foo'); + }, "foo"); + + err(function(){ + ({}).should.not.be.an.instanceof(Object); + }, "expected {} not to be an instance of Object"); + }, + + 'test instanceOf (non-reserved)': function(){ + function Foo(){} + new Foo().should.be.an.instanceOf(Foo); + + new Date().should.be.an.instanceOf(Date); + + var tobi = { name: 'Tobi', age: 2 }; + tobi.should.be.an.instanceOf(Object); + + var getSomething = function() {return "something"}; + getSomething.should.be.an.instanceOf(Function); + + err(function(){ + (9).should.an.instanceOf(Foo); + }, "expected 9 to be an instance of Foo"); + + err(function(){ + (9).should.an.instanceOf(Foo, 'foo'); + }, "foo"); + + function Foo2(){} + Foo2.prototype.valueOf = function (){ return 'foo'; }; + new Foo2().should.be.an.instanceOf(Foo2); + }, + + 'test Function': function() { + var f = function() {}; + f.should.be.a.Function; + + Object.should.be.a.Function; + + Function.should.be.a.Function; + + (new Function("1 * 1")).should.be.a.Function; + + err(function() { + (1).should.be.a.Function; + }, "expected 1 to be a function"); + }, + + 'test Object': function() { + ({}).should.be.an.Object; + Function.should.not.be.an.Object; + + (new Object()).should.be.an.Object; + (new Date()).should.be.an.Object; + + err(function() { + (1).should.be.an.Object; + }, 'expected 1 to be an object'); + }, + + 'test String': function() { + ''.should.be.a.String; + ({}).should.not.be.a.String; + (0).should.not.be.a.String; + + (new String("")).should.be.a.String; + + err(function() { + (1).should.be.a.String + }, 'expected 1 to be a string'); + }, + + 'test Array': function() { + [].should.be.an.Array; + (new Array(10)).should.be.an.Array; + + ''.should.not.be.Array; + (1).should.not.be.Array; + + err(function() { + [].should.not.be.Array + }, 'expected [] not to be an array'); + }, + + 'test Number': function() { + (1).should.be.a.Number; + (new Number(10)).should.be.a.Number; + + NaN.should.be.a.Number; + Infinity.should.be.a.Number; + + ({}).should.not.be.a.Number; + + err(function() { + ([]).should.be.a.Number; + }, 'expected [] to be a number'); + }, + 'test Boolean': function() { + (true).should.be.a.Boolean; + (false).should.be.a.Boolean; + + (new Boolean(false)).should.be.a.Boolean; + + ({}).should.not.be.a.Boolean; + + err(function() { + [].should.be.a.Boolean + }, 'expected [] to be a boolean'); + }, + 'test Error': function() { + (new Error()).should.be.an.Error; + + ({}).should.not.be.Error; + + err(function() { + [].should.be.an.Error + }, 'expected [] to be an error'); + }, +} \ No newline at end of file diff --git a/test/should.test.js b/test/should.test.js index bde1890..07b0614 100644 --- a/test/should.test.js +++ b/test/should.test.js @@ -24,24 +24,7 @@ module.exports = { 'test'.should.be.a.string; should.equal('foo', 'foo'); }, - - - - '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; - }, "expected 'test' to be falsey"); - }, - + 'test .expected and .actual': function(){ try { 'foo'.should.equal('bar'); @@ -50,716 +33,14 @@ module.exports = { assert('bar' == err.expected, 'err.expected'); } }, - - 'test arguments': function(){ - var args = (function(){ return arguments; })(1,2,3); - args.should.be.arguments; - [].should.not.be.arguments; - - err(function() { - ((function(){ return arguments; })(1,2,3)).should.not.be.arguments; - }, "expected { '0': 1, '1': 2, '2': 3 } to not be arguments"); - - err(function() { - ({}).should.be.arguments; - }, "expected {} to be arguments"); - }, - - 'test .equal()': function(){ - var foo; - should.equal(undefined, foo); - }, - - 'test typeof': function(){ - 'test'.should.have.type('string'); - - err(function(){ - 'test'.should.not.have.type('string'); - }, "expected 'test' not to have type string"); - - err(function(){ - 'test'.should.not.have.type('string', 'foo'); - }, "expected 'test' not to have type string | foo"); - - err(function(){ - (10).should.have.type('string'); - }, "expected 10 to have type string"); - - (5).should.have.type('number'); - - err(function(){ - (5).should.not.have.type('number'); - }, "expected 5 not to have type number"); - - err(function(){ - (5).should.not.have.type('number', 'foo'); - }, "expected 5 not to have type number | foo"); - }, - - 'test instanceof': function(){ - function Foo(){} - new Foo().should.be.an.instanceof(Foo); - - new Date().should.be.an.instanceof(Date); - - var tobi = { name: 'Tobi', age: 2 }; - tobi.should.be.an.instanceof(Object); - - var getSomething = function() {return "something"}; - getSomething.should.be.an.instanceof(Function); - - var number = Object(5); - (number instanceof Number).should.be.true; - number.should.be.an.instanceof(Number); - - var boolean = Object(true); - (boolean instanceof Boolean).should.be.true; - boolean.should.be.an.instanceof(Boolean); - - var string = Object('string'); - (string instanceof String).should.be.true; - string.should.be.an.instanceof(String); - - err(function(){ - (3).should.an.instanceof(Foo); - }, "expected 3 to be an instance of Foo"); - - err(function(){ - (3).should.an.instanceof(Foo, 'foo'); - }, "expected 3 to be an instance of Foo | foo"); - - err(function(){ - ({}).should.not.be.an.instanceof(Object); - }, "expected {} not to be an instance of Object"); - }, - - 'test instanceOf (non-reserved)': function(){ - function Foo(){} - new Foo().should.be.an.instanceOf(Foo); - - new Date().should.be.an.instanceOf(Date); - - var tobi = { name: 'Tobi', age: 2 }; - tobi.should.be.an.instanceOf(Object); - - var getSomething = function() {return "something"}; - getSomething.should.be.an.instanceOf(Function); - - err(function(){ - (9).should.an.instanceOf(Foo); - }, "expected 9 to be an instance of Foo"); - - err(function(){ - (9).should.an.instanceOf(Foo, 'foo'); - }, "expected 9 to be an instance of Foo | foo"); - - function Foo2(){} - Foo2.prototype.valueOf = function (){ return 'foo'; }; - new Foo2().should.be.an.instanceOf(Foo2); - }, - - 'test Function': function() { - var f = function() {}; - f.should.be.a.Function; - - Object.should.be.a.Function; - - Function.should.be.a.Function; - - (new Function("1 * 1")).should.be.a.Function; - - err(function() { - (1).should.be.a.Function; - }, "expected 1 to be a function"); - }, - - 'test Object': function() { - [].should.not.be.an.Object; - ({}).should.be.an.Object; - Function.should.not.be.an.Object; - - (new Object()).should.be.an.Object; - (new Date()).should.be.an.Object; - - err(function() { - (1).should.be.an.Object; - }, 'expected 1 to be an object'); - }, - - 'test String': function() { - ''.should.be.a.String; - ({}).should.not.be.a.String; - (0).should.not.be.a.String; - - (new String("")).should.be.a.String; - - err(function() { - (1).should.be.a.String - }, 'expected 1 to be a string'); - }, - - 'test Array': function() { - [].should.be.an.Array; - (new Array(10)).should.be.an.Array; - - ''.should.not.be.Array; - (1).should.not.be.Array; - - err(function() { - [].should.not.be.Array - }, 'expected [] not to be an array'); - }, - - 'test Number': function() { - (1).should.be.a.Number; - (new Number(10)).should.be.a.Number; - - NaN.should.be.a.Number; - Infinity.should.be.a.Number; - - ({}).should.not.be.a.Number; - - err(function() { - ([]).should.be.a.Number; - }, 'expected [] to be a number'); - }, - 'test Boolean': function() { - (true).should.be.a.Boolean; - (false).should.be.a.Boolean; - - (new Boolean(false)).should.be.a.Boolean; - - ({}).should.not.be.a.Boolean; - - err(function() { - [].should.be.a.Boolean - }, 'expected [] to be a boolean'); - }, - 'test Error': function() { - (new Error()).should.be.an.Error; - - ({}).should.not.be.Error; - - err(function() { - [].should.be.an.Error - }, 'expected [] to be an error'); - }, - - 'test match(regexp)': function(){ - 'foobar'.should.match(/^foo/) - 'foobar'.should.not.match(/^bar/) - - err(function(){ - 'foobar'.should.match(/^bar/i) - }, "expected 'foobar' to match /^bar/i"); - - err(function(){ - 'foobar'.should.not.match(/^foo/i) - }, "expected 'foobar' not to match /^foo/i"); - - err(function(){ - 'foobar'.should.match(/^bar/i, 'foo') - }, "expected 'foobar' to match /^bar/i | foo"); - - err(function(){ - 'foobar'.should.not.match(/^foo/i, 'foo') - }, "expected 'foobar' not to match /^foo/i | foo"); - }, - - 'test length(n)': function(){ - 'test'.should.have.length(4); - 'test'.should.not.have.length(3); - [1,2,3].should.have.length(3); - ({ length: 10}).should.have.length(10); - - err(function(){ - (4).should.have.length(3); - }, 'expected 4 to have a property \'length\''); - - err(function(){ - 'asd'.should.not.have.length(3); - }, "expected 'asd' to not have a length of 3"); - - err(function(){ - 'asd'.should.have.length(4, 'foo'); - }, "expected 'asd' to have a length of 4 but got 3 | foo"); - - err(function(){ - 'asd'.should.not.have.length(3, 'foo'); - }, "expected 'asd' to not have a length of 3 | foo"); - - }, - - 'test eql(val)': function(){ - 'test'.should.eql('test'); - ({ foo: 'bar' }).should.eql({ foo: 'bar' }); - (1).should.eql(1); - '4'.should.eql(4); - var memo = []; - function memorize() { - memo.push(arguments); - } - memorize('a', [1, 2]); - memorize('a', [1, 2]); - memo[0].should.eql(memo[1]); - - err(function(){ - (4).should.eql(3); - }, 'expected 4 to equal 3'); - }, - - 'test .json': function(){ - var req = { - headers: { - 'content-type': 'application/json' - } - }; - - req.should.be.json; - - var req = { - headers: { - 'content-type': 'application/json; charset=utf-8' - } - }; - - req.should.be.json; - }, - - 'test equal(val)': function(){ - 'test'.should.equal('test'); - (1).should.equal(1); - - err(function(){ - (4).should.equal(3); - }, 'expected 4 to be 3'); - - err(function(){ - '4'.should.equal(4); - }, "expected '4' to be 4"); - - var date = new Date; - date.should.equal(date); - }, - - 'test empty': function(){ - ''.should.be.empty; - [].should.be.empty; - ({}).should.be.empty; - ({ length: 10 }).should.not.be.empty; - - (function() { - arguments.should.be.empty; - })(); - - err(function(){ - ({}).should.not.be.empty; - }, 'expected {} not to be empty'); - - err(function(){ - ({ length: 10 }).should.be.empty; - }, 'expected { length: 10 } to be empty'); - - err(function(){ - 'asd'.should.be.empty; - }, "expected 'asd' to be empty"); - - err(function(){ - ''.should.not.be.empty; - }, "expected '' not to be empty"); - }, - - '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'"); - - err(function(){ - 'asd'.should.have.property('foo', undefined, 'foo'); - }, "expected 'asd' to have a property 'foo' | foo"); - - err(function(){ - 'asd'.should.not.have.property('length', undefined, 'foo'); - }, "expected 'asd' to not have a property 'length' | foo"); - }, - - 'test property(name, val)': function(){ - 'test'.should.have.property('length', 4); - 'asd'.should.have.property('constructor', String); - - err(function(){ - 'asd'.should.have.property('length', 4); - }, "expected 'asd' to have a property 'length' of 4, but got 3"); - - err(function(){ - 'asd'.should.not.have.property('length', 3); - }, "expected 'asd' to not have a property 'length' of 3"); - - err(function(){ - 'asd'.should.not.have.property('foo', 3); - }, "'asd' has no property 'foo'"); - - err(function(){ - 'asd'.should.have.property('constructor', Number); - }, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]"); - - err(function(){ - 'asd'.should.have.property('length', 4, 'foo'); - }, "expected 'asd' to have a property 'length' of 4, but got 3 | foo"); - - err(function(){ - 'asd'.should.not.have.property('length', 3, 'foo'); - }, "expected 'asd' to not have a property 'length' of 3 | foo"); - - err(function(){ - 'asd'.should.not.have.property('foo', 3, 'foo'); - }, "'asd' has no property 'foo' | foo"); - - err(function(){ - 'asd'.should.have.property('constructor', Number, 'foo'); - }, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String] | foo"); - }, - - 'test properties(name1, name2, ...)': function(){ - 'test'.should.have.properties('length', 'indexOf'); - (4).should.not.have.properties('length'); - - err(function(){ - 'asd'.should.have.properties('foo'); - }, "expected 'asd' to have a property 'foo'"); - - err(function(){ - 'asd'.should.not.have.properties('length', 'indexOf'); - }, "expected 'asd' to not have properties 'length', and 'indexOf'"); - }, - - 'test properties([names])': function(){ - 'test'.should.have.properties(['length', 'indexOf']); - (4).should.not.have.properties(['length']); - - err(function(){ - 'asd'.should.have.properties(['foo']); - }, "expected 'asd' to have a property 'foo'"); - }, - - 'test ownProperty(name)': function(){ - 'test'.should.have.ownProperty('length'); - 'test'.should.haveOwnProperty('length'); - ({ length: 12 }).should.have.ownProperty('length'); - - err(function(){ - ({ length: 12 }).should.not.have.ownProperty('length'); - }, "expected { length: 12 } to not have own property 'length'"); - - err(function(){ - ({ length: 12 }).should.not.have.ownProperty('length', 'foo'); - }, "expected { length: 12 } to not have own property 'length' | foo"); - - err(function(){ - ({ length: 12 }).should.have.ownProperty('foo', 'foo'); - }, "expected { length: 12 } to have own property 'foo' | foo"); - }, - - 'test ownProperty(name).equal(val)': function() { - ({length: 10}).should.have.ownProperty('length').equal(10); - }, - - 'test startWith()': function() { - 'foobar'.should.startWith('foo'); - 'foobar'.should.not.startWith('bar'); - - err(function() { - 'foobar'.should.startWith('bar'); - }, "expected 'foobar' to start with 'bar'"); - - err(function() { - 'foobar'.should.not.startWith('foo'); - }, "expected 'foobar' to not start with 'foo'"); - - err(function() { - 'foobar'.should.startWith('bar', 'baz'); - }, "expected 'foobar' to start with 'bar' | baz"); - - err(function() { - 'foobar'.should.not.startWith('foo', 'baz'); - }, "expected 'foobar' to not start with 'foo' | baz"); - }, - - 'test endWith()': function() { - 'foobar'.should.endWith('bar'); - 'foobar'.should.not.endWith('foo'); - - err(function() { - 'foobar'.should.endWith('foo'); - }, "expected 'foobar' to end with 'foo'"); - - err(function() { - 'foobar'.should.not.endWith('bar'); - }, "expected 'foobar' to not end with 'bar'"); - - err(function() { - 'foobar'.should.endWith('foo', 'baz'); - }, "expected 'foobar' to end with 'foo' | baz"); - - err(function() { - 'foobar'.should.not.endWith('bar', 'baz'); - }, "expected 'foobar' to not end with 'bar' | baz"); - }, - - 'test include() with string': function(){ - 'foobar'.should.include('bar'); - 'foobar'.should.include('foo'); - 'foobar'.should.not.include('baz'); - - err(function(){ - 'foobar'.should.include('baz'); - }, "expected 'foobar' to include 'baz'"); - - err(function(){ - 'foobar'.should.not.include('bar'); - }, "expected 'foobar' to not include 'bar'"); - - err(function(){ - 'foobar'.should.include('baz', 'foo'); - }, "expected 'foobar' to include 'baz' | foo"); - - err(function(){ - 'foobar'.should.not.include('bar', 'foo'); - }, "expected 'foobar' to not include 'bar' | foo"); - }, - - 'test include() with array': function(){ - ['foo', 'bar'].should.include('foo'); - ['foo', 'bar'].should.include('foo'); - ['foo', 'bar'].should.include('bar'); - [1,2].should.include(1); - ['foo', 'bar'].should.not.include('baz'); - ['foo', 'bar'].should.not.include(1); - - err(function(){ - ['foo'].should.include('bar'); - }, "expected [ 'foo' ] to include 'bar'"); - - err(function(){ - ['bar', 'foo'].should.not.include('foo'); - }, "expected [ 'bar', 'foo' ] to not include 'foo'"); - - err(function(){ - ['foo'].should.include('bar', 'foo'); - }, "expected [ 'foo' ] to include 'bar' | foo"); - - err(function(){ - ['bar', 'foo'].should.not.include('foo', 'foo'); - }, "expected [ 'bar', 'foo' ] to not include 'foo' | foo"); - }, - - 'test include() with object': function(){ - var tobi = { name: 'Tobi', age: 2 }; - var jane = { name: 'Jane', age: 2 }; - - var user = { name: 'TJ', pet: tobi, age: 24 }; - - user.should.include({ pet: tobi }); - user.should.include({ pet: tobi, name: 'TJ' }); - user.should.not.include({ pet: tobi, name: 'Someone else' }); - user.should.not.include({ pet: jane }); - user.should.not.include({ pet: jane, name: 'TJ' }); - - err(function(){ - user.should.include({ pet: { name: 'Luna' } }); - }, "expected { name: 'TJ', pet: { name: 'Tobi', age: 2 }, age: 24 } to include an object equal to { pet: { name: 'Luna' } }"); - }, - - 'test includeEql() with array': function(){ - [['foo'], ['bar']].should.includeEql(['foo']); - [['foo'], ['bar']].should.includeEql(['bar']); - [['foo'], ['bar']].should.not.includeEql(['baz']); - [].should.not.includeEql(['baz']); - - err(function(){ - [['foo']].should.includeEql(['bar']); - }, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ]"); - - err(function(){ - [['foo']].should.not.includeEql(['foo']); - }, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ]"); - - err(function(){ - [['foo']].should.includeEql(['bar'], 'foo'); - }, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ] | foo"); - - err(function(){ - [['foo']].should.not.includeEql(['foo'], 'foo'); - }, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ] | foo"); - }, - - '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'); - - 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.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'"); - }, - + 'test chaining': function(){ var user = { name: 'tj', pets: ['tobi', 'loki', 'jane', 'bandit'] }; - user.should.have.property('pets').with.a.lengthOf(4); - - err(function(){ - user.should.have.property('pets').with.lengthOf(5); - }, "expected [ 'tobi', 'loki', 'jane', 'bandit' ] to have a length of 5 but got 4"); user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); - }, - - 'test throw()': function(){ - (function(){}).should.not.throw(); - (function(){ throw new Error('fail') }).should.throw(); - - err(function(){ - (function(){}).should.throw(); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ - throw new Error('fail'); - }).should.not.throw(); - }, 'expected no exception to be thrown, got "fail"'); - }, - - 'test throw() with regex message': function(){ - (function(){ throw new Error('fail'); }).should.throw(/fail/); - - err(function(){ - (function(){}).should.throw(/fail/); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw new Error('error'); }).should.throw(/fail/); - }, "expected an exception to be thrown with a message matching /fail/, but got 'error'"); - }, - 'test throw() with string message': function(){ - (function(){ throw new Error('fail'); }).should.throw('fail'); - - err(function(){ - (function(){}).should.throw('fail'); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw new Error('error'); }).should.throw('fail'); - }, "expected an exception to be thrown with a message matching 'fail', but got 'error'"); - }, - - 'test throw() with type': function(){ - (function(){ throw new Error('fail'); }).should.throw(Error); - - err(function(){ - (function(){}).should.throw(Error); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw 'error'; }).should.throw(Error); - }, "expected an exception to be thrown of type Error, but got String"); - }, - - 'test throwError()': function(){ - (function(){}).should.not.throwError(); - (function(){ throw new Error('fail') }).should.throwError(); - - err(function(){ - (function(){}).should.throwError(); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ - throw new Error('fail'); - }).should.not.throwError(); - }, 'expected no exception to be thrown, got "fail"'); - }, - - 'test throwError() with regex message': function(){ - (function(){ throw new Error('fail'); }).should.throwError(/fail/); - - err(function(){ - (function(){}).should.throwError(/fail/); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw new Error('error'); }).should.throwError(/fail/); - }, "expected an exception to be thrown with a message matching /fail/, but got 'error'"); - }, - - 'test throwError() with string message': function(){ - (function(){ throw new Error('fail'); }).should.throwError('fail'); - - err(function(){ - (function(){}).should.throwError('fail'); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw new Error('error'); }).should.throwError('fail'); - }, "expected an exception to be thrown with a message matching 'fail', but got 'error'"); - }, - - 'test throwError() with type': function(){ - (function(){ throw new Error('fail'); }).should.throw(Error); - - err(function(){ - (function(){}).should.throw(Error); - }, 'expected an exception to be thrown'); - - err(function(){ - (function(){ throw 'error'; }).should.throw(Error); - }, "expected an exception to be thrown of type Error, but got String"); - }, - - 'test .inspect to format Dates': function() { - var d = new Date(); - should(d).inspect.should.be.exactly(d.toISOString()); - }, - - 'test .inspect to use custom inspect on Dates': function() { - var d = new Date(); - d.inspect = function() { return this.getTime(); } - should(d).inspect.should.be.exactly(String(d.getTime())); + user.should.have.ownProperty('name') + .which.not.have.length(3) + .and.be.equal('tj'); } - };