コード例 #1
0
ファイル: variable_tests.py プロジェクト: knub/skypyblue
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)
コード例 #2
0
ファイル: variable_tests.py プロジェクト: timfel/skypyblue
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)
コード例 #3
0
    def test_equality_constraint(self):
        variable1 = Variable("variable1", 10, self.constraint_system)
        variable2 = Variable("variable2", 20, self.constraint_system)

        constraint = ConstraintFactory.equality_constraint(
            variable1, variable2, Strength.STRONG)

        self.constraint_system.add_constraint(constraint)

        self.assertTrue(variable1.get_value() == variable2.get_value())
コード例 #4
0
  def test_equality_constraint(self):
    variable1 = Variable("variable1", 10, self.constraint_system)
    variable2 = Variable("variable2", 20, self.constraint_system)

    constraint = ConstraintFactory.equality_constraint(
        variable1,
        variable2,
        Strength.STRONG
      )

    self.constraint_system.add_constraint(constraint)

    self.assertTrue(variable1.get_value()==variable2.get_value())
コード例 #5
0
    def test_scale_constraint(self):
        destination = Variable("destination", 1, self.constraint_system)
        source = Variable("source", 2, self.constraint_system)
        scale = Variable("scale", 3, self.constraint_system)
        offset = Variable("offset", 4, self.constraint_system)

        constraint = ConstraintFactory.scale_constraint(
            destination, source, scale, offset, Strength.STRONG)

        self.constraint_system.add_constraint(constraint)

        self.assertTrue(
            destination.get_value() == scale.get_value() * source.get_value() +
            offset.get_value())
        destination.set_value(100)
        self.assertTrue(
            destination.get_value() == scale.get_value() * source.get_value() +
            offset.get_value())
コード例 #6
0
  def test_scale_constraint(self):
    destination = Variable("destination", 1, self.constraint_system)
    source = Variable("source", 2, self.constraint_system)
    scale = Variable("scale", 3, self.constraint_system)
    offset = Variable("offset", 4, self.constraint_system)

    constraint = ConstraintFactory.scale_constraint(
        destination,
        source,
        scale,
        offset,
        Strength.STRONG
      )

    self.constraint_system.add_constraint(constraint)

    self.assertTrue(destination.get_value() == scale.get_value() * source.get_value() + offset.get_value())
    destination.set_value(100)
    self.assertTrue(destination.get_value() == scale.get_value() * source.get_value() + offset.get_value())
コード例 #7
0
class MidpointTest(unittest.TestCase):
    __name__ = "MidpointTest"

    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.midpoint = Variable("midpoint", Point(0, 0),
                                 self.constraint_system)

        mMp = Method([self.point1, self.point2], [self.midpoint],
                     lambda p1, p2: Point((p1.X + p2.X) / 2,
                                          (p1.Y + p2.Y) / 2))

        mP1 = Method([self.midpoint, self.point2], [self.point1],
                     lambda mp, p2: Point((2 * mp.X - p2.X),
                                          (2 * mp.Y - p2.Y)))

        mP2 = Method([self.midpoint, self.point1], [self.point2],
                     lambda mp, p1: Point((2 * mp.X - p1.X),
                                          (2 * mp.Y - p1.Y)))

        constraint = Constraint(
            lambda point1, point2, midpoint: midpoint.is_midpoint(
                point1, point2), Strength.STRONG,
            [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2])

        self.constraint_system.add_constraint(constraint)

    def test_contraint(self):
        midpoint = self.midpoint.get_value()
        point1 = self.point1.get_value()
        point2 = self.point2.get_value()
        self.assertTrue(midpoint.is_midpoint(point1, point2))

    def test_change_point1(self):
        self.point1.set_value(Point(0, 0))
        point1 = self.point1.get_value()
        self.test_contraint()
        self.assertEqual(point1.X, 0)
        self.assertEqual(point1.Y, 0)

    def test_change_midpoint(self):
        self.midpoint.set_value(Point(0, 0))
        midpoint = self.midpoint.get_value()
        self.test_contraint()
        self.assertEqual(midpoint.X, 0)
        self.assertEqual(midpoint.Y, 0)

    def test_change_several_points(self):
        self.midpoint.set_value(Point(0, 0))
        self.point1.set_value(Point(10, 10))
        self.point2.set_value(Point(50, 50))
        point2 = self.point2.get_value()
        self.test_contraint()
        self.assertEqual(point2.X, 50)
        self.assertEqual(point2.Y, 50)

    def test_change_two_points_at_the_same_time(self):
        self.constraint_system.change_variable_values(
            [self.point1, self.point2],
            [Point(0, 0), Point(20, 20)])
        point1 = self.point1.get_value()
        point2 = self.point2.get_value()
        self.test_contraint()
        self.assertEqual(point1.X, 0)
        self.assertEqual(point1.Y, 0)
        self.assertEqual(point2.X, 20)
        self.assertEqual(point2.Y, 20)
コード例 #8
0
class MidpointTest(unittest.TestCase):
  __name__ = "MidpointTest"
  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.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system)

    mMp = Method([self.point1, self.point2], [self.midpoint],
      lambda p1, p2: Point((p1.X + p2.X) / 2 , (p1.Y + p2.Y) / 2))

    mP1 = Method([self.midpoint, self.point2], [self.point1],
      lambda mp, p2: Point((2 * mp.X - p2.X) , (2 * mp.Y - p2.Y)))

    mP2 = Method([self.midpoint, self.point1], [self.point2],
      lambda mp, p1: Point((2 * mp.X - p1.X) , (2 * mp.Y - p1.Y)))


    constraint = Constraint(
        lambda point1, point2, midpoint: midpoint.is_midpoint(point1, point2),
        Strength.STRONG,
        [self.point1, self.point2, self.midpoint],
        [mMp, mP1, mP2])

    self.constraint_system.add_constraint(constraint)


  def test_contraint(self):
    midpoint = self.midpoint.get_value()
    point1 = self.point1.get_value()
    point2 = self.point2.get_value()
    self.assertTrue(midpoint.is_midpoint(point1,point2))

  def test_change_point1(self):
    self.point1.set_value(Point(0, 0))
    point1 = self.point1.get_value()
    self.test_contraint()
    self.assertEqual(point1.X, 0)
    self.assertEqual(point1.Y, 0)

  def test_change_midpoint(self):
    self.midpoint.set_value(Point(0, 0))
    midpoint = self.midpoint.get_value()
    self.test_contraint()
    self.assertEqual(midpoint.X, 0)
    self.assertEqual(midpoint.Y, 0)

  def test_change_several_points(self):
    self.midpoint.set_value(Point(0, 0))
    self.point1.set_value(Point(10, 10))
    self.point2.set_value(Point(50, 50))
    point2 = self.point2.get_value()
    self.test_contraint()
    self.assertEqual(point2.X, 50)
    self.assertEqual(point2.Y, 50)

  def test_change_two_points_at_the_same_time(self):
    self.constraint_system.change_variable_values(
        [self.point1, self.point2],
        [Point(0, 0), Point(20, 20)]
      )
    point1 = self.point1.get_value()
    point2 = self.point2.get_value()
    self.test_contraint()
    self.assertEqual(point1.X, 0)
    self.assertEqual(point1.Y, 0)
    self.assertEqual(point2.X, 20)
    self.assertEqual(point2.Y, 20)
コード例 #9
0
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))