from test_pil_builder_utils import TestParser, Expr
def test_pil_builder_list_comp():
with TestParser():
@TestParser.test
def listcomp_simple():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)]]
@TestParser.test
def listcomp_one_if():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)] if Expr.true(var_x)]
@TestParser.test
def listcomp_two_ifs():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)]
if Expr.true(var_x) if var_x != 2]
@TestParser.test
def listcomp_if_boolop():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)]
if Expr.true(0) and Expr.true(var_x)]
@TestParser.test
def listcomp_if_ifexp():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if (Expr.true(var_x) if Expr.true(0) else Expr.false(var_x))]
@TestParser.test
def listcomp_target_tuple():
var_l = [Expr.str(var_a) for var_a, var_b in [(Expr.int(0), Expr.int(1)), (Expr.int(2), Expr.int(3))]]
@TestParser.test
def listcomp_target_list():
var_l = [Expr.str(var_a) for [var_a, var_b] in [[Expr.int(0), Expr.int(1)], [Expr.int(2), Expr.int(3)]]]
@TestParser.test
def listcomp_target_attr():
var_obj = Expr(0)
var_obj.val = None
var_l = [Expr.str(var_obj.val) for var_obj.val in [Expr.int(0), Expr.int(1)]]
@TestParser.test
def listcomp_target_subscript():
var_arr = Expr(0)
var_arr[0] = None
var_l = [Expr.str(var_arr[0]) for var_arr[0] in [Expr.int(0), Expr.int(1)]]
@TestParser.test
def listcomp_two_fors():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(2), Expr.int(3)]]
@TestParser.test
def listcomp_two_fors_with_if():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)] if Expr.true(var_x)
for var_y in [Expr.int(2), Expr.int(3)] if Expr.true(var_y)]
@TestParser.test
def listcomp_three_fors():
var_l = [Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(0), Expr.int(1)]
for var_z in [Expr.int(0), Expr.int(1)]]
def test_pil_builder_set_comp():
with TestParser():
@TestParser.test
def setcomp_simple():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)]}
@TestParser.test
def setcomp_one_if():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)] if Expr.true(var_x)}
@TestParser.test
def setcomp_if_boolop():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if Expr.true(0) and Expr.true(var_x)}
@TestParser.test
def setcomp_if_ifexp():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if (Expr.true(var_x) if Expr.true(0) else Expr.false(var_x))}
@TestParser.test
def setcomp_target_tuple():
var_s = {Expr.str(var_a) for var_a, var_b in [(Expr.int(0), Expr.int(1)), (Expr.int(2), Expr.int(3))]}
@TestParser.test
def setcomp_target_list():
var_s = {Expr.str(var_a) for [var_a, var_b] in [[Expr.int(0), Expr.int(1)], [Expr.int(2), Expr.int(3)]]}
@TestParser.test
def setcomp_target_attr():
var_obj = Expr(0)
var_obj.val = None
var_s = {Expr.str(var_obj.val) for var_obj.val in [Expr.int(0), Expr.int(1)]}
@TestParser.test
def setcomp_target_subscript():
var_arr = Expr(0)
var_arr[0] = None
var_s = {Expr.str(var_arr[0]) for var_arr[0] in [Expr.int(0), Expr.int(1)]}
@TestParser.test
def setcomp_two_fors():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(2), Expr.int(3)]}
@TestParser.test
def setcomp_three_fors():
var_s = {Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(0), Expr.int(1)]
for var_z in [Expr.int(0), Expr.int(1)]}
def test_pil_builder_dict_comp():
with TestParser():
@TestParser.test
def dictcomp_simple():
var_d = {Expr.int(var_x): Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]}
@TestParser.test
def dictcomp_one_if():
var_d = {Expr.int(var_x): Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)] if Expr.true(var_x)}
@TestParser.test
def dictcomp_if_boolop():
var_d = {Expr.int(var_x): Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if Expr.true(0) and Expr.true(var_x)}
@TestParser.test
def dictcomp_if_ifexp():
var_d = {Expr.int(var_x): Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if (Expr.true(var_x) if Expr.true(0) else Expr.false(var_x))}
@TestParser.test
def dictcomp_target_tuple():
var_d = {Expr.int(var_a): Expr.str(var_b)
for var_a, var_b in [(Expr.int(0), Expr.int(1)), (Expr.int(2), Expr.int(3))]}
@TestParser.test
def dictcomp_target_list():
var_d = {Expr.int(var_a): Expr.str(var_b)
for [var_a, var_b] in [[Expr.int(0), Expr.int(1)], [Expr.int(2), Expr.int(3)]]}
@TestParser.test
def dictcomp_target_attr():
var_obj = Expr(0)
var_obj.val = None
var_d = {Expr.int(var_obj.val): Expr.str(var_obj.val) for var_obj.val in [Expr.int(0), Expr.int(1)]}
@TestParser.test
def dictcomp_target_subscript():
var_arr = Expr(0)
var_arr[0] = None
var_d = {Expr.int(var_arr[0]): Expr.str(var_arr[0]) for var_arr[0] in [Expr.int(0), Expr.int(1)]}
@TestParser.test
def dictcomp_two_fors():
var_d = {Expr.int(var_x): Expr.str(var_y)
for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(2), Expr.int(3)]}
@TestParser.test
def dictcomp_three_fors():
var_d = {Expr.int(var_x): Expr.str(var_z)
for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(0), Expr.int(1)]
for var_z in [Expr.int(0), Expr.int(1)]}
def test_pil_builder_generator_exp():
with TestParser():
@TestParser.test
def genexp_simple():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)])
var_l = list(g)
@TestParser.test
def genexp_one_if():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)] if Expr.true(var_x))
var_l = list(g)
@TestParser.test
def genexp_if_boolop():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if Expr.true(0) and Expr.true(var_x))
var_l = list(g)
@TestParser.test
def genexp_if_ifexp():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
if (Expr.true(var_x) if Expr.true(0) else Expr.false(var_x)))
var_l = list(g)
@TestParser.test
def genexp_target_tuple():
g = (Expr.str(var_a) for var_a, var_b in [(Expr.int(0), Expr.int(1)), (Expr.int(2), Expr.int(3))])
var_l = list(g)
@TestParser.test
def genexp_target_list():
g = (Expr.str(var_a) for [var_a, var_b] in [[Expr.int(0), Expr.int(1)], [Expr.int(2), Expr.int(3)]])
var_l = list(g)
@TestParser.test
def genexp_target_attr():
var_obj = Expr(0)
var_obj.val = None
g = (Expr.str(var_obj.val) for var_obj.val in [Expr.int(0), Expr.int(1)])
var_l = list(g)
@TestParser.test
def genexp_target_subscript():
var_arr = Expr(0)
var_arr[0] = None
g = (Expr.str(var_arr[0]) for var_arr[0] in [Expr.int(0), Expr.int(1)])
var_l = list(g)
@TestParser.test
def genexp_two_fors():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(2), Expr.int(3)])
var_l = list(g)
@TestParser.test
def genexp_three_fors():
g = (Expr.str(var_x) for var_x in [Expr.int(0), Expr.int(1)]
for var_y in [Expr.int(0), Expr.int(1)]
for var_z in [Expr.int(0), Expr.int(1)])
var_l = list(g)
def test_pil_builder_yield():
with TestParser():
@TestParser.test
def yield_bare():
def gen():
yield
var_l = list(gen())
@TestParser.test
def yield_name():
def gen():
var_x = Expr.int(0)
yield var_x
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_call_expr():
def gen():
yield Expr.int(0)
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_constant():
def gen():
yield 42
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_binop():
def gen():
yield Expr.int(0) + Expr.int(1)
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_attr():
def gen():
var_obj = Expr(0)
var_obj.val = Expr.int(99)
yield var_obj.val
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_subscript():
def gen():
var_arr = [Expr.int(0), Expr.int(1)]
yield var_arr[0]
var_l = list(gen())
Expr.str(var_l[0])
@TestParser.test
def yield_multiple():
def gen():
yield Expr.int(0)
yield Expr.int(1)
yield Expr.int(2)
var_l = list(gen())
Expr.str(var_l[0])
Expr.str(var_l[1])
Expr.str(var_l[2])
@TestParser.test
def yield_in_if():
def gen(flag):
if flag:
yield Expr.int(0)
else:
yield Expr.int(1)
var_l0 = list(gen(Expr.true(0)))
Expr.str(var_l0[0])
var_l1 = list(gen(Expr.false(1)))
Expr.str(var_l1[0])
@TestParser.test
def yield_in_for():
def gen():
for var_x in [Expr.int(0), Expr.int(1), Expr.int(2)]:
yield var_x
var_l = list(gen())
Expr.str(var_l[0])
Expr.str(var_l[1])
Expr.str(var_l[2])
@TestParser.test
def yield_from_name():
def inner():
yield Expr.int(0)
yield Expr.int(1)
def gen():
var_g = inner()
yield from var_g
var_l = list(gen())
Expr.str(var_l[0])
Expr.str(var_l[1])
@TestParser.test
def yield_from_call():
def inner():
yield Expr.int(0)
yield Expr.int(1)
def gen():
yield from inner()
var_l = list(gen())
Expr.str(var_l[0])
Expr.str(var_l[1])
@TestParser.test
def yield_send_value():
def gen():
var_sent = yield Expr.int(0)
Expr.str(var_sent)
g = gen()
next(g)
try:
g.send(Expr.int(1))
except StopIteration:
pass