This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
eql.js
145 lines (126 loc) · 3.87 KB
/
eql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* should.js - assertion library
* Copyright(c) 2010-2013 TJ Holowaychuk <tj@vision-media.ca>
* Copyright(c) 2013-2016 Denis Bardadym <bardadymchik@gmail.com>
* MIT Licensed
*/
import eql from 'should-equal';
import getType from 'should-type';
import { formatProp, format } from '../format';
import { forEach } from 'should-type-adaptors';
function formatEqlResult(r, a, b) {
return ((r.path.length > 0 ? 'at ' + r.path.map(formatProp).join(' -> ') : '') +
(r.a === a ? '' : ', A has ' + format(r.a)) +
(r.b === b ? '' : ' and B has ' + format(r.b)) +
(r.showReason ? ' because ' + r.reason : '')).trim();
}
export default function(should, Assertion) {
/**
* Deep object equality comparison. For full spec see [`should-equal tests`](https://github.com/shouldjs/equal/blob/master/test.js).
*
* @name eql
* @memberOf Assertion
* @category assertion equality
* @alias Assertion#eqls
* @alias Assertion#deepEqual
* @param {*} val Expected value
* @param {string} [description] Optional message
* @example
*
* (10).should.be.eql(10);
* ('10').should.not.be.eql(10);
* (-0).should.not.be.eql(+0);
*
* NaN.should.be.eql(NaN);
*
* ({ a: 10}).should.be.eql({ a: 10 });
* [ 'a' ].should.not.be.eql({ '0': 'a' });
*/
Assertion.add('eql', function(val, description) {
this.params = {operator: 'to equal', expected: val, message: description};
var obj = this.obj;
var fails = eql(this.obj, val, should.config);
this.params.details = fails.map(function(fail) {
return formatEqlResult(fail, obj, val);
}).join(', ');
this.params.showDiff = eql(getType(obj), getType(val)).length === 0;
this.assert(fails.length === 0);
});
/**
* Exact comparison using ===.
*
* @name equal
* @memberOf Assertion
* @category assertion equality
* @alias Assertion#equals
* @alias Assertion#exactly
* @param {*} val Expected value
* @param {string} [description] Optional message
* @example
*
* 10.should.be.equal(10);
* 'a'.should.be.exactly('a');
*
* should(null).be.exactly(null);
*/
Assertion.add('equal', function(val, description) {
this.params = {operator: 'to be', expected: val, message: description};
this.params.showDiff = eql(getType(this.obj), getType(val)).length === 0;
this.assert(val === this.obj);
});
Assertion.alias('equal', 'equals');
Assertion.alias('equal', 'exactly');
Assertion.alias('eql', 'eqls');
Assertion.alias('eql', 'deepEqual');
function addOneOf(name, message, method) {
Assertion.add(name, function(vals) {
if (arguments.length !== 1) {
vals = Array.prototype.slice.call(arguments);
} else {
should(vals).be.Array();
}
this.params = {operator: message, expected: vals};
var obj = this.obj;
var found = false;
forEach(vals, function(val) {
try {
should(val)[method](obj);
found = true;
return false;
} catch (e) {
if (e instanceof should.AssertionError) {
return;//do nothing
}
throw e;
}
});
this.assert(found);
});
}
/**
* Exact comparison using === to be one of supplied objects.
*
* @name equalOneOf
* @memberOf Assertion
* @category assertion equality
* @param {Array|*} vals Expected values
* @example
*
* 'ab'.should.be.equalOneOf('a', 10, 'ab');
* 'ab'.should.be.equalOneOf(['a', 10, 'ab']);
*/
addOneOf('equalOneOf', 'to be equals one of', 'equal');
/**
* Exact comparison using .eql to be one of supplied objects.
*
* @name oneOf
* @memberOf Assertion
* @category assertion equality
* @param {Array|*} vals Expected values
* @example
*
* ({a: 10}).should.be.oneOf('a', 10, 'ab', {a: 10});
* ({a: 10}).should.be.oneOf(['a', 10, 'ab', {a: 10}]);
*/
addOneOf('oneOf', 'to be one of', 'eql');
}