Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
修订版 | eb6a71735c23dd9708126a22493e2353edaaa316 (tree) |
---|---|
时间 | 2024-05-12 19:33:44 |
作者 | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
More sieveCastle; small mypy issue: 'Is Call a starement?' _(currently not, it a expression which is not a statement)_
@@ -5,8 +5,12 @@ | ||
5 | 5 | .. see also:: :file:`./__init__.py` for a general intro |
6 | 6 | """ |
7 | 7 | |
8 | +__all__ = ['Sieve'] | |
8 | 9 | |
9 | -from castle.aigr import ComponentImplementation, Body | |
10 | + | |
11 | +from castle import aigr | |
12 | +from castle.aigr import ComponentImplementation, ID, Method | |
13 | + | |
10 | 14 | |
11 | 15 | |
12 | 16 | # implement Sieve { |
@@ -18,17 +22,33 @@ | ||
18 | 22 | Sieve = ComponentImplementation('Sieve', |
19 | 23 | interface=components.SieveMoat, |
20 | 24 | parameters=(), |
21 | - body=Body()) # Body in filed below | |
22 | - | |
23 | - | |
24 | - | |
25 | + body=aigr.Body()) # Body in filed below | |
25 | 26 | |
26 | 27 | # init(int:onPrime) // `init` is (typically) part of the construction of a element. |
27 | 28 | # { |
28 | 29 | # super.init(); // `super` acts as port to the base-class |
29 | 30 | # .myPrime := onPrime; |
30 | 31 | # } |
31 | -# | |
32 | + | |
33 | +init_method = Method(ID('init'), | |
34 | + returns=None, | |
35 | + parameters=(aigr.TypedParameter(name=ID('onPrime'), type=int),), | |
36 | + body=aigr.Body(statements=[ | |
37 | + aigr.Call( | |
38 | + callable=aigr.Part( | |
39 | + base=aigr.Call(callable=ID('super')), attribute=ID('init')), | |
40 | + arguments=()), | |
41 | + aigr.Become( | |
42 | + targets=(aigr.Part(base=ID('self'), attribute=ID('myPrime', context=aigr.Set())),), | |
43 | + values=(ID('onPrime', context=aigr.Ref()),))])) | |
44 | + | |
45 | +assert isinstance(Sieve.body, aigr.Body) # This make mypy happy for the next line :-) | |
46 | +Sieve.body.expand(init_method) | |
47 | + | |
48 | + | |
49 | + | |
50 | + | |
51 | + | |
32 | 52 | # SimpleSieve.input(try) on .try |
33 | 53 | # { |
34 | 54 | # if ( (try % .myPrime) !=0 ) { |
@@ -0,0 +1,21 @@ | ||
1 | +# (C) Albert Mietus 2024, Part of Castle/CCastle project | |
2 | +"""Test the AIGR TestDoubles of the Sieve protocols (basic1 variant) | |
3 | + See: http://docideas.mietus.nl/en/default/CCastle/4.Blog/b.TheSieve.html#the-design | |
4 | +""" | |
5 | +import logging; logger = logging.getLogger(__name__) | |
6 | + | |
7 | +import typing as PTH # Python TypeHints | |
8 | +from castle import aigr | |
9 | + | |
10 | + | |
11 | +def find_name_in_body(name, body) -> PTH.Optional[aigr.statements.callables._Named_callable]: | |
12 | + logger.info("find_name_in_body(name=%s, body=%s", name, body) | |
13 | + for s in body.statements: | |
14 | + logger.debug("\t: %s", s) | |
15 | + if isinstance(s, aigr.NamedNode): | |
16 | + logger.debug("\t: name=%s in %s", s.name, s) | |
17 | + if s.name == name: | |
18 | + logger.debug("\t: FOUND name=%s in %s", s.name, s) | |
19 | + return s | |
20 | + logger.debug("NOT FOUND: %s", name) | |
21 | + return None |
@@ -2,11 +2,15 @@ | ||
2 | 2 | """Test the AIGR TestDoubles of the Sieve protocols (basic1 variant) |
3 | 3 | See: http://docideas.mietus.nl/en/default/CCastle/4.Blog/b.TheSieve.html#the-design |
4 | 4 | """ |
5 | +import logging; logger = logging.getLogger(__name__) | |
5 | 6 | |
6 | 7 | import pytest |
8 | + | |
7 | 9 | from castle.TESTDOUBLES.aigr.sieve.basic1 import sieveCastle |
10 | +from castle import aigr | |
8 | 11 | |
9 | -from castle import aigr | |
12 | +from . import find_name_in_body | |
13 | + | |
10 | 14 | |
11 | 15 | @pytest.fixture |
12 | 16 | def comp(): |
@@ -14,7 +18,7 @@ | ||
14 | 18 | |
15 | 19 | |
16 | 20 | def test_0a_types(comp): |
17 | - assert isinstance(comp, aigr.ComponentImplementation) | |
21 | + assert isinstance(comp, aigr.ComponentImplementation) | |
18 | 22 | assert isinstance(comp.interface, aigr.ComponentInterface) |
19 | 23 | assert isinstance(comp.parameters, (type(None), tuple)) |
20 | 24 | assert isinstance(comp.body, aigr.Body) |
@@ -24,5 +28,13 @@ | ||
24 | 28 | assert comp.name == 'Sieve' |
25 | 29 | |
26 | 30 | def test_0c_noParms(comp): |
27 | - assert comp.parameters is () | |
31 | + assert comp.parameters == () | |
28 | 32 | |
33 | +def test_has_init(comp): | |
34 | + init = find_name_in_body('init', comp.body) | |
35 | + assert isinstance(init, aigr.Method), f"Expected an init method, got {init}" | |
36 | + | |
37 | +def test_init_has_body(comp): | |
38 | + init = find_name_in_body('init', comp.body) | |
39 | + assert len(init.body)==2, f"Expected that 'init' has 2 statements, but found: {len(init.body.statements)}" | |
40 | + |