2 shouldBe("'abc'.length", "3");
3 shouldBe("(new String('abcd')).length", "4");
4 shouldBe("String('abcde').length", "5");
7 shouldBe("'abc'.charAt(0)", "'a'");
8 shouldBe("'abc'.charAt(1)", "'b'");
9 shouldBe("'abc'.charAt(-1)", "''");
10 shouldBe("'abc'.charAt(99)", "''");
11 shouldBe("'abc'.charAt()", "'a'");
13 // String.prototype.indexOf()
14 shouldBe("'ab'.indexOf('a')", "0");
15 shouldBe("'ab'.indexOf('b')", "1");
16 shouldBe("'ab'.indexOf('x')", "-1");
17 shouldBe("'ab'.indexOf('')", "0");
18 shouldBe("''.indexOf('')", "0");
19 shouldBe("'ab'.indexOf('a', -1)", "0");
20 shouldBe("'ab'.indexOf('b', 1)", "1");
21 shouldBe("'ab'.indexOf('a', 1)", "-1");
22 shouldBe("' '.indexOf('', 1)", "1");
24 // String.prototype.search()
25 shouldBe("String('abc').search(/b/)", "1");
26 shouldBe("String('xyz').search(/b/)", "-1");
28 // String.prototype.match()
29 shouldBe("String('abcb').match(/b/) + ''", "'b'");
30 shouldBe("typeof String('abc').match(/b/)", "'object'");
31 shouldBe("'xyz'.match(/b/)", "null");
32 shouldBe("'xyz'.match(/b/g)", "null");
33 shouldBe("String('aabab'.match(/ab/g))", "'ab,ab'");
34 shouldBe("String('aabab'.match(/(a)(b)/))","'ab,a,b'");
35 shouldBe("String('aabab'.match(/(a)(b)/g))","'ab,ab'");
36 shouldBe("String('abc'.match(/./g))","'a,b,c'");
37 shouldBe("String('abc'.match(/.*/g))","'abc,'");
38 // match() doesn't modify lastIndex (at least in moz)
39 shouldBe("var reg = /ab/g; 'aabab'.match(reg); reg.lastIndex", "0");
40 shouldBe("var reg = /ab/g; 'aabab'.match(reg).length", "2");
41 shouldBe("var reg = /ab/g; 'xxx'.match(reg); reg.lastIndex", "0");
42 shouldBe("var reg = /ab/g; 'xxx'.match(reg)", "null");
43 shouldBe( "myRe=/d(b+)d/g; 'cdbbdbsbz'.match(myRe)[0]", "'dbbd'" );
45 // String.prototype.replace()
46 shouldBe("'abcd'.replace(/b./, 'xy')", "'axyd'");
47 shouldBe("'abcd'.replace('bc', 'x')", "'axd'");
48 shouldBe("'abcd'.replace('x', 'y')", "'abcd'");
49 shouldBe("'abcd'.replace(/(ab)(cd)/,'$2$1')", "'cdab'");
50 shouldBe("'abcd'.replace(/(ab)(cd)/,'$2$1$')", "'cdab$'");
51 shouldBe("'BEGINabcEND'.replace(/abc/,'x$')", "'BEGINx$END'");
53 var f2c_str
, f2c_p1
, f2c_offset
, f2c_s
;
56 var test
= /(\d+(?:\.\d*)?)F\b/g
57 return s
.replace(test
,
58 function (str
,p1
,offset
,s
) {
59 f2c_str
=str
; f2c_p1
=p1
; f2c_offset
=offset
; f2c_s
=s
;
60 return ((p1
-32) * 5/9) + "C";
65 shouldBe("f2c('The value is 212F')", "'The value is 100C'");
66 shouldBe("f2c_str", "'212F'");
67 shouldBe("f2c_p1", "'212'");
68 shouldBe("f2c_offset", "13");
69 shouldBe("f2c_s", "'The value is 212F'");
71 // String.prototype.split()
72 shouldBe("'axb'.split('x').length", "2");
73 shouldBe("'axb'.split('x')[0]", "'a'");
74 shouldBe("'axb'.split('x')[1]", "'b'");
75 shouldBe("String('abc'.split(''))", "'a,b,c'");
76 shouldBe("String('abc'.split(new RegExp()))", "'a,b,c'");
77 shouldBe("''.split('').length", "0");
78 shouldBe("'axb'.split('x', 0).length", "0");
79 shouldBe("'axb'.split('x', 0)[0]", "undefined");
80 shouldBe("'axb'.split('x', 1).length", "1");
81 shouldBe("'axb'.split('x', 99).length", "2");
82 shouldBe("'axb'.split('y') + ''", "'axb'");
83 shouldBe("'axb'.split('y').length", "1");
84 shouldBe("''.split('x') + ''", "''");
85 shouldBe("'abc'.split() + ''", "'abc'");
86 shouldBe("'axxb'.split(/x/) + ''", "'a,,b'");
87 shouldBe("'axxb'.split(/x+/) + ''", "'a,b'");
88 shouldBe("'axxb'.split(/x*/) + ''", "'a,b'"); // NS 4.7 is wrong here
89 // moved to evil-n.js shouldBe("''.split(/.*/).length", "0");
91 // String.prototype.slice()
92 shouldBe("'abcdef'.slice(2, 5)", "'cde'");
93 shouldBe("'abcdefghijklmnopqrstuvwxyz1234567890'.slice(-32, -6)",
94 "'efghijklmnopqrstuvwxyz1234'");
96 shouldBe("'abC1'.toUpperCase()", "'ABC1'");
97 shouldBe("'AbC2'.toLowerCase()", "'abc2'");
99 // String.prototype.localeCompare()
100 // ### not really testing the locale aspect
101 shouldBe("'a'.localeCompare('a')", "0");
102 shouldBe("'a'.localeCompare('aa') < 0", "true");
103 shouldBe("'a'.localeCompare('x') < 0", "true");
104 shouldBe("'x'.localeCompare('a') > 0", "true");
105 shouldBe("''.localeCompare('')", "0");
106 shouldBe("''.localeCompare()", "0");
107 shouldBe("''.localeCompare(undefined)", "-1");
108 shouldBe("''.localeCompare(null)", "-1");
109 shouldBe("'a'.localeCompare('')", "1");
110 shouldBe("'a'.localeCompare()", "0");
112 // warning: prototype modification below
113 shouldBe("'abc'[0]", "'a'");
114 shouldBeUndefined("'abc'[-1]");
115 shouldBeUndefined("'abc'[-4]");
116 shouldBeUndefined("'abc'[10]");
117 String
.prototype[10] = "x";
118 shouldBe("'abc'[10]", "'x'");
120 var foo
= "This is a test.";
121 var bar
= foo
.link( "javascript:foo( 'This ', 'is ', 'a test' )");
122 var html
= "<a href=\"javascript:foo( 'This ', 'is ', 'a test' )\">This is a test.</a>"
123 shouldBe("bar", "html");