|
| 1 | +import ast |
| 2 | +from datetime import datetime, timezone |
| 3 | +import inspect |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | + |
| 6 | +from twinforge.exporters import ( |
| 7 | + PLCOPEN_201_NAMESPACE, |
| 8 | + PLCopenExporter, |
| 9 | + PLCopenProfile, |
| 10 | +) |
| 11 | +from twinforge.model import ( |
| 12 | + Controller, |
| 13 | + Identity, |
| 14 | + LadderRung, |
| 15 | + Program, |
| 16 | + Routine, |
| 17 | + Tag, |
| 18 | + Task, |
| 19 | +) |
| 20 | +from twinforge.targets.openplc import OpenPLCExporter |
| 21 | +from twinforge.targets.openplc import exporter as openplc_module |
| 22 | + |
| 23 | + |
| 24 | +FIXED_TIME = datetime(2026, 7, 30, tzinfo=timezone.utc) |
| 25 | + |
| 26 | + |
| 27 | +def _controller() -> Controller: |
| 28 | + controller = Controller(name="OpenPLCTest", identity=Identity()) |
| 29 | + controller.add_tag(Tag(name="Enable", data_type="BOOL")) |
| 30 | + controller.add_tag(Tag(name="Output", data_type="BOOL")) |
| 31 | + program = Program(name="PLC_PRG") |
| 32 | + routine = Routine(name="MainRoutine", language="RLL") |
| 33 | + routine.ladder_rungs.append( |
| 34 | + LadderRung(number=0, text="XIC(Enable)OTE(Output);") |
| 35 | + ) |
| 36 | + program.add_routine(routine) |
| 37 | + controller.add_program(program) |
| 38 | + controller.add_task( |
| 39 | + Task( |
| 40 | + name="MainTask", |
| 41 | + task_type="Periodic", |
| 42 | + rate=20, |
| 43 | + priority=1, |
| 44 | + scheduled_program_names=[program.name], |
| 45 | + scheduled_programs=[program], |
| 46 | + ) |
| 47 | + ) |
| 48 | + return controller |
| 49 | + |
| 50 | + |
| 51 | +def test_openplc_target_matches_standard_plcopen_profile() -> None: |
| 52 | + controller = _controller() |
| 53 | + expected = PLCopenExporter(PLCopenProfile.STANDARD_201).export( |
| 54 | + controller, |
| 55 | + project_name="OpenPLC Project", |
| 56 | + creation_time=FIXED_TIME, |
| 57 | + ) |
| 58 | + |
| 59 | + actual = OpenPLCExporter().export( |
| 60 | + controller, |
| 61 | + project_name="OpenPLC Project", |
| 62 | + creation_time=FIXED_TIME, |
| 63 | + ) |
| 64 | + |
| 65 | + assert actual.xml == expected.xml |
| 66 | + assert actual.diagnostics == expected.diagnostics |
| 67 | + |
| 68 | + |
| 69 | +def test_openplc_document_contains_no_codesys_extensions() -> None: |
| 70 | + result = OpenPLCExporter().export( |
| 71 | + _controller(), |
| 72 | + creation_time=FIXED_TIME, |
| 73 | + ) |
| 74 | + root = ET.fromstring(result.xml) |
| 75 | + |
| 76 | + assert root.tag == f"{{{PLCOPEN_201_NAMESPACE}}}project" |
| 77 | + assert "3s-software.com" not in result.xml |
| 78 | + assert "ProjectStructure" not in result.xml |
| 79 | + assert "ObjectId" not in result.xml |
| 80 | + |
| 81 | + |
| 82 | +def test_openplc_adapter_has_no_direct_codesys_import() -> None: |
| 83 | + tree = ast.parse(inspect.getsource(openplc_module)) |
| 84 | + imported = { |
| 85 | + alias.name |
| 86 | + for node in ast.walk(tree) |
| 87 | + if isinstance(node, ast.Import) |
| 88 | + for alias in node.names |
| 89 | + } |
| 90 | + imported.update( |
| 91 | + node.module or "" |
| 92 | + for node in ast.walk(tree) |
| 93 | + if isinstance(node, ast.ImportFrom) |
| 94 | + ) |
| 95 | + |
| 96 | + assert not any("codesys" in module.casefold() for module in imported) |
0 commit comments