Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | ebe02ce214ec506922e504213fd0ba4e9d38981f (tree) |
---|---|
Time | 2023-10-21 04:32:07 |
Author | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
ASIS, refatored to remove mypy warning; added extra test for sub-NS, to raise coverty
@@ -11,16 +11,17 @@ | ||
11 | 11 | from . import AIGR |
12 | 12 | |
13 | 13 | |
14 | - | |
15 | 14 | class NameError(AttributeError):pass |
16 | 15 | |
17 | 16 | @dataclass |
18 | 17 | class NamedNode(AIGR): |
19 | 18 | name :str |
20 | 19 | _: KW_ONLY |
21 | - _ns :PTH.Optional[NameSpace]=dc_field(init=None, default=None) #type: ignore[call-overload] | |
20 | + # type(_ns) is NamedNode, but that leads to a cycle in imports, to use te more generic AIGR | |
21 | + _ns :PTH.Optional[AIGR]=dc_field(init=None, default=None) #type: ignore[call-overload] | |
22 | 22 | |
23 | - def register_in_NS(self, ns): | |
23 | + | |
24 | + def register_in_NS(self, ns: AIGR): #same: type(ns) is NameSpace, but ... | |
24 | 25 | self._ns = ns |
25 | 26 | |
26 | 27 | @property |
@@ -13,7 +13,8 @@ | ||
13 | 13 | from enum import Enum |
14 | 14 | from dataclasses import dataclass, KW_ONLY |
15 | 15 | from dataclasses import field as dc_field |
16 | -from .namednodes import * | |
16 | + | |
17 | +from .namednodes import NamedNode, NameError | |
17 | 18 | |
18 | 19 | from . import AIGR, _Marker |
19 | 20 |
@@ -10,9 +10,9 @@ | ||
10 | 10 | from . import AIGR |
11 | 11 | from .events import Event |
12 | 12 | from .aid import TypedParameter, Argument # Castle/AIGR types |
13 | +from .namednodes import NamedNode | |
13 | 14 | |
14 | -from .namednodes import * | |
15 | -from .namespaces import NameSpace | |
15 | + | |
16 | 16 | |
17 | 17 | __all__ = ['ProtocolKind', 'Protocol', 'EventProtocol'] |
18 | 18 | # DataProtocol, StreamProtocol are added/implemented later |
@@ -43,7 +43,6 @@ | ||
43 | 43 | kind :ProtocolKind |
44 | 44 | based_on :PTH.Optional[Protocol]=dc_field(default_factory= lambda :Protocol._BASE) # pragma: no mutate |
45 | 45 | typedParameters :PTH.Optional[PTH.Sequence[TypedParameter]]=() |
46 | - _ns :PTH.Optional[NameSpace]=dc_field(init=None, default=None) #type: ignore[call-overload] | |
47 | 46 | |
48 | 47 | |
49 | 48 | @dataclass # pragma: no mutate |
@@ -28,6 +28,17 @@ | ||
28 | 28 | return ns |
29 | 29 | |
30 | 30 | @pytest.fixture |
31 | +def top(): | |
32 | + top = NameSpace('top') | |
33 | + return top | |
34 | + | |
35 | +@pytest.fixture | |
36 | +def sub(top): | |
37 | + sub = NameSpace('sub') | |
38 | + top.register(sub) | |
39 | + return sub | |
40 | + | |
41 | +@pytest.fixture | |
31 | 42 | def sourceNS(a_node): |
32 | 43 | ns = Source_NS("sourceNS", source="dummy") |
33 | 44 | ns.register(a_node) |
@@ -72,7 +83,7 @@ | ||
72 | 83 | assert aNS.getID(name) is two #The test |
73 | 84 | |
74 | 85 | |
75 | -def test_5b_ns_in_ns(): | |
86 | +def test_5a_ns_in_ns(): | |
76 | 87 | "when we import a NS, we get a NS in a NS ..." |
77 | 88 | top = NameSpace('top') |
78 | 89 | sub = NameSpace('sub') |
@@ -84,6 +95,18 @@ | ||
84 | 95 | assert sub.getID('elm') is elm |
85 | 96 | assert top.search(dottedName="sub.elm") is elm |
86 | 97 | |
98 | + | |
99 | +def test_5b_seach_1level(aNS,a_node): | |
100 | + name = a_node.name | |
101 | + assert (aNS.search(name) is a_node) and (aNS.getID(name) is a_node), "serach should find that what getID returns" | |
102 | + | |
103 | + | |
104 | +def test_5c_seachNotFound_1(top): | |
105 | + assert top.search("Deze bestaat niet") is None | |
106 | + | |
107 | +def test_5d_seachNotFound_sub(top, sub): | |
108 | + assert top.search("top.Deze.bestaat.niet") is None | |
109 | + | |
87 | 110 | def test_6a_registered_is_2ways(aNS, a_node): |
88 | 111 | """When a NamedNode is registered in a NameSpace, it should a backlink (`ns property) to the NS again""" |
89 | 112 | assert a_node.ns is aNS |