def test_copy_family_no_isolation(self) -> None: grandparent = component.Component() parent = component.Component(parent=grandparent) test_component = component.Component(parent=parent) child = component.Component() test_component.add_child(child) copy_component = test_component.copy() self.assertIs( copy_component.parent, parent, msg= "Parenthood should be correctly assigned on non-isolated copying", ) self.assertEqual( list(copy_component.parents), [parent, grandparent], msg= "All parent relationships should be preserved on non-isolated copying", ) self.assertIsNot( child, copy_component.children[0], msg="Children should be copied, not reparented", ) self.assertTrue( test_component.same_children(copy_component), msg="Copied components should have equal children", ) self.assertEqual(test_component, copy_component, msg="Copied components must be equal")
def test_reparent_raise(self) -> None: parent = component.Component() child = component.Component(parent=parent) with self.assertRaises( component.ReparentException, msg= "Should error if we attempt to add a parent to a child that has one", ): child.parent = component.Component()
def test_grandparent(self) -> None: grandparent = component.Component() parent = component.Component(parent=grandparent) test_component = component.Component(parent=parent) self.assertEqual( list(test_component.parents), [parent, grandparent], msg="The parents iterator should contain all parents in order", )
def test_body_no_uncomposed_children(self) -> None: parent = component.Component() composed_child = component.Component() parent.compose(solid.union(), composed_child, make_children=True) self.assertIsNone( parent._body, msg= "The default `_body` of a component with no uncomposed children should be `None`", )
def test_composition_initializations(self) -> None: composee = component.Component() composition = (solid.union(), composee) composer = component.Component(compositions=[composition]) self.assertIn( composition, composer.compositions, msg="Compositions should be assigned correctly on initialization", )
def test_add_children(self) -> None: parent = component.Component() child_a = component.Component() child_b = component.Component() children = [child_a, child_b] parent.add_child(children) self.assertEqual(parent.children, children, msg="Children should be added to a parent")
def _arm(self, axis: vector.Vector, color: component.Color) -> component.Component: """Construct an arm for representing the frame's alignment Args: axis: The axis with which the arm should be aligned color: The color the arm should have """ arm_cylinder = cylinder.Cylinder(diameter=0.2, height=2.0) tip_cone = cone.Cone(bottom_diameter=0.3, height=0.3) # Put the cone on top of the cylinder and construct a container for them tip_cone.transform( tip_cone.bottom_anchor.align(arm_cylinder.top_anchor)) arm = component.Component(children=[arm_cylinder, tip_cone], color=color) # Align the arm with the desired axis arm.transform( arm_cylinder.bottom_anchor.align( connector.Connector.from_components(axis_x=axis.x, axis_y=axis.y, axis_z=axis.z))) return arm
def test_disembodied_component_raises(self) -> None: with self.assertRaises( component.DisembodiedComponent, msg= "Getting the body of a bare component with no children should error", ): component.Component().body
def test_copy_with_color(self) -> None: test_component = component.Component(color=(1.0, 1.0, 1.0, 1.0)) self.assertEqual( test_component.color, test_component.copy(with_color=True).color, msg="Copying with color should make a copy with the same color", )
def test_copy_children_in_isolation_with_transformaton(self) -> None: test_component = component.Component() child = component.Component() test_component.add_child(child) rotation = solid.rotate([11.0, 12.0, 13.0]) test_component.transform(rotation) copy_component = test_component.copy(isolate=True) self.assertTrue( test_component.same_children(copy_component), msg="Copied components should have equal children", ) self.assertEqual(test_component, copy_component, msg="Copied components must be equal")
def test_uncomposed_children(self) -> None: parent = component.Component() uncomposed_child = component.Component(parent=parent) # Transform the children, so they are nonequal uncomposed_child.transform(solid.rotate([1.0, 1.0, 1.0])) composed_child = component.Component() # Transform the children, so they are nonequal composed_child.transform(solid.rotate([2.0, 2.0, 2.0])) parent.compose(solid.union(), composed_child, make_children=True) self.assertEqual( list(parent.uncomposed_children), [uncomposed_child], msg= "No composed component should be in the uncomposed children list", )
def __init__( self, parent: component.Component = None, special_child: component.Component = None, ): if not special_child: special_child = component.Component() self.special_child = special_child super().__init__(parent=parent, children=[self.special_child])
def test_composed_components(self) -> None: parent = component.Component() # Transform the operands, so they are nonequal operand_a = component.Component() operand_a.transform(solid.rotate([1.0, 1.0, 1.0])) operand_b = component.Component() operand_b.transform(solid.rotate([2.0, 2.0, 2.0])) operands = [operand_a, operand_b] parent.compose(solid.union(), operands) self.assertEqual( list(parent.composed_components), operands, msg= "Composed components should contain components which have been composed", )
def test_body_with_uncomposed_children(self) -> None: parent = component.Component() uncomposed_child = MockEmbodiedComponent() uncomposed_child.parent = parent self.assertTrue( utils.compare_flattened_openscad_children( parent._body, solid.union()(solid.cube(1.0))), msg="Parents with uncomposed children should have defined `_body`s", )
def test_copy_without_color(self) -> None: test_component = component.Component(color=(1.0, 1.0, 1.0, 1.0)) copy_component = test_component.copy(with_color=False) self.assertNotEqual( test_component.color, copy_component.color, msg= "Copying without color should make a copy that doesn't have the same color", ) self.assertIsNone(copy_component.color, msg="The copy should have the default color")
def test_copy_family_in_isolation_with_transformaton(self) -> None: parent = component.Component() test_component = component.Component(parent=parent) child = component.Component() test_component.add_child(child) rotation = solid.rotate([11.0, 12.0, 13.0]) parent.transform(rotation) copy_component = test_component.copy(isolate=True) self.assertNotEqual( test_component, copy_component, msg= "Because the original component is transformed through a parent, the isolated copy is not equal", ) self.assertFalse( test_component.same_children(copy_component), msg= "Children of isolated copies do not retain parent transformations and so are unequal", )
def test_parent_child_initialization(self) -> None: parent = component.Component() child_a = component.Component() child_b = component.Component() test_component = component.Component(parent=parent, children=[child_a, child_b]) self.assertIs( test_component.parent, parent, msg="Parenthood should be correctly assigned on initialization", ) self.assertIn( child_a, test_component.children, msg="Children should be correctly assigned on initialization", ) self.assertIn( child_b, test_component.children, msg="Children should be correctly assigned on initialization", )
def test_copy_family_in_isolation(self) -> None: grandparent = component.Component() parent = component.Component(parent=grandparent) test_component = component.Component(parent=parent) child = component.Component() test_component.add_child(child) copy_component = test_component.copy(isolate=True) self.assertEqual( list(copy_component.parents), [], msg="An isolated copy should have no parents", ) self.assertTrue( test_component.same_children(copy_component), msg="Copied components should have equal children", ) self.assertEqual( test_component, copy_component, msg= "Isolated copied components should be equal unless the original's parents have been transformed", )
def test_body_pure_container(self) -> None: parent = component.Component() composer_1 = MockEmbodiedComponent(2.0) composer_2 = MockEmbodiedComponent(3.0) parent.compose(solid.union(), composer_1, make_children=False) parent.compose(solid.difference(), composer_2, make_children=False) self.assertTrue( utils.compare_flattened_openscad_children( parent.body, solid.difference()(solid.union()(solid.cube(size=2.0)), solid.cube(size=3.0)), ), msg= "The innermost composition on pure containers must first be evaluted on its own", )
def test_reparent_to_parent(self) -> None: """Redundantly assigning parenthood should not cause an issue""" parent = component.Component() child = component.Component(parent=parent) child.parent = parent