diff --git a/automat/_test/test_type_based.py b/automat/_test/test_type_based.py index f6e9368..06c3e23 100644 --- a/automat/_test/test_type_based.py +++ b/automat/_test/test_type_based.py @@ -1,11 +1,13 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Callable, List, Protocol, TypeVar +from typing import Callable, Generic, List, Protocol, TypeVar from unittest import TestCase from .. import AlreadyBuiltError, NoTransition, TypeMachineBuilder, pep614 +T = TypeVar("T") + class TestProtocol(Protocol): @@ -18,6 +20,7 @@ def value(self) -> int: class ArgTaker(Protocol): def takeSomeArgs(self, arg1: int = 0, arg2: str = "") -> None: ... + def value(self) -> int: ... class NoOpCore: @@ -416,6 +419,31 @@ def threevalue(proto: TestProtocol, core: NoOpCore, three: Three) -> int: 7, ) + def test_genericData(self) -> None: + builder = TypeMachineBuilder(ArgTaker, NoOpCore) + one = builder.state("one") + + @dataclass + class Gen(Generic[T]): + t: T + + def dat( + proto: ArgTaker, core: NoOpCore, arg1: int = 0, arg2: str = "" + ) -> Gen[int]: + return Gen(arg1) + + two = builder.state("two", dat) + one.upon(ArgTaker.takeSomeArgs).to(two).returns(None) + + @pep614(two.upon(ArgTaker.value).loop()) + def val(proto: ArgTaker, core: NoOpCore, data: Gen[int]) -> int: + return data.t + + b = builder.build() + m = b(NoOpCore()) + m.takeSomeArgs(3) + self.assertEqual(m.value(), 3) + def test_noMethodsInAltStateDataFactory(self) -> None: """ When the state machine is received by a data factory during