Example #1
0
def run_matcher_tests(
    matcher_configs: List[Path], coverage_config, step_registry: StepRegistry
):
    """Run the matcher config tests against all Steps in the Registry"""
    #: holds a set of all covered Step Implementations
    #  A Step Implementation only counts as covered if it
    #  was successfully tested against a positive (should_match) test.
    covered_step_impls = set()

    for matcher_config_path in matcher_configs:
        match_config = parse_matcher_config(matcher_config_path)

        # nothing to match against, because config was empty
        if not match_config:
            print(
                cf.orange(
                    "The matcher config {} was empty - Nothing to do :)".format(
                        matcher_config_path
                    )
                )
            )
            continue

        for step_to_match in match_config:
            # create a dummy Step object for the match config
            keyword, text = step_to_match["step"].split(maxsplit=1)
            step = Step(
                step_id=0,
                keyword=keyword,
                used_keyword=keyword,
                text=text,
                doc_string=None,
                data_table=None,
                path=None,
                line=None,
            )
            # provide a Mock Scenario to the Step which
            # is needed for some Step matching functionality.
            step.scenario = mock.MagicMock(name="Mocked Scenario")

            # verify the step matches accordingly to the expectations in the config
            if "should_match" in step_to_match:
                expected_step_func = step_to_match["should_match"]
                if "with_arguments" in step_to_match:
                    expected_step_arguments = step_to_match["with_arguments"]
                else:
                    expected_step_arguments = []  # no Step arguments to match against

                is_matched = assert_step_match(
                    step, expected_step_func, expected_step_arguments, step_registry
                )
                if is_matched:
                    covered_step_impls.add(step.step_impl)
            else:
                assert_step_not_match(
                    step, step_to_match["should_not_match"], step_registry
                )

    # do coverage analysis on the tested Step Implementation coverage
    coverage(covered_step_impls, step_registry, coverage_config)
Example #2
0
def test_matcher_should_match_step_impl_with_regex_pattern(mocker):
    """The matcher should match a Step with a parse-type pattern"""
    # given
    step_impl = StepImpl("Given", re.compile(r"pattern"), None)
    registry_mock = mocker.MagicMock()
    registry_mock.step_implementations.return_value = [step_impl]
    step = Step(1, "Given", "Given", "pattern", None, None, None, None)
    step.set_scenario(mocker.MagicMock(name="Scenario"))

    # when
    match_step(step, registry_mock)

    # then
    assert step.step_impl == step_impl
    assert isinstance(step.step_impl_match, RegexStepImplMatcher.Match)
Example #3
0
def test_matcher_should_raise_error_when_no_step_impl_found(mocker):
    """The matcher should raise an error when no Step Implementation is found"""
    # given
    registry_mock = mocker.MagicMock()
    registry_mock.step_implementations.return_value = []
    step = Step(1, "Given", "Given", "pattern", None, None, None, None)
    step.set_scenario(mocker.MagicMock(name="Scenario"))

    # then
    with pytest.raises(StepImplementationNotFoundError) as excinfo:
        # when
        match_step(step, registry_mock)

    # then
    assert excinfo.value.step == step
Example #4
0
    def step(self, subtree):
        """Transform the ``step``-subtree for the radish AST"""
        keyword, text, (doc_string, data_table) = subtree

        keyword_line = keyword.line
        keyword = keyword.strip()
        if self.__step_keyword_ctx is None:
            if keyword not in self.language_spec.first_level_step_keywords:
                raise RadishFirstStepMustUseFirstLevelKeyword()

            self.__step_keyword_ctx = keyword
        else:
            if keyword in self.language_spec.first_level_step_keywords:
                if keyword != self.__step_keyword_ctx:
                    self.__step_keyword_ctx = keyword

        english_keyword = next(
            key for key, value in self.language_spec.keywords.items()
            if value == self.__step_keyword_ctx)

        step = Step(
            self.__step_id,
            english_keyword,
            keyword,
            text,
            doc_string,
            data_table,
            self.featurefile_path,
            keyword_line,
        )

        # increment step id for the next step
        self.__step_id += 1
        return step
Example #5
0
def test_matcher_should_match_step_impl_with_step_with_constants(mocker):
    """The matcher should match a Step with Constants"""
    # given
    step_impl = StepImpl("Given", "pattern with A and B", None)
    registry_mock = mocker.MagicMock()
    registry_mock.step_implementations.return_value = [step_impl]
    step = Step(1, "Given", "Given", "pattern with ${x} and ${y}", None, None,
                None, None)
    scenario_mock = mocker.MagicMock(name="Scenario")
    scenario_mock.constants = {"x": "A", "y": "B"}
    step.set_scenario(scenario_mock)

    # when
    match_step(step, registry_mock)

    # then
    assert step.step_impl == step_impl
    assert isinstance(step.step_impl_match, ParseTypeStepImplMatcher.Match)
Example #6
0
def test_matcher_should_raise_error_when_no_matcher_for_pattern_type(mocker):
    """
    The matcher should raise an error when no Matcher supports a Step Implementation Pattern Type
    """

    # given
    class NotSupportedPatternType:
        pass

    step_impl = StepImpl("Given", NotSupportedPatternType(), None)
    registry_mock = mocker.MagicMock()
    registry_mock.step_implementations.return_value = [step_impl]
    step = Step(1, "Given", "Given", "pattern", None, None, None, None)
    step.set_scenario(mocker.MagicMock(name="Scenario"))

    # then
    with pytest.raises(StepImplementationPatternNotSupported) as excinfo:
        # when
        match_step(step, registry_mock)

    # then
    assert excinfo.value.step_impl == step_impl
Example #7
0
def test_matcher_should_match_best_step_impl_candidate(mocker):
    """The matcher should match the best matching Step Implementation Candidate"""
    # given
    step_impl_candidate_1 = StepImpl("Given", re.compile(r"fooo"), None)
    step_impl_candidate_2 = StepImpl("Given", re.compile(r"foo"), None)
    step_impl_candidate_3 = StepImpl("Given", re.compile(r"foooo"), None)
    step_impl_no_candidate = StepImpl("Given", re.compile(r"meh"), None)

    registry_mock = mocker.MagicMock()
    registry_mock.step_implementations.return_value = [
        step_impl_candidate_1,
        step_impl_candidate_2,
        step_impl_candidate_3,
        step_impl_no_candidate,
    ]
    step = Step(1, "Given", "Given", "foo", None, None, None, None)
    step.set_scenario(mocker.MagicMock(name="Scenario"))

    # when
    match_step(step, registry_mock)

    # then
    assert step.step_impl == step_impl_candidate_2