Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | 9736185df0c072a4c76a5306bd183938ddef91f5 (tree) |
---|---|
Zeit | 2023-01-16 05:55:45 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
Added CCompare to test CC2Cpy (Copy out IPython) + tests for it
@@ -1,6 +1,7 @@ | ||
1 | 1 | default: all |
2 | 2 | |
3 | -CURRENT_TESTS = pytst/auxiliary/test_1_pack_mk_tuple.py \ | |
3 | +CURRENT_TESTS = \ | |
4 | + pytst/writers/CC2Cpy/test_0.py \ | |
4 | 5 | |
5 | 6 | |
6 | 7 | all: current demo test pyanalyse XXX missing |
@@ -25,8 +26,11 @@ | ||
25 | 26 | mutmut results |
26 | 27 | open mutmut-report/index.html |
27 | 28 | |
29 | + | |
28 | 30 | current: |
29 | 31 | PYTHONPATH=`pwd` pytest ${PYTEST_OPTONS} ${CURRENT_TESTS} |
32 | +current-ds current-sd: | |
33 | + PYTHONPATH=`pwd` pytest ${PYTEST_OPTONS} --log-cli-level=DEBUG -s ${CURRENT_TESTS} | |
30 | 34 | |
31 | 35 | |
32 | 36 | demo: pytest-demo python-demo |
@@ -0,0 +1,36 @@ | ||
1 | +# (C) Albert Mietus, 2022, 2023. Part of Castle/CCastle project | |
2 | + | |
3 | +from typing import Generator | |
4 | + | |
5 | +__all__ = ['CCompare'] | |
6 | + | |
7 | + | |
8 | +def CCompare(ref_code: str, try_code: str, log=True, log_all=False) ->bool: | |
9 | + """"Compare two piece of (simple) C-code and return whether they are similar. | |
10 | + | |
11 | + It ignores most whitespace, but not ATS-based. Make sure the ref in correctly formated! | |
12 | + | |
13 | + Note: this is down-ported as zip(... strict=True) is python-3.10 | |
14 | + """ | |
15 | + if log_all: log=True | |
16 | + | |
17 | + ref_tokens = text2tokens(ref_code) | |
18 | + try_tokens = text2tokens(try_code) | |
19 | + for a, b in zip(ref_tokens, try_tokens): | |
20 | + if a != b: | |
21 | + if log: print(f'{a}!={b}') | |
22 | + return False | |
23 | + if log_all: print(f'{a}') | |
24 | + if len(list(ref_tokens)) == len(list(try_tokens)): | |
25 | + return True | |
26 | + #else | |
27 | + if log: print("Not the same length") | |
28 | + return False | |
29 | + | |
30 | + | |
31 | +def text2tokens(text: str) -> Generator[str, None, None]: | |
32 | + for txt in text.split(): | |
33 | + for t in txt.split(';'): | |
34 | + yield ';' if t=='' else t | |
35 | + | |
36 | + |
@@ -0,0 +1,28 @@ | ||
1 | +# (C) Albert Mietus, 2022, 2023. Part of Castle/CCastle project | |
2 | + | |
3 | +from . import * | |
4 | + | |
5 | +from castle.writers.CC2Cpy import * | |
6 | + | |
7 | +LOG_ALL=True | |
8 | + | |
9 | +def test_0_trival_same(): | |
10 | + abc='A B C' | |
11 | + assert CCompare(abc,abc, log_all=LOG_ALL) | |
12 | + | |
13 | +def test_1_not_same(): | |
14 | + abc='A B C' | |
15 | + assert False == CCompare(abc,abc[::-1], log_all=LOG_ALL) | |
16 | + | |
17 | +def test_2_diff_length(): | |
18 | + abc=' A B C ' | |
19 | + assert False == CCompare(abc, abc+abc, log_all=LOG_ALL) | |
20 | + assert False == CCompare(abc+abc, abc, log_all=LOG_ALL) | |
21 | + | |
22 | +def test_4_logvariants(): | |
23 | + abc=' A B C ' | |
24 | + assert CCompare(abc,abc, log=False, log_all=False) | |
25 | + assert CCompare(abc,abc, log=False, log_all=True) # log=.. will be overiden | |
26 | + assert CCompare(abc,abc, log=True , log_all=False) | |
27 | + assert CCompare(abc,abc, log=True, log_all=True) | |
28 | + |