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.gui;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  import com.google.common.collect.ImmutableList;
32  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.DetailNode;
35  import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode;
36  
37  public class CodeSelectorPresentationTest extends AbstractPathTestSupport {
38  
39      private MainFrameModel model;
40  
41      private DetailAST tree;
42  
43      private ImmutableList<Integer> linesToPosition;
44  
45      @BeforeEach
46      public void loadFile() throws Exception {
47          model = new MainFrameModel();
48          model.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS);
49          model.openFile(new File(getPath("InputCodeSelectorPresentation.java")));
50          tree = ((DetailAST) model.getParseTreeTableModel().getRoot())
51                  .getFirstChild().getNextSibling();
52          linesToPosition = ImmutableList.copyOf(convertLinesToPosition(model.getLinesToPosition()));
53      }
54  
55      @Override
56      protected String getPackageLocation() {
57          return "com/puppycrawl/tools/checkstyle/gui/codeselectorpresentation";
58      }
59  
60      /**
61       * Converts lineToPosition from multicharacter to one character line separator
62       * needs to support crossplatform line separators.
63       *
64       * @param systemLinesToPosition lines to position mapping for current system
65       * @return lines to position mapping with one character line separator
66       */
67      private static List<Integer> convertLinesToPosition(List<Integer> systemLinesToPosition) {
68          final List<Integer> convertedLinesToPosition = new ArrayList<>();
69          final int lineSeparationCorrection = System.lineSeparator().length() - 1;
70          convertedLinesToPosition.add(0, systemLinesToPosition.get(0));
71          for (int i = 1; i < systemLinesToPosition.size(); i++) {
72              convertedLinesToPosition.add(i,
73                      systemLinesToPosition.get(i) - lineSeparationCorrection * (i - 1));
74          }
75          return convertedLinesToPosition;
76      }
77  
78      @Test
79      public void testDetailASTSelection() {
80          final CodeSelectorPresentation selector = new CodeSelectorPresentation(tree,
81                  linesToPosition);
82          selector.findSelectionPositions();
83          assertWithMessage("Invalid selection start")
84                  .that(selector.getSelectionStart())
85                  .isEqualTo(94);
86          assertWithMessage("Invalid selection end")
87                  .that(selector.getSelectionEnd())
88                  .isEqualTo(279);
89      }
90  
91      @Test
92      public void testDetailASTLeafSelection() {
93          final DetailAST leaf = tree.getLastChild().getFirstChild();
94          final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
95                  linesToPosition);
96          selector.findSelectionPositions();
97          assertWithMessage("Invalid selection start")
98                  .that(selector.getSelectionStart())
99                  .isEqualTo(130);
100         assertWithMessage("Invalid selection end")
101                 .that(selector.getSelectionEnd())
102                 .isEqualTo(131);
103     }
104 
105     @Test
106     public void testDetailASTNoSelection() {
107         final DetailAST leaf = tree.getFirstChild();
108         final CodeSelectorPresentation selector = new CodeSelectorPresentation(leaf,
109                 linesToPosition);
110         selector.findSelectionPositions();
111         assertWithMessage("Invalid selection start")
112                 .that(selector.getSelectionStart())
113                 .isEqualTo(94);
114         assertWithMessage("Invalid selection end")
115                 .that(selector.getSelectionEnd())
116                 .isEqualTo(94);
117     }
118 
119     @Test
120     public void testDetailNodeSelection() {
121         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
122                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
123         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadoc,
124                 linesToPosition);
125         selector.findSelectionPositions();
126         assertWithMessage("Invalid selection start")
127                 .that(selector.getSelectionStart())
128                 .isEqualTo(74);
129         assertWithMessage("Invalid selection end")
130                 .that(selector.getSelectionEnd())
131                 .isEqualTo(96);
132     }
133 
134     @Test
135     public void testDetailNodeLeafSelection() {
136         final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel()
137                 .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0);
138         final DetailNode javadocLeaf = javadoc.getChildren()[2];
139         final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadocLeaf,
140                 linesToPosition);
141         selector.findSelectionPositions();
142         assertWithMessage("Invalid selection start")
143                 .that(selector.getSelectionStart())
144                 .isEqualTo(76);
145         assertWithMessage("Invalid selection end")
146                 .that(selector.getSelectionEnd())
147                 .isEqualTo(90);
148     }
149 
150 }