Skip to content

Commit

Permalink
Merge pull request #17765 from yoff/python/test-functional-behaviour
Browse files Browse the repository at this point in the history
Python: Add tests for functional-like programming
  • Loading branch information
yoff authored Feb 11, 2025
2 parents 7644012 + 9ed8fe5 commit 158430a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions python/ql/test/library-tests/dataflow/coverage/functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# All functions starting with "test_" should run and execute `print("OK")` exactly once.
# This can be checked by running validTest.py.

import sys
import os

sys.path.append(os.path.dirname(os.path.dirname((__file__))))
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
# ------------------------------------------------------------------------------

def read_sql(sql):
SINK(sql) # $ flow="SOURCE, l:+5 -> sql"

def process(func, arg):
func(arg)

process(func=read_sql, arg=SOURCE)


def read_sql_star(sql):
SINK(sql) # $ MISSING: flow="SOURCE, l:+5 -> sql"

def process_star(func, *args):
func(*args)

process_star(read_sql_star, SOURCE)


def read_sql_dict(sql):
SINK(sql) # $ flow="SOURCE, l:+5 -> sql"

def process_dict(func, **args):
func(**args)

process_dict(func=read_sql_dict, sql=SOURCE)


# TODO:
# Consider adding tests for
# threading.Thread, multiprocess.Process,
# concurrent.futures.ThreadPoolExecutor,
# and concurrent.futures.ProcessPoolExecutor.
1 change: 1 addition & 0 deletions python/ql/test/library-tests/dataflow/validTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def check_tests_valid_after_version(testFile, version):
check_tests_valid("coverage.datamodel")
check_tests_valid("coverage.test_builtins")
check_tests_valid("coverage.loops")
check_tests_valid("coverage.functional")
check_tests_valid("coverage-py2.classes")
check_tests_valid("coverage-py3.classes")
check_tests_valid("variable-capture.in")
Expand Down

0 comments on commit 158430a

Please sign in to comment.