forked from practicalmeteor/meteor-chai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chai-string-1.1.1.js
executable file
·185 lines (146 loc) · 5.11 KB
/
chai-string-1.1.1.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
(function (plugin) {
chai.use(plugin);
}(function (chai, utils) {
chai.string = chai.string || {};
chai.string.startsWith = function (str, prefix) {
return str.indexOf(prefix) === 0;
};
chai.string.endsWith = function (str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
chai.string.equalIgnoreCase = function (str1, str2) {
return str1.toLowerCase() === str2.toLowerCase();
};
chai.string.singleLine = function(str) {
return str.trim().indexOf("\n") === -1;
};
chai.string.reverseOf = function(str, reversed) {
return str.split('').reverse().join('') === reversed;
};
chai.string.palindrome = function(str) {
var len = str.length;
for ( var i = 0; i < Math.floor(len/2); i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
};
chai.string.entriesCount = function(str, substr, count) {
var i = 0,
len = str.length,
matches = 0;
while (i < len) {
var indx = str.indexOf(substr, i);
if (indx === -1) {
break;
}
else {
matches++;
i = indx + 1;
}
}
return matches === count;
};
var startsWithMethodWrapper = function (expected) {
var actual = this._obj;
return this.assert(
chai.string.startsWith(actual, expected),
'expected ' + this._obj + ' to starts with ' + expected,
'expected ' + this._obj + ' to not starts with ' + expected
);
};
chai.Assertion.addChainableMethod('startsWith', startsWithMethodWrapper);
chai.Assertion.addChainableMethod('startWith', startsWithMethodWrapper);
var endsWithMethodWrapper = function (expected) {
var actual = this._obj;
return this.assert(
chai.string.endsWith(actual, expected),
'expected ' + this._obj + ' to ends with ' + expected,
'expected ' + this._obj + ' to not ends with ' + expected
);
};
chai.Assertion.addChainableMethod('endsWith', endsWithMethodWrapper);
chai.Assertion.addChainableMethod('endWith', endsWithMethodWrapper);
chai.Assertion.addChainableMethod('equalIgnoreCase', function (expected) {
var actual = this._obj;
return this.assert(
chai.string.equalIgnoreCase(actual, expected),
'expected ' + this._obj + ' to be equal to ' + expected + ' ignoring case',
'expected ' + this._obj + ' to be not equal to ' + expected + ' ignoring case'
);
});
chai.Assertion.addChainableMethod('singleLine', function () {
var actual = this._obj;
return this.assert(
chai.string.singleLine(actual),
'expected ' + this._obj + ' to be single line',
'expected ' + this._obj + ' to be not single line'
);
});
chai.Assertion.addChainableMethod('reverseOf', function(expected) {
var actual = this._obj;
return this.assert(
chai.string.reverseOf(actual, expected),
'expected ' + this._obj + ' to be reverse of ' + expected,
'expected ' + this._obj + ' to be not reverse of ' + expected
);
});
chai.Assertion.addChainableMethod('palindrome', function () {
var actual = this._obj;
return this.assert(
chai.string.palindrome(actual),
'expected ' + this._obj + ' to be palindrome',
'expected ' + this._obj + ' to be not palindrome'
);
});
chai.Assertion.addChainableMethod('entriesCount', function(substr, expected) {
var actual = this._obj;
return this.assert(
chai.string.entriesCount(actual, substr, expected),
'expected ' + this._obj + ' to have ' + substr + ' ' + expected + ' time(s)',
'expected ' + this._obj + ' to not have ' + substr + ' ' + expected + ' time(s)'
);
});
// Asserts
var assert = chai.assert;
assert.startsWith = function (val, exp, msg) {
new chai.Assertion(val, msg).to.startsWith(exp);
};
assert.notStartsWith = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.startsWith(exp);
};
assert.endsWith = function (val, exp, msg) {
new chai.Assertion(val, msg).to.endsWith(exp);
};
assert.notEndsWith = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.endsWith(exp);
};
assert.equalIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.be.equalIgnoreCase(exp);
};
assert.notEqualIgnoreCase = function (val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.equalIgnoreCase(exp);
};
assert.singleLine = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.singleLine();
};
assert.notSingleLine = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.singleLine();
};
assert.reverseOf = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.reverseOf(exp);
};
assert.notReverseOf = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.reverseOf(exp);
};
assert.palindrome = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.palindrome();
};
assert.notPalindrome = function(val, exp, msg) {
new chai.Assertion(val, msg).to.not.be.palindrome();
};
assert.entriesCount = function(str, substr, count, msg) {
new chai.Assertion(str, msg).to.have.entriesCount(substr, count);
}
}));