private void printSnippets() { List<String> snippets = undefinedStepsTracker.getSnippets(); if (!snippets.isEmpty()) { out.append("\n"); out.println("You can implement missing steps with the snippets below:"); out.println(); for (String snippet : snippets) { out.println(snippet); } } }
private String getFirstGivenKeyword(GherkinDialect i18n) { for (String keyword : i18n.getGivenKeywords()) { if (!keyword.equals("* ")) { return convertToCodeKeyword(keyword); } } return null; }
private String givenWhenThenKeyword(String uri, List<PickleLocation> stepLocations) { String keyword = null; if (!stepLocations.isEmpty()) { if (pathToSourceMap.containsKey(uri)) { keyword = getKeywordFromSource(uri, stepLocations); } } return keyword != null ? keyword : getFirstGivenKeyword(dialectProvider.getDefaultDialect()); }
private String getKeywordFromSource(String path, List<PickleLocation> stepLocations) { if (!pathToStepMap.containsKey(path)) { createFeatureStepMap(path); } if (!pathToStepMap.containsKey(path)) { return null; } GherkinDialect featureDialect = pathToStepMap.get(path).dialect; List<String> givenThenWhenKeywords = getGivenWhenThenKeywords(featureDialect); Map<Integer, StepNode> stepMap = pathToStepMap.get(path).stepMap; for (PickleLocation stepLocation : stepLocations) { if (!stepMap.containsKey(stepLocation.getLine())) { continue; } for (StepNode stepNode = stepMap.get(stepLocation.getLine()); stepNode != null; stepNode = stepNode.previous) { for (String keyword : givenThenWhenKeywords) { if (!keyword.equals("* ") && keyword == stepNode.step.getKeyword()) { return convertToCodeKeyword(keyword); } } } } return getFirstGivenKeyword(featureDialect); }
@Override public void receive(SnippetsSuggestedEvent event) { handleSnippetsSuggested(event.uri, event.stepLocations, event.snippets); } };
void handleSnippetsSuggested(String uri, List<PickleLocation> stepLocations, List<String> snippets) { hasUndefinedSteps = true; String keyword = givenWhenThenKeyword(uri, stepLocations); for (String rawSnippet : snippets) { String snippet = rawSnippet.replace("**KEYWORD**", keyword); if (!this.snippets.contains(snippet)) { this.snippets.add(snippet); } } }
private void createFeatureStepMap(String path) { if (!pathToSourceMap.containsKey(path)) { return; } Parser<GherkinDocument> parser = new Parser<GherkinDocument>(new AstBuilder()); TokenMatcher matcher = new TokenMatcher(); try { GherkinDocument gherkinDocument = parser.parse(pathToSourceMap.get(path), matcher); Map<Integer, StepNode> stepMap = new HashMap<Integer, StepNode>(); StepNode initialPreviousNode = null; for (ScenarioDefinition child : gherkinDocument.getFeature().getChildren()) { StepNode lastStepNode = processScenarioDefinition(stepMap, initialPreviousNode, child); if (child instanceof Background) { initialPreviousNode = lastStepNode; } } pathToStepMap.put(path, new FeatureStepMap(new GherkinDialectProvider(gherkinDocument.getFeature().getLanguage()).getDefaultDialect(), stepMap)); } catch (ParserException e) { // Ignore exceptions } }
private boolean hasUndefinedSteps() { return undefinedStepsTracker.hasUndefinedSteps(); }
public List<String> getSnippets() { return undefinedStepsTracker.getSnippets(backends, runtimeOptions.getSnippetType().getFunctionNameGenerator()); }