-
Notifications
You must be signed in to change notification settings - Fork 78
/
test_configuration.py
83 lines (64 loc) · 2.75 KB
/
test_configuration.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
75
76
77
78
79
80
81
82
83
"""Unit-tests for `py2c.common.configuration.Configuration`
"""
from py2c.common.configuration import (
Configuration,
NoSuchOptionError, InvalidOptionError
)
from nose.tools import assert_is, assert_raises, assert_equal
from py2c.tests import Test, data_driven_test
class TestConfiguration(Test):
"""py2c.common.Configuration
"""
def setUp(self):
self.config = Configuration()
def test_does_not_set_unregistered_option(self):
with assert_raises(NoSuchOptionError):
self.config.set_option("unregistered_unset_option", "Won't set!")
def test_does_not_get_unset_and_unregistered_option(self):
with assert_raises(NoSuchOptionError):
self.config.get_option("unregistered_unset_option")
def test_does_get_unset_registered_option(self):
self.config.register_option("registered_unset_option")
val = self.config.get_option("registered_unset_option")
assert_is(val, None)
def test_does_set_and_get_a_registered_option(self):
obj = object()
self.config.register_option("registered_set_option")
self.config.set_option("registered_set_option", obj)
val = self.config.get_option("registered_set_option")
assert_is(val, obj)
@data_driven_test(described=True, prefix="registers valid option: ", data=[
("a simple name", "a_simple_name"),
("a dotted name", "a.dotted.name"),
])
def test_registers_valid_option(self, name):
try:
Configuration().register_option(name)
except Exception:
self.fail("Should have registered name: {}".format(name))
@data_driven_test(described=True, prefix="raises error when registering invalid option: ", data=[ # noqa
("a name starting with dot", ".invalid"),
("a dotted name starting with dot", ".invalid.name"),
("a name with spaces", "invalid name"),
("a non-string name", 1200),
])
def test_raises_error_registering_invalid_option(self, name):
try:
Configuration().register_option(name)
except Exception:
pass
else:
self.fail("Should not have registered name: {}".format(name))
def test_does_reset_options_to_default_value_correctly(self):
self.config.register_option("option_name", "Yo!")
# Sanity check
assert_equal(self.config.get_option("option_name"), "Yo!")
self.config.set_option("option_name", "Hi!")
# Sanity check
assert_equal(self.config.get_option("option_name"), "Hi!")
self.config.reset()
# Real purpose of this test
assert_equal(self.config.get_option("option_name"), "Yo!")
if __name__ == '__main__':
from py2c.tests import runmodule
runmodule(capture=False)