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

Global parameter type registry #13

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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [0.1.0] - 2023-12-29
### Added

- Global parameter type registry ([#13](https://github.com/kieran-ryan/behave-cucumber-matcher/pull/13))

## 0.1.0 - 2023-12-29

### Added

Expand Down
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,17 @@ pip install behave-cucumber-matcher

## Usage

Import and patch the matcher into Behave inside `environment.py` in your `features` directory.
Import and specify the matcher inside `environment.py` of your `features` directory.

```python
from behave.matchers import use_step_matcher, matcher_mapping
from behave_cucumber_matcher import build_step_matcher
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry

# Initialise a Cucumber Expressions parameter registry
parameter_registry = ParameterTypeRegistry()

# Create the step matcher to pass to behave
step_matcher = build_step_matcher(parameter_registry)

# Patch the step matcher into behave
matcher_mapping["cucumber_expressions"] = step_matcher
from behave.matchers import use_step_matcher
from behave_cucumber_matcher import CUCUMBER_EXPRESSIONS_MATCHER

# Specify to use the Cucumber Expressions step matcher
use_step_matcher("cucumber_expressions")
use_step_matcher(CUCUMBER_EXPRESSIONS_MATCHER)
```

Create a scenario inside `color.feature` in your `features` directory:
Create a scenario inside `color.feature` of your `features` directory:

```gherkin
Feature: Color selection
Expand All @@ -55,7 +45,7 @@ Feature: Color selection
Then the profile colour should be "red"
```

Create step definitions inside `color.py` in your `features/steps` directory:
Create step definitions inside `color.py` of your `features/steps` directory:

```python
from behave import given, then, when
Expand Down Expand Up @@ -124,7 +114,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 and direct import of the matcher at package level rather than via its module.
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.

## License

Expand Down
9 changes: 8 additions & 1 deletion behave_cucumber_matcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
"""Behave step definition matcher for Cucumber Expressions."""

from .__version__ import __version__
from .matcher import CucumberExpressionMatcher, build_step_matcher
from .matcher import (
CUCUMBER_EXPRESSIONS_MATCHER,
CucumberExpressionMatcher,
build_step_matcher,
parameter_registry,
)

__all__ = (
"__version__",
"build_step_matcher",
"CucumberExpressionMatcher",
"CUCUMBER_EXPRESSIONS_MATCHER",
"parameter_registry",
)
11 changes: 10 additions & 1 deletion behave_cucumber_matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

from typing import Any, Callable, List, Optional

from behave.matchers import Matcher
from behave.matchers import Matcher, matcher_mapping
from behave.model_core import Argument
from cucumber_expressions.expression import CucumberExpression
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry

CUCUMBER_EXPRESSIONS_MATCHER = "cucumber_expressions"

parameter_registry = ParameterTypeRegistry()


class CucumberExpressionMatcher(Matcher):
"""Matcher class that uses Cucumber Expressions."""
Expand Down Expand Up @@ -89,3 +93,8 @@ def step_matcher(
)

return step_matcher


# Register the Cucumber Expressions step matcher with Behave
step_matcher = build_step_matcher(parameter_registry)
matcher_mapping[CUCUMBER_EXPRESSIONS_MATCHER] = step_matcher
16 changes: 3 additions & 13 deletions features/environment.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
"""Behave environment setup."""

from behave.matchers import matcher_mapping, use_step_matcher
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry
from behave.matchers import use_step_matcher

from behave_cucumber_matcher import build_step_matcher

# Initialise a Cucumber Expressions parameter registry
parameter_registry = ParameterTypeRegistry()

# Create the step matcher to pass to behave
step_matcher = build_step_matcher(parameter_registry)

# Patch the step matcher into behave
matcher_mapping["cucumber_expressions"] = step_matcher
from behave_cucumber_matcher import CUCUMBER_EXPRESSIONS_MATCHER

# Specify to use the Cucumber Expressions step matcher
use_step_matcher("cucumber_expressions")
use_step_matcher(CUCUMBER_EXPRESSIONS_MATCHER)
3 changes: 2 additions & 1 deletion features/steps/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from behave import given, then, when
from cucumber_expressions.parameter_type import ParameterType
from environment import parameter_registry

from behave_cucumber_matcher import parameter_registry

# Define the parameter type
color_parameter = ParameterType(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_matcher.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Matcher unit tests."""

import behave.matchers
import pytest
from behave.model_core import Argument
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry

from behave_cucumber_matcher.matcher import (
CUCUMBER_EXPRESSIONS_MATCHER,
CucumberExpressionMatcher,
build_step_matcher,
)
Expand Down Expand Up @@ -82,3 +84,8 @@ def test_build_matcher_is_callable():
matcher = build_step_matcher(registry)
assert callable(matcher)
assert matcher(print, "I have {int} cukes in my {word} now")


def test_matcher_patched_into_behave():
"""Matcher is patched into Behave step matchers."""
assert CUCUMBER_EXPRESSIONS_MATCHER in behave.matchers.matcher_mapping
Loading