Skip to content

Commit 158430a

Browse files
authored
Merge pull request github#17765 from yoff/python/test-functional-behaviour
Python: Add tests for functional-like programming
2 parents 7644012 + 9ed8fe5 commit 158430a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
2+
# This can be checked by running validTest.py.
3+
4+
import sys
5+
import os
6+
7+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
8+
from testlib import expects
9+
10+
# These are defined so that we can evaluate the test code.
11+
NONSOURCE = "not a source"
12+
SOURCE = "source"
13+
14+
15+
def is_source(x):
16+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
17+
18+
19+
def SINK(x):
20+
if is_source(x):
21+
print("OK")
22+
else:
23+
print("Unexpected flow", x)
24+
25+
26+
def SINK_F(x):
27+
if is_source(x):
28+
print("Unexpected flow", x)
29+
else:
30+
print("OK")
31+
32+
# ------------------------------------------------------------------------------
33+
# Actual tests
34+
# ------------------------------------------------------------------------------
35+
36+
def read_sql(sql):
37+
SINK(sql) # $ flow="SOURCE, l:+5 -> sql"
38+
39+
def process(func, arg):
40+
func(arg)
41+
42+
process(func=read_sql, arg=SOURCE)
43+
44+
45+
def read_sql_star(sql):
46+
SINK(sql) # $ MISSING: flow="SOURCE, l:+5 -> sql"
47+
48+
def process_star(func, *args):
49+
func(*args)
50+
51+
process_star(read_sql_star, SOURCE)
52+
53+
54+
def read_sql_dict(sql):
55+
SINK(sql) # $ flow="SOURCE, l:+5 -> sql"
56+
57+
def process_dict(func, **args):
58+
func(**args)
59+
60+
process_dict(func=read_sql_dict, sql=SOURCE)
61+
62+
63+
# TODO:
64+
# Consider adding tests for
65+
# threading.Thread, multiprocess.Process,
66+
# concurrent.futures.ThreadPoolExecutor,
67+
# and concurrent.futures.ProcessPoolExecutor.

python/ql/test/library-tests/dataflow/validTest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def check_tests_valid_after_version(testFile, version):
6666
check_tests_valid("coverage.datamodel")
6767
check_tests_valid("coverage.test_builtins")
6868
check_tests_valid("coverage.loops")
69+
check_tests_valid("coverage.functional")
6970
check_tests_valid("coverage-py2.classes")
7071
check_tests_valid("coverage-py3.classes")
7172
check_tests_valid("variable-capture.in")

0 commit comments

Comments
 (0)