View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  public class FullIdentTest {
26  
27      @Test
28      public void testToString() {
29          final DetailAST ast = new DetailAST();
30          ast.setType(TokenTypes.LITERAL_NEW);
31          ast.setColumnNo(14);
32          ast.setLineNo(15);
33          ast.setText("MyTest");
34  
35          final FullIdent indent = FullIdent.createFullIdent(ast);
36          Assert.assertEquals("Invalid full indent", "MyTest[15x14]", indent.toString());
37      }
38  
39      @Test
40      public void testNonValidCoordinatesWithNegative() {
41          final FullIdent fullIdent = prepareFullIdentWithCoordinates(14, 15);
42          Assert.assertEquals("Invalid full indent", "MyTest.MyTestik[15x14]", fullIdent.toString());
43      }
44  
45      @Test
46      public void testNonValidCoordinatesWithZero() {
47          final FullIdent fullIdent = prepareFullIdentWithCoordinates(0, 0);
48          Assert.assertEquals("Invalid full indent", "MyTest.MyTestik[15x14]", fullIdent.toString());
49      }
50  
51      private static FullIdent prepareFullIdentWithCoordinates(int columnNo, int lineNo) {
52          final DetailAST ast = new DetailAST();
53          ast.setType(TokenTypes.DOT);
54          ast.setColumnNo(1);
55          ast.setLineNo(2);
56          ast.setText("Root");
57  
58          final DetailAST ast2 = new DetailAST();
59          ast2.setType(TokenTypes.LE);
60          ast2.setColumnNo(columnNo);
61          ast2.setLineNo(lineNo);
62          ast2.setText("MyTestik");
63  
64          final DetailAST ast1 = new DetailAST();
65          ast1.setType(TokenTypes.LITERAL_NEW);
66          ast1.setColumnNo(14);
67          ast1.setLineNo(15);
68          ast1.setText("MyTest");
69  
70          ast.addChild(ast1);
71          ast.addChild(ast2);
72  
73          return FullIdent.createFullIdent(ast);
74      }
75  }