View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.grammars.comments;
21  
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;
26  import com.puppycrawl.tools.checkstyle.JavaParser;
27  import com.puppycrawl.tools.checkstyle.api.Comment;
28  
29  public class CommentsTest extends AbstractTreeTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/grammars/comments";
34      }
35  
36      @Test
37      public void testCompareExpectedTreeWithInput1() throws Exception {
38          verifyAst(getPath("InputComments1Ast.txt"), getPath("InputComments1.java"),
39                  JavaParser.Options.WITH_COMMENTS);
40      }
41  
42      @Test
43      public void testCompareExpectedTreeWithInput2() throws Exception {
44          verifyAst(getPath("InputComments2Ast.txt"), getPath("InputComments2.java"),
45                  JavaParser.Options.WITH_COMMENTS);
46      }
47  
48      @Test
49      public void testToString() {
50          final Comment comment = new Comment(new String[] {"value"}, 1, 2, 3);
51          Assert.assertEquals("Invalid toString result",
52                  "Comment[text=[value], startLineNo=2, endLineNo=2, startColNo=1, endColNo=3]",
53                  comment.toString());
54      }
55  
56      @Test
57      public void testGetCommentMeasures() {
58          final String[] commentText = {"/**",
59              "     * Creates new instance.",
60              "     * @param text the lines that make up the comment.",
61              "     * @param firstCol number of the first column of the comment.",
62              "     * @param lastLine number of the last line of the comment.",
63              "     * @param lastCol number of the last column of the comment.",
64              "     */"};
65          final Comment comment = new Comment(commentText, 5, 49, 66);
66  
67          Assert.assertEquals("Invalid comment start line number", 43, comment.getStartLineNo());
68          Assert.assertEquals("Invalid comment start column number", 5, comment.getStartColNo());
69          Assert.assertEquals("Invalid comment end line number", 49, comment.getEndLineNo());
70          Assert.assertEquals("Invalid comment end column number", 66, comment.getEndColNo());
71      }
72  
73      @Test
74      public void testIntersects() {
75          final String[] commentText = {"// compute a single number for start and end",
76              "// to simplify conditional logic"};
77          final Comment comment = new Comment(commentText, 9, 89, 53);
78  
79          Assert.assertTrue("Invalid intersection result", comment.intersects(89, 9, 89, 41));
80          Assert.assertTrue("Invalid intersection result", comment.intersects(89, 53, 90, 50));
81          Assert.assertTrue("Invalid intersection result", comment.intersects(87, 7, 88, 9));
82          Assert.assertFalse("Invalid intersection result", comment.intersects(90, 7, 91, 20));
83          Assert.assertFalse("Invalid intersection result", comment.intersects(89, 56, 89, 80));
84      }
85  
86  }