Search in sources :

Example 1 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldStoreAndReadObjects.

private void shouldStoreAndReadObjects(Method methodStoring, boolean resetContext) throws IntrospectionException {
    // Given
    if (resetContext) {
        setupContext();
    }
    SomeSteps stepsInstance = new SomeSteps();
    InjectableStepsFactory stepsFactory = new InstanceStepsFactory(new MostUsefulConfiguration(), stepsInstance);
    StepMatcher stepMatcher = new RegexStepMatcher(StepType.WHEN, "I read from context", Pattern.compile("I read from context"), new String[] {});
    StepCreator stepCreator = new StepCreator(stepsInstance.getClass(), stepsFactory, stepsContext, null, new ParameterControls(), stepMatcher, new SilentStepMonitor());
    // When
    Method methodRead = SomeSteps.methodFor("aMethodReadingFromContext");
    StepResult stepResult = stepCreator.createParametrisedStep(methodStoring, "When I store in context", "I store in context", new HashMap<String, String>()).perform(null);
    StepResult stepResultRead = stepCreator.createParametrisedStep(methodRead, "And I read from context", "I read from context", new HashMap<String, String>()).perform(null);
    // Then
    assertThat(stepResult, instanceOf(Successful.class));
    assertThat(stepResultRead, instanceOf(Successful.class));
    assertThat(stepsInstance.args, instanceOf(String.class));
    assertThat((String) stepsInstance.args, is("someValue"));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Successful(org.jbehave.core.steps.AbstractStepResult.Successful) RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher)

Example 2 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method assertThatParametrisedStepHasMarkedNamedParameterValues.

private void assertThatParametrisedStepHasMarkedNamedParameterValues(String firstParameterValue, String secondParameterValue) throws IntrospectionException {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    StepMatcher stepMatcher = mock(StepMatcher.class);
    StepCreator stepCreator = stepCreatorUsing(stepsInstance, stepMatcher, new ParameterControls().useDelimiterNamedParameters(false));
    Map<String, String> parameters = new HashMap<>();
    parameters.put("theme", firstParameterValue);
    parameters.put("variant", secondParameterValue);
    // When
    when(stepMatcher.parameterNames()).thenReturn(parameters.keySet().toArray(new String[parameters.size()]));
    when(stepMatcher.parameter(1)).thenReturn(parameters.get(firstParameterValue));
    when(stepMatcher.parameter(2)).thenReturn(parameters.get(secondParameterValue));
    StepResult stepResult = stepCreator.createParametrisedStep(SomeSteps.methodFor("aMethodWithANamedParameter"), "When I use parameters <theme> and <variant>", "I use parameters <theme> and <variant>", parameters).perform(null);
    // Then
    assertThat(stepResult, instanceOf(Successful.class));
    String expected = "When I use parameters " + PARAMETER_VALUE_START + firstParameterValue + PARAMETER_VALUE_END + " and " + PARAMETER_VALUE_START + secondParameterValue + PARAMETER_VALUE_END;
    assertThat(stepResult.parametrisedStep(), equalTo(expected));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Successful(org.jbehave.core.steps.AbstractStepResult.Successful)

Example 3 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldMatchParametersByDelimitedNameWithDistinctNamedAnnotations.

@SuppressWarnings("unchecked")
@Test
public void shouldMatchParametersByDelimitedNameWithDistinctNamedAnnotations() throws Exception {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    parameterConverters = new ParameterConverters(new LoadFromClasspath(), new TableTransformers());
    StepMatcher stepMatcher = mock(StepMatcher.class);
    ParameterControls parameterControls = new ParameterControls().useDelimiterNamedParameters(true);
    StepCreator stepCreator = stepCreatorUsing(stepsInstance, stepMatcher, parameterControls);
    Map<String, String> params = new HashMap<>();
    params.put("t", "distinct theme");
    params.put("v", "distinct variant");
    when(stepMatcher.parameterNames()).thenReturn(params.keySet().toArray(new String[params.size()]));
    when(stepMatcher.parameter(1)).thenReturn("<t>");
    when(stepMatcher.parameter(2)).thenReturn("<v>");
    // When
    Step step = stepCreator.createParametrisedStep(SomeSteps.methodFor("aMethodWithANamedParameter"), "When I use parameters <t> and <v>", "I use parameters <t> and <v>", params);
    step.perform(null);
    // Then
    Map<String, String> results = (Map<String, String>) stepsInstance.args;
    assertThat(results.get("theme"), equalTo("distinct theme"));
    assertThat(results.get("variant"), equalTo("distinct variant"));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) LoadFromClasspath(org.jbehave.core.io.LoadFromClasspath) Matchers.anyString(org.mockito.Matchers.anyString) ParametrisedStep(org.jbehave.core.steps.StepCreator.ParametrisedStep) HashMap(java.util.HashMap) Map(java.util.Map) TableTransformers(org.jbehave.core.model.TableTransformers) Test(org.junit.Test)

Example 4 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method shouldMatchParametersByDelimitedNameWithNoNamedAnnotations.

@Test
public void shouldMatchParametersByDelimitedNameWithNoNamedAnnotations() throws Exception {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    parameterConverters = new ParameterConverters(new LoadFromClasspath(), new TableTransformers());
    StepMatcher stepMatcher = mock(StepMatcher.class);
    ParameterControls parameterControls = new ParameterControls().useDelimiterNamedParameters(true);
    StepCreator stepCreator = stepCreatorUsing(stepsInstance, stepMatcher, parameterControls);
    Map<String, String> params = Collections.singletonMap("param", "value");
    when(stepMatcher.parameterNames()).thenReturn(params.keySet().toArray(new String[params.size()]));
    when(stepMatcher.parameter(1)).thenReturn("<param>");
    // When
    Step step = stepCreator.createParametrisedStep(SomeSteps.methodFor("aMethodWithoutNamedAnnotation"), "When a parameter <param> is set", "a parameter <param> is set", params);
    step.perform(null);
    // Then
    assertThat((String) stepsInstance.args, equalTo("value"));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) LoadFromClasspath(org.jbehave.core.io.LoadFromClasspath) Matchers.anyString(org.mockito.Matchers.anyString) ParametrisedStep(org.jbehave.core.steps.StepCreator.ParametrisedStep) TableTransformers(org.jbehave.core.model.TableTransformers) Test(org.junit.Test)

Example 5 with StepMatcher

use of org.jbehave.core.parsers.StepMatcher in project jbehave-core by jbehave.

the class StepCreatorBehaviour method assertThatParametrisedStepHasMarkedParsedParametersValues.

private void assertThatParametrisedStepHasMarkedParsedParametersValues(String firstParameterValue, String secondParameterValue) throws IntrospectionException {
    // Given
    SomeSteps stepsInstance = new SomeSteps();
    StepMatcher stepMatcher = new RegexStepMatcher(StepType.WHEN, "I use parameters $theme and $variant", Pattern.compile("When I use parameters (.*) and (.*)"), new String[] { "theme", "variant" });
    StepCreator stepCreator = stepCreatorUsing(stepsInstance, stepMatcher, new ParameterControls());
    Map<String, String> parameters = new HashMap<>();
    // When
    StepResult stepResult = stepCreator.createParametrisedStep(SomeSteps.methodFor("aMethodWithANamedParameter"), "When I use parameters " + firstParameterValue + " and " + secondParameterValue, "When I use parameters " + firstParameterValue + " and " + secondParameterValue, parameters).perform(null);
    // Then
    assertThat(stepResult, instanceOf(Successful.class));
    String expected = "When I use parameters " + PARAMETER_VALUE_START + firstParameterValue + PARAMETER_VALUE_END + " and " + PARAMETER_VALUE_START + secondParameterValue + PARAMETER_VALUE_END;
    assertThat(stepResult.parametrisedStep(), equalTo(expected));
}
Also used : RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) StepMatcher(org.jbehave.core.parsers.StepMatcher) HashMap(java.util.HashMap) RegexStepMatcher(org.jbehave.core.parsers.RegexStepMatcher) Matchers.anyString(org.mockito.Matchers.anyString) Successful(org.jbehave.core.steps.AbstractStepResult.Successful)

Aggregations

RegexStepMatcher (org.jbehave.core.parsers.RegexStepMatcher)8 StepMatcher (org.jbehave.core.parsers.StepMatcher)8 HashMap (java.util.HashMap)6 Matchers.anyString (org.mockito.Matchers.anyString)6 Test (org.junit.Test)5 MostUsefulConfiguration (org.jbehave.core.configuration.MostUsefulConfiguration)3 LoadFromClasspath (org.jbehave.core.io.LoadFromClasspath)3 TableTransformers (org.jbehave.core.model.TableTransformers)3 Successful (org.jbehave.core.steps.AbstractStepResult.Successful)3 ParametrisedStep (org.jbehave.core.steps.StepCreator.ParametrisedStep)3 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 BeforeOrAfterFailed (org.jbehave.core.failures.BeforeOrAfterFailed)1 Failed (org.jbehave.core.steps.AbstractStepResult.Failed)1 ObjectNotStoredException (org.jbehave.core.steps.context.StepsContext.ObjectNotStoredException)1