150c56da93e9fd801f846228a0e0646a4d90d02b
[WebKit-https.git] / LayoutTests / inspector / runtime / parse.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     function outputErrorSourceRange(range) {
9         var str = " ".repeat("Source: ".length);
10         str += " ".repeat(range.startOffset);
11         str += "^";
12         str += "~".repeat(range.endOffset - range.startOffset - 1);
13         InspectorTest.log(str);
14     }
15
16     let suite = InspectorTest.createAsyncSuite("Runtime.parse");
17
18     suite.addTestCase({
19         name: "SyntaxErrorType.None",
20         description: "Test source without an issue.",
21         test(resolve, reject) {
22             function testSource(source) {
23                 RuntimeAgent.parse(source, (error, result, message, range) => {
24                     InspectorTest.assert(!error, "Should not be a protocol error.");
25                     InspectorTest.expectThat(result === RuntimeAgent.SyntaxErrorType.None, "Should be SyntaxErrorType None.");
26                     InspectorTest.log("Source: " + source);
27                     InspectorTest.assert(!message);
28                     InspectorTest.assert(!range);
29                     InspectorTest.log("");
30                 });
31             }
32
33             // Complete programs without syntax errors (could have runtime errors).
34             testSource("");
35             testSource(";");
36             testSource("myVariable");
37             testSource("var x = 1; x");
38             testSource("[1,2,3]");
39             testSource("({})");
40             testSource("({prop:true}).x.x.x");
41             testSource("if(1)1");
42             InspectorBackend.runAfterPendingDispatches(resolve);
43         }
44     });
45
46     suite.addTestCase({
47         name: "SyntaxErrorType.UnterminatedLiteral",
48         description: "Test source with an unterminated literal.",
49         test(resolve, reject) {
50             function testSource(source) {
51                 RuntimeAgent.parse(source, (error, result, message, range) => {
52                     InspectorTest.assert(!error, "Should not be a protocol error.");
53                     InspectorTest.expectThat(result === RuntimeAgent.SyntaxErrorType.UnterminatedLiteral, "Should be SyntaxErrorType UnterminatedLiteral.");
54                     InspectorTest.log("Source: " + source);
55                     outputErrorSourceRange(range);
56                     InspectorTest.log("Error Message: " + message);
57                     InspectorTest.log("Range: " + JSON.stringify(range));
58                     InspectorTest.log("");
59                 });
60             }
61
62             // A literal that is not yet complete, but would be a single token.
63             testSource("0b"); // binary
64             testSource("0o"); // octal
65             testSource("0x"); // hex
66             testSource("1e"); // scientific notation number
67             testSource("'");  // string
68             testSource("\""); // string
69             testSource("var \\u007"); // identifier unicode escape
70             // testSource("/r"); // regexp - Not detected by JavaScriptCore.
71             InspectorBackend.runAfterPendingDispatches(resolve);
72         }
73     });
74
75     suite.addTestCase({
76         name: "SyntaxErrorType.Recoverable",
77         description: "Test source with a recoverable error.",
78         test(resolve, reject) {
79             function testSource(source) {
80                 RuntimeAgent.parse(source, (error, result, message, range) => {
81                     InspectorTest.assert(!error, "Should not be a protocol error.");
82                     InspectorTest.expectThat(result === RuntimeAgent.SyntaxErrorType.Recoverable, "Should be SyntaxErrorType Recoverable.");
83                     InspectorTest.log("Source: " + source);
84                     outputErrorSourceRange(range);
85                     InspectorTest.log("Error Message: " + message);
86                     InspectorTest.log("Error Range: " + JSON.stringify(range));
87                     InspectorTest.log("");
88                 });
89             }
90
91             // Not yet a syntax error, just an incomplete program.
92             testSource("(");
93             testSource("{");
94             testSource("!");
95             testSource("-"); // unary
96             testSource("+"); // unary
97             testSource("~"); // unary
98             testSource("1,");
99             testSource("var");
100             testSource("'use strict'; let"); // When not in strict mode, let is treated as the start of an identifier, not a keyword!
101             testSource("const");
102             testSource("var {x"); // destructuring
103             testSource("var [x"); // destructuring
104             testSource("var x,");
105             testSource("if(1)");
106             testSource("if(1){");
107             testSource("if(1){}else");
108             testSource("if(1){}else{");
109             testSource("while(1)");
110             testSource("while(1){");
111             testSource("for(;;)");
112             testSource("for(;;){");
113             testSource("()=>");  // arrow function
114             testSource("()=>{"); // arrow function
115             testSource("/*");    // comment
116             testSource("`");     // template string
117             testSource("`${1}"); // template string
118             testSource("`${");   // template string
119             testSource("function");
120             testSource("function foo");
121             testSource("function foo(");
122             testSource("function foo(){");
123             testSource("(function() {");
124             testSource("(function() {}");
125             testSource("(function() {})(");
126             testSource("switch(x)");
127             testSource("switch(x){");
128             testSource("switch(x){case");
129             testSource("switch(x){case 1");
130             testSource("switch(x){case 1:");
131             testSource("class");
132             testSource("class Klass");
133             testSource("class Klass {");
134             testSource("var x = {");
135             testSource("var x = {prop");
136             testSource("var x = {prop:");
137             testSource("var x = {prop:123");
138             testSource("var x = {prop:123 ");
139             testSource("var x = {prop:123,");
140             testSource("var x = {prop:123, ");
141             testSource("var x = {prop:123, 'prop2'");
142             testSource("var x = {prop:123, [12");
143             testSource("var x = {prop:123, [12]");
144             testSource("var x = {prop:123, [12]:");
145             testSource("var x = {prop:123, [12]:567");
146             testSource("var x = [");
147             testSource("var x = [1");
148             testSource("var x = [1,");
149             testSource("var x = [1,[]");
150             testSource("var x = [1,{}");
151             InspectorBackend.runAfterPendingDispatches(resolve);
152         }
153     });
154
155     suite.addTestCase({
156         name: "SyntaxErrorType.Irrecoverable",
157         description: "Test source with an irrecoverable issue.",
158         test(resolve, reject) {
159             function testSource(source) {
160                 RuntimeAgent.parse(source, (error, result, message, range) => {
161                     InspectorTest.assert(!error, "Should not be a protocol error.");
162                     InspectorTest.expectThat(result === RuntimeAgent.SyntaxErrorType.Irrecoverable, "Should be SyntaxErrorType Irrecoverable.");
163                     InspectorTest.log("Source: " + source);
164                     outputErrorSourceRange(range);
165                     InspectorTest.log("Error Message: " + message);
166                     InspectorTest.log("Error Range: " + JSON.stringify(range));
167                     InspectorTest.log("");
168                 });
169             }
170
171             // SyntaxErrors. Any typing after the syntax error is detected does not matter.
172             testSource("1$");
173             testSource("var x = {}}");
174             testSource("({}))");
175             testSource("var x = =");
176             testSource("const x;");
177             testSource("let x = 1; let x = 2;");
178             testSource("if x(1)");
179             InspectorBackend.runAfterPendingDispatches(resolve);
180         }
181     });
182
183     suite.runTestCasesAndFinish();
184 }
185 </script>
186 </head>
187 <body onload="runTest()">
188 <p>Tests for the Runtime.parse command.</p>
189 </body>
190 </html>