Skip to content

Commit

Permalink
initial Migration to cucumber 7
Browse files Browse the repository at this point in the history
  • Loading branch information
FilippoR committed May 27, 2022
1 parent af440c3 commit 1ac4ada
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import io.cucumber.eclipse.editor.document.GherkinEditorDocument;
import io.cucumber.eclipse.editor.launching.ILauncher;
import io.cucumber.eclipse.editor.launching.ILauncher.Mode;
import io.cucumber.messages.Messages.GherkinDocument.Feature;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Tag;
import io.cucumber.messages.Messages.Location;
import io.cucumber.messages.types.Feature;
import io.cucumber.messages.types.Location;
import io.cucumber.messages.types.Scenario;
import io.cucumber.messages.types.Tag;
import io.cucumber.tagexpressions.TagExpressionParser;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import io.cucumber.eclipse.editor.document.GherkinEditorDocument;
import io.cucumber.eclipse.editor.document.GherkinKeyword;
import io.cucumber.gherkin.GherkinDialect;
import io.cucumber.messages.Messages.GherkinDocument.Feature;
import io.cucumber.messages.Messages.GherkinDocument.Feature.FeatureChild;
import io.cucumber.messages.types.Feature;
import io.cucumber.messages.types.FeatureChild;

/**
* Provides content assist for gherkin keywords
Expand Down Expand Up @@ -53,9 +53,9 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
.map(featureKeyWord -> createFeatureKeyWordProposal(featureKeyWord.getKey(), offset, typed))
.toArray(ICompletionProposal[]::new);
}
Predicate<FeatureChild> hasTopLevelElement = FeatureChild::hasBackground;
hasTopLevelElement = hasTopLevelElement.or(FeatureChild::hasRule);
hasTopLevelElement = hasTopLevelElement.or(FeatureChild::hasScenario);
Predicate<FeatureChild> hasTopLevelElement = f->f.getBackground().isPresent();
hasTopLevelElement = hasTopLevelElement.or(f->f.getRule().isPresent());
hasTopLevelElement = hasTopLevelElement.or(f->f.getScenario().isPresent());
if (editorDocument.getFeatureChilds().filter(hasTopLevelElement).count() == 0) {
// in this case Rule, Scenario or background are required
return editorDocument.getTopLevelKeywords()//
Expand Down Expand Up @@ -102,7 +102,7 @@ public IContextInformation[] computeContextInformation(ITextViewer viewer, int o
try {
IRegion line = viewer.getDocument().getLineInformationOfOffset(offset);

String typed = viewer.getDocument().get(line.getOffset(), offset - line.getOffset()).stripLeading();
/* String typed = */ viewer.getDocument().get(line.getOffset(), offset - line.getOffset()).stripLeading();

editorDocument.keyWords(GherkinDialect::getFeatureKeywords);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IDebugElement;

import io.cucumber.messages.Messages.TestCase.TestStep.StepMatchArgumentsList.StepMatchArgument.Group;
import io.cucumber.messages.types.Group;

/**
* A group value
Expand All @@ -22,7 +22,7 @@ public GherkingGroupValue(IDebugElement parent, String type, Group group) {

@Override
public String getValueString() throws DebugException {
return group.getValue();
return group.getValue().orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public GherkingStackFrame(IThread thread, int lineNumber, String name) {
this.name = name;
}



@Override
public boolean canStepInto() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.model.IDebugElement;

import io.cucumber.messages.Messages.StepDefinition;
import io.cucumber.messages.types.StepDefinition;

/**
* A value representing a step definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.core.model.IVariable;

import io.cucumber.messages.Messages.GherkinDocument.Feature.Step;
import io.cucumber.messages.Messages.StepDefinition;
import io.cucumber.messages.Messages.TestCase.TestStep;
import io.cucumber.messages.types.Step;
import io.cucumber.messages.types.StepDefinition;
import io.cucumber.messages.types.TestStep;

/**
* Special stackframe that represents a test-step
Expand All @@ -25,7 +25,7 @@ public class GherkingStepStackFrame extends GherkingStackFrame {
private DebugRunnable stepOverHandler;

public GherkingStepStackFrame(IThread thread, TestStep testStep, Step step, StepDefinition stepDefinition) {
super(thread, step.getLocation().getLine(), "[" + step.getKeyword().strip() + "] " + step.getText());
super(thread, step.getLocation().getLine().intValue(), "[" + step.getKeyword().strip() + "] " + step.getText());
this.testStep = testStep;
this.step = step;
this.stepDefinition = stepDefinition;
Expand All @@ -41,11 +41,12 @@ public GherkingStepStackFrame(IThread thread, TestStep testStep, Step step, Step

private void addGroups(GherkingStackFrame stepFrame, Consumer<IVariable> variableConsumer) {
AtomicInteger counter = new AtomicInteger();
testStep.getStepMatchArgumentsListsList().stream().flatMap(list -> list.getStepMatchArgumentsList().stream())
testStep.getStepMatchArgumentsLists().stream().flatMap(list -> list.stream().flatMap(p->p.getStepMatchArguments().stream()))
.forEach(argument -> {
String type = argument.getParameterTypeName();
variableConsumer.accept(new GherkingStepVariable(stepFrame, "arg" + counter.get(),
new GherkingGroupValue(stepFrame.getDebugTarget(), type, argument.getGroup())));
argument.getParameterTypeName().ifPresent(type->
variableConsumer.accept(new GherkingStepVariable(stepFrame, "arg" + counter.get(),
new GherkingGroupValue(stepFrame.getDebugTarget(), type, argument.getGroup())))
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.osgi.service.component.annotations.Component;

import io.cucumber.eclipse.editor.document.GherkinEditorDocument;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Step;
import io.cucumber.messages.Messages.Location;
import io.cucumber.messages.types.Step;
import io.cucumber.messages.types.Location;

/**
* Service for providing breakpoints to the generic editor
Expand Down Expand Up @@ -102,7 +102,7 @@ public boolean canToggleBreakpoints(IWorkbenchPart part, ISelection selection) {
ITextSelection textSelection = (ITextSelection) selection;
int lineNumber = textSelection.getStartLine() + 1;
Stream<Location> stream = editorDocument.getSteps().map(Step::getLocation);
return stream.mapToInt(Location::getLine).filter(line -> line == lineNumber).findAny().isPresent();
return stream.mapToLong(Location::getLine).filter(line -> line == lineNumber).findAny().isPresent();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;

import io.cucumber.gherkin.Gherkin;
//import io.cucumber.gherkin.Gherkin;
import io.cucumber.gherkin.GherkinDialect;
import io.cucumber.gherkin.GherkinDialectProvider;
import io.cucumber.gherkin.Location;
import io.cucumber.gherkin.ParserException.NoSuchLanguageException;
import io.cucumber.gherkin.GherkinParser;
import io.cucumber.messages.IdGenerator;
import io.cucumber.messages.Messages.Envelope;
import io.cucumber.messages.types.Attachment;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.Source;
import io.cucumber.messages.types.SourceMediaType;

/**
*
Expand Down Expand Up @@ -70,8 +72,8 @@ public final class GherkinEditorDocument extends GherkinStream {
private Supplier<IResource> resourceSupplier;

private GherkinEditorDocument(IDocument document, Supplier<IResource> resourceSupplier) {
super(Gherkin.fromSources(Collections.singletonList(Gherkin.makeSourceEnvelope(document.get(), "")), true, true,
false, new IdGenerator.Incrementing()).toArray(Envelope[]::new));

super(getEnvelopes(document, resourceSupplier));
this.resourceSupplier = resourceSupplier;
document.addDocumentListener(new IDocumentListener() {

Expand All @@ -87,28 +89,45 @@ public void documentAboutToBeChanged(DocumentEvent event) {
}
});
this.document = document;
dialect = getFeature().map(f -> f.getLanguage()).filter(Objects::nonNull).filter(Predicate.not(String::isBlank))
.map(lang -> provider.getDialect(lang, new Location(-1, -1))).orElseGet(() -> {
try {
IRegion firstLine = document.getLineInformation(0);
String line = document.get(firstLine.getOffset(), firstLine.getLength()).trim();
if (line.startsWith("#")) {
String[] split = line.split("language:", 2);
if (split.length == 2) {
try {
return provider.getDialect(split[1].trim(), new Location(-1, -1));
} catch (NoSuchLanguageException e) {
}
}

Optional<String> langOpt = getFeature().map(f -> f.getLanguage()).filter(Objects::nonNull)
.filter(Predicate.not(String::isBlank));

dialect = langOpt.map(lang -> provider.getDialect(lang, null)).orElseGet(() -> {
try {
IRegion firstLine = document.getLineInformation(0);
String line = document.get(firstLine.getOffset(), firstLine.getLength()).trim();
if (line.startsWith("#")) {
String[] split = line.split("language:", 2);
if (split.length == 2) {
try {
return provider.getDialect(split[1].trim(), null);
} catch (Exception e) {
}
} catch (BadLocationException e) {
}
return provider.getDefaultDialect();
});

}
} catch (BadLocationException e) {
}
return provider.getDefaultDialect();
});

locale = Locale.forLanguageTag(dialect.getLanguage());

}

private static Envelope[] getEnvelopes(IDocument document, Supplier<IResource> resourceSupplier) {
// Gherkin.fromSources(Collections.singletonList(Gherkin.makeSourceEnvelope(document.get(), "")), true, true,
// false, new IdGenerator.Incrementing()).toArray(Envelope[]::new)
GherkinParser parser = GherkinParser.builder()
.includeSource(true)
.includePickles(true)
.build();
Source source = new Source(document.get(), "", SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN);
Envelope envelope = Envelope.of(source);
return parser.parse(envelope).toArray(Envelope[]::new);
}

/**
* @return the dialect of the document
*/
Expand All @@ -127,19 +146,19 @@ public IDocument getDocument() {
return document;
}

public Position getPosition(io.cucumber.messages.Messages.Location location) throws BadLocationException {
public Position getPosition(io.cucumber.messages.types.Location location) throws BadLocationException {
return getPosition(location, 0);
}

public Position getPosition(io.cucumber.messages.Messages.Location location, int lineOffset)
public Position getPosition(io.cucumber.messages.types.Location location, int lineOffset)
throws BadLocationException {
int line = location.getLine();
int line = location.getLine().intValue();
int offset = document.getLineOffset(line - 1 - lineOffset);
return new Position(offset + location.getColumn() - 1, 1);
return new Position(offset + location.getColumn().orElse(0l).intValue() - 1, 1);
}

public Position getEolPosition(io.cucumber.messages.Messages.Location location) throws BadLocationException {
int line = location.getLine();
public Position getEolPosition(io.cucumber.messages.types.Location location) throws BadLocationException {
int line = location.getLine().intValue();
int offset = document.getLineOffset(line - 1);
int lineLength = document.getLineLength(line - 1);
// Workaround for Bug 570740
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import java.util.stream.Stream;

import io.cucumber.eclipse.editor.launching.EnvelopeListener;
import io.cucumber.messages.Messages.Envelope;
import io.cucumber.messages.Messages.GherkinDocument.Feature;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Step;
import io.cucumber.messages.Messages.Pickle;
import io.cucumber.messages.Messages.Pickle.PickleStep;
import io.cucumber.messages.Messages.StepDefinition;
import io.cucumber.messages.Messages.TestCase;
import io.cucumber.messages.Messages.TestCase.TestStep;
import io.cucumber.messages.Messages.TestStepFinished;
import io.cucumber.messages.Messages.TestStepStarted;
import io.cucumber.messages.types.Envelope;
import io.cucumber.messages.types.Feature;
import io.cucumber.messages.types.Scenario;
import io.cucumber.messages.types.Step;
import io.cucumber.messages.types.Pickle;
import io.cucumber.messages.types.PickleStep;
import io.cucumber.messages.types.StepDefinition;
import io.cucumber.messages.types.TestCase;
import io.cucumber.messages.types.TestStep;
import io.cucumber.messages.types.TestStepFinished;
import io.cucumber.messages.types.TestStepStarted;

/**
* Handler that links the individual message parts together
Expand All @@ -35,38 +35,38 @@ public abstract class GherkinMessageHandler implements EnvelopeListener {

@Override
public void handleEnvelope(Envelope env) {
if (env.hasTestCase()) {
TestCase testCase = env.getTestCase();
for (TestStep step : testCase.getTestStepsList()) {
if (env.getTestCase().isPresent()) {
TestCase testCase = env.getTestCase().get();
for (TestStep step : testCase.getTestSteps()) {
testStepMap.put(step.getId(), new TestStepLink(testCase, step));
}
return;
}
if (env.hasPickle()) {
Pickle pickle = env.getPickle();
for (PickleStep step : pickle.getStepsList()) {
if (env.getPickle().isPresent()) {
Pickle pickle = env.getPickle().get();
for (PickleStep step : pickle.getSteps()) {
pickleStepMap.put(step.getId(), new PickleStepLink(pickle, step));
}
return;
}
if (env.hasGherkinDocument()) {
if (env.getGherkinDocument().isPresent()) {
stream = new GherkinStream(env);
return;
}
if (env.hasStepDefinition()) {
StepDefinition stepDefinition = env.getStepDefinition();
if (env.getStepDefinition().isPresent()) {
StepDefinition stepDefinition = env.getStepDefinition().get();
stepDefinitionMap.put(stepDefinition.getId(), stepDefinition);
return;
}
boolean testStepStarted = env.hasTestStepStarted();
boolean testStepFinished = env.hasTestStepFinished();
boolean testStepStarted = env.getTestStepStarted().isPresent();
boolean testStepFinished = env.getTestStepFinished().isPresent();
if (testStepStarted || testStepFinished) {
TestStepLink stepLink;
if (testStepStarted) {
TestStepStarted stepStarted = env.getTestStepStarted();
TestStepStarted stepStarted = env.getTestStepStarted().get();
stepLink = testStepMap.get(stepStarted.getTestStepId());
} else if (testStepFinished) {
TestStepFinished stepFinished = env.getTestStepFinished();
TestStepFinished stepFinished = env.getTestStepFinished().get();
stepLink = testStepMap.get(stepFinished.getTestStepId());
} else {
stepLink = null;
Expand Down Expand Up @@ -97,37 +97,37 @@ public void handleEnvelope(Envelope env) {

private final class TestStepLink {

private TestCase testCase;
// private TestCase testCase;
private TestStep testStep;

public TestStepLink(TestCase testCase, TestStep step) {
this.testCase = testCase;
// this.testCase = testCase;
this.testStep = step;
}

Optional<PickleStepLink> link() {
return Optional.ofNullable(pickleStepMap.get(testStep.getPickleStepId()));
return testStep.getPickleStepId().flatMap(id-> Optional.ofNullable(pickleStepMap.get(id)));
}

Stream<StepDefinition> stepDefinitions() {
return testStep.getStepDefinitionIdsList().stream().map(stepDefinitionMap::get).filter(Objects::nonNull);
return testStep.getStepDefinitionIds().map(l->l.stream().map(stepDefinitionMap::get).filter(Objects::nonNull)).orElse(Stream.of());
}
}

private final class PickleStepLink {

private Pickle pickle;
// private Pickle pickle;
private PickleStep step;

public PickleStepLink(Pickle pickle, PickleStep step) {
this.pickle = pickle;
// this.pickle = pickle;
this.step = step;
}

Optional<Backtrace> backtrace() {

if (stream != null) {
return step.getAstNodeIdsList().stream().flatMap(astId -> {
return step.getAstNodeIds().stream().flatMap(astId -> {
return findByAst(astId);
}).findAny();
}
Expand Down
Loading

0 comments on commit 1ac4ada

Please sign in to comment.