-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
110 lines (99 loc) · 2.69 KB
/
index.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
(function(plugin){
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){
var _;
if (
typeof window === "object"
&& typeof window._ == "function"
) {
// browser-side
_ = window._;
} else {
// server-side
_ = require('underscore');
}
// contain => _.where, check _.isEqual
// containOnce => contain, check size of returned array
// like => _.isEqual
chai.Assertion.addMethod('like', function(expected){
var obj = this._obj
this.assert(
_.isEqual(obj, expected)
, "expected #{this} to be like #{exp}"
, "expected #{this} not to be like #{exp}"
, expected
, obj
, true
)
});
chai.Assertion.addMethod('jsonOf', function(expected){
var obj = this._obj;
var expectedAsJSON = JSON.parse(JSON.stringify(expected));
this.assert(
_.isEqual(obj, expectedAsJSON)
, "expected #{this} to be like JSON #{exp}"
, "expected #{this} not to be like JSON #{exp}"
, expectedAsJSON
, obj
, true
)
});
chai.Assertion.addMethod('containOneLike', function(expected){
var obj = this._obj
var _obj = _(obj);
if (!_obj.isObject() && !_obj.isArray()) {
this.assert(
false
, "expected #{this} to be an array, object, or string"
, "expected #{this} to be an array, object, or string"
)
}
var found = _obj.some(function(needle){
return _.isEqual(needle,expected);
});
this.assert(
found
, "expected #{this} to contain one thing like #{exp}"
, "expected #{this} not to contain one thing like #{exp}"
, expected
)
});
//export tdd style
var assert = chai.assert;
assert.like = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.like(exp);
};
assert.notLike = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.like(exp);
};
assert.containOneLike = function (val, exp, msg) {
new chai.Assertion(val, msg).to.containOneLike(exp);
};
assert.notContainOneLike = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.containOneLike(exp);
};
assert.jsonOf = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.jsonOf(exp);
};
assert.notJsonOf = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.jsonOf(exp);
};
}));