-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started implementing tests for column numbers. Fixing bugs as found.
- Loading branch information
1 parent
05c0476
commit 433f116
Showing
3 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
tests/src/test/java/org/mozilla/javascript/tests/ParserLineColumnNumberTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mozilla.javascript.CompilerEnvirons; | ||
import org.mozilla.javascript.Context; | ||
import org.mozilla.javascript.ast.AstNode; | ||
import org.mozilla.javascript.ast.AstRoot; | ||
import org.mozilla.javascript.ast.ExpressionStatement; | ||
import org.mozilla.javascript.ast.IfStatement; | ||
import org.mozilla.javascript.ast.Name; | ||
import org.mozilla.javascript.ast.Scope; | ||
import org.mozilla.javascript.ast.VariableDeclaration; | ||
import org.mozilla.javascript.ast.VariableInitializer; | ||
|
||
class ParserLineColumnNumberTest { | ||
CompilerEnvirons environment; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
environment = new CompilerEnvirons(); | ||
} | ||
|
||
@Test | ||
void declarationVar() { | ||
AstRoot root = myParse("var a =\n1;"); | ||
VariableDeclaration varStatement = assertInstanceOf(VariableDeclaration.class, root.getFirstChild()); | ||
assertEquals(0, varStatement.getLineno()); | ||
assertEquals(1, varStatement.column()); | ||
assertEquals(1, varStatement.getVariables().size()); | ||
VariableInitializer varInitializer = varStatement.getVariables().get(0); | ||
assertEquals(0, varInitializer.getTarget().getLineno()); | ||
assertEquals(5, varInitializer.getTarget().column()); | ||
assertEquals(1, varInitializer.getInitializer().getLineno()); | ||
assertEquals(1, varInitializer.getInitializer().column()); | ||
} | ||
|
||
@Test | ||
void declarationConst() { | ||
AstRoot root = myParse("const a =\n1;"); | ||
VariableDeclaration varStatement = assertInstanceOf(VariableDeclaration.class, root.getFirstChild()); | ||
assertEquals(0, varStatement.getLineno()); | ||
assertEquals(1, varStatement.column()); | ||
assertEquals(1, varStatement.getVariables().size()); | ||
VariableInitializer varInitializer = varStatement.getVariables().get(0); | ||
assertEquals(0, varInitializer.getTarget().getLineno()); | ||
assertEquals(7, varInitializer.getTarget().column()); | ||
assertEquals(1, varInitializer.getInitializer().getLineno()); | ||
assertEquals(1, varInitializer.getInitializer().column()); | ||
} | ||
|
||
@Test | ||
void declarationLet() { | ||
AstRoot root = myParse("let a =\n1;"); | ||
VariableDeclaration varStatement = assertInstanceOf(VariableDeclaration.class, root.getFirstChild()); | ||
assertEquals(0, varStatement.getLineno()); | ||
assertEquals(1, varStatement.column()); | ||
assertEquals(1, varStatement.getVariables().size()); | ||
VariableInitializer varInitializer = varStatement.getVariables().get(0); | ||
assertEquals(0, varInitializer.getTarget().getLineno()); | ||
assertEquals(5, varInitializer.getTarget().column()); | ||
assertEquals(1, varInitializer.getInitializer().getLineno()); | ||
assertEquals(1, varInitializer.getInitializer().column()); | ||
} | ||
|
||
@Test | ||
void controlFlowIf() { | ||
AstRoot root = myParse( | ||
"if (a) {\n" + | ||
" b\n" + | ||
"} else {\n" + | ||
" c;\n" + | ||
"}"); | ||
IfStatement ifStatement = assertInstanceOf(IfStatement.class, root.getFirstChild()); | ||
assertEquals(0, ifStatement.getLineno()); | ||
assertEquals(1, ifStatement.column()); | ||
assertEquals(0, ifStatement.getCondition().getLineno()); | ||
assertEquals(5, ifStatement.getCondition().column()); | ||
|
||
Scope thenPart = assertInstanceOf(Scope.class, ifStatement.getThenPart()); | ||
assertEquals(0, thenPart.getLineno()); | ||
assertEquals(8, thenPart.column()); | ||
assertEquals(1, thenPart.getStatements().get(0).getLineno()); | ||
assertEquals(3, thenPart.getStatements().get(0).column()); | ||
|
||
assertEquals(3, ifStatement.getElsePart().getFirstChild().getLineno()); | ||
assertEquals(3, ifStatement.getElsePart().getFirstChild().column()); | ||
} | ||
|
||
@Test | ||
void expressionName() { | ||
AstRoot root = myParse(" a"); | ||
ExpressionStatement nameExpr = assertInstanceOf(ExpressionStatement.class, root.getFirstChild()); | ||
assertEquals(0, nameExpr.getLineno()); | ||
assertEquals(2, nameExpr.column()); | ||
Name name = assertInstanceOf(Name.class, nameExpr.getExpression()); | ||
assertEquals(0, name.getLineno()); | ||
assertEquals(2, name.column()); | ||
} | ||
|
||
@Test | ||
void expressionInBlock() { | ||
AstRoot root = myParse(" {a}"); | ||
Scope block = assertInstanceOf(Scope.class, root.getFirstChild()); | ||
assertEquals(0, block.getLineno()); | ||
assertEquals(2, block.column()); | ||
|
||
assertEquals(1, block.getStatements().size()); | ||
AstNode firstStatement = block.getStatements().get(0); | ||
assertEquals(0, firstStatement.getLineno()); | ||
assertEquals(3, firstStatement.column()); | ||
} | ||
|
||
private AstRoot myParse(String source) { | ||
environment.setLanguageVersion(Context.VERSION_ES6); | ||
return ParserTest.parse(source, null, null, true, environment); | ||
} | ||
} |