-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathtest_verbosity_mgr.py
74 lines (56 loc) · 1.85 KB
/
test_verbosity_mgr.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
import unittest
from meep.verbosity_mgr import Verbosity
class VerbosityForTest(Verbosity):
"""Allows for testing of Verbosity without interfering with the singleton."""
_instance = None
class MyCvar:
def __init__(self):
self.verbosity = 1
class TestVerbosity(unittest.TestCase):
def setUp(self):
VerbosityForTest.reset()
self.v1 = VerbosityForTest(name="foo")
self.v2 = VerbosityForTest(MyCvar(), "bar")
def test_identity(self):
# Ensure each verbosity is really the same singleton instance
v1, v2 = self.v1, self.v2
self.assertTrue(v1 is v2)
self.assertEqual(id(v1), id(v2))
self.assertEqual(v1.get_all(), [1, 1])
def test_initial_value(self):
v1, v2 = self.v1, self.v2
self.assertEqual(v1.get(), 1)
v2.set(2)
self.assertEqual(v1.get(), 2)
def test_properties(self):
v1, v2 = self.v1, self.v2
self.assertEqual(v1.foo, 1)
self.assertEqual(v1.bar, 1)
v1.foo = 2
v2.bar = 3
self.assertEqual(v2.foo, 2)
self.assertEqual(v2.bar, 3)
def test_operators(self):
v1, v2 = self.v1, self.v2
self.assertTrue(v1 == 1)
self.assertFalse(v1 == 2)
self.assertFalse(v1 > 1)
self.assertTrue(v1 < 3)
self.assertFalse(v1 >= 2)
self.assertTrue(v1 <= 1)
v1(3)
self.assertFalse(v2 == 1)
self.assertFalse(v2 == 2)
self.assertTrue(v2 == 3)
def test_out_of_range(self):
v1, v2 = self.v1, self.v2
with self.assertRaises(ValueError):
v1.set(5)
with self.assertRaises(ValueError):
v1.set(-5)
with self.assertRaises(ValueError):
v2.foo = 5
with self.assertRaises(ValueError):
v2.bar = -5
if __name__ == "__main__":
unittest.main()