Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / script-tests / reserved-words-as-property.js
blobf86f3a1a15b8e3f033068eb4fe964c7e38b12a34
1 description("Tests to ensure that we can use ES reserved words as property names.");
3 var reservedWords = ["true", "false", "null", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for",
4 "function", "if", "in", "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with",
5 "class", "const", "enum", "export", "extends", "import", "super"];
7 var strictReservedWords = [
8 "implements",
9 "let",
10 "private",
11 "public",
12 "yield",
13 "interface",
14 "package",
15 "protected",
16 "static"
19 var unreservedWords = [
20 "abstract",
21 "boolean",
22 "byte",
23 "char",
24 "double",
25 "final",
26 "float",
27 "goto",
28 "int",
29 "long",
30 "native",
31 "short",
32 "synchronized",
33 "throws",
34 "transient",
35 "volatile"
39 function testWordEvalAndFunction(str, strShouldThrow) {
40 if (strShouldThrow) {
41 shouldThrow(str);
42 shouldThrow("(function(){"+str+"}); true");
43 } else {
44 shouldBeTrue(str);
45 shouldBeTrue("(function(){"+str+"}); true");
49 function testWord(word, strictPrefix, expectedResult) {
50 testWordEvalAndFunction(strictPrefix + "var " + word + "; true", expectedResult);
51 testWordEvalAndFunction(strictPrefix + "var " + word + " = 42; " + word + " === 42", expectedResult);
52 testWordEvalAndFunction(strictPrefix + "function g(" + word + "){ " + strictPrefix + " }; true", expectedResult);
53 testWordEvalAndFunction(strictPrefix + "/" + word + "/.test(function g(" + word + "){ " + strictPrefix + " })", expectedResult);
54 testWordEvalAndFunction(strictPrefix + "try{}catch(" + word + "){}; true", expectedResult);
55 testWordEvalAndFunction(strictPrefix + "function " + word + "(){ " + strictPrefix + " }; true", expectedResult);
56 // These should be allowed for all words, even reserved ones.
57 testWordEvalAndFunction(strictPrefix + "({ \"" + word + "\": 42 }." + word + " === 42)", false);
58 testWordEvalAndFunction(strictPrefix + "({ " + word + ": 42 }." + word + " === 42)", false);
59 testWordEvalAndFunction(strictPrefix + "({ get " + word + "(){}, set " + word + "(_){}, parsedOkay: 42 }.parsedOkay === 42)", false);
62 function testWordStrictAndNonStrict(word, condition) {
63 testWord(word, '', condition == "keyword");
64 testWord(word, '"use strict";', condition != "identifier");
67 for (var i = 0; i < reservedWords.length; i++)
68 testWordStrictAndNonStrict(reservedWords[i], "keyword");
69 for (var i = 0; i < strictReservedWords.length; i++)
70 testWordStrictAndNonStrict(strictReservedWords[i], "strict");
71 for (var i = 0; i < unreservedWords.length; i++)
72 testWordStrictAndNonStrict(unreservedWords[i], "identifier");
74 // test access via window.
75 var yield = 42;
76 shouldBeTrue("window.yield === 42");