diff options
| author | Sean Dague <sdague@linux.vnet.ibm.com> | 2012-03-22 17:32:31 -0400 |
|---|---|---|
| committer | Sean Dague <sdague@linux.vnet.ibm.com> | 2012-03-22 17:32:31 -0400 |
| commit | 34343acba97f41535b2e75866f99d58e44da088d (patch) | |
| tree | 72bbec25eff7b96cce3092893041f3ed574d3e30 | |
| parent | a43bfa116dfaefe199c9b4f02344a4b4176baf01 (diff) | |
| download | oslo-34343acba97f41535b2e75866f99d58e44da088d.tar.gz oslo-34343acba97f41535b2e75866f99d58e44da088d.tar.xz oslo-34343acba97f41535b2e75866f99d58e44da088d.zip | |
Make 'yes' also a true boolean
Adds yes, Yes, and all other capitalizations as a true boolean.
Updated unit tests to test for these.
Change-Id: I97915f870dd6bba612f74f0ceb78f2399fd81a93
| -rw-r--r-- | openstack/common/utils.py | 4 | ||||
| -rw-r--r-- | tests/unit/test_utils.py | 13 |
2 files changed, 13 insertions, 4 deletions
diff --git a/openstack/common/utils.py b/openstack/common/utils.py index 8cc400a..0191036 100644 --- a/openstack/common/utils.py +++ b/openstack/common/utils.py @@ -55,7 +55,7 @@ def bool_from_string(subject): Interpret a string as a boolean. Any string value in: - ('True', 'true', 'On', 'on', '1') + ('True', 'true', 'On', 'on', 'Yes', 'yes', '1') is interpreted as a boolean True. Useful for JSON-decoded stuff and config file parsing @@ -63,7 +63,7 @@ def bool_from_string(subject): if isinstance(subject, bool): return subject if isinstance(subject, basestring): - if subject.strip().lower() in ('true', 'on', '1'): + if subject.strip().lower() in ('true', 'on', 'yes', '1'): return True return False diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 1d464bb..bf6edba 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -36,13 +36,17 @@ class UtilsTest(unittest.TestCase): self.assertTrue(utils.bool_from_string('true')) self.assertTrue(utils.bool_from_string('TRUE')) self.assertTrue(utils.bool_from_string('on')) - self.assertTrue(utils.bool_from_string('on')) + self.assertTrue(utils.bool_from_string('On')) + self.assertTrue(utils.bool_from_string('yes')) + self.assertTrue(utils.bool_from_string('YES')) + self.assertTrue(utils.bool_from_string('yEs')) self.assertTrue(utils.bool_from_string('1')) self.assertFalse(utils.bool_from_string('false')) self.assertFalse(utils.bool_from_string('FALSE')) self.assertFalse(utils.bool_from_string('off')) self.assertFalse(utils.bool_from_string('OFF')) + self.assertFalse(utils.bool_from_string('no')) self.assertFalse(utils.bool_from_string('0')) self.assertFalse(utils.bool_from_string('42')) self.assertFalse(utils.bool_from_string('This should not be True')) @@ -51,13 +55,18 @@ class UtilsTest(unittest.TestCase): self.assertTrue(utils.bool_from_string(u'true')) self.assertTrue(utils.bool_from_string(u'TRUE')) self.assertTrue(utils.bool_from_string(u'on')) - self.assertTrue(utils.bool_from_string(u'on')) + self.assertTrue(utils.bool_from_string(u'On')) + self.assertTrue(utils.bool_from_string(u'yes')) + self.assertTrue(utils.bool_from_string(u'YES')) + self.assertTrue(utils.bool_from_string(u'yEs')) self.assertTrue(utils.bool_from_string(u'1')) self.assertFalse(utils.bool_from_string(u'false')) self.assertFalse(utils.bool_from_string(u'FALSE')) self.assertFalse(utils.bool_from_string(u'off')) self.assertFalse(utils.bool_from_string(u'OFF')) + self.assertFalse(utils.bool_from_string(u'no')) + self.assertFalse(utils.bool_from_string(u'NO')) self.assertFalse(utils.bool_from_string(u'0')) self.assertFalse(utils.bool_from_string(u'42')) self.assertFalse(utils.bool_from_string(u'This should not be True')) |
