File indexing completed on 2024-04-28 15:28:39
0001 // String object 0002 shouldBe("'abc'.length", "3"); 0003 shouldBe("(new String('abcd')).length", "4"); 0004 shouldBe("String('abcde').length", "5"); 0005 0006 // String.charAt() 0007 shouldBe("'abc'.charAt(0)", "'a'"); 0008 shouldBe("'abc'.charAt(1)", "'b'"); 0009 shouldBe("'abc'.charAt(-1)", "''"); 0010 shouldBe("'abc'.charAt(99)", "''"); 0011 shouldBe("'abc'.charAt()", "'a'"); 0012 0013 // String.prototype.indexOf() 0014 shouldBe("'ab'.indexOf('a')", "0"); 0015 shouldBe("'ab'.indexOf('b')", "1"); 0016 shouldBe("'ab'.indexOf('x')", "-1"); 0017 shouldBe("'ab'.indexOf('')", "0"); 0018 shouldBe("''.indexOf('')", "0"); 0019 shouldBe("'ab'.indexOf('a', -1)", "0"); 0020 shouldBe("'ab'.indexOf('b', 1)", "1"); 0021 shouldBe("'ab'.indexOf('a', 1)", "-1"); 0022 shouldBe("' '.indexOf('', 1)", "1"); 0023 0024 // String.prototype.search() 0025 shouldBe("String('abc').search(/b/)", "1"); 0026 shouldBe("String('xyz').search(/b/)", "-1"); 0027 0028 // String.prototype.match() 0029 shouldBe("String('abcb').match(/b/) + ''", "'b'"); 0030 shouldBe("typeof String('abc').match(/b/)", "'object'"); 0031 shouldBe("'xyz'.match(/b/)", "null"); 0032 shouldBe("'xyz'.match(/b/g)", "null"); 0033 shouldBe("String('aabab'.match(/ab/g))", "'ab,ab'"); 0034 shouldBe("String('aabab'.match(/(a)(b)/))","'ab,a,b'"); 0035 shouldBe("String('aabab'.match(/(a)(b)/g))","'ab,ab'"); 0036 shouldBe("String('abc'.match(/./g))","'a,b,c'"); 0037 shouldBe("String('abc'.match(/.*/g))","'abc,'"); 0038 // match() doesn't modify lastIndex (at least in moz) 0039 shouldBe("var reg = /ab/g; 'aabab'.match(reg); reg.lastIndex", "0"); 0040 shouldBe("var reg = /ab/g; 'aabab'.match(reg).length", "2"); 0041 shouldBe("var reg = /ab/g; 'xxx'.match(reg); reg.lastIndex", "0"); 0042 shouldBe("var reg = /ab/g; 'xxx'.match(reg)", "null"); 0043 shouldBe( "myRe=/d(b+)d/g; 'cdbbdbsbz'.match(myRe)[0]", "'dbbd'" ); 0044 0045 // String.prototype.replace() 0046 shouldBe("'abcd'.replace(/b./, 'xy')", "'axyd'"); 0047 shouldBe("'abcd'.replace('bc', 'x')", "'axd'"); 0048 shouldBe("'abcd'.replace('x', 'y')", "'abcd'"); 0049 shouldBe("'abcd'.replace(/(ab)(cd)/,'$2$1')", "'cdab'"); 0050 shouldBe("'abcd'.replace(/(ab)(cd)/,'$2$1$')", "'cdab$'"); 0051 shouldBe("'BEGINabcEND'.replace(/abc/,'x$')", "'BEGINx$END'"); 0052 0053 var f2c_str, f2c_p1, f2c_offset, f2c_s; 0054 function f2c(x) { 0055 var s = String(x) 0056 var test = /(\d+(?:\.\d*)?)F\b/g 0057 return s.replace(test, 0058 function (str,p1,offset,s) { 0059 f2c_str=str; f2c_p1=p1; f2c_offset=offset; f2c_s=s; 0060 return ((p1-32) * 5/9) + "C"; 0061 } 0062 ) 0063 } 0064 0065 shouldBe("f2c('The value is 212F')", "'The value is 100C'"); 0066 shouldBe("f2c_str", "'212F'"); 0067 shouldBe("f2c_p1", "'212'"); 0068 shouldBe("f2c_offset", "13"); 0069 shouldBe("f2c_s", "'The value is 212F'"); 0070 0071 // String.prototype.split() 0072 shouldBe("'axb'.split('x').length", "2"); 0073 shouldBe("'axb'.split('x')[0]", "'a'"); 0074 shouldBe("'axb'.split('x')[1]", "'b'"); 0075 shouldBe("String('abc'.split(''))", "'a,b,c'"); 0076 shouldBe("String('abc'.split(new RegExp()))", "'a,b,c'"); 0077 shouldBe("''.split('').length", "0"); 0078 shouldBe("'axb'.split('x', 0).length", "0"); 0079 shouldBe("'axb'.split('x', 0)[0]", "undefined"); 0080 shouldBe("'axb'.split('x', 1).length", "1"); 0081 shouldBe("'axb'.split('x', 99).length", "2"); 0082 shouldBe("'axb'.split('y') + ''", "'axb'"); 0083 shouldBe("'axb'.split('y').length", "1"); 0084 shouldBe("''.split('x') + ''", "''"); 0085 shouldBe("'abc'.split() + ''", "'abc'"); 0086 shouldBe("'axxb'.split(/x/) + ''", "'a,,b'"); 0087 shouldBe("'axxb'.split(/x+/) + ''", "'a,b'"); 0088 shouldBe("'axxb'.split(/x*/) + ''", "'a,b'"); // NS 4.7 is wrong here 0089 // moved to evil-n.js shouldBe("''.split(/.*/).length", "0"); 0090 0091 // String.prototype.slice() 0092 shouldBe("'abcdef'.slice(2, 5)", "'cde'"); 0093 shouldBe("'abcdefghijklmnopqrstuvwxyz1234567890'.slice(-32, -6)", 0094 "'efghijklmnopqrstuvwxyz1234'"); 0095 0096 shouldBe("'abC1'.toUpperCase()", "'ABC1'"); 0097 shouldBe("'AbC2'.toLowerCase()", "'abc2'"); 0098 0099 // String.prototype.localeCompare() 0100 // ### not really testing the locale aspect 0101 shouldBe("'a'.localeCompare('a')", "0"); 0102 shouldBe("'a'.localeCompare('aa') < 0", "true"); 0103 shouldBe("'a'.localeCompare('x') < 0", "true"); 0104 shouldBe("'x'.localeCompare('a') > 0", "true"); 0105 shouldBe("''.localeCompare('')", "0"); 0106 shouldBe("''.localeCompare()", "0"); 0107 shouldBe("''.localeCompare(undefined)", "-1"); 0108 shouldBe("''.localeCompare(null)", "-1"); 0109 shouldBe("'a'.localeCompare('')", "1"); 0110 shouldBe("'a'.localeCompare()", "0"); 0111 0112 // String.prototype.substr() 0113 shouldBe('"abcdef".substr(1)', '"bcdef"'); 0114 shouldBe('"abcdef".substr(-1)', '"f"'); 0115 shouldBe('"abcdef".substr(1, 2)', '"bc"'); 0116 shouldBe('"abcdef".substr(-1, 2)', '"f"'); 0117 shouldBe('"abcdef".substr(1, 0)', '""'); 0118 shouldBe('"abcdef".substr(-1, 0)', '""'); 0119 shouldBe('"abcdef".substr(1, NaN)', '""'); 0120 shouldBe('"abcdef".substr(-1, NaN)', '""'); 0121 0122 // warning: prototype modification below 0123 shouldBe("'abc'[0]", "'a'"); 0124 shouldBeUndefined("'abc'[-1]"); 0125 shouldBeUndefined("'abc'[-4]"); 0126 shouldBeUndefined("'abc'[10]"); 0127 String.prototype[10] = "x"; 0128 shouldBe("'abc'[10]", "'x'"); 0129 0130 var foo = "This is a test."; 0131 var bar = foo.link( "javascript:foo( 'This ', 'is ', 'a test' )"); 0132 var html = "<a href=\"javascript:foo( 'This ', 'is ', 'a test' )\">This is a test.</a>" 0133 shouldBe("bar", "html"); 0134 0135