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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
package otto
import (
"fmt"
"testing"
)
func TestRegExp(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[
/abc/.toString(),
/abc/gim.toString(),
""+/abc/gi.toString(),
new RegExp("1(\\d+)").toString(),
];
`, "/abc/,/abc/gim,/abc/gi,/1(\\d+)/")
test(`
[
new RegExp("abc").exec("123abc456"),
null === new RegExp("xyzzy").exec("123abc456"),
new RegExp("1(\\d+)").exec("123abc456"),
new RegExp("xyzzy").test("123abc456"),
new RegExp("1(\\d+)").test("123abc456"),
new RegExp("abc").exec("123abc456"),
];
`, "abc,true,123,23,false,true,abc")
test(`new RegExp("abc").toString()`, "/abc/")
test(`new RegExp("abc", "g").toString()`, "/abc/g")
test(`new RegExp("abc", "mig").toString()`, "/abc/gim")
result := test(`/(a)?/.exec('b')`, ",")
is(result._object().get("0"), "")
is(result._object().get("1"), "undefined")
is(result._object().get("length"), 2)
result = test(`/(a)?(b)?/.exec('b')`, "b,,b")
is(result._object().get("0"), "b")
is(result._object().get("1"), "undefined")
is(result._object().get("2"), "b")
is(result._object().get("length"), 3)
test(`/\u0041/.source`, "\\u0041")
test(`/\a/.source`, "\\a")
test(`/\;/.source`, "\\;")
test(`/a\a/.source`, "a\\a")
test(`/,\;/.source`, ",\\;")
test(`/ \ /.source`, " \\ ")
// Start sanity check...
test("eval(\"/abc/\").source", "abc")
test("eval(\"/\u0023/\").source", "#")
test("eval(\"/\u0058/\").source", "X")
test("eval(\"/\\\u0023/\").source == \"\\\u0023\"", true)
test("'0x' + '0058'", "0x0058")
test("'\\\\' + '0x' + '0058'", "\\0x0058")
// ...stop sanity check
test(`abc = '\\' + String.fromCharCode('0x' + '0058'); eval('/' + abc + '/').source`, "\\X")
test(`abc = '\\' + String.fromCharCode('0x0058'); eval('/' + abc + '/').source == "\\\u0058"`, true)
test(`abc = '\\' + String.fromCharCode('0x0023'); eval('/' + abc + '/').source == "\\\u0023"`, true)
test(`abc = '\\' + String.fromCharCode('0x0078'); eval('/' + abc + '/').source == "\\\u0078"`, true)
test(`
var abc = Object.getOwnPropertyDescriptor(RegExp, "prototype");
[ [ typeof RegExp.prototype ],
[ abc.writable, abc.enumerable, abc.configurable ] ];
`, "object,false,false,false")
})
}
func TestRegExp_global(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var abc = /(?:ab|cd)\d?/g;
var found = [];
do {
match = abc.exec("ab cd2 ab34 cd");
if (match !== null) {
found.push(match[0]);
} else {
break;
}
} while (true);
found;
`, "ab,cd2,ab3,cd")
})
}
func TestRegExp_exec(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
abc = /./g;
def = '123456';
ghi = 0;
while (ghi < 100 && abc.exec(def) !== null) {
ghi += 1;
}
[ ghi, def.length, ghi == def.length ];
`, "6,6,true")
test(`
abc = /[abc](\d)?/g;
def = 'a0 b c1 d3';
ghi = 0;
lastIndex = 0;
while (ghi < 100 && abc.exec(def) !== null) {
lastIndex = abc.lastIndex;
ghi += 1;
}
[ ghi, lastIndex ];
`, "3,7")
test(`
var abc = /[abc](\d)?/.exec("a0 b c1 d3");
[ abc.length, abc.input, abc.index, abc ];
`, "2,a0 b c1 d3,0,a0,0")
test(`raise:
var exec = RegExp.prototype.exec;
exec("Xyzzy");
`, "TypeError: Calling RegExp.exec on a non-RegExp object")
test(`
var abc = /\w{3}\d?/.exec("CE\uFFFFL\uFFDDbox127");
[ abc.input.length, abc.length, abc.input, abc.index, abc ];
`, "11,1,CE\uFFFFL\uFFDDbox127,5,box1")
test(`RegExp.prototype.exec.length`, 1)
test(`RegExp.prototype.exec.prototype`, "undefined")
})
}
func TestRegExp_test(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`RegExp.prototype.test.length`, 1)
test(`RegExp.prototype.test.prototype`, "undefined")
})
}
func TestRegExp_toString(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`RegExp.prototype.toString.length`, 0)
test(`RegExp.prototype.toString.prototype`, "undefined")
})
}
func TestRegExp_zaacbbbcac(t *testing.T) {
if true {
return
}
tt(t, func() {
test, _ := test()
// FIXME? TODO /(z)((a+)?(b+)?(c))*/.exec("zaacbbbcac")
test(`
var abc = /(z)((a+)?(b+)?(c))*/.exec("zaacbbbcac");
[ abc.length, abc.index, abc ];
`, "6,0,zaacbbbcac,z,ac,a,,c")
})
}
func TestRegExpCopying(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
abc = /xyzzy/i;
def = RegExp(abc);
abc.indicator = 1;
[ abc.indicator, def.indicator ];
`, "1,1")
test(`raise:
RegExp(new RegExp("\\d"), "1");
`, "TypeError: Cannot supply flags when constructing one RegExp from another")
})
}
func TestRegExp_multiline(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var abc = /s$/m.exec("pairs\nmakes\tdouble");
[ abc.length, abc.index, abc ];
`, "1,4,s")
})
}
func TestRegExp_source(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
[ /xyzzy/i.source, /./i.source ];
`, "xyzzy,.")
test(`
var abc = /./i;
var def = new RegExp(abc);
[ abc.source, def.source, abc.source === def.source ];
`, ".,.,true")
test(`
var abc = /./i;
var def = abc.hasOwnProperty("source");
var ghi = abc.source;
abc.source = "xyzzy";
[ def, abc.source ];
`, "true,.")
})
}
func TestRegExp_newRegExp(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
Math.toString();
var abc = new RegExp(Math,eval("\"g\""));
[ abc, abc.global ];
`, "/[object Math]/g,true")
})
}
func TestRegExp_flags(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var abc = /./i;
var def = new RegExp(abc);
[ abc.multiline == def.multiline, abc.global == def.global, abc.ignoreCase == def.ignoreCase ];
`, "true,true,true")
})
}
func TestRegExp_controlCharacter(t *testing.T) {
tt(t, func() {
test, _ := test()
for code := 0x41; code < 0x5a; code++ {
string_ := string(code - 64)
test(fmt.Sprintf(`
var code = 0x%x;
var string = String.fromCharCode(code %% 32);
var result = (new RegExp("\\c" + String.fromCharCode(code))).exec(string);
[ code, string, result ];
`, code), fmt.Sprintf("%d,%s,%s", code, string_, string_))
}
})
}
func TestRegExp_notNotEmptyCharacterClass(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var abc = /[\s\S]a/m.exec("a\naba");
[ abc.length, abc.input, abc ];
`, "1,a\naba,\na")
})
}
func TestRegExp_compile(t *testing.T) {
tt(t, func() {
test, _ := test()
test(`
var abc = /[\s\S]a/;
abc.compile('^\w+');
`, "undefined")
})
}
|