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