forked from relevance/blue-ridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
screw.matchers.js
187 lines (153 loc) · 5.55 KB
/
screw.matchers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Screw.Matchers = (function($) {
return matchers = {
expect: function(actual) {
return {
to: function(matcher, expected, not) {
var matched = matcher.match(expected, actual);
if (not ? matched : !matched) {
throw(matcher.failure_message(expected, actual, not));
}
},
to_not: function(matcher, expected) {
this.to(matcher, expected, true);
}
}
},
equal: {
match: function(expected, actual) {
if(expected == actual) return true;
if(actual == undefined) return false;
if (expected instanceof Array) {
for (var i = 0; i < actual.length; i++)
if (!Screw.Matchers.equal.match(expected[i], actual[i])) return false;
return actual.length == expected.length;
} else if (expected instanceof Object) {
for (var key in expected)
if (!this.match(expected[key], actual[key])) return false;
for (var key in actual)
if (!this.match(actual[key], expected[key])) return false;
return true;
}
return false;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not equal ' : ' to equal ') + $.print(expected);
}
},
be_gt: {
match: function(expected, actual) {
return actual > expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be greater than ' + $.print(expected);
}
},
be_gte: {
match: function(expected, actual) {
return actual >= expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be greater than or equal to ' + $.print(expected);
}
},
be_lt: {
match: function(expected, actual) {
return actual < expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be less than ' + $.print(expected);
}
},
be_lte: {
match: function(expected, actual) {
return actual <= expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be less than or equal to ' + $.print(expected);
}
},
match: {
match: function(expected, actual) {
if (expected.constructor == RegExp)
return expected.exec(actual.toString());
else
return actual.indexOf(expected) > -1;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not match ' : ' to match ') + $.print(expected);
}
},
be_empty: {
match: function(expected, actual) {
if (actual.length == undefined) throw(actual.toString() + " does not respond to length");
return actual.length == 0;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be empty' : ' to be empty');
}
},
have_length: {
match: function(expected, actual) {
if (actual.length == undefined) throw(actual.toString() + " does not respond to length");
return actual.length == expected;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not' : ' to') + ' have length ' + expected;
}
},
be_null: {
match: function(expected, actual) {
return actual == null;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be null' : ' to be null');
}
},
be_undefined: {
match: function(expected, actual) {
return actual == undefined;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be undefined' : ' to be undefined');
}
},
be_true: {
match: function(expected, actual) {
return actual;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be true' : ' to be true');
}
},
be_false: {
match: function(expected, actual) {
return !actual;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be false' : ' to be false');
}
},
match_selector: {
match: function(expected, actual) {
if (!(actual instanceof jQuery)) {
throw expected.toString() + " must be an instance of jQuery to match against a selector"
}
return actual.is(expected);
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not match selector ' : ' to match selector ') + expected;
}
},
contain_selector: {
match: function(expected, actual) {
if (!(actual instanceof jQuery)) {
throw expected.toString() + " must be an instance of jQuery to match against a selector"
}
return actual.find(expected).length > 0;
},
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not contain selector ' : ' to contain selector ') + expected;
}
}
}
})(jQuery);