summaryrefslogtreecommitdiffstats
path: root/debian/tests/test_modules/chai-string/chai-string.js
blob: 669995d69b4cac21559137484f2bdc7e12425110 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(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) {
  chai.string = chai.string || {};

  function isString(value) {
    return typeof value === 'string';
  }

  chai.string.startsWith = function (str, prefix) {
    if (!isString(str) || !isString(prefix)) {
      return false;
    }
    return str.indexOf(prefix) === 0;
  };

  chai.string.endsWith = function (str, suffix) {
    if (!isString(str) || !isString(suffix)) {
      return false;
    }
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
  };

  chai.string.equalIgnoreCase = function (str1, str2) {
    if (!isString(str1) || !isString(str2)) {
      return false;
    }
    return str1.toLowerCase() === str2.toLowerCase();
  };

  chai.string.equalIgnoreSpaces = function (str1, str2) {
    if (!isString(str1) || !isString(str2)) {
      return false;
    }
    return str1.replace(/\s/g, '') === str2.replace(/\s/g, '');
  };

  chai.string.containIgnoreSpaces = function (str1, str2) {
    if (!isString(str1) || !isString(str2)) {
      return false;
    }
    return str1.replace(/\s/g, '').indexOf(str2.replace(/\s/g, '')) > -1;
  };

  chai.string.containIgnoreCase = function (str1, str2) {
    if (!isString(str1) || !isString(str2)) {
      return false;
    }
    return str1.toLowerCase().indexOf(str2.toLowerCase()) > -1;
  }

  chai.string.singleLine = function (str) {
    if (!isString(str)) {
      return false;
    }
    return str.trim().indexOf("\n") === -1;
  };

  chai.string.reverseOf = function (str, reversed) {
    if (!isString(str) || !isString(reversed)) {
      return false;
    }
    return str.split('').reverse().join('') === reversed;
  };

  chai.string.palindrome = function (str) {
    if (!isString(str)) {
      return false;
    }
    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 matches = 0;
    if (isString(str) && isString(substr)) {
      var i = 0;
      var len = str.length;
      while (i < len) {
        var indx = str.indexOf(substr, i);
        if (indx === -1) {
          break;
        }
        else {
          matches++;
          i = indx + 1;
        }
      }
    }
    return matches === count;
  };

  chai.string.indexOf = function (str, substr, index) {
    var indx = !isString(str) || !isString(substr) ? -1 : str.indexOf(substr);
    return indx === index;
  };

  var startsWithMethodWrapper = function (expected) {
    var actual = this._obj;

    return this.assert(
      chai.string.startsWith(actual, expected),
      'expected ' + this._obj + ' to start with ' + expected,
      'expected ' + this._obj + ' not to start 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 end with ' + expected,
      'expected ' + this._obj + ' not to end 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 equal ' + expected + ' ignoring case',
      'expected ' + this._obj + ' not to equal ' + expected + ' ignoring case'
    );
  });

  chai.Assertion.addChainableMethod('equalIgnoreSpaces', function (expected) {
    var actual = this._obj;

    return this.assert(
      chai.string.equalIgnoreSpaces(actual, expected),
      'expected ' + this._obj + ' to equal ' + expected + ' ignoring spaces',
      'expected ' + this._obj + ' not to equal ' + expected + ' ignoring spaces'
    );
  });

  chai.Assertion.addChainableMethod('containIgnoreSpaces', function (expected) {
    var actual = this._obj;

    return this.assert(
      chai.string.containIgnoreSpaces(actual, expected),
      'expected ' + this._obj + ' to contain ' + expected + ' ignoring spaces',
      'expected ' + this._obj + ' not to contain ' + expected + ' ignoring spaces'
    );
  });

  chai.Assertion.addChainableMethod('containIgnoreCase', function (expected) {
    var actual = this._obj;

    return this.assert(
      chai.string.containIgnoreCase(actual, expected),
      'expected ' + this._obj + ' to contain ' + expected + ' ignoring case',
      'expected ' + this._obj + ' not to contain ' + expected + ' ignoring case'
    );
  });

  chai.Assertion.addChainableMethod('singleLine', function () {
    var actual = this._obj;

    return this.assert(
      chai.string.singleLine(actual),
      'expected ' + this._obj + ' to be a single line',
      'expected ' + this._obj + ' not to be a 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 the reverse of ' + expected,
      'expected ' + this._obj + ' not to be the reverse of ' + expected
    );
  });

  chai.Assertion.addChainableMethod('palindrome', function () {
    var actual = this._obj;

    return this.assert(
      chai.string.palindrome(actual),
      'expected ' + this._obj + ' to be a palindrome',
      'expected ' + this._obj + ' not to be a 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)'
    );
  });

  chai.Assertion.addChainableMethod('indexOf', function (substr, index) {
    var actual = this._obj;

    return this.assert(
      chai.string.indexOf(actual, substr, index),
      'expected ' + this._obj + ' to have ' + substr + ' on index ' + index,
      'expected ' + this._obj + ' to not have ' + substr + ' on index ' + index
    );
  });

  // 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.equalIgnoreSpaces = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.be.equalIgnoreSpaces(exp);
  };

  assert.notEqualIgnoreSpaces = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.not.be.equalIgnoreSpaces(exp);
  };

  assert.containIgnoreSpaces = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.be.containIgnoreSpaces(exp);
  };

  assert.notContainIgnoreSpaces = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.not.be.containIgnoreSpaces(exp);
  };

  assert.containIgnoreCase = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.be.containIgnoreCase(exp);
  };

  assert.notContainIgnoreCase = function (val, exp, msg) {
    new chai.Assertion(val, msg).to.not.be.containIgnoreCase(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);
  };

  assert.indexOf = function (str, substr, index, msg) {
    new chai.Assertion(str, msg).to.have.indexOf(substr, index);
  };

}));