diff --git a/README.md b/README.md index c808ae6..2dec6e5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/behave_cucumber_matcher/__init__.py b/behave_cucumber_matcher/__init__.py index e2c91a3..ff59776 100644 --- a/behave_cucumber_matcher/__init__.py +++ b/behave_cucumber_matcher/__init__.py @@ -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", ) diff --git a/behave_cucumber_matcher/matcher.py b/behave_cucumber_matcher/matcher.py index ab8d1dd..0c5658c 100644 --- a/behave_cucumber_matcher/matcher.py +++ b/behave_cucumber_matcher/matcher.py @@ -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.""" @@ -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 diff --git a/features/environment.py b/features/environment.py index a23c86b..7ef4936 100644 --- a/features/environment.py +++ b/features/environment.py @@ -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) diff --git a/features/steps/color.py b/features/steps/color.py index 2441604..e108a87 100644 --- a/features/steps/color.py +++ b/features/steps/color.py @@ -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( @@ -14,7 +15,6 @@ prefer_for_regexp_match=False, ) -# Pass the parameter type to the registry instance parameter_registry.define_parameter_type(color_parameter)