Author: chrisz Date: Mon Oct 20 04:18:47 2008 New Revision: 5549 URL: http://trac.turbogears.org/changeset/5549
Log: Added simple unit tests for two util functions. Modified: branches/1.0/turbogears/tests/test_util.py branches/1.0/turbogears/util.py branches/1.1/turbogears/tests/test_util.py branches/1.1/turbogears/util.py branches/1.5/turbogears/tests/test_util.py branches/1.5/turbogears/util.py Modified: branches/1.0/turbogears/tests/test_util.py ============================================================================== --- branches/1.0/turbogears/tests/test_util.py (original) +++ branches/1.0/turbogears/tests/test_util.py Mon Oct 20 04:18:47 2008 @@ -172,6 +172,35 @@ assert not util.mime_type_has_charset('application/json') +def test_find_precision(): + assert util.find_precision(0) == 0 + assert util.find_precision(42) == 0 + assert util.find_precision(0.1) == 1 + assert util.find_precision(1234567.8) == 1 + assert util.find_precision(34.25) == 2 + assert util.find_precision(1234567.123) == 3 + assert util.find_precision(123.1234567) == 7 + + +def test_copy_if_mutable(): + test_values = ((None, False), + ('foo', False), (42, False), ((1, 2), False), + ([1, 2], True), ({1: 2}, True)) + for value, mutable in test_values: + if mutable: + assert util.copy_if_mutable(value) == value + assert util.copy_if_mutable(value) is not value + ret = util.copy_if_mutable(value, True) + assert ret[0] == value + assert ret[0] is not value + assert ret[1] is True + else: + assert util.copy_if_mutable(value) is value + ret = util.copy_if_mutable(value, True) + assert ret[0] is value + assert ret[1] is False + + def test_fixentities(): assert util.fixentities('Chip & Chap') \ == 'Chip & Chap' Modified: branches/1.0/turbogears/util.py ============================================================================== --- branches/1.0/turbogears/util.py (original) +++ branches/1.0/turbogears/util.py Mon Oct 20 04:18:47 2008 @@ -635,6 +635,12 @@ def copy_if_mutable(value, feedback=False): + """Make a copy of the value if it is mutable. + + Returns the value. If feedback is set to true, also returns + whether value was mutable as the second element of a tuple. + + """ if isinstance(value, dict): mutable = True value = value.copy() @@ -644,7 +650,7 @@ else: mutable = False if feedback: - return (value, mutable) + return value, mutable else: return value Modified: branches/1.1/turbogears/tests/test_util.py ============================================================================== --- branches/1.1/turbogears/tests/test_util.py (original) +++ branches/1.1/turbogears/tests/test_util.py Mon Oct 20 04:18:47 2008 @@ -181,6 +181,35 @@ assert not util.mime_type_has_charset('application/json') +def test_find_precision(): + assert util.find_precision(0) == 0 + assert util.find_precision(42) == 0 + assert util.find_precision(0.1) == 1 + assert util.find_precision(1234567.8) == 1 + assert util.find_precision(34.25) == 2 + assert util.find_precision(1234567.123) == 3 + assert util.find_precision(123.1234567) == 7 + + +def test_copy_if_mutable(): + test_values = ((None, False), + ('foo', False), (42, False), ((1, 2), False), + ([1, 2], True), ({1: 2}, True)) + for value, mutable in test_values: + if mutable: + assert util.copy_if_mutable(value) == value + assert util.copy_if_mutable(value) is not value + ret = util.copy_if_mutable(value, True) + assert ret[0] == value + assert ret[0] is not value + assert ret[1] is True + else: + assert util.copy_if_mutable(value) is value + ret = util.copy_if_mutable(value, True) + assert ret[0] is value + assert ret[1] is False + + def test_fixentities(): assert util.fixentities('Chip & Chap') \ == 'Chip & Chap' Modified: branches/1.1/turbogears/util.py ============================================================================== --- branches/1.1/turbogears/util.py (original) +++ branches/1.1/turbogears/util.py Mon Oct 20 04:18:47 2008 @@ -643,6 +643,12 @@ def copy_if_mutable(value, feedback=False): + """Make a copy of the value if it is mutable. + + Returns the value. If feedback is set to true, also returns + whether value was mutable as the second element of a tuple. + + """ if isinstance(value, dict): mutable = True value = value.copy() @@ -652,7 +658,7 @@ else: mutable = False if feedback: - return (value, mutable) + return value, mutable else: return value Modified: branches/1.5/turbogears/tests/test_util.py ============================================================================== --- branches/1.5/turbogears/tests/test_util.py (original) +++ branches/1.5/turbogears/tests/test_util.py Mon Oct 20 04:18:47 2008 @@ -207,6 +207,35 @@ assert not util.mime_type_has_charset('application/json') +def test_find_precision(): + assert util.find_precision(0) == 0 + assert util.find_precision(42) == 0 + assert util.find_precision(0.1) == 1 + assert util.find_precision(1234567.8) == 1 + assert util.find_precision(34.25) == 2 + assert util.find_precision(1234567.123) == 3 + assert util.find_precision(123.1234567) == 7 + + +def test_copy_if_mutable(): + test_values = ((None, False), + ('foo', False), (42, False), ((1, 2), False), + ([1, 2], True), ({1: 2}, True)) + for value, mutable in test_values: + if mutable: + assert util.copy_if_mutable(value) == value + assert util.copy_if_mutable(value) is not value + ret = util.copy_if_mutable(value, True) + assert ret[0] == value + assert ret[0] is not value + assert ret[1] is True + else: + assert util.copy_if_mutable(value) is value + ret = util.copy_if_mutable(value, True) + assert ret[0] is value + assert ret[1] is False + + def test_fixentities(): assert util.fixentities('Chip & Chap') \ == 'Chip & Chap' Modified: branches/1.5/turbogears/util.py ============================================================================== --- branches/1.5/turbogears/util.py (original) +++ branches/1.5/turbogears/util.py Mon Oct 20 04:18:47 2008 @@ -640,6 +640,12 @@ def copy_if_mutable(value, feedback=False): + """Make a copy of the value if it is mutable. + + Returns the value. If feedback is set to true, also returns + whether value was mutable as the second element of a tuple. + + """ if isinstance(value, dict): mutable = True value = value.copy() @@ -649,7 +655,7 @@ else: mutable = False if feedback: - return (value, mutable) + return value, mutable else: return value