1#!/usr/bin/env python
2
3import unittest
4from ephem import Angle, degrees, hours
5
6# Determine whether angles work reasonably.
7
8arcsecond_places = 5
9
10class AngleTests(unittest.TestCase):
11    def setUp(self):
12        self.d = degrees(1.5)
13        self.h = hours(1.6)
14
15    def test_Angle_constructor(self):
16        self.assertRaises(TypeError, Angle, 1.1)
17
18    def test_degrees_constructor(self):
19        self.assertAlmostEqual(self.d, degrees('85:56:37'),
20                               places=arcsecond_places)
21        self.assertAlmostEqual(self.d, degrees('85::3397'),
22                               places=arcsecond_places)
23        self.assertAlmostEqual(self.d, degrees('::309397'),
24                               places=arcsecond_places)
25
26        self.assertAlmostEqual(self.d, degrees('85 56 37'),
27                               places=arcsecond_places)
28        self.assertAlmostEqual(self.d, degrees('85 56:37'),
29                               places=arcsecond_places)
30        self.assertAlmostEqual(self.d, degrees('85:56 37'),
31                               places=arcsecond_places)
32        self.assertAlmostEqual(self.d, degrees('85 : 56 : 37'),
33                               places=arcsecond_places)
34        self.assertAlmostEqual(self.d, degrees(' 85 : 56 : 37 '),
35                               places=arcsecond_places)
36
37        self.assertAlmostEqual(self.d, degrees(' :  :   309397    '),
38                               places=arcsecond_places)
39
40    def test_degrees_constructor_refuses_alphabetics(self):
41        self.assertRaises(ValueError, degrees, 'foo:bar')
42        self.assertRaises(ValueError, degrees, '1:bar')
43        self.assertRaises(ValueError, degrees, '1:2:bar')
44        self.assertRaises(ValueError, degrees, '1:2:3bar')
45
46    def test_degrees_float_value(self):
47        self.assertAlmostEqual(self.d, 1.5)
48    def test_degrees_string_value(self):
49        self.assertEqual(str(self.d), '85:56:37.2')
50
51    def test_hours_constructor(self):
52        self.assertAlmostEqual(self.h, hours('6:06:41.6'),
53                               places=arcsecond_places)
54    def test_hours_float_value(self):
55        self.assertAlmostEqual(self.h, 1.6)
56    def test_hours_string_value(self):
57        self.assertEqual(str(self.h), '6:06:41.58')
58
59    def test_angle_addition(self):
60        self.assertAlmostEqual(degrees('30') + degrees('90'), degrees('120'))
61    def test_angle_subtraction(self):
62        self.assertAlmostEqual(degrees('180') - hours('9'), degrees('45'))
63