- Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathtest_examples.py
executable file
·75 lines (50 loc) · 1.71 KB
/
test_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
importpytest
fromglobimportglob
fromitertoolsimportchain, count
frompathimportPath
fromdocutils.parsers.rstimportdirectives
fromdocutils.coreimportpublish_doctree
fromdocutils.utilsimportReporter
importcadqueryascq
fromcadqueryimportcqgi
fromcadquery.cq_directiveimportcq_directive
deffind_examples(pattern="examples/*.py", path=Path("examples")):
forpinglob(pattern):
withopen(p, encoding="UTF-8") asf:
code=f.read()
yieldcode, path
deffind_examples_in_docs(pattern="doc/*.rst", path=Path("doc")):
# dummy CQ directive for code
classdummy_cq_directive(cq_directive):
codes= []
defrun(self):
self.codes.append("\n".join(self.content))
return []
directives.register_directive("cadquery", dummy_cq_directive)
# read and parse all rst files
forpinglob(pattern):
withopen(p, encoding="UTF-8") asf:
doc=f.read()
publish_doctree(
doc, settings_overrides={"report_level": Reporter.SEVERE_LEVEL+1}
)
# yield all code snippets
forcindummy_cq_directive.codes:
yieldc, path
@pytest.mark.parametrize(
"code, path", chain(find_examples(), find_examples_in_docs()), ids=count(0)
)
deftest_example(code, path):
# build
withpath:
res=cqgi.parse(code).build()
assertres.exceptionisNone
# check if the resulting objects are valid
forrinres.results:
r=r.shape
ifisinstance(r, cq.Workplane):
forvinr.vals():
ifisinstance(v, cq.Shape):
assertv.isValid()
elifisinstance(r, cq.Shape):
assertr.isValid()