def test_recursive_formatter_vformat_with_special_tuple(): """Multiple special types call get_value and supersedes string.""" special = Mock() special.get_value = Mock(return_value=123) my_string = MyString('blah') d = { 'one': 1, 'two': special, 'three': '{two}', 'arbkey': 456, 'my_string': my_string } format_me = {'k1': '{three}', 'k2': '{my_string}'} formatter = RecursiveFormatter(special_types=(Mock, MyString)) assert formatter.vformat(format_me, None, d) == { 'k1': 123, 'k2': '123 456' } special.get_value.assert_called_once_with(d) assert my_string == 'blah'
def test_recursive_formatter_vformat_with_passthrough_tuple(): """Multiple passthrough types do not format.""" d = { 'one': 1, 'two': '{one}', 'three': { '{one}': '{two}' }, 'four': ['{one}', 'arb'] } formatter = RecursiveFormatter(passthrough_types=(dict, list)) assert formatter.vformat('{three}', None, d) == {'{one}': '{two}'} assert formatter.vformat(d['four'], None, d) == ['{one}', 'arb']
def test_recursive_formatter_iterate_list(): """Recurse through a list.""" special = Mock() special.get_value = Mock(return_value=123) my_string = MyString('blah') my_string_derived = MyStringDerived('blah derived') d = { 'one': 1, 'two': special, 'three': '{two}', 'arbkey': 456, 'my_string': my_string, 'my_string_derived': my_string_derived } repeating_item = Mock() repeating_item.get_value = Mock(return_value=999) passthrough_derived_obj = ValueError('arb') input_obj = [ repeating_item, 'literal string', 'string {one} expression', repeating_item, passthrough_derived_obj, special, '{my_string}', MyStringDerived, set([1, 2, 3, 4, '{arbkey}']), [5, 6, 7], { 'a': 'b', '{one}': MyStringDerived }, b'\x00\x10', 890, 1.13 ] formatter = RecursiveFormatter(passthrough_types=(Exception, MyStringDerived), special_types=(Mock, MyString)) out = formatter.vformat(input_obj, None, d) assert out == [ 999, 'literal string', 'string 1 expression', 999, passthrough_derived_obj, 123, '123 456', MyStringDerived, {1, 2, 3, 4, 456}, [5, 6, 7], { 'a': 'b', 1: MyStringDerived }, b'\x00\x10', 890, 1.13 ]