-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #204. Go to step definition from a feature file.
- Loading branch information
Showing
6 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
....eclipse.editor.test/src/main/java/cucumber/eclipse/editor/editors/StepHyperlinkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package cucumber.eclipse.editor.editors; | ||
|
||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.Region; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import cucumber.eclipse.steps.integration.Step; | ||
|
||
public class StepHyperlinkTest { | ||
|
||
private StepHyperlink stepHyperlink; | ||
IRegion region; | ||
|
||
@Before | ||
public void setUp() { | ||
region = new Region(0, 10); | ||
Step step = new Step(); | ||
step.setText("Given I have a cat"); | ||
stepHyperlink = new StepHyperlink(region, step); | ||
} | ||
|
||
@Test | ||
public void shouldHaveATypeLabel() { | ||
assertThat(stepHyperlink.getTypeLabel(), equalTo("Gherkin step")); | ||
} | ||
|
||
@Test | ||
public void shouldHaveAnAlternateText() { | ||
assertThat(stepHyperlink.getHyperlinkText(), equalTo("Open step definition")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTheExpectedRegion() { | ||
assertThat(stepHyperlink.getHyperlinkRegion(), equalTo(region)); | ||
} | ||
|
||
// should have UI automate test to validate the step hyperlink open. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
cucumber.eclipse.editor/src/main/java/cucumber/eclipse/editor/editors/StepHyperlink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cucumber.eclipse.editor.editors; | ||
|
||
import java.util.HashMap; | ||
|
||
import org.eclipse.core.resources.IMarker; | ||
import org.eclipse.core.resources.IResource; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.hyperlink.IHyperlink; | ||
import org.eclipse.ui.IWorkbenchPage; | ||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.ui.ide.IDE; | ||
|
||
import cucumber.eclipse.steps.integration.Step; | ||
|
||
public class StepHyperlink implements IHyperlink { | ||
|
||
private IRegion region; | ||
private Step step; | ||
|
||
public StepHyperlink(IRegion region, Step step) { | ||
this.region = region; | ||
this.step = step; | ||
} | ||
|
||
@Override | ||
public IRegion getHyperlinkRegion() { | ||
return this.region; | ||
} | ||
|
||
@Override | ||
public String getHyperlinkText() { | ||
return "Open step definition"; | ||
} | ||
|
||
@Override | ||
public String getTypeLabel() { | ||
return "Gherkin step"; | ||
} | ||
|
||
@Override | ||
public void open() { | ||
IResource file = this.step.getSource(); | ||
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | ||
|
||
HashMap<String, Integer> map = new HashMap<String, Integer>(); | ||
map.put(IMarker.LINE_NUMBER, step.getLineNumber()); | ||
IMarker marker; | ||
try { | ||
marker = file.createMarker(IMarker.TEXT); | ||
marker.setAttributes(map); | ||
IDE.openEditor(page, marker); | ||
marker.delete(); | ||
} catch (CoreException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
...r.eclipse.editor/src/main/java/cucumber/eclipse/editor/editors/StepHyperlinkDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cucumber.eclipse.editor.editors; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IMarker; | ||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.ITextViewer; | ||
import org.eclipse.jface.text.Region; | ||
import org.eclipse.jface.text.hyperlink.IHyperlink; | ||
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IFileEditorInput; | ||
import org.eclipse.ui.texteditor.ITextEditor; | ||
|
||
import cucumber.eclipse.editor.markers.MarkerIds; | ||
import cucumber.eclipse.editor.markers.MarkerManager; | ||
import cucumber.eclipse.editor.steps.IStepProvider; | ||
import cucumber.eclipse.steps.integration.Step; | ||
import gherkin.lexer.LexingError; | ||
import gherkin.parser.Parser; | ||
|
||
public class StepHyperlinkDetector implements IHyperlinkDetector { | ||
|
||
private Editor editor; | ||
|
||
public StepHyperlinkDetector(Editor editor) { | ||
this.editor = editor; | ||
} | ||
|
||
@Override | ||
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { | ||
if (region == null || textViewer == null) { | ||
return null; | ||
} | ||
|
||
IDocument document = textViewer.getDocument(); | ||
if (document == null) { | ||
return null; | ||
} | ||
|
||
int offset = region.getOffset(); | ||
int lineStartOffset = 0; | ||
|
||
IRegion lineInfo = null; | ||
String currentLine = null; | ||
try { | ||
lineInfo = document.getLineInformationOfOffset(offset); | ||
lineStartOffset = lineInfo.getOffset(); | ||
currentLine = document.get(lineStartOffset, lineInfo.getLength()); | ||
} catch (BadLocationException e) { | ||
return null; | ||
} | ||
|
||
IStepProvider stepProvider = this.editor.getStepProvider(); | ||
Set<Step> steps = stepProvider.getStepsInEncompassingProject(); | ||
|
||
StepMatcher stepMatcher = new StepMatcher(); | ||
String language = DocumentUtil.getDocumentLanguage(document); | ||
|
||
// hack to support scenario outline examples | ||
int currentLineNumber = textViewer.getTextWidget().getLineAtOffset(offset) + 1; | ||
List<String> resolvedStepsExpressions = resolveLineStep(editor, currentLineNumber); | ||
Step step = null; | ||
for (String variant : resolvedStepsExpressions) { | ||
step = stepMatcher.matchSteps(language, steps, variant); | ||
if(step != null) { | ||
break; | ||
} | ||
} | ||
|
||
if (step == null) { | ||
return null; | ||
} | ||
|
||
// define the hyperlink region | ||
String textStatement = stepMatcher.getTextStatement(language, currentLine); | ||
int statementStartOffset = lineStartOffset + currentLine.indexOf(textStatement); | ||
|
||
IRegion stepRegion = new Region(statementStartOffset, textStatement.length()); | ||
|
||
return new IHyperlink[] { new StepHyperlink(stepRegion, step) }; | ||
} | ||
|
||
// go through all examples of current scenario outline and generate step strings with replaced variables values | ||
protected static List<String> resolveLineStep(IEditorPart editorPart, int currentLineNumber) { | ||
ITextEditor editor = (ITextEditor) editorPart; | ||
|
||
IDocument document = editor.getDocumentProvider().getDocument(editorPart.getEditorInput()); | ||
|
||
IFileEditorInput fileEditorInput = (IFileEditorInput) editorPart.getEditorInput(); | ||
IFile featureFile = fileEditorInput.getFile(); | ||
MarkerManager markerManager = new MarkerManager(); | ||
PopupMenuFindStepFormatter findStepFormatter = new PopupMenuFindStepFormatter(currentLineNumber); | ||
Parser p = new Parser(findStepFormatter, false); | ||
try { | ||
p.parse(document.get(), "", 0); | ||
} catch (LexingError l) { | ||
markerManager.add(MarkerIds.LEXING_ERROR, featureFile, IMarker.SEVERITY_ERROR, l.getLocalizedMessage(), 1, 0, 0); | ||
} | ||
return findStepFormatter.getResolvedStepNames(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters