Skip to content

Commit

Permalink
Global parameter type registry
Browse files Browse the repository at this point in the history
  • Loading branch information
kieran-ryan committed Mar 16, 2024
1 parent 7e2abe4 commit 2a64444
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
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
13 changes: 10 additions & 3 deletions 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",
"build_step_matcher", # Deprecated
"CucumberExpressionMatcher", # Deprecated
"CUCUMBER_EXPRESSIONS_MATCHER",
"parameter_registry",
)
18 changes: 16 additions & 2 deletions behave_cucumber_matcher/matcher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# -*- coding: utf-8 -*-
"""Behave step definition matcher for Cucumber Expressions."""
"""Behave step definition matcher for Cucumber Expressions.
Todo:
* Check backwards compatibility.
* Check how cucumber handles a missing parameter type. Still runs?
"""

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 +98,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)
4 changes: 2 additions & 2 deletions 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 All @@ -14,7 +15,6 @@
prefer_for_regexp_match=False,
)

# Pass the parameter type to the registry instance
parameter_registry.define_parameter_type(color_parameter)


Expand Down

0 comments on commit 2a64444

Please sign in to comment.