Python: More tests of conditonals

Also use better formatter
(better because comments are close to what they comment)
This commit is contained in:
Rasmus Lerchedahl Petersen
2020-10-06 10:49:15 +02:00
parent 339c0721c5
commit 8f13d586b7
3 changed files with 362 additions and 364 deletions

View File

@@ -43,16 +43,14 @@ def test_tuple_negative():
y = x[0]
SINK_F(y)
# 6.2.1. Identifiers (Names)
def test_names():
x = SOURCE
SINK(x)
# 6.2.2. Literals
def test_string_literal():
x = "source"
SINK(x)
@@ -77,16 +75,14 @@ def test_imagnumber_literal():
x = 42j
SINK(x) # Flow missing
# 6.2.3. Parenthesized forms
def test_parenthesized_form():
x = (SOURCE)
x = SOURCE
SINK(x)
# 6.2.5. List displays
def test_list_display():
x = [SOURCE]
SINK(x[0])
@@ -114,12 +110,11 @@ def test_list_comprehension_inflow():
def test_nested_list_display():
x = [* [SOURCE]]
x = [*[SOURCE]]
SINK(x[0]) # Flow missing
# 6.2.6. Set displays
def test_set_display():
x = {SOURCE}
SINK(x.pop())
@@ -142,12 +137,11 @@ def test_set_comprehension_inflow():
def test_nested_set_display():
x = {* {SOURCE}}
x = {*{SOURCE}}
SINK(x.pop()) # Flow missing
# 6.2.7. Dictionary displays
def test_dict_display():
x = {"s": SOURCE}
SINK(x["s"])
@@ -169,17 +163,16 @@ def test_dict_comprehension_pop():
def test_nested_dict_display():
x = {** {"s": SOURCE}}
x = {**{"s": SOURCE}}
SINK(x["s"]) # Flow missing
def test_nested_dict_display_pop():
x = {** {"s": SOURCE}}
x = {**{"s": SOURCE}}
SINK(x.pop("s")) # Flow missing
# Nested comprehensions
def test_nested_comprehension():
x = [y for z in [[SOURCE]] for y in z]
SINK(x[0])
@@ -200,16 +193,14 @@ def test_nested_comprehension_paren():
x = [y for y in (z for z in [SOURCE])]
SINK(x[0])
# 6.2.8. Generator expressions
def test_generator():
x = (SOURCE for y in [NONSOURCE])
SINK([*x][0]) # Flow missing
# 6.2.9. Yield expressions
def gen(x):
yield x
@@ -227,16 +218,14 @@ def test_yield_from():
g = gen_from(SOURCE)
SINK(next(g)) # Flow missing
# a statement rather than an expression, but related to generators
def test_for():
for x in gen(SOURCE):
SINK(x) # Flow missing
# 6.2.9.1. Generator-iterator methods
def test___next__():
g = gen(SOURCE)
SINK(g.__next__()) # Flow missing
@@ -266,21 +255,20 @@ def test_throw():
n = next(g)
SINK(g.throw(TypeError)) # Flow missing
# no `test_close` as `close` involves no data flow
# 6.2.9.3. Asynchronous generator functions
async def agen(x):
yield x
# 6.2.9.4. Asynchronous generator-iterator methods
# helper to run async test functions
def runa(a):
import asyncio
asyncio.run(a)
@@ -325,9 +313,8 @@ async def atest_athrow():
def test_athrow():
runa(atest_athrow())
# 6.3.1. Attribute references
class C:
a = SOURCE
@@ -335,11 +322,10 @@ class C:
def test_attribute_reference():
SINK(C.a) # Flow missing
# overriding __getattr__ should be tested by the class coverage tests
# 6.3.2. Subscriptions
def test_subscription_tuple():
SINK((SOURCE,)[0])
@@ -351,6 +337,7 @@ def test_subscription_list():
def test_subscription_mapping():
SINK({"s": SOURCE}["s"])
# overriding __getitem__ should be tested by the class coverage tests
@@ -362,11 +349,10 @@ def test_slicing():
s = l[0:1:1]
SINK(s[0]) # Flow missing
# The grammar seems to allow `l[0:1:1, 0:1]`, but the interpreter does not like it
# 6.3.4. Calls
def second(a, b):
return b
@@ -406,79 +392,92 @@ def f_extra_keyword(a, **b):
def test_call_extra_keyword():
SINK(f_extra_keyword(NONSOURCE, b=SOURCE)) # Flow missing
# return the name of the first extra keyword argument
def f_extra_keyword_flow(**a):
return [*a][0]
# call the function with our source as the name of the keyword arguemnt
def test_call_extra_keyword_flow():
SINK(f_extra_keyword_flow(**{SOURCE: None})) # Flow missing
# 6.12. Assignment expressions
def test_assignment_expression():
x = NONSOURCE
SINK(x := SOURCE) # Flow missing
# 6.13. Conditional expressions
def test_conditional_true():
SINK(SOURCE if True else NONSOURCE) # Flow missing
def test_conditional_true_guards():
SINK_F(NONSOURCE if True else SOURCE)
def test_conditional_false():
SINK(NONSOURCE if False else SOURCE) # Flow missing
def test_conditional_false_guards():
SINK_F(SOURCE if False else NONSOURCE)
# Condition is evaluated first, so x is SOURCE once chosen
def test_conditional_evaluation_true():
x = NONSOURCE
SINK(x if (SOURCE == (x := SOURCE)) else NONSOURCE) # Flow missing
# Condition is evaluated first, so x is SOURCE once chosen
def test_conditional_evaluation_false():
x = NONSOURCE
SINK(NONSOURCE if (NONSOURCE == (x := SOURCE)) else x) # Flow missing
# 6.14. Lambdas
def test_lambda():
def f(x): return x
def f(x):
return x
SINK(f(SOURCE))
def test_lambda_positional():
def second(a, b): return b
def second(a, b):
return b
SINK(second(NONSOURCE, SOURCE))
def test_lambda_positional_negative():
def second(a, b): return b
def second(a, b):
return b
SINK_F(second(SOURCE, NONSOURCE))
def test_lambda_keyword():
def second(a, b): return b
def second(a, b):
return b
SINK(second(NONSOURCE, b=SOURCE)) # Flow missing
def test_lambda_unpack_iterable():
def second(a, b): return b
def second(a, b):
return b
SINK(second(NONSOURCE, *[SOURCE])) # Flow missing
def test_lambda_unpack_mapping():
def second(a, b): return b
def second(a, b):
return b
SINK(second(NONSOURCE, **{"b": SOURCE})) # Flow missing
@@ -491,9 +490,8 @@ def test_lambda_extra_keyword():
f_extra_keyword = lambda a, **b: b["b"]
SINK(f_extra_keyword(NONSOURCE, b=SOURCE)) # Flow missing
# call the function with our source as the name of the keyword arguemnt
def test_lambda_extra_keyword_flow():
# return the name of the first extra keyword argument
f_extra_keyword_flow = lambda **a: [*a][0]