UNPKG

@achingbrain/chai-string

Version:
169 lines (130 loc) 3.86 kB
import * as chai from 'chai' import string from '../index.js' chai.use(string) var assert = chai.assert; var expect = chai.expect; chai.use(function (chai, utils) { var inspect = utils.objDisplay; chai.Assertion.addMethod('fail', function (message) { var obj = this._obj; new chai.Assertion(obj).is.a('function'); try { obj(); } catch (err) { this.assert( err instanceof chai.AssertionError, 'expected #{this} to fail, but it threw ' + inspect(err) ); this.assert( err.message === message, 'expected #{this} to fail with ' + inspect(message) + ', but got ' + inspect(err.message) ); return; } this.assert(false, 'expected #{this} to fail'); }); }); describe('chai-string', function () { beforeEach(function () { this.str = 'abcdef'; this.str2 = 'a\nb\tc\r d ef'; }); it('.startsWith', function () { assert.startsWith(this.str, 'abc'); }); it('.notStartsWith', function () { assert.notStartsWith(this.str, 'cba'); }); it('.notStartsWith (2)', function () { assert.notStartsWith('12abc', 12.0); }); it('.endsWith', function () { assert.endsWith(this.str, 'def'); }); it('.notEndsWith', function () { assert.notEndsWith(this.str, 'fed'); }); it('.notEndsWith (2)', function () { assert.notEndsWith('abc12', 12.0); }); it('.equalIgnoreCase', function () { assert.equalIgnoreCase(this.str, 'AbCdEf'); }); it('.notEqualIgnoreCase', function () { assert.notEqualIgnoreCase(this.str, 'abDDD'); }); it('.notEqualIgnoreCase (2)', function () { assert.notEqualIgnoreCase('12', 12); }); it('.notEqualIgnoreCase (3)', function () { assert.notEqualIgnoreCase(12, '12'); }); it('.equalIgnoreSpaces', function () { assert.equalIgnoreSpaces(this.str, this.str2); }); it('.notEqualIgnoreSpaces', function () { assert.notEqualIgnoreSpaces(this.str, this.str2 + 'g'); }); it('.notEqualIgnoreSpaces (2)', function () { assert.notEqualIgnoreSpaces('12', 12); }); it('.notEqualIgnoreSpaces (3)', function () { assert.notEqualIgnoreSpaces(12, '12'); }); it('.containIgnoreSpaces', function () { assert.containIgnoreSpaces(this.str, this.str2); }); it('.notContainIgnoreSpaces', function () { assert.notContainIgnoreSpaces(this.str, this.str2 + 'g'); }); it('.singleLine', function () { assert.singleLine(this.str); }); it('.notSingleLine', function () { assert.notSingleLine("abc\ndef"); }); it('.notSingleLine (2)', function () { assert.notSingleLine(12); }); it('.reverseOf', function () { assert.reverseOf(this.str, 'fedcba'); }); it('.notReverseOf', function () { assert.notReverseOf(this.str, 'aaaaa'); }); it('.notReverseOf (2)', function () { assert.notReverseOf('12', 12); }); it('.notReverseOf (3)', function () { assert.notReverseOf(12, '12'); }); it('.palindrome', function () { assert.palindrome('abcba'); assert.palindrome('abccba'); assert.palindrome(''); }); it('.notPalindrome', function () { assert.notPalindrome(this.str); }); it('.notPalindrome (2)', function () { assert.notPalindrome(12); }); it('.entriesCount', function () { assert.entriesCount('abcabd', 'ab', 2); assert.entriesCount('ababd', 'ab', 2); assert.entriesCount('abab', 'ab', 2); assert.entriesCount('', 'ab', 0); assert.entriesCount(12, 'ab', 0); assert.entriesCount('12', 12, 0); }); it('.indexOf', function () { assert.indexOf('abcabd', 'ab', 0); assert.indexOf('abcabd', 'ca', 2); assert.indexOf('ababab', 'ba', 1); assert.indexOf('ababab', 'ba', 1); assert.indexOf(12, '12', -1); assert.indexOf('12', 12, -1); expect('ababab').to.have.indexOf('ba', 1); }); });