View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.lang.ecmascript.ast;
    5
    6   import static org.junit.Assert.assertEquals;
    7
    8   import java.io.Reader;
    9   import java.io.StringReader;
    10
    11  import net.sourceforge.pmd.PMD;
    12  import net.sourceforge.pmd.lang.ast.Node;
    13  import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions;
    14
    15  import org.junit.Test;
    16  import org.mozilla.javascript.ast.AstRoot;
    17
    18  public class EcmascriptParserTest {
    19
    20      @Test
    21      public void testLineNumbers() {
    22
    23  	EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
    24  	Reader sourceCode = new StringReader(SOURCE_CODE);
    25  	EcmascriptNode<AstRoot> node = parser.parse(sourceCode);
    26
    27  	assertEquals(1, node.getBeginLine());
    28  	assertEquals(1, node.getBeginColumn());
    29  	assertEquals(3, node.getEndLine());
    30  	assertEquals(1, node.getEndColumn());
    31
    32  	Node child = node.getFirstChildOfType(ASTFunctionNode.class);
    33  	assertEquals(1, child.getBeginLine());
    34  	assertEquals(1, child.getBeginColumn());
    35  	assertEquals(3, child.getEndLine());
    36  	assertEquals(1, child.getEndColumn());
    37
    38  	child = node.getFirstDescendantOfType(ASTFunctionCall.class);
    39  	assertEquals(2, child.getBeginLine());
    40  	assertEquals(3, child.getBeginColumn());
    41  	assertEquals(2, child.getEndLine());
    42  	assertEquals(16, child.getEndColumn());
    43      }
    44
    45      private static final String SOURCE_CODE =
    46  	    "function a() {" + PMD.EOL
    47  	  + "  alert('hello');" + PMD.EOL
    48  	  + "}" + PMD.EOL;
    49  }