Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with parameter types missing defaults #14

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Added

- Compatibility with parameter types missing intended defaults ([#14](https://github.com/kieran-ryan/behave-cucumber-matcher/pull/14))

## 0.2.0 - 2024-03-17

### Added
Expand Down
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,
)
Loading