aboutsummaryrefslogtreecommitdiff
path: root/catapult/devil/devil/utils/geometry_test.py
blob: af694429306a8ac9306928e2567a06827c21be43 (plain)
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
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for the geometry module."""

import unittest

from devil.utils import geometry as g


class PointTest(unittest.TestCase):

  def testStr(self):
    p = g.Point(1, 2)
    self.assertEquals(str(p), '(1, 2)')

  def testAdd(self):
    p = g.Point(1, 2)
    q = g.Point(3, 4)
    r = g.Point(4, 6)
    self.assertEquals(p + q, r)

  def testAdd_TypeErrorWithInvalidOperands(self):
    # pylint: disable=pointless-statement
    p = g.Point(1, 2)
    with self.assertRaises(TypeError):
      p + 4  # Can't add point and scalar.
    with self.assertRaises(TypeError):
      4 + p  # Can't add scalar and point.

  def testMult(self):
    p = g.Point(1, 2)
    r = g.Point(2, 4)
    self.assertEquals(2 * p, r)  # Multiply by scalar on the left.

  def testMult_TypeErrorWithInvalidOperands(self):
    # pylint: disable=pointless-statement
    p = g.Point(1, 2)
    q = g.Point(2, 4)
    with self.assertRaises(TypeError):
      p * q  # Can't multiply points.
    with self.assertRaises(TypeError):
      p * 4  # Can't multiply by a scalar on the right.


class RectangleTest(unittest.TestCase):

  def testStr(self):
    r = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
    self.assertEquals(str(r), '[(0, 1), (2, 3)]')

  def testCenter(self):
    r = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
    c = g.Point(1, 2)
    self.assertEquals(r.center, c)

  def testFromJson(self):
    r1 = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
    r2 = g.Rectangle.FromDict({'top': 1, 'left': 0, 'bottom': 3, 'right': 2})
    self.assertEquals(r1, r2)