1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
from __future__ import generator_stop
from utils import check_on_input
TYPES = "keys", "items", "values"
DICT_ITER = (
"""\
x.iter{type}()
""",
"""\
from __future__ import absolute_import
import six
six.iter{type}(x)
""",
)
DICT_VIEW = (
"""\
x.view{type}()
""",
"""\
from __future__ import absolute_import
import six
six.view{type}(x)
""",
)
DICT_PLAIN = (
"""\
x.{type}()
""",
"""\
list(x.{type}())
""",
)
DICT_IN_LOOP = (
"""\
for k in x.items():
pass
""",
"""\
for k in x.items():
pass
""",
)
DICT_ITER_IN_LOOP = (
"""\
for k in x.iter{type}():
pass
""",
"""\
from __future__ import absolute_import
import six
for k in six.iter{type}(x):
pass
""",
)
DICT_ITER_IN_LIST = (
"""\
for k in list(x.iter{type}()):
pass
""",
"""\
from __future__ import absolute_import
import six
for k in list(six.iter{type}(x)):
pass
""",
)
CHAINED_CALLS = (
"""\
(x + y).foo().iter{type}().bar()
""",
"""\
from __future__ import absolute_import
import six
six.iter{type}((x + y).foo()).bar()
""",
)
def check_all_types(input, output):
for type_ in TYPES:
check_on_input(input.format(type=type_), output.format(type=type_))
def test_dict_iter():
check_all_types(*DICT_ITER)
def test_dict_view():
check_all_types(*DICT_VIEW)
def test_dict_plain():
check_all_types(*DICT_PLAIN)
def test_dict_in_loop():
check_on_input(*DICT_IN_LOOP)
def test_dict_iter_in_loop():
check_all_types(*DICT_ITER_IN_LOOP)
def test_dict_iter_in_list():
check_all_types(*DICT_ITER_IN_LIST)
def test_chained_calls():
check_all_types(*CHAINED_CALLS)
|