diff options
| author | MoonEyes <ducoinbenjamin@yahoo.fr> | 2026-12-02 12:03:46 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-12-02 12:03:46 +0000 |
| commit | f6dd2242e02b5f579d245f1c19dd5150d42e00a1 (patch) | |
| tree | 22af90f369a76130dea2a209848685c29d125946 | |
| parent | f9d4ca90746256db604de8619c2b69c830801e09 (diff) | |
| parent | b83249219d15d8d7b2e282f0d7b9f9a4e8bbb98b (diff) | |
Merge pull request #3083 from MoonEyes/carry/340
test: unit tests for tools/gen_gdk_runtime_classes.py (TEST-GEN-GDK-RUNTIME, carry of #340)
| -rw-r--r-- | tests/test_gen_gdk_runtime_classes.py | 112 |
1 files changed, 110 insertions, 2 deletions
diff --git a/tests/test_gen_gdk_runtime_classes.py b/tests/test_gen_gdk_runtime_classes.py index dc6681a33..e03a9698e 100644 --- a/tests/test_gen_gdk_runtime_classes.py +++ b/tests/test_gen_gdk_runtime_classes.py | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | """Tests for atomic output from gen_gdk_runtime_classes.""" | 1 | """Tests for tools/gen_gdk_runtime_classes.py: atomic output, parsing and the CLI.""" |
| 2 | import contextlib | ||
| 3 | import io | ||
| 2 | import json | 4 | import json |
| 3 | import os | 5 | import os |
| 4 | from pathlib import Path | 6 | from pathlib import Path |
| @@ -11,9 +13,16 @@ import subprocess | |||
| 11 | import sys | 13 | import sys |
| 12 | import tempfile | 14 | import tempfile |
| 13 | import unittest | 15 | import unittest |
| 16 | from tempfile import TemporaryDirectory | ||
| 17 | from unittest import mock | ||
| 14 | 18 | ||
| 15 | 19 | ||
| 16 | SCRIPT = Path(__file__).resolve().parents[1] / "tools" / "gen_gdk_runtime_classes.py" | 20 | REPO = Path(__file__).resolve().parents[1] |
| 21 | sys.path.insert(0, str(REPO / "tools")) | ||
| 22 | |||
| 23 | import gen_gdk_runtime_classes as gen # noqa: E402 | ||
| 24 | |||
| 25 | SCRIPT = REPO / "tools" / "gen_gdk_runtime_classes.py" | ||
| 17 | 26 | ||
| 18 | 27 | ||
| 19 | def make_docs(root: Path) -> Path: | 28 | def make_docs(root: Path) -> Path: |
| @@ -89,5 +98,104 @@ class GenGdkRuntimeClassesTests(unittest.TestCase): | |||
| 89 | self.assertEqual(table["interfaces"], {}) | 98 | self.assertEqual(table["interfaces"], {}) |
| 90 | 99 | ||
| 91 | 100 | ||
| 101 | |||
| 102 | class TestParseLayout(unittest.TestCase): | ||
| 103 | def test_returns_none_without_layout_section(self) -> None: | ||
| 104 | self.assertIsNone(gen.parse_layout("# IFoo\nNo known methods.\n")) | ||
| 105 | |||
| 106 | def test_parses_methods_and_unknown_slots(self) -> None: | ||
| 107 | doc = ( | ||
| 108 | "# IExample\n\n" | ||
| 109 | "Layout:\n" | ||
| 110 | "- [Query](Query.md)\n" | ||
| 111 | "- *unknown*\n" | ||
| 112 | "- PlainMethod\n" | ||
| 113 | "\nTrailing paragraph stops parsing.\n" | ||
| 114 | "- IgnoredBullet\n" | ||
| 115 | ) | ||
| 116 | self.assertEqual(gen.parse_layout(doc), ["Query", None, "PlainMethod"]) | ||
| 117 | |||
| 118 | |||
| 119 | class TestParseDocsAndCheck(unittest.TestCase): | ||
| 120 | def test_missing_com_folder_raises(self) -> None: | ||
| 121 | with TemporaryDirectory() as tmp: | ||
| 122 | with self.assertRaises(FileNotFoundError): | ||
| 123 | gen.parse_docs(tmp) | ||
| 124 | |||
| 125 | def test_parses_classes_and_interfaces_and_detects_missing_links(self) -> None: | ||
| 126 | with TemporaryDirectory() as tmp: | ||
| 127 | root = Path(tmp) | ||
| 128 | cls_dir = root / "COM" / "XUserImpl" | ||
| 129 | cls_dir.mkdir(parents=True) | ||
| 130 | |||
| 131 | (cls_dir / "README.md").write_text( | ||
| 132 | "Class Id: `AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE`\n" | ||
| 133 | "Implements: [IXUser](IXUser.md), [IMissing](IMissing.md)\n", | ||
| 134 | encoding="utf-8", | ||
| 135 | ) | ||
| 136 | (cls_dir / "IXUser.md").write_text( | ||
| 137 | "Interface Id: `11111111-2222-3333-4444-555555555555`\n" | ||
| 138 | "Inherits: IUnknown\n" | ||
| 139 | "Layout:\n" | ||
| 140 | "- GetId\n", | ||
| 141 | encoding="utf-8", | ||
| 142 | ) | ||
| 143 | (cls_dir / "IXUser2.md").write_text( | ||
| 144 | "Interface Id: `66666666-7777-8888-9999-000000000000`\n" | ||
| 145 | "Inherits: [IXMissingBase](IXMissingBase.md)\n", | ||
| 146 | encoding="utf-8", | ||
| 147 | ) | ||
| 148 | |||
| 149 | classes, interfaces = gen.parse_docs(tmp) | ||
| 150 | self.assertEqual( | ||
| 151 | classes["XUserImpl"], | ||
| 152 | {"id": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "implements": ["IXUser", "IMissing"]}, | ||
| 153 | ) | ||
| 154 | self.assertIsNone(interfaces["IXUser"]["inherits"]) | ||
| 155 | self.assertEqual(interfaces["IXUser"]["methods"], ["GetId"]) | ||
| 156 | self.assertEqual(interfaces["IXUser2"]["inherits"], "IXMissingBase") | ||
| 157 | self.assertIsNone(interfaces["IXUser2"]["methods"]) | ||
| 158 | |||
| 159 | problems = gen.check(classes, interfaces) | ||
| 160 | self.assertIn("XUserImpl implements IMissing, which has no page", problems) | ||
| 161 | self.assertIn("IXUser2 inherits IXMissingBase, which has no page", problems) | ||
| 162 | |||
| 163 | |||
| 164 | class TestBuildAndMain(unittest.TestCase): | ||
| 165 | def test_revision_fallback_when_not_git_repo(self) -> None: | ||
| 166 | with TemporaryDirectory() as tmp: | ||
| 167 | self.assertEqual(gen.revision(tmp), "unknown revision") | ||
| 168 | |||
| 169 | def test_main_writes_json_and_handles_missing_docs(self) -> None: | ||
| 170 | with TemporaryDirectory() as tmp: | ||
| 171 | missing_dir = str(Path(tmp) / "does_not_exist") | ||
| 172 | with contextlib.redirect_stderr(io.StringIO()): | ||
| 173 | self.assertEqual(gen.main([missing_dir]), 1) | ||
| 174 | |||
| 175 | docs = Path(tmp) / "docs" | ||
| 176 | cls_dir = docs / "COM" / "Demo" | ||
| 177 | cls_dir.mkdir(parents=True) | ||
| 178 | (cls_dir / "README.md").write_text( | ||
| 179 | "Class Id: `00000000-0000-0000-0000-000000000001`\n" | ||
| 180 | "Implements: [IDemo](IDemo.md)\n", | ||
| 181 | encoding="utf-8", | ||
| 182 | ) | ||
| 183 | (cls_dir / "IDemo.md").write_text( | ||
| 184 | "Interface Id: `00000000-0000-0000-0000-000000000002`\n" | ||
| 185 | "Inherits: IUnknown\n" | ||
| 186 | "Layout:\n" | ||
| 187 | "- Ping\n", | ||
| 188 | encoding="utf-8", | ||
| 189 | ) | ||
| 190 | out_json = Path(tmp) / "out.json" | ||
| 191 | with mock.patch.object(gen, "revision", return_value="deadbeef"), \ | ||
| 192 | contextlib.redirect_stdout(io.StringIO()): | ||
| 193 | rc = gen.main([str(docs), "--out", str(out_json)]) | ||
| 194 | self.assertEqual(rc, 0) | ||
| 195 | data = json.loads(out_json.read_text(encoding="utf-8")) | ||
| 196 | self.assertTrue(data["source"].endswith("deadbeef")) | ||
| 197 | self.assertEqual(data["interfaces"]["IDemo"]["methods"], ["Ping"]) | ||
| 198 | |||
| 199 | |||
| 92 | if __name__ == "__main__": | 200 | if __name__ == "__main__": |
| 93 | unittest.main() | 201 | unittest.main() |