Skip to content

Commit cb69992

Browse files
committed
TST: add test for function coverage
1 parent c490c5d commit cb69992

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.22.0

tests/test_function_coverage.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import re
2+
import requests
3+
4+
FUNCTION_SIGNATURES_FILE = "https://raw.githubusercontent.com/stan-dev/stan/develop/src/stan/lang/function_signatures.h"
5+
SYNTAX_FILE = "syntax/stan.vim"
6+
STAN_ADD_FUNCTIONS = [
7+
"add",
8+
"add_unary",
9+
"add_unary_vectorized",
10+
"add_binary",
11+
"add_ternary",
12+
"add_nullary",
13+
]
14+
15+
# Request Stan file
16+
req = requests.get(FUNCTION_SIGNATURES_FILE)
17+
if req.status_code == requests.codes.ok:
18+
stan_file = req.text
19+
else:
20+
msg = "Could not request Stan file."
21+
raise RuntimeError(msg)
22+
23+
# Read syntax file
24+
with open(SYNTAX_FILE, "r") as f:
25+
syntax_file = f.read()
26+
27+
# Find all functions from Stan file
28+
add_functions = "|".join(STAN_ADD_FUNCTIONS)
29+
regex = r"(?<=[{}]\(['\"])(\w+)(?=['\"])".format(add_functions)
30+
stan_functions = set(re.findall(regex, stan_file))
31+
# Remove _log functions
32+
stan_functions = {
33+
function for function in stan_functions if not function.endswith("_log")
34+
}
35+
# Add bare distribution names
36+
stan_functions = stan_functions | {
37+
function[:-4] for function in stan_functions if function.endswith("_rng")
38+
}
39+
40+
regex1 = r"(?<=syntax keyword stanFunction )(.*)"
41+
regex2 = r"(?<=syntax keyword stanConstant )(.*)"
42+
matches = re.findall(regex1, syntax_file) + re.findall(regex2, syntax_file)
43+
44+
# Flatten
45+
highlighted_functions = set([word for match in matches for word in match.split()])
46+
47+
missing = stan_functions - highlighted_functions
48+
unnecessary = highlighted_functions - stan_functions
49+
50+
print("Missing:")
51+
print(missing)
52+
print()
53+
print("Unnecessary:")
54+
print(unnecessary)

0 commit comments

Comments
 (0)