1 /* 2 * Copyright (C) 2007-2010 Júlio 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.steps; 23 24 import com.github.javaparser.Position; 25 import com.github.javaparser.Range; 26 import org.jbehave.core.annotations.BeforeScenario; 27 import org.jbehave.core.annotations.Given; 28 import org.jbehave.core.annotations.Then; 29 import org.jbehave.core.annotations.When; 30 31 import static com.github.javaparser.Position.pos; 32 import static com.github.javaparser.Range.range; 33 import static org.junit.jupiter.api.Assertions.assertFalse; 34 import static org.junit.jupiter.api.Assertions.assertTrue; 35 36 public class PositionRangeSteps { 37 38 private Position position; 39 private Position secondPosition; 40 private Range range; 41 private Range secondRange; 42 43 @BeforeScenario reset()44 public void reset() { 45 position = null; 46 secondPosition = null; 47 range = null; 48 secondRange = null; 49 } 50 /* 51 * Given steps 52 */ 53 54 @Given("the position $line, $column") givenThePosition(int line, int column)55 public void givenThePosition(int line, int column) { 56 this.position = pos(line, column); 57 } 58 59 @Given("the range $line1, $column1 - $line2, $column2") givenTheRange(int line1, int column1, int line2, int column2)60 public void givenTheRange(int line1, int column1, int line2, int column2) { 61 this.range = range(line1, column1, line2, column2); 62 } 63 64 /* 65 * When steps 66 */ 67 68 @When("I compare to position $line, $column") iCompareToPosition(int line, int column)69 public void iCompareToPosition(int line, int column) { 70 secondPosition = pos(line, column); 71 } 72 73 @When("I compare to range $line1, $column1 - $line2, $column2") whenICompareToRange(int line1, int column1, int line2, int column2)74 public void whenICompareToRange(int line1, int column1, int line2, int column2) { 75 this.secondRange = range(line1, column1, line2, column2); 76 } 77 78 /* 79 * Then steps 80 */ 81 82 @Then("the positions are equal") thenThePositionsAreEqual()83 public void thenThePositionsAreEqual() { 84 assertTrue(position.equals(secondPosition)); 85 } 86 87 @Then("it is after the {first|} position") thenItIsAfterTheFirstPosition()88 public void thenItIsAfterTheFirstPosition() { 89 if (secondPosition != null) { 90 assertTrue(secondPosition.isAfter(position)); 91 } else { 92 assertTrue(secondRange.isAfter(position)); 93 } 94 } 95 96 @Then("it is before the {first|} position") thenItIsBeforeTheFirstPosition()97 public void thenItIsBeforeTheFirstPosition() { 98 if (secondPosition != null) { 99 assertTrue(secondPosition.isBefore(position)); 100 } else { 101 assertTrue(secondRange.isBefore(position)); 102 } 103 } 104 105 @Then("the positions are not equal") thenThePositionsAreNotEqual()106 public void thenThePositionsAreNotEqual() { 107 assertFalse(position.equals(secondPosition)); 108 } 109 110 @Then("it is not after the {first|} position") thenItIsNotAfterTheFirstPosition()111 public void thenItIsNotAfterTheFirstPosition() { 112 assertFalse(secondPosition.isAfter(position)); 113 } 114 115 @Then("it is not before the {first|} position") thenItIsNotBeforeTheFirstPosition()116 public void thenItIsNotBeforeTheFirstPosition() { 117 assertFalse(secondPosition.isBefore(position)); 118 } 119 120 @Then("the ranges are equal") theRangesAreEqual()121 public void theRangesAreEqual() { 122 assertTrue(range.equals(secondRange)); 123 } 124 125 @Then("it is contained in the first range") itIsContainedInTheFirstRange()126 public void itIsContainedInTheFirstRange() { 127 assertTrue(range.contains(secondRange)); 128 } 129 } 130