diff options
author | Rick Harris <rconradharris@gmail.com> | 2013-05-10 23:07:27 +0000 |
---|---|---|
committer | Rick Harris <rconradharris@gmail.com> | 2013-05-10 23:28:35 +0000 |
commit | d9b0719f02433b243a20fe705af4799c619f4e28 (patch) | |
tree | 8bff94d4cfcaad1d6bbfb649ef26df0cf0123580 /tests/unit/test_strutils.py | |
parent | 89d1f8b51d05f3282d11b3928443897e28459c7a (diff) | |
download | oslo-d9b0719f02433b243a20fe705af4799c619f4e28.tar.gz oslo-d9b0719f02433b243a20fe705af4799c619f4e28.tar.xz oslo-d9b0719f02433b243a20fe705af4799c619f4e28.zip |
Handle ints passed to `boolean_from_string`
The existing code would return True for '1' but False for int(1),
unlike Nova's version which would return True for both inputs.
Nova's version feels safer (i.e. it will do the most-likely-correct
thing if an int is passed in), so this patch changes
Openstack-Common's version to match.
Note that this patch does not make Openstack-Common's
`boolean_from_string` match Nova's `bool_from_string` exactly though.
Nova's version would return True for both '42' and 42, while
Openstack-Common's returns False. Though Nova's version, again, seems
safer, the presence of unit-tests in Openstack-Common asserting the
defined-correctness of this behavior makes me reluctant to change it.
Fixes bug 1178760
Change-Id: I8b2a31c4852884187bcbda3a4e78c8513b1bb8c8
Diffstat (limited to 'tests/unit/test_strutils.py')
-rw-r--r-- | tests/unit/test_strutils.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/tests/unit/test_strutils.py b/tests/unit/test_strutils.py index 1863c40..b0af958 100644 --- a/tests/unit/test_strutils.py +++ b/tests/unit/test_strutils.py @@ -48,6 +48,10 @@ class StrUtilsTest(utils.BaseTestCase): self.assertFalse(strutils.bool_from_string(c( 'This should not be True'))) + # Whitespace should be stripped + self.assertTrue(strutils.bool_from_string(c(' true '))) + self.assertFalse(strutils.bool_from_string(c(' false '))) + def test_bool_from_string(self): self._test_bool_from_string(lambda s: s) @@ -55,8 +59,16 @@ class StrUtilsTest(utils.BaseTestCase): self._test_bool_from_string(six.text_type) def test_other_bool_from_string(self): + self.assertFalse(strutils.bool_from_string(None)) self.assertFalse(strutils.bool_from_string(mock.Mock())) + def test_int_bool_from_string(self): + self.assertTrue(strutils.bool_from_string(1)) + + self.assertFalse(strutils.bool_from_string(-1)) + self.assertFalse(strutils.bool_from_string(0)) + self.assertFalse(strutils.bool_from_string(2)) + def test_int_from_bool_as_string(self): self.assertEqual(1, strutils.int_from_bool_as_string(True)) self.assertEqual(0, strutils.int_from_bool_as_string(False)) |