Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jit-test / tests / basic / bug576837-regexp.js
1 /*
2  * Check that builtin character classes within ranges produce syntax
3  * errors.
4  *
5  * Note that, per the extension in bug 351463, SpiderMonkey permits hyphens
6  * adjacent to character class escapes in character classes, treating them as a
7  * hyphen pattern character. Therefore /[\d-\s]/ is okay
8  *
9  * Note: /\b/ is the backspace escape, which is a single pattern character,
10  * though it looks deceptively like a character class.
11  */
12
13 function isRegExpSyntaxError(pattern) {
14     try {
15         var re = new RegExp(pattern);
16     } catch (e) {
17         if (e instanceof SyntaxError)
18             return true;
19     }
20     return false;
21 }
22
23 assertEq(isRegExpSyntaxError('[C-\\s]'), true);
24 assertEq(isRegExpSyntaxError('[C-\\d]'), true);
25 assertEq(isRegExpSyntaxError('[C-\\W]'), true);
26 assertEq(isRegExpSyntaxError('[C-]'), false);
27 assertEq(isRegExpSyntaxError('[-C]'), false);
28 assertEq(isRegExpSyntaxError('[C-C]'), false);
29 assertEq(isRegExpSyntaxError('[C-C]'), false);
30 assertEq(isRegExpSyntaxError('[\\b-\\b]'), false);
31 assertEq(isRegExpSyntaxError('[\\B-\\B]'), false);
32 assertEq(isRegExpSyntaxError('[\\b-\\B]'), false);
33 assertEq(isRegExpSyntaxError('[\\B-\\b]'), true);
34 assertEq(isRegExpSyntaxError('[\\b-\\w]'), true);
35 assertEq(isRegExpSyntaxError('[\\B-\\w]'), true);
36
37 /* Extension. */
38 assertEq(isRegExpSyntaxError('[\\s-\\s]'), false);
39 assertEq(isRegExpSyntaxError('[\\W-\\s]'), false);
40 assertEq(isRegExpSyntaxError('[\\s-\\W]'), false);
41 assertEq(isRegExpSyntaxError('[\\W-C]'), false);
42 assertEq(isRegExpSyntaxError('[\\d-C]'), false);
43 assertEq(isRegExpSyntaxError('[\\s-C]'), false);
44 assertEq(isRegExpSyntaxError('[\\w-\\b]'), false);
45 assertEq(isRegExpSyntaxError('[\\w-\\B]'), false);