Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples and start testing them #609

Merged
merged 9 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion conda/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ requirements:

test:
requires:
- pytest
- pytest
- docutils
source_files:
- tests/
commands:
Expand Down
4 changes: 2 additions & 2 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ A Parametric Enclosure
topOfLid = topOfLid.rotateAboutCenter((1,0,0),180)

#return the combined result
result =topOfLid.combineSolids(bottom)
result =topOfLid.union(bottom)


.. topic:: Api References
Expand All @@ -893,7 +893,7 @@ A Parametric Enclosure
* :py:meth:`Workplane.workplane`
* :py:meth:`Workplane.fillet`
* :py:meth:`Workplane.cut`
* :py:meth:`Workplane.combineSolids`
* :py:meth:`Workplane.union`
* :py:meth:`Workplane.rotateAboutCenter`
* :py:meth:`Workplane.cboreHole`
* :py:meth:`Workplane.cskHole`
Expand Down
3 changes: 2 additions & 1 deletion examples/Ex009_Polylines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# Define the points that the polyline will be drawn to/thru
pts = [
(0, H / 2.0),
(W / 2.0, H / 2.0),
(W / 2.0, (H / 2.0 - t)),
(t / 2.0, (H / 2.0 - t)),
Expand All @@ -30,7 +31,7 @@
# 3. Only half of the I-beam profile has been drawn so far. That half is
# mirrored around the Y-axis to create the complete I-beam profile.
# 4. The I-beam profile is extruded to the final length of the beam.
result = cq.Workplane("front").moveTo(0, H / 2.0).polyline(pts).mirrorY().extrude(L)
result = cq.Workplane("front").polyline(pts).mirrorY().extrude(L)

# Displays the result of this script
show_object(result)
2 changes: 1 addition & 1 deletion examples/Ex010_Defining_an_Edge_with_a_Spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# 2. Generate our plate with the spline feature and make sure it is a
# closed entity
r = s.lineTo(3.0, 0).lineTo(3.0, 1.0).spline(sPnts).close()
r = s.lineTo(3.0, 0).lineTo(3.0, 1.0).spline(sPnts, includeCurrent=True).close()

# 3. Extrude to turn the wire into a plate
result = r.extrude(0.5)
Expand Down
2 changes: 1 addition & 1 deletion examples/Ex013_Locating_a_Workplane_on_a_Vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 3a. The top-most Z face is selected using the >Z selector.
# 3b. The lower-left vertex of the faces is selected with the <XY selector.
# 3c. A new workplane is created on the vertex to build future geometry on.
result = result.faces(">Z").vertices("<XY").workplane()
result = result.faces(">Z").vertices("<XY").workplane(centerOption="CenterOfMass")

# 4. A circle is drawn with the selected vertex as its center.
# 4a. The circle is cut down through the box to cut the corner out.
Expand Down
4 changes: 2 additions & 2 deletions examples/Ex023_Sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
defaultRect = cq.Workplane("XY").rect(1.0, 1.0).sweep(path)

# Switch to a polyline path, but have it use the same points as the spline
path = cq.Workplane("XZ").polyline(pts)
path = cq.Workplane("XZ").polyline(pts, includeCurrent=True)

# Using a polyline path leads to the resulting solid having segments rather than a single swept outer face
plineSweep = cq.Workplane("XY").circle(1.0).sweep(path)
Expand All @@ -32,5 +32,5 @@
show_object(defaultSweep)
show_object(frenetShell.translate((5, 0, 0)))
show_object(defaultRect.translate((10, 0, 0)))
show_object(plineSweep.translate((15, 0, 0)))
show_object(plineSweep)
show_object(arcSweep.translate((20, 0, 0)))
4 changes: 2 additions & 2 deletions examples/Ex024_Sweep_With_Multiple_Sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
.workplane(offset=-5)
.moveTo(0, 4)
.circle(1.5)
.workplane(offset=5)
.workplane(offset=5, centerOption="CenterOfMass")
.circle(1.5)
.moveTo(0, -8)
.circle(1.0)
.workplane(offset=-5)
.workplane(offset=-5, centerOption="CenterOfMass")
.circle(1.0)
.sweep(path, multisection=True)
)
Expand Down
72 changes: 72 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from glob import glob
from itertools import chain, count

from docutils.parsers.rst import directives, Directive
from docutils.core import publish_doctree
from docutils.utils import Reporter

import cadquery as cq
from cadquery import cqgi
from cadquery.cq_directive import cq_directive


def find_examples(pattern="examples/*.py"):

for p in glob(pattern):
with open(p, encoding="UTF-8") as f:
code = f.read()

yield code


def find_examples_in_docs(pattern="doc/*.rst"):

# dummy CQ directive for code
class dummy_cq_directive(cq_directive):

codes = []

def run(self):

self.codes.append("\n".join(self.content))

return []

directives.register_directive("cadquery", dummy_cq_directive)

# read and parse all rst files
for p in glob(pattern):
with open(p, encoding="UTF-8") as f:
doc = f.read()

publish_doctree(
doc, settings_overrides={"report_level": Reporter.SEVERE_LEVEL + 1}
)

# yield all code snippets
for c in dummy_cq_directive.codes:

yield c


@pytest.mark.parametrize(
"code", chain(find_examples(), find_examples_in_docs()), ids=count(0)
)
def test_example(code):

# build
res = cqgi.parse(code).build()

assert res.exception is None

# check if the resulting objects are valid
for r in res.results:
r = r.shape
if isinstance(r, cq.Workplane):
for v in r.vals():
if isinstance(v, cq.Shape):
assert v.isValid()
elif isinstance(r, cq.Shape):
assert r.isValid()