View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.utils;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.api.DetailAST;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class ParserUtilTest {
31  
32      @Test
33      public void testIsProperUtilsClass() throws ReflectiveOperationException {
34          assertWithMessage("Constructor is not private")
35                  .that(isUtilsClassHasPrivateConstructor(ParserUtil.class))
36                  .isTrue();
37      }
38  
39      @Test
40      public void testCreationOfFakeCommentBlock() {
41          final DetailAST testCommentBlock =
42              ParserUtil.createBlockCommentNode("test_comment");
43          assertWithMessage("Invalid token type")
44                  .that(testCommentBlock.getType())
45                  .isEqualTo(TokenTypes.BLOCK_COMMENT_BEGIN);
46          assertWithMessage("Invalid text")
47                  .that(testCommentBlock.getText())
48                  .isEqualTo("/*");
49          assertWithMessage("Invalid line number")
50                  .that(testCommentBlock.getLineNo())
51                  .isEqualTo(0);
52  
53          final DetailAST contentCommentBlock = testCommentBlock.getFirstChild();
54          assertWithMessage("Invalid token type")
55                  .that(contentCommentBlock.getType())
56                  .isEqualTo(TokenTypes.COMMENT_CONTENT);
57          assertWithMessage("Invalid text")
58                  .that(contentCommentBlock.getText())
59                  .isEqualTo("*test_comment");
60          assertWithMessage("Invalid line number")
61                  .that(contentCommentBlock.getLineNo())
62                  .isEqualTo(0);
63          assertWithMessage("Invalid column number")
64                  .that(contentCommentBlock.getColumnNo())
65                  .isEqualTo(-1);
66  
67          final DetailAST endCommentBlock = contentCommentBlock.getNextSibling();
68          assertWithMessage("Invalid token type")
69                  .that(endCommentBlock.getType())
70                  .isEqualTo(TokenTypes.BLOCK_COMMENT_END);
71          assertWithMessage("Invalid text")
72                  .that(endCommentBlock.getText())
73                  .isEqualTo("*/");
74      }
75  }