Search in sources :

Example 1 with Suggestion

use of io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion in project cucumber-jvm by cucumber.

the class Runner method emitSnippetSuggestedEvent.

private void emitSnippetSuggestedEvent(Pickle pickle, Step step) {
    List<String> snippets = generateSnippetsForStep(step);
    if (snippets.isEmpty()) {
        return;
    }
    Suggestion suggestion = new Suggestion(step.getText(), snippets);
    Location scenarioLocation = pickle.getLocation();
    Location stepLocation = step.getLocation();
    SnippetsSuggestedEvent event = new SnippetsSuggestedEvent(bus.getInstant(), pickle.getUri(), scenarioLocation, stepLocation, suggestion);
    bus.send(event);
}
Also used : Suggestion(io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion) SnippetsSuggestedEvent(io.cucumber.plugin.event.SnippetsSuggestedEvent) Location(io.cucumber.plugin.event.Location)

Example 2 with Suggestion

use of io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion in project cucumber-jvm by cucumber.

the class TestCaseResultObserverTest method should_be_failed_for_undefined_result.

@Test
public void should_be_failed_for_undefined_result() {
    TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus);
    bus.send(new SnippetsSuggestedEvent(now(), uri, location, location, new Suggestion("some step", singletonList("stub snippet"))));
    Result stepResult = new Result(UNDEFINED, ZERO, error);
    bus.send(new TestStepFinished(now(), testCase, step, stepResult));
    Result testCaseResult = new Result(UNDEFINED, ZERO, error);
    bus.send(new TestCaseFinished(now(), testCase, testCaseResult));
    Exception exception = expectThrows(Exception.class, resultListener::assertTestCasePassed);
    assertThat(exception.getCause(), instanceOf(SkipException.class));
    SkipException skipException = (SkipException) exception.getCause();
    assertThat(skipException.isSkip(), is(false));
    assertThat(skipException.getMessage(), is("" + "The step 'some step' is undefined.\n" + "You can implement this step using the snippet(s) below:\n" + "\n" + "stub snippet\n"));
}
Also used : Suggestion(io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion) TestCaseFinished(io.cucumber.plugin.event.TestCaseFinished) TestStepFinished(io.cucumber.plugin.event.TestStepFinished) SnippetsSuggestedEvent(io.cucumber.plugin.event.SnippetsSuggestedEvent) SkipException(org.testng.SkipException) SkipException(org.testng.SkipException) Result(io.cucumber.plugin.event.Result) Test(org.testng.annotations.Test)

Example 3 with Suggestion

use of io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion in project cucumber-jvm by cucumber.

the class UndefinedStepException method createMessage.

private static String createMessage(Collection<Suggestion> suggestions) {
    if (suggestions.isEmpty()) {
        return "This step is undefined";
    }
    Suggestion first = suggestions.iterator().next();
    StringBuilder sb = new StringBuilder("The step '" + first.getStep() + "'");
    if (suggestions.size() == 1) {
        sb.append(" is undefined.");
    } else {
        sb.append(" and ").append(suggestions.size() - 1).append(" other step(s) are undefined.");
    }
    sb.append("\n");
    if (suggestions.size() == 1) {
        sb.append("You can implement this step using the snippet(s) below:\n\n");
    } else {
        sb.append("You can implement these steps using the snippet(s) below:\n\n");
    }
    String snippets = suggestions.stream().map(Suggestion::getSnippets).flatMap(Collection::stream).distinct().collect(Collectors.joining("\n", "", "\n"));
    sb.append(snippets);
    return sb.toString();
}
Also used : Suggestion(io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion) Collection(java.util.Collection)

Example 4 with Suggestion

use of io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion in project cucumber-jvm by cucumber.

the class TestCaseResultObserverTest method undefined.

@Test
void undefined() {
    bus.send(new TestCaseStarted(Instant.now(), testCase));
    bus.send(new TestStepStarted(Instant.now(), testCase, testStep));
    bus.send(new SnippetsSuggestedEvent(Instant.now(), uri, testCase.getLocation(), testStep.getStep().getLocation(), new Suggestion(testStep.getStep().getText(), asList("mocked snippet 1", "mocked snippet 2", "mocked snippet 3"))));
    Result result = new Result(Status.UNDEFINED, Duration.ZERO, null);
    bus.send(new TestStepFinished(Instant.now(), testCase, testStep, result));
    bus.send(new TestCaseFinished(Instant.now(), testCase, result));
    Exception exception = assertThrows(Exception.class, observer::assertTestCasePassed);
    assertThat(exception.getCause(), instanceOf(UndefinedStepException.class));
    assertThat(exception.getCause().getMessage(), is("" + "The step 'mocked' is undefined.\n" + "You can implement this step using the snippet(s) below:\n" + "\n" + "mocked snippet 1\n" + "mocked snippet 2\n" + "mocked snippet 3\n"));
}
Also used : Suggestion(io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion) TestCaseFinished(io.cucumber.plugin.event.TestCaseFinished) TestStepFinished(io.cucumber.plugin.event.TestStepFinished) SnippetsSuggestedEvent(io.cucumber.plugin.event.SnippetsSuggestedEvent) TestStepStarted(io.cucumber.plugin.event.TestStepStarted) TestCaseStarted(io.cucumber.plugin.event.TestCaseStarted) TestAbortedException(org.opentest4j.TestAbortedException) Result(io.cucumber.plugin.event.Result) Test(org.junit.jupiter.api.Test)

Example 5 with Suggestion

use of io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion in project cucumber-jvm by cucumber.

the class TeamCityPlugin method getSnippets.

private String getSnippets(TestCase testCase) {
    URI uri = testCase.getUri();
    Location location = testCase.getLocation();
    List<Suggestion> suggestionForTestCase = suggestions.stream().filter(suggestion -> suggestion.getUri().equals(uri) && suggestion.getTestCaseLocation().equals(location)).map(SnippetsSuggestedEvent::getSuggestion).collect(Collectors.toList());
    return createMessage(suggestionForTestCase);
}
Also used : Suggestion(io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion) URI(java.net.URI) Location(io.cucumber.plugin.event.Location)

Aggregations

Suggestion (io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion)7 SnippetsSuggestedEvent (io.cucumber.plugin.event.SnippetsSuggestedEvent)5 Result (io.cucumber.plugin.event.Result)4 Location (io.cucumber.plugin.event.Location)3 TestCaseFinished (io.cucumber.plugin.event.TestCaseFinished)3 TestStepFinished (io.cucumber.plugin.event.TestStepFinished)3 Test (org.junit.jupiter.api.Test)3 TestCaseStarted (io.cucumber.plugin.event.TestCaseStarted)2 TestStepStarted (io.cucumber.plugin.event.TestStepStarted)2 CucumberException (io.cucumber.core.exception.CucumberException)1 TestRunFinished (io.cucumber.plugin.event.TestRunFinished)1 URI (java.net.URI)1 Collection (java.util.Collection)1 Failure (org.junit.runner.notification.Failure)1 TestAbortedException (org.opentest4j.TestAbortedException)1 SkipException (org.testng.SkipException)1 Test (org.testng.annotations.Test)1