|
| 1 | +import sys |
| 2 | +import os |
| 3 | + |
| 4 | +sys.path.append(os.path.dirname(os.path.dirname((__file__)))) # $ unresolved_call=sys.path.append(..) |
| 5 | +from testlib import expects |
| 6 | + |
| 7 | +# These are defined so that we can evaluate the test code. |
| 8 | +NONSOURCE = "not a source" |
| 9 | +SOURCE = "source" |
| 10 | + |
| 11 | + |
| 12 | +def is_source(x): |
| 13 | + return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j |
| 14 | + |
| 15 | + |
| 16 | +def SINK(x): |
| 17 | + if is_source(x): |
| 18 | + print("OK") |
| 19 | + else: |
| 20 | + print("Unexpected flow", x) |
| 21 | + |
| 22 | + |
| 23 | +def SINK_F(x): |
| 24 | + if is_source(x): |
| 25 | + print("Unexpected flow", x) |
| 26 | + else: |
| 27 | + print("OK") |
| 28 | + |
| 29 | + |
| 30 | +# ------------------------------------------------------------------------------ |
| 31 | +# Actual tests |
| 32 | +# ------------------------------------------------------------------------------ |
| 33 | + |
| 34 | +@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..) |
| 35 | +def test_dict_literal(): |
| 36 | + d = {"key": SOURCE} |
| 37 | + SINK(d["key"]) # $ flow="SOURCE, l:-1 -> d['key']" |
| 38 | + SINK(d.get("key")) # $ MISSING: flow |
| 39 | + SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow |
| 40 | + |
| 41 | + |
| 42 | +@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..) |
| 43 | +def test_dict_update(): |
| 44 | + d = {} |
| 45 | + d["key"] = SOURCE |
| 46 | + SINK(d["key"]) # $ MISSING: flow |
| 47 | + SINK(d.get("key")) # $ MISSING: flow |
| 48 | + SINK(d.setdefault("key", NONSOURCE)) # $ MISSING: flow |
| 49 | + |
| 50 | + |
| 51 | +@expects(2) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..) |
| 52 | +def test_dict_override(): |
| 53 | + d = {} |
| 54 | + d["key"] = SOURCE |
| 55 | + SINK(d["key"]) # $ MISSING: flow |
| 56 | + |
| 57 | + d["key"] = NONSOURCE |
| 58 | + SINK_F(d["key"]) |
| 59 | + |
| 60 | + |
| 61 | +def test_dict_setdefault(): |
| 62 | + d = {} |
| 63 | + d.setdefault("key", SOURCE) |
| 64 | + SINK(d["key"]) # $ MISSING: flow |
| 65 | + |
| 66 | + |
| 67 | +@expects(3) # $ unresolved_call=expects(..) unresolved_call=expects(..)(..) |
| 68 | +def test_dict_nonstring_key(): |
| 69 | + d = {} |
| 70 | + d[42] = SOURCE |
| 71 | + SINK(d[42]) # $ MISSING: flow |
| 72 | + SINK(d.get(42)) # $ MISSING: flow |
| 73 | + SINK(d.setdefault(42, NONSOURCE)) # $ MISSING: flow |
0 commit comments