Home | History | Annotate | Download | only in steps
      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.ast.CompilationUnit;
     25 import com.github.javaparser.ast.body.Parameter;
     26 import com.github.javaparser.ast.body.VariableDeclarator;
     27 import com.github.javaparser.ast.visitor.CloneVisitor;
     28 import com.github.javaparser.ast.visitor.GenericListVisitorAdapter;
     29 import com.github.javaparser.ast.visitor.GenericVisitorAdapter;
     30 import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
     31 import com.github.javaparser.bdd.visitors.PositionTestVisitor;
     32 import org.jbehave.core.annotations.Given;
     33 import org.jbehave.core.annotations.Then;
     34 import org.jbehave.core.annotations.When;
     35 
     36 import java.util.Collections;
     37 import java.util.List;
     38 import java.util.Map;
     39 import java.util.concurrent.atomic.AtomicReference;
     40 
     41 import static org.hamcrest.CoreMatchers.is;
     42 import static org.hamcrest.MatcherAssert.assertThat;
     43 
     44 public class VisitorSteps {
     45 
     46     /* Fields used to maintain step state within this step class */
     47     private VoidVisitorAdapter<AtomicReference<String>> toUpperCaseVariableNameVisitor;
     48     private VoidVisitorAdapter<AtomicReference<String>> collectVariableNameVisitor;
     49     private PositionTestVisitor positionTestVisitor;
     50     private GenericVisitorAdapter<String, Void> nameReturningVisitor;
     51     private GenericListVisitorAdapter<String, Void> allNameReturningVisitor;
     52     private AtomicReference<String> collectedVariableName;
     53     private String returnedVariableName;
     54     private List<String> returnedVariableNames;
     55 
     56     /* Map that maintains shares state across step classes.  If manipulating the objects in the map you must update the state */
     57     private Map<String, Object> state;
     58 
     59     public VisitorSteps(Map<String, Object> state) {
     60         this.state = state;
     61     }
     62 
     63     @Given("a VoidVisitorAdapter with a visit method that changes variable names to uppercase")
     64     public void givenAVoidVisitorAdapterWithAVisitMethodThatChangesVariableNamesToUppercase() {
     65         toUpperCaseVariableNameVisitor = new VoidVisitorAdapter<AtomicReference<String>>() {
     66             @Override
     67             public void visit(VariableDeclarator n, AtomicReference<String> arg) {
     68                 n.setName(n.getNameAsString().toUpperCase());
     69             }
     70         };
     71     }
     72 
     73     @Given("a VoidVisitorAdapter with a visit method and collects the variable names")
     74     public void givenAVoidVisitorAdapterWithAVisitMethodThatCollectsTheVariableName() {
     75         collectVariableNameVisitor = new VoidVisitorAdapter<AtomicReference<String>>() {
     76             @Override
     77             public void visit(VariableDeclarator n, AtomicReference<String> arg) {
     78                 arg.set(arg.get() + n.getName() + ";");
     79             }
     80 
     81             @Override
     82             public void visit(Parameter n, AtomicReference<String> arg) {
     83                 arg.set(arg.get() + n.getName() + ";");
     84             }
     85         };
     86     }
     87 
     88     @Given("a GenericVisitorAdapter with a visit method that returns variable names")
     89     public void givenAGenericVisitorAdapterWithAVisitMethodThatReturnsVariableNames() {
     90         nameReturningVisitor = new GenericVisitorAdapter<String, Void>() {
     91             @Override
     92             public String visit(VariableDeclarator n, Void arg) {
     93                 return n.getNameAsString();
     94             }
     95         };
     96     }
     97 
     98     @Given("a GenericListVisitorAdapter with a visit method that returns all variable names")
     99     public void givenAGenericListVisitorAdapterWithAVisitMethodThatReturnsAllVariableNames() {
    100         allNameReturningVisitor = new GenericListVisitorAdapter<String, Void>() {
    101             @Override
    102             public List<String> visit(VariableDeclarator n, Void arg) {
    103                 return Collections.singletonList(n.getNameAsString());
    104             }
    105         };
    106     }
    107 
    108     @Given("a VoidVisitorAdapter with a visit method that asserts sensible line positions")
    109     public void givenAVoidVisitorAdapterWithAVisitMethodThatAssertsSensibleLinePositions() {
    110         positionTestVisitor = new PositionTestVisitor();
    111     }
    112 
    113     @When("the CompilationUnit is cloned to the second CompilationUnit")
    114     public void whenTheSecondCompilationUnitIsCloned() {
    115         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    116         CompilationUnit compilationUnit2 = (CompilationUnit) compilationUnit.accept(new CloneVisitor(), null);
    117         state.put("cu2", compilationUnit2);
    118     }
    119 
    120     @When("the CompilationUnit is visited by the to uppercase visitor")
    121     public void whenTheCompilationUnitIsVisitedByTheVistor() {
    122         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    123         toUpperCaseVariableNameVisitor.visit(compilationUnit, null);
    124         state.put("cu1", compilationUnit);
    125     }
    126 
    127     @When("the CompilationUnit is visited by the variable name collector visitor")
    128     public void whenTheCompilationUnitIsVisitedByTheVariableNameCollectorVisitor() {
    129         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    130         collectedVariableName = new AtomicReference<>("");
    131         collectVariableNameVisitor.visit(compilationUnit, collectedVariableName);
    132     }
    133 
    134     @When("the CompilationUnit is visited by the visitor that returns variable names")
    135     public void whenTheCompilationUnitIsVisitedByTheVisitorThatReturnsVariableNames() {
    136         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    137         returnedVariableName = nameReturningVisitor.visit(compilationUnit, null);
    138     }
    139 
    140     @When("the CompilationUnit is visited by the visitor that returns all variable names")
    141     public void whenTheCompilationUnitIsVisitedByTheVisitorThatReturnsAllVariableNames() {
    142         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    143         returnedVariableNames = allNameReturningVisitor.visit(compilationUnit, null);
    144     }
    145 
    146     @When("the CompilationUnit is visited by the PositionTestVisitor")
    147     public void whenTheCompilationUnitIsVisitedByThePositionTestVisitor() {
    148         CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
    149         compilationUnit.accept(positionTestVisitor, null);
    150     }
    151 
    152     @Then("the collected variable name is \"$nameUnderTest\"")
    153     public void thenTheCollectedVariableNameIs(String nameUnderTest) {
    154         assertThat(collectedVariableName.get(), is(nameUnderTest));
    155     }
    156 
    157     @Then("the return variable name is \"$nameUnderTest\"")
    158     public void thenTheReturnVariableNameIs(String nameUnderTest) {
    159         assertThat(returnedVariableName, is(nameUnderTest));
    160     }
    161 
    162     @Then("the first return variable name is \"$nameUnderTest\"")
    163     public void thenTheFirstReturnVariableNameIs(String nameUnderTest) {
    164         assertThat(returnedVariableNames.get(0), is(nameUnderTest));
    165     }
    166 
    167     @Then("the total number of nodes visited is $expectedCount")
    168     public void thenTheTotalNumberOfNodesVisitedIs(int expectedCount) {
    169         assertThat(positionTestVisitor.getNumberOfNodesVisited(), is(expectedCount));
    170     }
    171 }
    172