Skip to content

Commit

Permalink
Fixup name collision
Browse files Browse the repository at this point in the history
  • Loading branch information
elchupanebrej committed Dec 10, 2024
1 parent 98588f3 commit 9a69541
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
8 changes: 7 additions & 1 deletion python/src/cucumber_messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from . import json_converter
from .messages import *

ExpressionType = Type1
# Renaming types because of confusing collision naming
HookType = Type
PickleStepType = Type1
ExpressionType = Type2

del Type
del Type1
del Type2
15 changes: 7 additions & 8 deletions python/src/cucumber_messages/json_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from dataclasses import fields, is_dataclass, Field, MISSING
from datetime import datetime, date
from enum import Enum
from typing import Any, Dict, List, Optional, Union, get_args, get_origin, TypeVar, Type, Tuple
from typing import Any, Dict, List, Optional, Union, get_args, get_origin, TypeVar, Type as TypingType, Tuple

T = TypeVar('T')


def camel_to_snake(name: str) -> str:
"""Convert string from camelCase to snake_case."""
# Validate field name - must start with letter or underscore and contain only alphanumeric and underscore
Expand Down Expand Up @@ -40,7 +39,7 @@ def __init__(self, module_scope: types.ModuleType):
self.module_scope = module_scope
self.type_cache = {}

def resolve_container_type(self, container_name: str) -> Type:
def resolve_container_type(self, container_name: str) -> TypingType:
container_types = {
'Sequence': typing.Sequence,
'List': list,
Expand All @@ -52,7 +51,7 @@ def resolve_container_type(self, container_name: str) -> Type:
raise ValueError(f"Unsupported container type: {container_name}")
return container_types[container_name]

def parse_generic_type(self, type_str: str) -> Tuple[Type, List[Type]]:
def parse_generic_type(self, type_str: str) -> Tuple[TypingType, List[TypingType]]:
container_name = type_str[:type_str.index('[')].strip()
args_str = type_str[type_str.index('[') + 1:type_str.rindex(']')]

Expand All @@ -65,7 +64,7 @@ def parse_generic_type(self, type_str: str) -> Tuple[Type, List[Type]]:

return container_type, arg_types

def resolve_type(self, type_str: str) -> Type:
def resolve_type(self, type_str: str) -> TypingType:
if type_str in self.type_cache:
return self.type_cache[type_str]

Expand All @@ -84,7 +83,7 @@ def resolve_type(self, type_str: str) -> Type:
self.type_cache[type_str] = resolved
return resolved

def _resolve_union_type(self, type_str: str) -> Type:
def _resolve_union_type(self, type_str: str) -> TypingType:
types_str = [t.strip() for t in type_str.split('|')]
resolved_types = [
self.resolve_type(t)
Expand Down Expand Up @@ -293,11 +292,11 @@ def to_dict(self) -> Dict[str, Any]:
return DataclassJSONEncoder.encode(self)

@classmethod
def from_json(cls: Type[T], json_str: str, module_scope: Optional[types.ModuleType] = None) -> T:
def from_json(cls: TypingType[T], json_str: str, module_scope: Optional[types.ModuleType] = None) -> T:
data = json.loads(json_str)
return cls.from_dict(data, module_scope)

@classmethod
def from_dict(cls: Type[T], data: Dict[str, Any], module_scope: Optional[types.ModuleType] = None) -> T:
def from_dict(cls: TypingType[T], data: Dict[str, Any], module_scope: Optional[types.ModuleType] = None) -> T:
decoder = DataclassJSONDecoder(module_scope or sys.modules[cls.__module__])
return decoder.decode(data, cls)
54 changes: 50 additions & 4 deletions python/src/cucumber_messages/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ class Tag(JSONDataclassMixin):
"""


class Type(Enum):
before_test_run = "BEFORE_TEST_RUN"
after_test_run = "AFTER_TEST_RUN"
before_test_case = "BEFORE_TEST_CASE"
after_test_case = "AFTER_TEST_CASE"
before_test_step = "BEFORE_TEST_STEP"
after_test_step = "AFTER_TEST_STEP"


@dataclass
class JavaMethod(JSONDataclassMixin):
class_name: str
Expand Down Expand Up @@ -154,7 +163,7 @@ class PickleDocString(JSONDataclassMixin):
media_type: str | None = None


class Type(Enum):
class Type1(Enum):
unknown = "Unknown"
context = "Context"
action = "Action"
Expand All @@ -180,15 +189,15 @@ class PickleTag(JSONDataclassMixin):
"""


class Type1(Enum):
class Type2(Enum):
cucumber_expression = "CUCUMBER_EXPRESSION"
regular_expression = "REGULAR_EXPRESSION"


@dataclass
class StepDefinitionPattern(JSONDataclassMixin):
source: str
type: Type1
type: Type2


@dataclass
Expand Down Expand Up @@ -295,6 +304,7 @@ class Exception(JSONDataclassMixin):
@dataclass
class TestRunStarted(JSONDataclassMixin):
timestamp: Timestamp
id: str | None = None


class Status(Enum):
Expand Down Expand Up @@ -326,6 +336,23 @@ class TestStepStarted(JSONDataclassMixin):
timestamp: Timestamp


@dataclass
class TestRunHookStarted(JSONDataclassMixin):
id: str
"""
Unique identifier for this hook execution
"""
test_run_started_id: str
"""
Identifier for the test run that this hook execution belongs to
"""
hook_id: str
"""
Identifier for the hook that will be executed
"""
timestamp: Timestamp


@dataclass
class UndefinedParameterType(JSONDataclassMixin):
expression: str
Expand Down Expand Up @@ -384,6 +411,7 @@ class Attachment(JSONDataclassMixin):
reduce bandwidth of message consumers. It also makes it easier to process and download attachments
separately from reports.
"""
test_run_started_id: str | None = None


@dataclass
Expand Down Expand Up @@ -494,6 +522,10 @@ class TestCase(JSONDataclassMixin):
The ID of the `Pickle` this `TestCase` is derived from.
"""
test_steps: Sequence[TestStep]
test_run_started_id: str | None = None
"""
Identifier for the test run that this test case belongs to
"""


@dataclass
Expand Down Expand Up @@ -521,6 +553,7 @@ class TestRunFinished(JSONDataclassMixin):
"""
Any exception thrown during the test run, if any. Does not include exceptions thrown while executing steps.
"""
test_run_started_id: str | None = None


@dataclass
Expand All @@ -537,6 +570,16 @@ class TestStepResult(JSONDataclassMixin):
"""


@dataclass
class TestRunHookFinished(JSONDataclassMixin):
test_run_hook_started_id: str
"""
Identifier for the hook execution that has finished
"""
result: TestStepResult
timestamp: Timestamp


@dataclass
class Background(JSONDataclassMixin):
location: Location
Expand Down Expand Up @@ -571,6 +614,7 @@ class Hook(JSONDataclassMixin):
source_reference: SourceReference
name: str | None = None
tag_expression: str | None = None
type: Type | None = None


@dataclass
Expand Down Expand Up @@ -632,7 +676,7 @@ class PickleStep(JSONDataclassMixin):
"""
text: str
argument: PickleStepArgument | None = None
type: Type | None = None
type: Type1 | None = None
"""
The context in which the step was specified: context (Given), action (When) or outcome (Then).
Expand Down Expand Up @@ -767,4 +811,6 @@ class Envelope(JSONDataclassMixin):
test_run_started: TestRunStarted | None = None
test_step_finished: TestStepFinished | None = None
test_step_started: TestStepStarted | None = None
test_run_hook_started: TestRunHookStarted | None = None
test_run_hook_finished: TestRunHookFinished | None = None
undefined_parameter_type: UndefinedParameterType | None = None
4 changes: 2 additions & 2 deletions python/tests/test_model_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
TestStepResult,
TestStepStarted,
Timestamp,
Type,
PickleStepType,
)

resource_path = Path(__file__).parent.absolute() / "data"
Expand Down Expand Up @@ -118,7 +118,7 @@
uri="samples/minimal/minimal.feature",
name="cukes",
language="en",
steps=[PickleStep(ast_node_ids=["1"], id="3", type=Type.context, text="I have 42 cukes in my belly")],
steps=[PickleStep(ast_node_ids=["1"], id="3", type=PickleStepType.context, text="I have 42 cukes in my belly")],
tags=[],
ast_node_ids=["2"],
),
Expand Down

0 comments on commit 9a69541

Please sign in to comment.