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 org.junit.Test;
9   
10  /**
11   * Unit test for {@link SourceCodePositioner}.
12   */
13  public class SourceCodePositionerTest {
14  
15      private final String SOURCE_CODE = "abcd\ndefghi\n\njklmn\nopq";
16  
17      /**
18       * Tests whether the lines and columns are calculated correctly.
19       */
20      @Test
21      public void testLineNumberFromOffset() {
22  	SourceCodePositioner positioner = new SourceCodePositioner(SOURCE_CODE);
23  	
24  	int offset;
25  
26  	offset = SOURCE_CODE.indexOf('a');
27  	assertEquals(1, positioner.lineNumberFromOffset(offset));
28  	assertEquals(1, positioner.columnFromOffset(offset));
29  
30  	offset = SOURCE_CODE.indexOf('b');
31  	assertEquals(1, positioner.lineNumberFromOffset(offset));
32  	assertEquals(2, positioner.columnFromOffset(offset));
33  
34  	offset = SOURCE_CODE.indexOf('e');
35  	assertEquals(2, positioner.lineNumberFromOffset(offset));
36  	assertEquals(2, positioner.columnFromOffset(offset));
37  
38  	offset = SOURCE_CODE.indexOf('q');
39  	assertEquals(5, positioner.lineNumberFromOffset(offset));
40  	assertEquals(3, positioner.columnFromOffset(offset));
41      }
42  }