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.*;
    7
    8   import org.junit.Test;
    9
    10  public class SourceCodePositionerTest {
    11
    12      private final String SOURCE_CODE = "abcd\ndefghi\n\njklmn\nopq";
    13
    14      @Test
    15      public void testLineNumberFromOffset() {
    16  	SourceCodePositioner positioner = new SourceCodePositioner(SOURCE_CODE);
    17
    18  	int offset;
    19
    20  	offset = SOURCE_CODE.indexOf('a');
    21  	assertEquals(1, positioner.lineNumberFromOffset(offset));
    22  	assertEquals(1, positioner.columnFromOffset(offset));
    23
    24  	offset = SOURCE_CODE.indexOf('b');
    25  	assertEquals(1, positioner.lineNumberFromOffset(offset));
    26  	assertEquals(2, positioner.columnFromOffset(offset));
    27
    28  	offset = SOURCE_CODE.indexOf('e');
    29  	assertEquals(2, positioner.lineNumberFromOffset(offset));
    30  	assertEquals(2, positioner.columnFromOffset(offset));
    31
    32  	offset = SOURCE_CODE.indexOf('q');
    33  	assertEquals(5, positioner.lineNumberFromOffset(offset));
    34  	assertEquals(3, positioner.columnFromOffset(offset));
    35      }
    36  }