chai-string
用於 Chai 的匹配器,以協助常見的字串比較斷言。
使用方式
瀏覽器
<script src="chai.js"></script>
<script src="chai-string.js"></script>
伺服器
var chai = require('chai');
chai.use(require('chai-string'));
斷言
- startsWith/startWith
- endsWith/endWith
- equalIgnoreCase
- equalIgnoreSpaces
- containIgnoreSpaces
- singleLine
- reverseOf
- palindrome
- entriesCount
- indexOf
所有斷言都為 BDD 和 TDD 語法定義,但存在一些別名,使斷言在兩種風格下看起來都不錯。
var d1 = 'abcdef',
d2 = 'abc';
d1.should.startWith.d2
expect(d1).to.startsWith(d2)
assert.startsWith(d1, d2)
startsWith/startWith
assert.startsWith('abcdef', 'abc');
expect('abcdef').to.startsWith('abc');
'abcdef'.should.startWith('abc');
endsWith/endWith
assert.endsWith('abcdef', 'def');
expect('abcdef').to.endsWith('def');
'abcdef'.should.endWith('def');
equalIgnoreCase
assert.equalIgnoreCase('abcdef', 'AbCdEf');
expect('abcdef').to.equalIgnoreCase('AbCdEf');
equalIgnoreSpaces
assert.equalIgnoreSpaces('abcdef', 'a\nb\tc\r d ef');
expect('abcdef').to.equalIgnoreSpaces('a\nb\tc\r d ef');
containIgnoreSpaces
assert.containIgnoreSpaces('abcdefgh', 'a\nb\tc\r d ef');
expect('abcdefgh').to.containIgnoreSpaces('a\nb\tc\r d ef');
containIgnoreCase
assert.containIgnoreCase('abcdefgh', 'AbcDefGH');
expect('abcdefgh').to.containIgnoreCase('AbcDefGH');
'abcdef'.should.containIgnoreCase('cDe');
singleLine
assert.singleLine('abcdef');
expect('abcdef').to.be.singleLine();
reverseOf
assert.reverseOf('abcdef', 'fedcba');
expect('abcdef').to.be.reverseOf('fedcba');
palindrome
assert.palindrome('abccba');
expect('abccba').to.be.palindrome();
entriesCount
assert.entriesCount('abcabd', 'ab', 2);
expect('abcabd').to.have.entriesCount('ab', 2);
indexOf
assert.indexOf('abcabd', 'ab', 0);
expect('abcabd').to.have.indexOf('ab', 0);
感謝
感謝 chai-datetime 模組,為我提供了如何建構和測試 Chai 外掛程式的想法。