|
| 1 | +import pytest |
| 2 | + |
| 3 | +from glob import glob |
| 4 | +from itertools import chain, count |
| 5 | + |
| 6 | +from docutils.parsers.rst import directives, Directive |
| 7 | +from docutils.core import publish_doctree |
| 8 | +from docutils.utils import Reporter |
| 9 | + |
| 10 | +import cadquery as cq |
| 11 | +from cadquery import cqgi |
| 12 | +from cadquery.cq_directive import cq_directive |
| 13 | + |
| 14 | + |
| 15 | +def find_examples(pattern="examples/*.py"): |
| 16 | + |
| 17 | + for p in glob(pattern): |
| 18 | + with open(p, encoding="UTF-8") as f: |
| 19 | + code = f.read() |
| 20 | + |
| 21 | + yield code |
| 22 | + |
| 23 | + |
| 24 | +def find_examples_in_docs(pattern="doc/*.rst"): |
| 25 | + |
| 26 | + # dummy CQ directive for code |
| 27 | + class dummy_cq_directive(cq_directive): |
| 28 | + |
| 29 | + codes = [] |
| 30 | + |
| 31 | + def run(self): |
| 32 | + |
| 33 | + self.codes.append("\n".join(self.content)) |
| 34 | + |
| 35 | + return [] |
| 36 | + |
| 37 | + directives.register_directive("cadquery", dummy_cq_directive) |
| 38 | + |
| 39 | + # read and parse all rst files |
| 40 | + for p in glob(pattern): |
| 41 | + with open(p, encoding="UTF-8") as f: |
| 42 | + doc = f.read() |
| 43 | + |
| 44 | + publish_doctree( |
| 45 | + doc, settings_overrides={"report_level": Reporter.SEVERE_LEVEL + 1} |
| 46 | + ) |
| 47 | + |
| 48 | + # yield all code snippets |
| 49 | + for c in dummy_cq_directive.codes: |
| 50 | + |
| 51 | + yield c |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize( |
| 55 | + "code", chain(find_examples(), find_examples_in_docs()), ids=count(0) |
| 56 | +) |
| 57 | +def test_example(code): |
| 58 | + |
| 59 | + # build |
| 60 | + res = cqgi.parse(code).build() |
| 61 | + |
| 62 | + assert res.exception is None |
| 63 | + |
| 64 | + # check if the resulting objects are valid |
| 65 | + for r in res.results: |
| 66 | + r = r.shape |
| 67 | + if isinstance(r, cq.Workplane): |
| 68 | + for v in r.vals(): |
| 69 | + if isinstance(v, cq.Shape): |
| 70 | + assert v.isValid() |
| 71 | + elif isinstance(r, cq.Shape): |
| 72 | + assert r.isValid() |
0 commit comments