forked from vbardales/chai-properties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chai-properties.js
117 lines (97 loc) · 3.05 KB
/
chai-properties.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
(function (plugin) {
'use strict';
if (typeof require === 'function' &&
typeof exports === 'object' &&
typeof module === 'object') {
// NodeJS
module.exports = plugin;
} else if (typeof define === 'function' && define.amd) {
// AMD
define(function () {
return plugin;
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(plugin);
}
}(function (chai, utils) {
'use strict';
var _;
if (typeof window === 'object' &&
typeof window._ === 'function') {
// browser-side
_ = window._;
} else {
// server-side
_ = require('lodash');
}
var flag = utils.flag;
var inspect = utils.inspect;
chai.Assertion.addMethod('properties', function(expected) {
var obj = flag(this, 'object');
if (flag(this, 'negate')) {
throw new Error('Not implemented yet.');
}
function check(testDescr, testVal) {
try {
return _.every(testDescr, function (val, attr) {
if (!(attr in testVal)) {
throw new Error('No ' + attr + ' in ' + inspect(testVal));
}
if (typeof val !== typeof testVal[attr]) {
throw new Error('Types are incompatible (' + (typeof val) + ' vs ' + (typeof testVal[attr]) + ')');
}
if (_.isArray(val)) {
if (_.size(val) !== _.size(testVal[attr])) {
throw new Error('Array sizes are incompatible (' + _.size(val) + ' vs ' + _.size(testVal[attr]) + ')');
}
return check(val, testVal[attr]);
}
if(_.isDate(val) && _.isDate(testVal[attr])){
return val.getTime() === testVal[attr].getTime()
}
if (_.isObject(val)) {
return check(val, testVal[attr]);
}
if (val !== testVal[attr]) {
throw new Error('Values are incompatible (' + inspect(val) + ' vs ' + inspect(testVal[attr]) + ')');
}
return true;
});
} catch (e) {
return false;
}
}
function diffFn(testDescr, testVal) {
return _.transform(testVal, function (result, val, key) {
if (!_.has(testDescr, key)) {
return;
}
if (_.isObject(val)) {
var innerDiff = diffFn(testDescr[key], val);
if (_.size(innerDiff)) {
result[key] = innerDiff;
}
return;
}
result[key] = val;
}, {});
}
//var diff = _.pick(obj, _.keys(expected));
var diff = diffFn(expected, obj);
var moreMessage = _.size(diff) ? ', but found ' + inspect(diff) : '';
this.assert(
check(expected, obj),
'expected ' + inspect(obj) + ' to have properties ' + inspect(expected) + moreMessage,
'expected ' + inspect(obj) + ' to not have properties ' + inspect(expected) + moreMessage,
expected,
obj,
true
);
});
//export tdd style
var assert = chai.assert;
assert.haveProperties = function (val, exp, msg) {
new chai.Assertion(val, msg).to.have.properties(exp);
};
}));