Version 1
[yaffs-website] / node_modules / es5-shim / tests / spec / s-string.js
1 /* global describe, it, expect */
2
3 describe('String', function () {
4     'use strict';
5
6     describe('#trim()', function () {
7         var test = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFFHello, World!\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF';
8
9         it('trims all ES5 whitespace', function () {
10             expect(test.trim()).toBe('Hello, World!');
11             expect(test.trim().length).toBe(13);
12         });
13
14         it('does not trim the zero-width space', function () {
15             expect('\u200b'.trim()).toBe('\u200b');
16             expect('\u200b'.trim().length).toBe(1);
17         });
18     });
19
20     describe('#replace()', function () {
21         it('returns undefined for non-capturing groups', function () {
22             var groups = [];
23             'x'.replace(/x(.)?/g, function (m, group) {
24                 groups.push(group); /* "" in FF, `undefined` in CH/WK/IE */
25             });
26             expect(groups.length).toBe(1);
27             expect(groups[0]).toBeUndefined();
28         });
29
30         it('should not fail in Firefox', function () {
31             expect(function () {
32                 return '* alef\n* beth \n* gimel~0\n'.replace(
33                     /(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
34                     function (match, m1, m2, m3, m4) { return '<li>' + m4 + '</li>\n'; }
35                 );
36             }).not.toThrow();
37         });
38     });
39
40     describe('#split()', function () {
41         var test = 'ab';
42
43         it('If "separator" is undefined must return Array with one String - "this" string', function () {
44             expect(test.split()).toEqual([test]);
45             expect(test.split(void 0)).toEqual([test]);
46         });
47
48         it('If "separator" is undefined and "limit" set to 0 must return Array[]', function () {
49             expect(test.split(void 0, 0)).toEqual([]);
50         });
51
52         describe('Tests from Steven Levithan', function () {
53             it("''.split() results in ['']", function () {
54                 expect(''.split()).toEqual(['']);
55             });
56             it("''.split(/./) results in ['']", function () {
57                 expect(''.split(/./)).toEqual(['']);
58             });
59             it("''.split(/.?/) results in []", function () {
60                 expect(''.split(/.?/)).toEqual([]);
61             });
62             it("''.split(/.??/) results in []", function () {
63                 expect(''.split(/.??/)).toEqual([]);
64             });
65             it("'ab'.split(/a*/) results in ['', 'b']", function () {
66                 expect('ab'.split(/a*/)).toEqual(['', 'b']);
67             });
68             it("'ab'.split(/a*?/) results in ['a', 'b']", function () {
69                 expect('ab'.split(/a*?/)).toEqual(['a', 'b']);
70             });
71             it("'ab'.split(/(?:ab)/) results in ['', '']", function () {
72                 expect('ab'.split(/(?:ab)/)).toEqual(['', '']);
73             });
74             it("'ab'.split(/(?:ab)*/) results in ['', '']", function () {
75                 expect('ab'.split(/(?:ab)*/)).toEqual(['', '']);
76             });
77             it("'ab'.split(/(?:ab)*?/) results in ['a', 'b']", function () {
78                 expect('ab'.split(/(?:ab)*?/)).toEqual(['a', 'b']);
79             });
80             it("'test'.split('') results in ['t', 'e', 's', 't']", function () {
81                 expect('test'.split('')).toEqual(['t', 'e', 's', 't']);
82             });
83             it("'test'.split() results in ['test']", function () {
84                 expect('test'.split()).toEqual(['test']);
85             });
86             it("'111'.split(1) results in ['', '', '', '']", function () {
87                 expect('111'.split(1)).toEqual(['', '', '', '']);
88             });
89             it("'test'.split(/(?:)/, 2) results in ['t', 'e']", function () {
90                 expect('test'.split(/(?:)/, 2)).toEqual(['t', 'e']);
91             });
92             it("'test'.split(/(?:)/, -1) results in ['t', 'e', 's', 't']", function () {
93                 expect('test'.split(/(?:)/, -1)).toEqual(['t', 'e', 's', 't']);
94             });
95             it("'test'.split(/(?:)/, undefined) results in ['t', 'e', 's', 't']", function () {
96                 expect('test'.split(/(?:)/, undefined)).toEqual(['t', 'e', 's', 't']);
97             });
98             it("'test'.split(/(?:)/, null) results in []", function () {
99                 expect('test'.split(/(?:)/, null)).toEqual([]);
100             });
101             it("'test'.split(/(?:)/, NaN) results in []", function () {
102                 expect('test'.split(/(?:)/, NaN)).toEqual([]);
103             });
104             it("'test'.split(/(?:)/, true) results in ['t']", function () {
105                 expect('test'.split(/(?:)/, true)).toEqual(['t']);
106             });
107             it("'test'.split(/(?:)/, '2') results in ['t', 'e']", function () {
108                 expect('test'.split(/(?:)/, '2')).toEqual(['t', 'e']);
109             });
110             it("'test'.split(/(?:)/, 'two') results in []", function () {
111                 expect('test'.split(/(?:)/, 'two')).toEqual([]);
112             });
113             it("'a'.split(/-/) results in ['a']", function () {
114                 expect('a'.split(/-/)).toEqual(['a']);
115             });
116             it("'a'.split(/-?/) results in ['a']", function () {
117                 expect('a'.split(/-?/)).toEqual(['a']);
118             });
119             it("'a'.split(/-??/) results in ['a']", function () {
120                 expect('a'.split(/-??/)).toEqual(['a']);
121             });
122             it("'a'.split(/a/) results in ['', '']", function () {
123                 expect('a'.split(/a/)).toEqual(['', '']);
124             });
125             it("'a'.split(/a?/) results in ['', '']", function () {
126                 expect('a'.split(/a?/)).toEqual(['', '']);
127             });
128             it("'a'.split(/a??/) results in ['a']", function () {
129                 expect('a'.split(/a??/)).toEqual(['a']);
130             });
131             it("'ab'.split(/-/) results in ['ab']", function () {
132                 expect('ab'.split(/-/)).toEqual(['ab']);
133             });
134             it("'ab'.split(/-?/) results in ['a', 'b']", function () {
135                 expect('ab'.split(/-?/)).toEqual(['a', 'b']);
136             });
137             it("'ab'.split(/-??/) results in ['a', 'b']", function () {
138                 expect('ab'.split(/-??/)).toEqual(['a', 'b']);
139             });
140             it("'a-b'.split(/-/) results in ['a', 'b']", function () {
141                 expect('a-b'.split(/-/)).toEqual(['a', 'b']);
142             });
143             it("'a-b'.split(/-?/) results in ['a', 'b']", function () {
144                 expect('a-b'.split(/-?/)).toEqual(['a', 'b']);
145             });
146             it("'a-b'.split(/-??/) results in ['a', '-', 'b']", function () {
147                 expect('a-b'.split(/-??/)).toEqual(['a', '-', 'b']);
148             });
149             it("'a--b'.split(/-/) results in ['a', '', 'b']", function () {
150                 expect('a--b'.split(/-/)).toEqual(['a', '', 'b']);
151             });
152             it("'a--b'.split(/-?/) results in ['a', '', 'b']", function () {
153                 expect('a--b'.split(/-?/)).toEqual(['a', '', 'b']);
154             });
155             it("'a--b'.split(/-??/) results in ['a', '-', '-', 'b']", function () {
156                 expect('a--b'.split(/-??/)).toEqual(['a', '-', '-', 'b']);
157             });
158             it("''.split(/()()/) results in []", function () {
159                 expect(''.split(/()()/)).toEqual([]);
160             });
161             it("'.'.split(/()()/) results in ['.']", function () {
162                 expect('.'.split(/()()/)).toEqual(['.']);
163             });
164             it("'.'.split(/(.?)(.?)/) results in ['', '.', '', '']", function () {
165                 expect('.'.split(/(.?)(.?)/)).toEqual(['', '.', '', '']);
166             });
167             it("'.'.split(/(.??)(.??)/) results in ['.']", function () {
168                 expect('.'.split(/(.??)(.??)/)).toEqual(['.']);
169             });
170             it("'.'.split(/(.)?(.)?/) results in ['', '.', undefined, '']", function () {
171                 expect('.'.split(/(.)?(.)?/)).toEqual(['', '.', undefined, '']);
172             });
173             it("'A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\\/)?([^<>]+)>/) results in ['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']", function () {
174                 expect('A<B>bold</B>and<CODE>coded</CODE>'.split(/<(\/)?([^<>]+)>/)).toEqual(['A', undefined, 'B', 'bold', '/', 'B', 'and', undefined, 'CODE', 'coded', '/', 'CODE', '']);
175             });
176             it("'tesst'.split(/(s)*/) results in ['t', undefined, 'e', 's', 't']", function () {
177                 expect('tesst'.split(/(s)*/)).toEqual(['t', undefined, 'e', 's', 't']);
178             });
179             it("'tesst'.split(/(s)*?/) results in ['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']", function () {
180                 expect('tesst'.split(/(s)*?/)).toEqual(['t', undefined, 'e', undefined, 's', undefined, 's', undefined, 't']);
181             });
182             it("'tesst'.split(/(s*)/) results in ['t', '', 'e', 'ss', 't']", function () {
183                 expect('tesst'.split(/(s*)/)).toEqual(['t', '', 'e', 'ss', 't']);
184             });
185             it("'tesst'.split(/(s*?)/) results in ['t', '', 'e', '', 's', '', 's', '', 't']", function () {
186                 expect('tesst'.split(/(s*?)/)).toEqual(['t', '', 'e', '', 's', '', 's', '', 't']);
187             });
188             it("'tesst'.split(/(?:s)*/) results in ['t', 'e', 't']", function () {
189                 expect('tesst'.split(/(?:s)*/)).toEqual(['t', 'e', 't']);
190             });
191             it("'tesst'.split(/(?=s+)/) results in ['te', 's', 'st']", function () {
192                 expect('tesst'.split(/(?=s+)/)).toEqual(['te', 's', 'st']);
193             });
194             it("'test'.split('t') results in ['', 'es', '']", function () {
195                 expect('test'.split('t')).toEqual(['', 'es', '']);
196             });
197             it("'test'.split('es') results in ['t', 't']", function () {
198                 expect('test'.split('es')).toEqual(['t', 't']);
199             });
200             it("'test'.split(/t/) results in ['', 'es', '']", function () {
201                 expect('test'.split(/t/)).toEqual(['', 'es', '']);
202             });
203             it("'test'.split(/es/) results in ['t', 't']", function () {
204                 expect('test'.split(/es/)).toEqual(['t', 't']);
205             });
206             it("'test'.split(/(t)/) results in ['', 't', 'es', 't', '']", function () {
207                 expect('test'.split(/(t)/)).toEqual(['', 't', 'es', 't', '']);
208             });
209             it("'test'.split(/(es)/) results in ['t', 'es', 't']", function () {
210                 expect('test'.split(/(es)/)).toEqual(['t', 'es', 't']);
211             });
212             it("'test'.split(/(t)(e)(s)(t)/) results in ['', 't', 'e', 's', 't', '']", function () {
213                 expect('test'.split(/(t)(e)(s)(t)/)).toEqual(['', 't', 'e', 's', 't', '']);
214             });
215             it("'.'.split(/(((.((.??)))))/) results in ['', '.', '.', '.', '', '', '']", function () {
216                 expect('.'.split(/(((.((.??)))))/)).toEqual(['', '.', '.', '.', '', '', '']);
217             });
218             it("'.'.split(/(((((.??)))))/) results in ['.']", function () {
219                 expect('.'.split(/(((((.??)))))/)).toEqual(['.']);
220             });
221             it("'a b c d'.split(/ /, -(Math.pow(2, 32) - 1)) results in ['a']", function () {
222                 expect('a b c d'.split(/ /, -(Math.pow(2, 32) - 1))).toEqual(['a']);
223             });
224             it("'a b c d'.split(/ /, Math.pow(2, 32) + 1) results in ['a']", function () {
225                 expect('a b c d'.split(/ /, Math.pow(2, 32) + 1)).toEqual(['a']);
226             });
227             it("'a b c d'.split(/ /, Infinity) results in []", function () {
228                 expect('a b c d'.split(/ /, Infinity)).toEqual([]);
229             });
230         });
231
232         it('works with the second argument', function () {
233             expect('a b'.split(/ /, 1)).toEqual(['a']);
234         });
235     });
236
237     describe('#indexOf()', function () {
238         it('has basic support', function () {
239             expect('abcab'.indexOf('a')).toBe(0);
240             expect('abcab'.indexOf('a', 1)).toBe(3);
241             expect('abcab'.indexOf('a', 4)).toBe(-1);
242         });
243
244         it('works with unicode', function () {
245             expect('あいabcあいabc'.indexOf('あい')).toBe(0);
246             expect('あいabcあいabc'.indexOf('あい', 0)).toBe(0);
247             expect('あいabcあいabc'.indexOf('あい', 1)).toBe(5);
248             expect('あいabcあいabc'.indexOf('あい', 6)).toBe(-1);
249         });
250     });
251
252     describe('#lastIndexOf()', function () {
253         it('has the right length', function () {
254             expect(String.prototype.lastIndexOf.length).toBe(1);
255         });
256
257         it('has basic support', function () {
258             expect('abcd'.lastIndexOf('d')).toBe(3);
259             expect('abcd'.lastIndexOf('d', 3)).toBe(3);
260             expect('abcd'.lastIndexOf('d', 2)).toBe(-1);
261         });
262
263         it('works with unicode', function () {
264             expect('abcあい'.lastIndexOf('あい')).toBe(3);
265             expect('abcあい'.lastIndexOf('あい', 3)).toBe(3);
266             expect('abcあい'.lastIndexOf('あい', 2)).toBe(-1);
267         });
268     });
269 });