Skip to content

Commit

Permalink
Compatibility with parameter types missing defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
kieran-ryan committed Mar 17, 2024
1 parent 1f1c8b7 commit 8645ce9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ color = ParameterType(
regexp="red|blue|yellow",
type=str,
transformer=lambda s: s,
use_for_snippets=True,
prefer_for_regexp_match=False,
)

# Pass the parameter type to the registry instance
Expand Down Expand Up @@ -113,7 +111,7 @@ For detailed usage of _behave_, see the [official documentation](https://behave.

## Acknowledgements

Based on the Behave step matcher base class and built on the architecture of [cuke4behave](https://gitlab.com/cuke4behave/cuke4behave) by [Dev Kumar Gupta](https://github.com/mrkaiser), with extended type hints, a fix for detecting patterns without arguments, a default parameter type registry, additional documentation for arguments and return types, direct import of the matcher at package level rather than via its module, and a global parameter type registry.
Based on the Behave step matcher base class and built on the architecture of [cuke4behave](https://gitlab.com/cuke4behave/cuke4behave) by [Dev Kumar Gupta](https://github.com/mrkaiser), with extended type hints, a fix for detecting patterns without arguments, a default parameter type registry, additional documentation for arguments and return types, direct import of the matcher at package level rather than via its module, backwards compatibility with Cucumber Expressions missing parameter type defaults, and a global parameter type registry.

## License

Expand Down
24 changes: 24 additions & 0 deletions behave_cucumber_matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from typing import Any, Callable, List, Optional

import cucumber_expressions.parameter_type
from behave.matchers import Matcher, matcher_mapping
from behave.model_core import Argument
from cucumber_expressions.expression import CucumberExpression
Expand All @@ -13,6 +14,29 @@
parameter_registry = ParameterTypeRegistry()


class ParameterTypeOverrides(cucumber_expressions.parameter_type.ParameterType):
"""Matcher compatibility for Cucumber Expressions parameter types."""

def __init__( # noqa: D107
self,
*args,
# Fixes missing defaults in Cucumber Expressions below 17.0.2
# See https://github.com/cucumber/cucumber-expressions/pull/259
use_for_snippets: bool = True,
prefer_for_regexp_match: bool = False,
**kwargs,
):
super().__init__(
*args,
use_for_snippets=use_for_snippets,
prefer_for_regexp_match=prefer_for_regexp_match,
**kwargs,
)


cucumber_expressions.parameter_type.ParameterType = ParameterTypeOverrides


class CucumberExpressionMatcher(Matcher):
"""Matcher class that uses Cucumber Expressions."""

Expand Down
2 changes: 0 additions & 2 deletions features/steps/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
regexp="red|blue|yellow",
type=str,
transformer=lambda s: s,
use_for_snippets=True,
prefer_for_regexp_match=False,
)

# Pass the parameter type to the registry instance
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import behave.matchers
import pytest
from behave.model_core import Argument
from cucumber_expressions.parameter_type import ParameterType
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry

from behave_cucumber_matcher.matcher import (
Expand Down Expand Up @@ -89,3 +90,18 @@ def test_build_matcher_is_callable():
def test_matcher_patched_into_behave():
"""Matcher is patched into Behave step matchers."""
assert CUCUMBER_EXPRESSIONS_MATCHER in behave.matchers.matcher_mapping


def test_no_exception_without_parameter_type_defaults():
"""Compatible with earlier Cucumber Expressions versions.
Cucumber Expressions below 17.0.2 do not set expected defaults
for `use_for_snippets` and `prefer_for_regexp_match` in the
parameter type.
"""
ParameterType(
name="color",
regexp="red|blue|yellow",
type=str,
transformer=lambda s: s,
)

0 comments on commit 8645ce9

Please sign in to comment.