Comparing version 2.3.0 to 3.0.0
53
API.md
@@ -91,4 +91,2 @@ <!-- version --> | ||
The following words toggle a status flag for the current assertion: | ||
- `deep` - performs a deep comparison instead of simple equality (`===`). Required when trying to compare | ||
objects to an identical copy that is not the same reference. Used by `equal()` and `include()`. | ||
- `not` - inverses the expected result of any assertion. | ||
@@ -98,2 +96,3 @@ - `once` - requires that inclusion matches appear only once in the provided value. Used by `include()`. | ||
- `part` - allows a partial match when asserting inclusion. Used by `include()`. | ||
- `shallow` - performs a comparison using strict equality (`===`). Code defaults to deep comparison. Used by `equal()` and `include()`. | ||
@@ -105,3 +104,3 @@ ```js | ||
expect(10).to.not.be.above(20); | ||
expect([{ a: 1 }]).to.deep.include({ a: 1 }); | ||
expect([1, 2, 3]).to.shallow.include(3); | ||
expect([1, 1, 2]).to.only.include([1, 2]); | ||
@@ -262,3 +261,3 @@ expect([1, 2]).to.once.include([1, 2]); | ||
Asserts that the reference value is equals to a predefined value. | ||
Asserts that the reference value is equal to a predefined value. | ||
@@ -315,4 +314,3 @@ ##### `true()` | ||
- `values` - a single or array of values. If the reference value is a string, the values must be strings. | ||
If the reference value is an array, the values can be any array member (`deep` is required to compare | ||
non-literal types). If the reference value is an object, the values can be key names, or a single object | ||
If the reference value is an array, the values can be any array member. If the reference value is an object, the values can be key names, or a single object | ||
with key-value pairs to match. | ||
@@ -332,9 +330,9 @@ | ||
expect([1, 2, 3]).to.include(1); | ||
expect([{ a: 1 }]).to.deep.include({ a: 1 }); | ||
expect([{ a: 1 }]).to.include({ a: 1 }); | ||
expect([1, 2, 3]).to.include([1, 2]); | ||
expect([{ a: 1 }]).to.deep.include([{ a: 1 }]); | ||
expect([{ a: 1 }]).to.include([{ a: 1 }]); | ||
expect([1, 1, 2]).to.only.include([1, 2]); | ||
expect([1, 2]).to.once.include([1, 2]); | ||
expect([1, 2, 3]).to.part.include([1, 4]); | ||
expect([[1], [2]]).to.deep.include([[1]]); | ||
expect([[1], [2]]).to.include([[1]]); | ||
@@ -348,3 +346,3 @@ expect({ a: 1, b: 2, c: 3 }).to.include('a'); | ||
expect({ a: 1, b: 2, c: 3 }).to.only.include({ a: 1, b: 2, c: 3 }); | ||
expect({ a: [1], b: [2], c: [3] }).to.deep.include({ a: [1], c: [3] }); | ||
expect({ a: [1], b: [2], c: [3] }).to.include({ a: [1], c: [3] }); | ||
``` | ||
@@ -427,7 +425,5 @@ | ||
Asserts that the reference value equals the provided value (`deep` is required to compare non-literal | ||
types) where: | ||
Asserts that the reference value equals the provided value where: | ||
- `value` - the value to compare to. | ||
- `options` - optional object specifying comparison options. This is only used on | ||
deep comparisons, and is ignored otherwise. | ||
- `options` - optional object specifying comparison options. This is ignored on `shallow` comparisons. | ||
@@ -439,7 +435,7 @@ ```js | ||
expect(5).to.equal(5); | ||
expect({ a: 1 }).to.deep.equal({ a: 1 }); | ||
expect({ a: 1 }).to.equal({ a: 1 }); | ||
``` | ||
Deep comparisons are performed using | ||
[`Hoek.deepEqual()`](https://github.com/hapijs/hoek#deepequalb-a-options). The | ||
Deep comparisons (the default) are performed using | ||
[`Hoek.deepEqual()`](https://github.com/hapijs/hoek/blob/master/API.md#deepequalb-a-options). The | ||
optional `options` argument is passed directly to `Hoek.deepEqual()`. An example | ||
@@ -452,5 +448,15 @@ deep comparison which ignores object prototypes is shown below. | ||
expect(Object.create(null)).to.deep.equal({}, { prototype: false }); | ||
expect(Object.create(null)).to.equal({}, { prototype: false }); | ||
``` | ||
Strict equality can be checked using the `shallow` modifier. This yields the same output as a `===` check. | ||
```js | ||
const Code = require('code'); | ||
const expect = Code.expect; | ||
expect(5).to.shallow.equal(5); | ||
expect({ a: 1 }).to.shallow.equal({ a: 1 }); // fails as they are not the same reference | ||
``` | ||
#### `above(value)` | ||
@@ -693,3 +699,3 @@ | ||
Code.settings.truncateMessages = false; | ||
expect(foo).to.deep.equal([]); | ||
expect(foo).to.equal([]); | ||
``` | ||
@@ -699,3 +705,3 @@ | ||
A boolean value that, when `false`, ignores object prototypes when doing a deep comparison. Defaults to `true`. | ||
A Boolean value that, when `false`, ignores object prototypes when doing a deep comparison. Defaults to `false`. | ||
@@ -707,4 +713,7 @@ ```js | ||
Code.setting.comparePrototypes = false; | ||
expect(foo).to.deep.equal({}); | ||
Code.settings.comparePrototypes = false; | ||
expect(foo).to.equal({}); | ||
Code.settings.comparePrototypes = true; | ||
expect(foo).to.equal({}); // fails | ||
``` |
@@ -12,3 +12,3 @@ 'use strict'; | ||
const internals = { | ||
flags: ['deep', 'not', 'once', 'only', 'part'], | ||
flags: ['not', 'once', 'only', 'part', 'shallow'], | ||
grammar: ['a', 'an', 'and', 'at', 'be', 'have', 'in', 'to'], | ||
@@ -22,3 +22,3 @@ locations: {}, | ||
truncateMessages: false, | ||
comparePrototypes: true | ||
comparePrototypes: false | ||
}; | ||
@@ -202,2 +202,3 @@ | ||
this._flags.deep = !this._flags.shallow; | ||
return this.assert(Hoek.contain(this._ref, value, this._flags), 'include ' + internals.display(value)); | ||
@@ -266,4 +267,4 @@ }; | ||
const compare = this._flags.deep ? (a, b) => Hoek.deepEqual(a, b, settings) | ||
: (a, b) => a === b; | ||
const compare = this._flags.shallow ? (a, b) => a === b | ||
: (a, b) => Hoek.deepEqual(a, b, settings); | ||
@@ -270,0 +271,0 @@ return this.assert(compare(this._ref, value), 'equal specified value', this._ref, value); |
{ | ||
"name": "code", | ||
"description": "assertion library", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"repository": "git://github.com/hapijs/code", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102861
2111