Merge "If 'bool_from_string' provided a boolean just return it"

This commit is contained in:
Jenkins 2015-09-17 16:13:36 +00:00 committed by Gerrit Code Review
commit b644e2b27b
2 changed files with 8 additions and 0 deletions

View File

@ -119,6 +119,8 @@ def bool_from_string(subject, strict=False, default=False):
ValueError which is useful when parsing values passed in from an API call. ValueError which is useful when parsing values passed in from an API call.
Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'. Strings yielding False are 'f', 'false', 'off', 'n', 'no', or '0'.
""" """
if isinstance(subject, bool):
return subject
if not isinstance(subject, six.string_types): if not isinstance(subject, six.string_types):
subject = six.text_type(subject) subject = six.text_type(subject)

View File

@ -30,6 +30,12 @@ load_tests = testscenarios.load_tests_apply_scenarios
class StrUtilsTest(test_base.BaseTestCase): class StrUtilsTest(test_base.BaseTestCase):
@mock.patch("six.text_type")
def test_bool_bool_from_string_no_text(self, mock_text):
self.assertTrue(strutils.bool_from_string(True))
self.assertFalse(strutils.bool_from_string(False))
self.assertEqual(0, mock_text.call_count)
def test_bool_bool_from_string(self): def test_bool_bool_from_string(self):
self.assertTrue(strutils.bool_from_string(True)) self.assertTrue(strutils.bool_from_string(True))
self.assertFalse(strutils.bool_from_string(False)) self.assertFalse(strutils.bool_from_string(False))