Search in sources :

Example 1 with Lifecycle

use of org.jbehave.core.model.Lifecycle in project jbehave-core by jbehave.

the class RegexStoryParserBehaviour method shouldParseStoryWithLifecycleAfterUponOutcome.

@Test
public void shouldParseStoryWithLifecycleAfterUponOutcome() {
    String wholeStory = "Lifecycle: " + NL + "After:" + NL + NL + "Outcome: ANY " + NL + "Given a step after any scenario" + NL + "Outcome: SUCCESS " + NL + "Given a step after successful scenario" + NL + "Outcome: FAILURE " + NL + "Given a step after failed scenario" + NL + "Scenario:" + NL + "Given a scenario";
    Story story = parser.parseStory(wholeStory, storyPath);
    List<String> beforeSteps = story.getLifecycle().getBeforeSteps();
    assertThat(beforeSteps.isEmpty(), equalTo(true));
    Lifecycle lifecycle = story.getLifecycle();
    List<String> afterSteps = lifecycle.getAfterSteps();
    assertThat(afterSteps.get(0), equalTo("Given a step after any scenario"));
    assertThat(afterSteps.get(1), equalTo("Given a step after successful scenario"));
    assertThat(afterSteps.get(2), equalTo("Given a step after failed scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).get(0), equalTo("Given a step after any scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).get(0), equalTo("Given a step after successful scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).get(0), equalTo("Given a step after failed scenario"));
    Scenario scenario = story.getScenarios().get(0);
    List<String> steps = scenario.getSteps();
    assertThat(steps.get(0), equalTo("Given a scenario"));
}
Also used : Lifecycle(org.jbehave.core.model.Lifecycle) Matchers.containsString(org.hamcrest.Matchers.containsString) GivenStory(org.jbehave.core.model.GivenStory) Story(org.jbehave.core.model.Story) Scenario(org.jbehave.core.model.Scenario) Test(org.junit.Test)

Example 2 with Lifecycle

use of org.jbehave.core.model.Lifecycle in project jbehave-core by jbehave.

the class RegexStoryParser method parseLifecycle.

private Lifecycle parseLifecycle(String storyAsText) {
    String scenarioKeyword = keywords.scenario();
    // use text before scenario keyword, if found
    String beforeScenario = "";
    if (StringUtils.contains(storyAsText, scenarioKeyword)) {
        beforeScenario = StringUtils.substringBefore(storyAsText, scenarioKeyword);
    }
    Matcher findingLifecycle = findingLifecycle().matcher(beforeScenario);
    String lifecycle = findingLifecycle.find() ? findingLifecycle.group(1).trim() : NONE;
    Matcher findingBeforeAndAfter = compile(".*" + keywords.before() + "(.*)\\s*" + keywords.after() + "(.*)\\s*", DOTALL).matcher(lifecycle);
    if (findingBeforeAndAfter.matches()) {
        String beforeLifecycle = findingBeforeAndAfter.group(1).trim();
        List<Steps> beforeSteps = parseBeforeLifecycle(beforeLifecycle);
        String afterLifecycle = findingBeforeAndAfter.group(2).trim();
        List<Steps> afterSteps = parseAfterLifecycle(afterLifecycle);
        return new Lifecycle(beforeSteps, afterSteps);
    }
    Matcher findingBefore = compile(".*" + keywords.before() + "(.*)\\s*", DOTALL).matcher(lifecycle);
    if (findingBefore.matches()) {
        String beforeLifecycle = findingBefore.group(1).trim();
        List<Steps> beforeSteps = parseBeforeLifecycle(beforeLifecycle);
        return new Lifecycle(beforeSteps, Arrays.<Steps>asList());
    }
    Matcher findingAfter = compile(".*" + keywords.after() + "(.*)\\s*", DOTALL).matcher(lifecycle);
    if (findingAfter.matches()) {
        List<Steps> beforeSteps = asList();
        String afterLifecycle = findingAfter.group(1).trim();
        List<Steps> afterSteps = parseAfterLifecycle(afterLifecycle);
        return new Lifecycle(beforeSteps, afterSteps);
    }
    return Lifecycle.EMPTY;
}
Also used : Steps(org.jbehave.core.model.Lifecycle.Steps) Matcher(java.util.regex.Matcher) Lifecycle(org.jbehave.core.model.Lifecycle)

Example 3 with Lifecycle

use of org.jbehave.core.model.Lifecycle in project jbehave-core by jbehave.

the class PerformableTree method performableScenario.

private PerformableScenario performableScenario(RunContext context, Story story, Map<String, String> storyParameters, FilteredStory filterContext, Meta storyMeta, boolean runBeforeAndAfterScenarioSteps, Scenario scenario) {
    PerformableScenario performableScenario = new PerformableScenario(scenario, story.getPath());
    // scenario also inherits meta from story
    boolean scenarioAllowed = true;
    if (failureOccurred(context) && context.configuration().storyControls().skipScenariosAfterFailure()) {
        return performableScenario;
    }
    if (!filterContext.allowed(scenario)) {
        scenarioAllowed = false;
    }
    performableScenario.allowed(scenarioAllowed);
    if (scenarioAllowed) {
        Lifecycle lifecycle = story.getLifecycle();
        Meta storyAndScenarioMeta = scenario.getMeta().inheritFrom(storyMeta);
        NormalPerformableScenario normalScenario = normalScenario(context, lifecycle, scenario, storyAndScenarioMeta, storyParameters);
        // run before scenario steps, if allowed
        if (runBeforeAndAfterScenarioSteps) {
            normalScenario.addBeforeSteps(context.beforeOrAfterScenarioSteps(storyAndScenarioMeta, Stage.BEFORE, ScenarioType.NORMAL));
        }
        if (isParameterisedByExamples(scenario)) {
            ExamplesTable table = scenario.getExamplesTable();
            for (Map<String, String> scenarioParameters : table.getRows()) {
                Meta exampleScenarioMeta = parameterMeta(context, scenarioParameters).inheritFrom(storyAndScenarioMeta);
                boolean exampleScenarioAllowed = context.filter().allow(exampleScenarioMeta);
                if (exampleScenarioAllowed) {
                    ExamplePerformableScenario exampleScenario = exampleScenario(context, lifecycle, scenario, storyAndScenarioMeta, scenarioParameters);
                    performableScenario.addExampleScenario(exampleScenario);
                }
            }
        } else {
            // plain old scenario
            performableScenario.useNormalScenario(normalScenario);
        }
        // after scenario steps, if allowed
        if (runBeforeAndAfterScenarioSteps) {
            normalScenario.addAfterSteps(context.beforeOrAfterScenarioSteps(storyAndScenarioMeta, Stage.AFTER, ScenarioType.NORMAL));
        }
    }
    return performableScenario;
}
Also used : Meta(org.jbehave.core.model.Meta) Lifecycle(org.jbehave.core.model.Lifecycle) ExamplesTable(org.jbehave.core.model.ExamplesTable)

Example 4 with Lifecycle

use of org.jbehave.core.model.Lifecycle in project jbehave-core by jbehave.

the class RegexStoryParserBehaviour method shouldParseStoryWithLifecycleAfterUponOutcomeAndMetaFilter.

@Test
public void shouldParseStoryWithLifecycleAfterUponOutcomeAndMetaFilter() {
    String wholeStory = "Lifecycle: " + NL + "After:" + NL + NL + "Outcome: ANY " + NL + "MetaFilter: +all" + NL + "Given a step after any scenario" + NL + "Outcome: SUCCESS " + NL + "MetaFilter: +happy" + NL + "Given a step after successful scenario" + NL + "Outcome: FAILURE " + NL + "MetaFilter: +sad" + NL + "Given a step after failed scenario" + NL + "Scenario:" + NL + "Given a scenario";
    Story story = parser.parseStory(wholeStory, storyPath);
    List<String> beforeSteps = story.getLifecycle().getBeforeSteps();
    assertThat(beforeSteps.isEmpty(), equalTo(true));
    Lifecycle lifecycle = story.getLifecycle();
    List<String> afterSteps = lifecycle.getAfterSteps();
    assertThat(afterSteps.get(0), equalTo("Given a step after any scenario"));
    assertThat(afterSteps.get(1), equalTo("Given a step after successful scenario"));
    assertThat(afterSteps.get(2), equalTo("Given a step after failed scenario"));
    assertThat(new ArrayList<>(lifecycle.getOutcomes()), equalTo(Arrays.asList(Outcome.ANY, Outcome.SUCCESS, Outcome.FAILURE)));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).get(0), equalTo("Given a step after any scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).get(0), equalTo("Given a step after successful scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).get(0), equalTo("Given a step after failed scenario"));
    assertThat(lifecycle.getMetaFilter(Outcome.ANY).asString(), equalTo("+all"));
    Keywords keywords = new Keywords();
    assertThat(lifecycle.getAfterSteps(Outcome.ANY, Meta.createMeta("@all", keywords)).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY, Meta.createMeta("@all", keywords)).get(0), equalTo("Given a step after any scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY, Meta.createMeta("@none", keywords)).size(), equalTo(0));
    assertThat(lifecycle.getMetaFilter(Outcome.SUCCESS).asString(), equalTo("+happy"));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS, Meta.createMeta("@happy", keywords)).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS, Meta.createMeta("@happy", keywords)).get(0), equalTo("Given a step after successful scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS, Meta.createMeta("@none", keywords)).size(), equalTo(0));
    assertThat(lifecycle.getMetaFilter(Outcome.FAILURE).asString(), equalTo("+sad"));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE, Meta.createMeta("@sad", keywords)).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE, Meta.createMeta("@sad", keywords)).get(0), equalTo("Given a step after failed scenario"));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE, Meta.createMeta("@none", keywords)).size(), equalTo(0));
    Scenario scenario = story.getScenarios().get(0);
    List<String> steps = scenario.getSteps();
    assertThat(steps.get(0), equalTo("Given a scenario"));
}
Also used : Keywords(org.jbehave.core.configuration.Keywords) LocalizedKeywords(org.jbehave.core.i18n.LocalizedKeywords) Lifecycle(org.jbehave.core.model.Lifecycle) Matchers.containsString(org.hamcrest.Matchers.containsString) GivenStory(org.jbehave.core.model.GivenStory) Story(org.jbehave.core.model.Story) Scenario(org.jbehave.core.model.Scenario) Test(org.junit.Test)

Example 5 with Lifecycle

use of org.jbehave.core.model.Lifecycle in project jbehave-core by jbehave.

the class RegexStoryParserBehaviour method shouldParseStoryWithLifecycleAfterUponOutcomeInNonEnglishLocale.

@Test
public void shouldParseStoryWithLifecycleAfterUponOutcomeInNonEnglishLocale() {
    String wholeStory = "Lebenszyklus: " + NL + "Nach:" + NL + NL + "Ergebnis: JEDES " + NL + "Gegeben im Lager sind 200 T-Shirts" + NL + "Ergebnis: ERFOLG " + NL + "Gegeben im Lager sind 300 T-Shirts" + NL + "Ergebnis: FEHLER " + NL + "Gegeben im Lager sind 400 T-Shirts" + NL + "Szenario:" + NL + "Wenn ein Kunde 20 T-Shirts bestellt";
    parser = new RegexStoryParser(new LocalizedKeywords(Locale.GERMAN), new LoadFromClasspath(), new TableTransformers());
    Story story = parser.parseStory(wholeStory, storyPath);
    List<String> beforeSteps = story.getLifecycle().getBeforeSteps();
    assertThat(beforeSteps.isEmpty(), equalTo(true));
    Lifecycle lifecycle = story.getLifecycle();
    List<String> afterSteps = lifecycle.getAfterSteps();
    assertThat(afterSteps.get(0), equalTo("Gegeben im Lager sind 200 T-Shirts"));
    assertThat(afterSteps.get(1), equalTo("Gegeben im Lager sind 300 T-Shirts"));
    assertThat(afterSteps.get(2), equalTo("Gegeben im Lager sind 400 T-Shirts"));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.ANY).get(0), equalTo("Gegeben im Lager sind 200 T-Shirts"));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.SUCCESS).get(0), equalTo("Gegeben im Lager sind 300 T-Shirts"));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).size(), equalTo(1));
    assertThat(lifecycle.getAfterSteps(Outcome.FAILURE).get(0), equalTo("Gegeben im Lager sind 400 T-Shirts"));
    Scenario scenario = story.getScenarios().get(0);
    List<String> steps = scenario.getSteps();
    assertThat(steps.get(0), equalTo("Wenn ein Kunde 20 T-Shirts bestellt"));
}
Also used : LoadFromClasspath(org.jbehave.core.io.LoadFromClasspath) Lifecycle(org.jbehave.core.model.Lifecycle) LocalizedKeywords(org.jbehave.core.i18n.LocalizedKeywords) Matchers.containsString(org.hamcrest.Matchers.containsString) GivenStory(org.jbehave.core.model.GivenStory) Story(org.jbehave.core.model.Story) TableTransformers(org.jbehave.core.model.TableTransformers) Scenario(org.jbehave.core.model.Scenario) Test(org.junit.Test)

Aggregations

Lifecycle (org.jbehave.core.model.Lifecycle)9 Test (org.junit.Test)6 Scenario (org.jbehave.core.model.Scenario)5 Story (org.jbehave.core.model.Story)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 GivenStory (org.jbehave.core.model.GivenStory)4 Meta (org.jbehave.core.model.Meta)3 LocalizedKeywords (org.jbehave.core.i18n.LocalizedKeywords)2 PendingStep (org.jbehave.core.steps.StepCreator.PendingStep)2 File (java.io.File)1 Matcher (java.util.regex.Matcher)1 Keywords (org.jbehave.core.configuration.Keywords)1 LoadFromClasspath (org.jbehave.core.io.LoadFromClasspath)1 Description (org.jbehave.core.model.Description)1 ExamplesTable (org.jbehave.core.model.ExamplesTable)1 GivenStories (org.jbehave.core.model.GivenStories)1 Steps (org.jbehave.core.model.Lifecycle.Steps)1 Narrative (org.jbehave.core.model.Narrative)1 TableTransformers (org.jbehave.core.model.TableTransformers)1