Python: Add test for dictionary flow

This commit is contained in:
Rasmus Wriedt Larsen
2022-11-15 20:21:09 +01:00
parent 4e49df1615
commit 6e31f64aaa
2 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname((__file__)))) # $ unresolved_call=sys.path.append(..)
from testlib import expects
# These are defined so that we can evaluate the test code.
NONSOURCE = "not a source"
SOURCE = "source"
def is_source(x):
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
def SINK(x):
if is_source(x):
print("OK")
else:
print("Unexpected flow", x)
def SINK_F(x):
if is_source(x):
print("Unexpected flow", x)
else:
print("OK")
# ------------------------------------------------------------------------------
# Actual tests
# ------------------------------------------------------------------------------
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
def test_dict_literal():
d = {"key": SOURCE}
SINK(d["key"]) # $ flow="SOURCE, l:-1 -> d['key']"
SINK(d.get("key")) # $ MISSING: flow
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
def test_dict_update():
d = {}
d["key"] = SOURCE
SINK(d["key"]) # $ MISSING: flow
SINK(d.get("key")) # $ MISSING: flow
SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow
@expects(2) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
def test_dict_override():
d = {}
d["key"] = SOURCE
SINK(d["key"]) # $ MISSING: flow
d["key"] = NONSOURCE
SINK_F(d["key"])
def test_dict_setdefault():
d = {}
d.setdefault("key", SOURCE)
SINK(d["key"]) # $ MISSING: flow
@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..)
def test_dict_nonstring_key():
d = {}
d[42] = SOURCE
SINK(d[42]) # $ MISSING: flow
SINK(d.get(42)) # $ MISSING: flow
SINK(d.setdefault(42, NONSOURCE)) # $ MISSING: flow

View File

@@ -72,6 +72,7 @@ if __name__ == "__main__":
check_tests_valid("variable-capture.collections")
check_tests_valid("module-initialization.multiphase")
check_tests_valid("fieldflow.test")
check_tests_valid("fieldflow.test_dict")
check_tests_valid_after_version("match.test", (3, 10))
check_tests_valid("exceptions.test")
check_tests_valid_after_version("exceptions.test_group", (3, 11))