/
string.js
49 lines (45 loc) · 1.69 KB
/
string.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
/*
* use mocha to test me
* http://visionmedia.github.com/mocha/
*/
if (this['window'] !== this) {
require('./helper.js');
require('../es2pi.js');
}
(function(root){
'use strict';
describe('String.prototype.repeat', function () {
it('"string".repeat(0)', eq("string".repeat(0), ''));
it('"".repeat(100)', eq("".repeat(100), ''));
it('"string".repeat(3)', eq("string".repeat(3), 'stringstringstring'));
});
describe('String.prototype.escape', function() {
var chars = (function(){
var a = []
for (var i = 0; i < 0x3000; ++i); a.push(String.fromCharCode(i));
return a.join('');
})();
var e, eu, euc;
it ('chars.escape() === escape(chars)',
eq(chars.escape(), e = escape(chars)));
it ('chars.encodeURI() === encodeURI(chars)',
eq(chars.encodeURI(), eu = encodeURI(chars)));
it ('chars.encodeURIComponent() === encodeURIComponent(chars)',
eq(chars.encodeURIComponent(), euc = encodeURIComponent(chars)));
it ('e.unescape() === chars',
eq(e.unescape(), chars));
it ('eu.decodeURI() === chars',
eq(eu.decodeURI(), chars));
it ('euc.decodeURIComponent() === chars',
eq(euc.decodeURIComponent(), chars));
});
describe('String.prototype.interpolate', function() {
var t = "#{here} #{there} #{nowhere}",
o = {here:'koko', there:'asoko'},
r;
it(t, eq(t.interpolate(o), 'koko asoko undefined'));
t = "$[here] $[there] $[nowhere]";
r = /\$\[(.+?)\]/g;
it(t + ", "+ r, eq(t.interpolate(o, r), 'koko asoko undefined'));
});
})(this);