1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 22 package com.github.javaparser.bdd.steps; 23 24 import com.github.javaparser.JavaParser; 25 import com.github.javaparser.ParseResult; 26 import com.github.javaparser.ParserConfiguration; 27 import com.github.javaparser.Range; 28 import com.github.javaparser.ast.CompilationUnit; 29 import com.github.javaparser.ast.body.*; 30 import com.github.javaparser.ast.comments.*; 31 import com.github.javaparser.ast.expr.Expression; 32 import com.github.javaparser.ast.expr.IntegerLiteralExpr; 33 import com.github.javaparser.ast.stmt.BlockStmt; 34 import com.github.javaparser.ast.stmt.ExpressionStmt; 35 import com.github.javaparser.ast.type.PrimitiveType; 36 import com.github.javaparser.printer.PrettyPrinter; 37 import com.github.javaparser.printer.PrettyPrinterConfiguration; 38 import org.jbehave.core.annotations.Alias; 39 import org.jbehave.core.annotations.Given; 40 import org.jbehave.core.annotations.Then; 41 import org.jbehave.core.annotations.When; 42 import org.jbehave.core.model.ExamplesTable; 43 import org.jbehave.core.steps.Parameters; 44 45 import java.io.IOException; 46 import java.nio.charset.Charset; 47 import java.util.Iterator; 48 import java.util.Set; 49 50 import static com.github.javaparser.ParseStart.COMPILATION_UNIT; 51 import static com.github.javaparser.Providers.provider; 52 import static com.github.javaparser.Range.range; 53 import static com.github.javaparser.bdd.TestUtils.getSampleStream; 54 import static com.github.javaparser.bdd.steps.SharedSteps.getMemberByTypeAndPosition; 55 import static org.hamcrest.CoreMatchers.*; 56 import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace; 57 import static org.junit.Assert.assertEquals; 58 import static org.junit.Assert.assertThat; 59 import static org.junit.Assert.fail; 60 61 public class CommentParsingSteps { 62 63 private CompilationUnit compilationUnit; 64 private CommentsCollection commentsCollection; 65 private String sourceUnderTest; 66 private ParserConfiguration configuration = new ParserConfiguration(); 67 private PrettyPrinter prettyPrinter = new PrettyPrinter(new PrettyPrinterConfiguration()); 68 69 @Given("the class:$classSrc") 70 public void givenTheClass(String classSrc) { 71 this.sourceUnderTest = classSrc.trim(); 72 } 73 74 @When("read sample \"$sampleName\" using encoding \"$encoding\"") 75 public void givenTheClassWithEncoding(String sampleName, String encoding) throws IOException { 76 sourceUnderTest = null; 77 ParseResult<CompilationUnit> parseResult = new JavaParser(new ParserConfiguration()).parse(COMPILATION_UNIT, provider(getSampleStream(sampleName), Charset.forName(encoding))); 78 commentsCollection = parseResult.getCommentsCollection().orElse(new CommentsCollection()); 79 } 80 81 @When("the class is parsed by the comment parser") 82 public void whenTheClassIsParsedByTheCommentParser() throws IOException { 83 ParseResult<CompilationUnit> parseResult = new JavaParser(new ParserConfiguration()).parse(COMPILATION_UNIT, provider(sourceUnderTest)); 84 commentsCollection = parseResult.getCommentsCollection().orElse(new CommentsCollection()); 85 } 86 87 @When("the do not consider annotations as node start for code attribution is $value on the Java parser") 88 public void whenTheDoNotConsiderAnnotationsAsNodeStartForCodeAttributionIsTrueOnTheJavaParser(boolean value) { 89 configuration.setDoNotConsiderAnnotationsAsNodeStartForCodeAttribution(value); 90 } 91 92 @When("the do not assign comments preceding empty lines is $value on the Java parser") 93 public void whenTheDoNotAssignCommentsPrecedingEmptyLinesIsTrueOnTheJavaParser(boolean value) { 94 configuration.setDoNotAssignCommentsPrecedingEmptyLines(value); 95 } 96 97 @When("the class is parsed by the Java parser") 98 public void whenTheClassIsParsedByTheJavaParser() { 99 ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(sourceUnderTest)); 100 compilationUnit = result.getResult().get(); 101 } 102 103 @Then("the Java parser cannot parse it because of an error") 104 public void javaParserCannotParseBecauseOfLexicalErrors() { 105 ParseResult<CompilationUnit> result = new JavaParser(configuration).parse(COMPILATION_UNIT, provider(sourceUnderTest)); 106 if (result.isSuccessful()) { 107 fail("Lexical error expected"); 108 } 109 } 110 111 @Then("the total number of comments is $expectedCount") 112 public void thenTheTotalNumberOfCommentsIs(int expectedCount) { 113 assertThat(commentsCollection.size(), is(expectedCount)); 114 } 115 116 private <T extends Comment> T getCommentAt(Set<T> set, int index) { 117 Iterator<T> iterator = set.iterator(); 118 T comment = null; 119 while (index >= 0) { 120 comment = iterator.next(); 121 index--; 122 } 123 return comment; 124 } 125 126 @Then("line comment $position is \"$expectedContent\"") 127 public void thenLineCommentIs(int position, String expectedContent) { 128 LineComment lineCommentUnderTest = getCommentAt(commentsCollection.getLineComments(), position - 1); 129 130 assertThat(lineCommentUnderTest.getContent(), is(expectedContent)); 131 } 132 133 @Then("block comment $position is \"$expectedContent\"") 134 public void thenBlockCommentIs(int position, String expectedContent) { 135 BlockComment lineCommentUnderTest = getCommentAt(commentsCollection.getBlockComments(), position - 1); 136 137 assertThat(lineCommentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 138 } 139 140 @Then("Javadoc comment $position is \"$expectedContent\"") 141 public void thenJavadocCommentIs(int position, String expectedContent) { 142 JavadocComment commentUnderTest = getCommentAt(commentsCollection.getJavadocComments(), position - 1); 143 144 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 145 } 146 147 @Then("the line comments have the following positions: $table") 148 public void thenTheLineCommentsHaveTheFollowingPositions(ExamplesTable examplesTable) { 149 int index = 0; 150 for (Parameters exampleRow : examplesTable.getRowsAsParameters()) { 151 Comment expectedLineComment = toComment(exampleRow, new LineComment()); 152 Comment lineCommentUnderTest = getCommentAt(commentsCollection.getLineComments(), index); 153 154 Range underTestRange = lineCommentUnderTest.getRange().get(); 155 Range expectedRange = expectedLineComment.getRange().get(); 156 157 assertThat(underTestRange.begin.line, is(expectedRange.begin.line)); 158 assertThat(underTestRange.begin.column, is(expectedRange.begin.column)); 159 assertThat(underTestRange.end.line, is(expectedRange.end.line)); 160 assertThat(underTestRange.end.column, is(expectedRange.end.column)); 161 index++; 162 } 163 } 164 165 @Then("the block comments have the following positions: $table") 166 public void thenTheBlockCommentsHaveTheFollowingPositions(ExamplesTable examplesTable) { 167 int index = 0; 168 for (Parameters exampleRow : examplesTable.getRowsAsParameters()) { 169 Comment expectedLineComment = toComment(exampleRow, new BlockComment()); 170 Comment lineCommentUnderTest = getCommentAt(commentsCollection.getBlockComments(), index); 171 172 Range underTestRange = lineCommentUnderTest.getRange().get(); 173 Range expectedRange = expectedLineComment.getRange().get(); 174 175 assertThat(underTestRange.begin.line, is(expectedRange.begin.line)); 176 assertThat(underTestRange.begin.column, is(expectedRange.begin.column)); 177 assertThat(underTestRange.end.line, is(expectedRange.end.line)); 178 assertThat(underTestRange.end.column, is(expectedRange.end.column)); 179 index++; 180 } 181 } 182 183 @Then("the Javadoc comments have the following positions: $table") 184 public void thenTheJavadocCommentsHaveTheFollowingPositions(ExamplesTable examplesTable) { 185 int index = 0; 186 for (Parameters exampleRow : examplesTable.getRowsAsParameters()) { 187 Comment expectedLineComment = toComment(exampleRow, new BlockComment()); 188 Comment lineCommentUnderTest = getCommentAt(commentsCollection.getJavadocComments(), index); 189 190 Range underTestRange = lineCommentUnderTest.getRange().get(); 191 Range expectedRange = expectedLineComment.getRange().get(); 192 193 assertThat(underTestRange.begin.line, is(expectedRange.begin.line)); 194 assertThat(underTestRange.begin.column, is(expectedRange.begin.column)); 195 assertThat(underTestRange.end.line, is(expectedRange.end.line)); 196 assertThat(underTestRange.end.column, is(expectedRange.end.column)); 197 index++; 198 } 199 } 200 201 @Then("it is printed as:$src") 202 public void isPrintedAs(String src) { 203 assertThat(prettyPrinter.print(compilationUnit).trim(), is(src.trim())); 204 } 205 206 @Then("the compilation unit is not commented") 207 public void thenTheCompilationUnitIsNotCommented() { 208 assertEquals(false, compilationUnit.getComment().isPresent()); 209 } 210 211 @Then("the compilation is commented \"$expectedContent\"") 212 public void thenTheCompilationIsCommentedCompilationUnitComment(String expectedContent) { 213 assertThat(compilationUnit.getComment().get().getContent(), is(expectedContent)); 214 } 215 216 @Then("the compilation unit has $expectedCount contained comments") 217 public void thenTheCompilationUnitHasContainedComments(int expectedCount) { 218 assertThat(compilationUnit.getComments().size(), is(expectedCount)); 219 } 220 221 @Then("the compilation unit has $expectedCount orphan comments") 222 public void thenTheCompilationUnitHasExpectedCountOrphanComments(int expectedCount) { 223 assertThat(compilationUnit.getOrphanComments().size(), is(expectedCount)); 224 } 225 226 @Then("the compilation unit orphan comment $position is \"$expectedContent\"") 227 public void thenTheCompilationUnitOrphanCommentIs(int position, String expectedContent) { 228 Comment commentUnderTest = compilationUnit.getOrphanComments().get(position - 1); 229 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 230 } 231 232 @Then("comment $commentPosition in compilation unit is not an orphan") 233 public void thenCommentInCompilationUnitIsNotAnOrphan(int commentPosition) { 234 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 235 assertThat(commentUnderTest.isOrphan(), is(false)); 236 } 237 238 @Then("comment $commentPosition in compilation unit is an orphan") 239 public void thenCommentInCompilationUnitIsAnOrphan(int commentPosition) { 240 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 241 assertThat(commentUnderTest.isOrphan(), is(true)); 242 } 243 244 @Then("comment $commentPosition in compilation unit is \"$expectedContent\"") 245 public void thenCommentInCompilationUnitIs(int position, String expectedContent) { 246 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(position - 1); 247 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 248 } 249 250 @Then("class $position is not commented") 251 public void thenClassIsNotCommented(int position) { 252 TypeDeclaration<?> classUnderTest = compilationUnit.getType(position - 1); 253 assertEquals(false, classUnderTest.getComment().isPresent()); 254 } 255 256 @Then("class $position is commented \"$expectedContent\"") 257 public void thenClassIsCommented(int position, String expectedContent) { 258 TypeDeclaration<?> classUnderTest = compilationUnit.getType(position - 1); 259 assertThat(classUnderTest.getComment().get().getContent(), is(expectedContent)); 260 } 261 262 @Then("class $position has $expectedCount total contained comments") 263 public void thenClassHasTotalContainedComments(int position, int expectedCount) { 264 TypeDeclaration<?> classUnderTest = compilationUnit.getType(position - 1); 265 assertThat(classUnderTest.getAllContainedComments().size(), is(expectedCount)); 266 } 267 268 @Then("class $position has $expectedCount orphan comment") 269 @Alias("class $position has $expectedCount orphan comments") 270 public void thenClassHasOrphanComments(int position, int expectedCount) { 271 TypeDeclaration<?> classUnderTest = compilationUnit.getType(position - 1); 272 assertThat(classUnderTest.getOrphanComments().size(), is(expectedCount)); 273 } 274 275 @Then("class $classPosition orphan comment $commentPosition is \"$expectedContent\"") 276 public void thenClassOrphanCommentIs(int classPosition, int commentPosition, String expectedContent) { 277 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 278 Comment commentUnderTest = classUnderTest.getOrphanComments().get(commentPosition - 1); 279 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 280 } 281 282 @Then("method $methodPosition in class $classPosition is commented \"$expectedContent\"") 283 public void thenMethodInClassIsCommented(int methodPosition, int classPosition, String expectedContent) { 284 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 285 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 286 MethodDeclaration.class); 287 assertThat(methodUnderTest.getComment().get().getContent(), equalToIgnoringWhiteSpace(expectedContent)); 288 } 289 290 @Then("method $methodPosition in class $classPosition has $expectedCount total contained comments") 291 public void thenMethodInClassHasTotalContainedComments(int methodPosition, int classPosition, int expectedCount) { 292 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 293 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 294 MethodDeclaration.class); 295 assertThat(methodUnderTest.getAllContainedComments().size(), is(expectedCount)); 296 } 297 298 @Then("comment $commentPosition in method $methodPosition in class $classPosition is \"$expectedContent\"") 299 public void thenCommentInMethodInClassIs(int commentPosition, int methodPosition, int classPosition, String expectedContent) { 300 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 301 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 302 MethodDeclaration.class); 303 Comment commentUnderTest = methodUnderTest.getAllContainedComments().get(commentPosition - 1); 304 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 305 } 306 307 @Then("method $methodPosition in class $classPosition has $expectedCount orphan comments") 308 public void thenMethodInClassHasOrphanComments(int methodPosition, int classPosition, int expectedCount) { 309 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 310 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 311 MethodDeclaration.class); 312 assertThat(methodUnderTest.getOrphanComments().size(), is(expectedCount)); 313 } 314 315 @Then("block statement in method $methodPosition in class $classPosition has $expectedCount total contained comments") 316 public void thenBlockStatementInMethodInClassHasTotalContainedComments(int methodPosition, int classPosition, int expectedCount) { 317 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 318 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 319 MethodDeclaration.class); 320 BlockStmt blockStmtUnderTest = methodUnderTest.getBody().orElse(null); 321 assertThat(blockStmtUnderTest.getAllContainedComments().size(), is(expectedCount)); 322 } 323 324 @Then("block statement in method $methodPosition in class $classPosition has $expectedCount orphan comments") 325 public void thenBlockStatementInMethodInClassHasOrphanComments(int methodPosition, int classPosition, int expectedCount) { 326 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 327 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 328 MethodDeclaration.class); 329 BlockStmt blockStmtUnderTest = methodUnderTest.getBody().orElse(null); 330 assertThat(blockStmtUnderTest.getOrphanComments().size(), is(expectedCount)); 331 } 332 333 @Then("block statement in method $methodPosition in class $classPosition orphan comment $commentPosition is \"$expectedContent\"") 334 public void thenBlockStatementInMethodInClassIs(int methodPosition, int classPosition, int commentPosition, String expectedContent) { 335 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 336 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 337 MethodDeclaration.class); 338 BlockStmt blockStmtUnderTest = methodUnderTest.getBody().orElse(null); 339 Comment commentUnderTest = blockStmtUnderTest.getOrphanComments().get(commentPosition - 1); 340 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 341 } 342 343 @Then("type of method $methodPosition in class $classPosition is commented \"$expectedContent\"") 344 public void thenTypeOfMethodInClassIsCommented(int methodPosition, int classPosition, String expectedContent) { 345 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 346 MethodDeclaration methodUnderTest = getMemberByTypeAndPosition(classUnderTest, methodPosition - 1, 347 MethodDeclaration.class); 348 Comment commentUnderTest = methodUnderTest.getType().getComment().get(); 349 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 350 } 351 352 @Then("field $fieldPosition in class $classPosition contains $expectedCount comments") 353 public void thenFieldInClassContainsComments(int fieldPosition, int classPosition, int expectedCount) { 354 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 355 FieldDeclaration fieldUnderTest = getMemberByTypeAndPosition(classUnderTest, fieldPosition - 1, 356 FieldDeclaration.class); 357 assertThat(fieldUnderTest.getAllContainedComments().size(), is(expectedCount)); 358 } 359 360 @Then("field $fieldPosition in class $classPosition is not commented") 361 public void thenFieldInClassIsNotCommented(int fieldPosition, int classPosition) { 362 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 363 FieldDeclaration fieldUnderTest = getMemberByTypeAndPosition(classUnderTest, fieldPosition - 1, 364 FieldDeclaration.class); 365 assertEquals(false, fieldUnderTest.getComment().isPresent()); 366 } 367 368 @Then("field $fieldPosition in class $classPosition is commented \"$expectedContent\"") 369 public void thenFieldInClassIsCommented(int fieldPosition, int classPosition, String expectedContent) { 370 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 371 FieldDeclaration fieldUnderTest = getMemberByTypeAndPosition(classUnderTest, fieldPosition - 1, 372 FieldDeclaration.class); 373 Comment commentUnderTest = fieldUnderTest.getComment().get(); 374 assertThat(commentUnderTest.getContent(), is(equalToIgnoringWhiteSpace(expectedContent))); 375 } 376 377 @Then("variable $variablePosition value of field $fieldPosition in class $classPosition is commented \"$expectedContent\"") 378 public void thenVariableValueOfFieldInClassIsCommented(int variablePosition, int fieldPosition, int classPosition, String expectedContent) { 379 TypeDeclaration<?> classUnderTest = compilationUnit.getType(classPosition - 1); 380 FieldDeclaration fieldUnderTest = getMemberByTypeAndPosition(classUnderTest, fieldPosition - 1, 381 FieldDeclaration.class); 382 VariableDeclarator variableUnderTest = fieldUnderTest.getVariable(variablePosition - 1); 383 Expression valueUnderTest = variableUnderTest.getInitializer().orElse(null); 384 Comment commentUnderTest = valueUnderTest.getComment().get(); 385 assertThat(commentUnderTest.getContent(), is(expectedContent)); 386 } 387 388 @Then("comment $commentPosition in compilation unit parent is ClassOrInterfaceDeclaration") 389 public void thenCommentInCompilationUnitParentIsClassOrInterfaceDeclaration(int commentPosition) { 390 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 391 assertThat(commentUnderTest.getParentNode().get(), instanceOf(ClassOrInterfaceDeclaration.class)); 392 } 393 394 @Then("comment $commentPosition in compilation unit commented node is ClassOrInterfaceDeclaration") 395 public void thenCommentInCompilationUnitCommentedNodeIsClassOrInterfaceDeclaration(int commentPosition) { 396 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 397 assertThat(commentUnderTest.getCommentedNode().get(), instanceOf(ClassOrInterfaceDeclaration.class)); 398 } 399 400 @Then("comment $commentPosition in compilation unit commented node is FieldDeclaration") 401 public void thenCommentInCompilationUnitCommentedNodeIsFieldDeclaration(int commentPosition) { 402 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 403 assertThat(commentUnderTest.getCommentedNode().get(), instanceOf(FieldDeclaration.class)); 404 } 405 406 @Then("comment $commentPosition in compilation unit commented node is IntegerLiteralExpr") 407 public void thenCommentInCompilationUnitCommentedNodeIsIntegerLiteralExpr(int commentPosition) { 408 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 409 assertThat(commentUnderTest.getCommentedNode().get(), instanceOf(IntegerLiteralExpr.class)); 410 } 411 412 @Then("comment $commentPosition in compilation unit commented node is ExpressionStmt") 413 public void thenCommentInCompilationUnitCommentedNodeIsIntegerExpressionStmt(int commentPosition) { 414 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 415 assertThat(commentUnderTest.getCommentedNode().get(), instanceOf(ExpressionStmt.class)); 416 } 417 418 @Then("comment $commentPosition in compilation unit commented node is PrimitiveType") 419 public void thenCommentInCompilationUnitCommentedNodeIsIntegerPrimitiveType(int commentPosition) { 420 Comment commentUnderTest = compilationUnit.getAllContainedComments().get(commentPosition - 1); 421 assertThat(commentUnderTest.getCommentedNode().get(), instanceOf(PrimitiveType.class)); 422 } 423 424 private Comment toComment(Parameters row, Comment comment) { 425 comment.setRange(range(Integer.parseInt(row.values().get("beginLine")), 426 Integer.parseInt(row.values().get("beginColumn")), 427 Integer.parseInt(row.values().get("endLine")), 428 Integer.parseInt(row.values().get("endColumn")))); 429 return comment; 430 } 431 } 432