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 插件的想法。