• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Tests for the geometry module."""
6
7import unittest
8
9from devil.utils import geometry as g
10
11
12class PointTest(unittest.TestCase):
13
14  def testStr(self):
15    p = g.Point(1, 2)
16    self.assertEquals(str(p), '(1, 2)')
17
18  def testAdd(self):
19    p = g.Point(1, 2)
20    q = g.Point(3, 4)
21    r = g.Point(4, 6)
22    self.assertEquals(p + q, r)
23
24  def testAdd_TypeErrorWithInvalidOperands(self):
25    # pylint: disable=pointless-statement
26    p = g.Point(1, 2)
27    with self.assertRaises(TypeError):
28      p + 4  # Can't add point and scalar.
29    with self.assertRaises(TypeError):
30      4 + p  # Can't add scalar and point.
31
32  def testMult(self):
33    p = g.Point(1, 2)
34    r = g.Point(2, 4)
35    self.assertEquals(2 * p, r)  # Multiply by scalar on the left.
36
37  def testMult_TypeErrorWithInvalidOperands(self):
38    # pylint: disable=pointless-statement
39    p = g.Point(1, 2)
40    q = g.Point(2, 4)
41    with self.assertRaises(TypeError):
42      p * q  # Can't multiply points.
43    with self.assertRaises(TypeError):
44      p * 4  # Can't multiply by a scalar on the right.
45
46
47class RectangleTest(unittest.TestCase):
48
49  def testStr(self):
50    r = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
51    self.assertEquals(str(r), '[(0, 1), (2, 3)]')
52
53  def testCenter(self):
54    r = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
55    c = g.Point(1, 2)
56    self.assertEquals(r.center, c)
57
58  def testFromJson(self):
59    r1 = g.Rectangle(g.Point(0, 1), g.Point(2, 3))
60    r2 = g.Rectangle.FromDict({'top': 1, 'left': 0, 'bottom': 3, 'right': 2})
61    self.assertEquals(r1, r2)
62