Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revisão | b9c8c8df87927d66b7a7903b9b618bfb6d8ef7f7 (tree) |
---|---|
Hora | 2023-02-05 03:35:53 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
asis
@@ -6,7 +6,6 @@ | ||
6 | 6 | pytst/writers/CC2Cpy/test_2a_groundwork.py \ |
7 | 7 | pytst/writers/CC2Cpy/test_2b_EventProtocol.py \ |
8 | 8 | pytst/writers/CC2Cpy/test_3a_CompPort.py \ |
9 | - | |
10 | 9 | # |
11 | 10 | CURRENT_TESTS = \ |
12 | 11 | pytst/writers/CC2Cpy/test_3b_CompInterface.py \ |
@@ -10,15 +10,15 @@ | ||
10 | 10 | CC_Component: TypeAlias = 'CC_Component' # Forward ref # pragma: no mutate |
11 | 11 | |
12 | 12 | class CC_PortDirection(Enum): |
13 | - Unknown = CC_B_PortDirectionIs_UNKNOWN = 0 | |
14 | - In = CC_B_PortDirectionIs_in = 1 | |
15 | - Out = CC_B_PortDirectionIs_out = 2 | |
16 | - BiDir = CC_B_PortDirectionIs_bidirect = 3 # Not supported yet | |
17 | - Master = CC_B_PortDirectionIs_master = 4 # Not supported yet | |
18 | - Slave = CC_B_PortDirectionIs_slave = 5 # Not supported yet | |
13 | + CC_B_PortDirectionIs_UNKNOWN = Unknown = 0 | |
14 | + CC_B_PortDirectionIs_in = In = 1 | |
15 | + CC_B_PortDirectionIs_out = Out = 2 | |
16 | + CC_B_PortDirectionIs_bidirect = BiDir = 3 # Not supported yet | |
17 | + CC_B_PortDirectionIs_master = Master = 4 # Not supported yet | |
18 | + CC_B_PortDirectionIs_slave = Slave = 5 # Not supported yet | |
19 | 19 | |
20 | - def render(self): ### CC_B_PortDirectionIs_{self.name}' | |
21 | - return f'CC_B_PortDirectionIs_{self.name}' | |
20 | + def portray_name(self): ### CC_B_PortDirectionIs_{self.name}' | |
21 | + return f'{self.name}' | |
22 | 22 | |
23 | 23 | @dataclass |
24 | 24 | class CC_Port(CC_Base): |
@@ -27,8 +27,15 @@ | ||
27 | 27 | direction: CC_PortDirection = CC_PortDirection.Unknown |
28 | 28 | type: type |
29 | 29 | |
30 | - def render(self) ->str: ### <port name> | |
31 | - return f'cc_P_{self.type if isinstance(self.type, str) else self.type.name}' | |
30 | + def portray_name(self) ->str: ### <port name> | |
31 | + return f'{self.name}' | |
32 | + | |
33 | + def portray_type(self) ->str: ### <port type> e.g a protocol | |
34 | + if isinstance(self.type, CC_Base): | |
35 | + return self.type.portray_name() | |
36 | + else: | |
37 | + tn = self.type if isinstance(self.type, str) else self.type.__name__ | |
38 | + return f'cc_P_{tn}' | |
32 | 39 | |
33 | 40 | |
34 | 41 | @dataclass |
@@ -37,6 +37,9 @@ | ||
37 | 37 | kind: CC_ProtocolKind |
38 | 38 | based_on: Optional[CC_B_Protocol]=dc_field(default_factory= lambda :CC_B_Protocol._BASE) |
39 | 39 | |
40 | + def portray_name(self): | |
41 | + return f'cc_P_{self.name}' | |
42 | + | |
40 | 43 | |
41 | 44 | baseProtocol = CC_B_Protocol("Protocol", kind=CC_ProtocolKind.Unknown, based_on=None) # pragma: no mutate |
42 | 45 | CC_B_Protocol._BASE=baseProtocol |
@@ -72,16 +75,15 @@ | ||
72 | 75 | self.render_indexes(prepend) + "\n" + |
73 | 76 | self.render_FTs(prepend) + "\n" ) |
74 | 77 | |
75 | - | |
76 | 78 | def render_struct(self, prepend:str="", indent=" ") ->str: ## struct CC_B_Protocol $name = {...} ; |
77 | - var_name = f'cc_P_{self.name}' | |
78 | - based_on_link = f'&cc_P_{self.based_on.name}' if self.based_on else "NULL" | |
79 | + var_name = self.portray_name() | |
80 | + based_on_ref = f'&{self.based_on.portray_name()}' if self.based_on else "NULL" | |
79 | 81 | |
80 | 82 | retval = [] |
81 | 83 | retval.append(f'{prepend}struct CC_B_Protocol {var_name} = {{') |
82 | 84 | retval.append(f'{prepend}{indent}.name = "{self.name}",') |
83 | 85 | retval.append(f'{prepend}{indent}.kind = CC_B_ProtocolKindIs_{self.kind.name},') |
84 | - retval.append(f'{prepend}{indent}.inherit_from = {based_on_link},') | |
86 | + retval.append(f'{prepend}{indent}.inherit_from = {based_on_ref},') | |
85 | 87 | retval.append(f'{prepend}{indent}.length = {len(self.events)},') |
86 | 88 | retval.append(f'{prepend}{indent}.events = {{') |
87 | 89 |
@@ -2,7 +2,7 @@ | ||
2 | 2 | """ |
3 | 3 | Test the supporting types (Enum, dataclasses ect) for CC_B_ComponentInterface, and ... |
4 | 4 | |
5 | -The more relevant test of Protocol can be found in test_3b_* and test_3c_*""" | |
5 | +The more relevant test of Component(s) can be found in test_3b_* and test_3c_*""" | |
6 | 6 | |
7 | 7 | import logging; logger = logging.getLogger(__name__) |
8 | 8 | import pytest |
@@ -10,6 +10,7 @@ | ||
10 | 10 | |
11 | 11 | from castle.writers.CC2Cpy.Component import * |
12 | 12 | |
13 | + | |
13 | 14 | def test_1a_CC_PortDirection(): |
14 | 15 | # Test the (int) value -- needed for the generated C code |
15 | 16 | assert CC_PortDirection.Unknown.value == 0 |
@@ -27,8 +28,14 @@ | ||
27 | 28 | assert CC_PortDirection.Master == CC_PortDirection.CC_B_PortDirectionIs_master |
28 | 29 | assert CC_PortDirection.Slave == CC_PortDirection.CC_B_PortDirectionIs_slave |
29 | 30 | |
30 | -@pytest.mark.skip(reason="Is rendering needed?") | |
31 | -def test_1b_render_PortDirection(): pass | |
31 | +def test_1b_portray_PortDirection(): | |
32 | + assert CCompare('CC_B_PortDirectionIs_UNKNOWN', CC_PortDirection.Unknown.portray_name()) | |
33 | + assert CCompare('CC_B_PortDirectionIs_in', CC_PortDirection.In.portray_name()) | |
34 | + assert CCompare('CC_B_PortDirectionIs_out', CC_PortDirection.Out.portray_name()) | |
35 | + assert CCompare('CC_B_PortDirectionIs_bidirect', CC_PortDirection.BiDir.portray_name()) | |
36 | + assert CCompare('CC_B_PortDirectionIs_master', CC_PortDirection.Master.portray_name()) | |
37 | + assert CCompare('CC_B_PortDirectionIs_slave', CC_PortDirection.Slave.portray_name()) | |
38 | + | |
32 | 39 | |
33 | 40 | def test_2a1_Port_defaults(): |
34 | 41 | n, t = "defaults", int |
@@ -46,11 +53,31 @@ | ||
46 | 53 | assert inp.direction == d |
47 | 54 | |
48 | 55 | |
49 | -@pytest.mark.skip(reason="Is rendering needed?") | |
50 | -def test_2z_render_Port(): pass | |
51 | - | |
56 | +def test_2b1_portray_Port_name(): | |
57 | + port = CC_Port(name="aPort", type="no_relevant") | |
58 | + assert CCompare('aPort', port.portray_name()) | |
52 | 59 | |
53 | - | |
60 | +def test_2b2a_portray_Port_strtype(): | |
61 | + port = CC_Port(name="=NoName", type="textType") | |
62 | + assert CCompare('cc_P_textType', port.portray_type()) | |
54 | 63 | |
55 | -@pytest.mark.skip(reason="More basic-testse are needed") | |
56 | -def test_more(): pass | |
64 | +def test_2b2b_portray_Port_inttype(): | |
65 | + port = CC_Port(name="=NoName", type=int) | |
66 | + assert CCompare('cc_P_int', port.portray_type()) | |
67 | + | |
68 | +def test_2b2c_portray_Port_floattype(): | |
69 | + port = CC_Port(name="=NoName", type=float) | |
70 | + assert CCompare('cc_P_float', port.portray_type()) | |
71 | + | |
72 | +from castle.writers.CC2Cpy.Protocol import * #CC_EventProtocol | |
73 | + | |
74 | +def test_2b2c_portray_Port_Protocol(): | |
75 | + proto = CC_EventProtocol("JustAProtocol", events=[], based_on=None) | |
76 | + port = CC_Port(name="=NoName", type=proto) | |
77 | + # Note: When a Port's type is a Protocol, than the port's portray_type is both | |
78 | + ## the string 'cc_P_<xxxx>', and | |
79 | + ## the portray_name of that protocol. | |
80 | + ## We check both for now. | |
81 | + assert CCompare('cc_P_JustAProtocol', port.portray_type()) | |
82 | + assert CCompare(proto.portray_name(), port.portray_type()) | |
83 | + |