class VariableTest(unittest.TestCase): """ This only tests basic getters and setters, no constraint solving technology. """ def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system.add_stay_constraint = Mock() def test_get_and_set_variable(self): self.assertEqual(self.variable.get_value(), 10) self.variable.set_value(5) self.assertEqual(5, self.variable.get_value()) def test_stay(self): self.assertTrue(self.variable.stay_constraint is None) self.variable.stay() self.assertFalse(self.variable.stay_constraint is None) self.variable.system.add_stay_constraint.assert_called_with(self.variable,Strength.WEAK) def test_stay_tow_times(self): self.variable.stay() self.assertFalse(self.variable.system.remove_constraint.called) self.variable.stay(Strength.STRONG) self.assertTrue(self.variable.system.remove_constraint.called) self.variable.system.add_stay_constraint.assert_called_with(self.variable,Strength.STRONG)
class VariableTest(unittest.TestCase): """ This only tests basic getters and setters, no constraint solving technology. """ def setUp(self): self.variable = Variable("variable", 10, Mock()) self.variable.system.remove_constraint = Mock() self.variable.system._add_stay_constraint = Mock() def test_get_and_set_variable(self): self.assertEqual(self.variable.get_value(), 10) self.variable.set_value(5) self.assertEqual(5, self.variable.get_value()) def test_stay(self): self.assertTrue(self.variable.stay_constraint is None) self.variable.stay() self.assertFalse(self.variable.stay_constraint is None) self.variable.system._add_stay_constraint.assert_called_with( self.variable, Strength.WEAK) def test_stay_tow_times(self): self.variable.stay() self.assertFalse(self.variable.system.remove_constraint.called) self.variable.stay(Strength.STRONG) self.assertTrue(self.variable.system.remove_constraint.called) self.variable.system._add_stay_constraint.assert_called_with( self.variable, Strength.STRONG)
import sys sys.path.append("../src") sys.path.append("../tests") from skypyblue.core import ConstraintSystem from skypyblue.models import Constraint, Strength, Variable, Method cs = ConstraintSystem() v1 = Variable("v1", 16, cs) v2 = Variable("v2", 16, cs) v3 = Variable("v3", 16, cs) v4 = Variable("v4", 10, cs) v4.stay() # v1 + v2 = v3, v1 - v2 = v4 m12_34 = Method([v1, v2], [v3, v4], lambda v1, v2: (v1 + v2, v1 - v2)) m34_12 = Method([v3, v4], [v1, v2], lambda v3, v4: ((v3 + v4) / 2, (v3 - v4) / 2)) cn1 = Constraint(lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4, Strength.STRONG, [v1, v2, v3, v4], [m12_34, m34_12]) cs.add_constraint(cn1) def print_values(): print("v1 = " + str(v1.get_value()))
import sys sys.path.append("../src") sys.path.append("../tests") from skypyblue.core import ConstraintSystem from skypyblue.models import Constraint, Strength, Variable, Method cs = ConstraintSystem() v1 = Variable("v1", 16, cs) v2 = Variable("v2", 16, cs) v3 = Variable("v3", 16, cs) v4 = Variable("v4", 10, cs) v4.stay() # v1 + v2 = v3, v1 - v2 = v4 m12_34 = Method([v1, v2], [v3, v4], lambda v1, v2: (v1 + v2 , v1 - v2)) m34_12 = Method([v3, v4], [v1, v2], lambda v3, v4: ((v3 + v4) / 2 , (v3 - v4) / 2)) cn1 = Constraint( lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4, Strength.STRONG, [v1, v2, v3, v4], [m12_34, m34_12]) cs.add_constraint(cn1)
class ExtendedMidpointTest(unittest.TestCase): __name__ = "ExtendedMidpointTest" def setUp(self): self.constraint_system = ConstraintSystem() self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system) self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system) self.point3 = Variable("Point 3", Point(50, 20), self.constraint_system) self.point4 = Variable("Point 4", Point(100, 30), self.constraint_system) self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system) mpmp3p4 = Method([self.midpoint, self.point3, self.point4], [self.point1, self.point2], lambda pm, p3, p4: ( Point( pm.X - (p4.X - p3.X), pm.Y - (p4.Y - p3.Y) ), Point( pm.X + (p4.X - p3.X), pm.Y + (p4.Y - p3.Y) ) ) ) mp1pmp3 = Method([self.point1, self.midpoint, self.point3], [self.point2, self.point4], lambda p1, pm, p3: ( Point( 2 * pm.X - p1.X, 2 * pm.Y - p1.Y ), Point( p3.X + (pm.X - p1.X), p3.Y + (pm.Y - p1.Y) ) ) ) mpmp2p4 = Method([self.midpoint, self.point2, self.point4], [self.point1, self.point3], lambda pm, p2, p4: ( Point( 2 * pm.X - p2.X, 2 * pm.Y - p2.Y ), Point( p4.X + (pm.X - p2.X), p4.Y + (pm.Y - p2.Y) ) ) ) constraint = Constraint( lambda p1, p2, p3, p4, pm: pm.is_midpoint(p1, p2) and p1.distance(pm) == p3.distance(p4), Strength.STRONG, [self.point1, self.point2, self.point3, self.point4, self.midpoint], [mpmp3p4, mp1pmp3, mpmp2p4]) self.constraint_system.add_constraint(constraint) self.point3.stay() self.point4.stay() def test_contraint(self): pm = self.midpoint.get_value() p1 = self.point1.get_value() p2 = self.point2.get_value() p3 = self.point3.get_value() p4 = self.point4.get_value() self.assertTrue(pm.is_midpoint(p1,p2)) self.assertTrue(p1.distance(pm) == p3.distance(p4)) def test_change_point1(self): pm_old = self.midpoint.get_value() self.point1.set_value(Point(0, 0)) self.test_contraint() self.assertTrue(self.point1.get_value().equals(Point(0,0))) self.assertTrue(self.midpoint.get_value().equals(pm_old)) def test_change_midpoint(self): self.midpoint.set_value(Point(0, 0)) midpoint = self.midpoint.get_value() self.test_contraint() self.assertTrue(self.midpoint.get_value(), Point(0,0)) def test_change_point3(self): pm_old = self.midpoint.get_value() self.point3.set_value(Point(0, 0)) self.test_contraint() self.assertTrue(self.point3.get_value().equals(Point(0,0))) self.assertTrue(self.midpoint.get_value().equals(pm_old))