71 """Check all metavariable types for specified file.
74 filepath (str): path to file containing REGISTER_METAVARIABLE
77 AssertionError: Raised if no expected function definition is found.
78 AssertionError: Rased if lambda function has no associated
79 type information, or no lambda function is defined.
82 int: number of metavariables in file.
83 Used for sanity checks of the coverage.
87 with open(filepath)
as fp:
88 filecontent = fp.read()
94 registering_statements = list(
96 lambda line: line.replace(
" ",
"").replace(
"\n",
""),
97 registering_statements,
101 for statement
in registering_statements:
104 function_name = match_content.groupdict()[
"function_name"]
105 enumtype = match_content.groupdict()[
"enumtype"]
111 f
"Metavariable '{function_name}' in file {filepath}:\n"
112 f
"Metavariable function return type and Manager::VariableDataType have to match.\n"
113 f
"Expected: Manager::VariableDataType::{self.hardcoded[function_name]}, actual: Manager::VariableDataType::{enumtype}",
118 function_definition_regex = re.compile(
119 r"Manager::FunctionPtr %s\(.*\)[^\{]*" % function_name
122 regular_definition_regex = re.compile(
123 r"(?P<return_type>double|int|bool) %s\(.*?\)" % function_name
127 definition = function_definition_regex.search(filecontent)
128 if definition
is not None:
129 func_body_start = definition.end()
133 regular_definition_regex.search(filecontent)
137 if return_type
is not None:
141 f
"Metavariable '{function_name}' in file {filepath}:\n"
142 "Metavariable function return type and Manager::VariableDataType have to match."
143 f
"Return type is {return_type} but it is registered as Manager::VariableDataType::c_{enumtype}.\n",
147 raise AssertionError(
148 f
"Metavariable '{function_name}' in file {filepath}:\n"
149 "Metavariable function return type and Manager::VariableDataType have to match."
150 "Return type of function could not be automatically determined from the source code."
151 "You can add an exception by adding the expected return type information to "
152 "the 'hardcoded' dictionary of this testcase."
156 func_body_end = func_body_start + findMatchedParenthesis(
157 filecontent[func_body_start:],
"{",
"}"
160 func_body = filecontent[func_body_start:func_body_end]
164 if lambdatype_match
is not None:
165 lambdatype = lambdatype_match.groupdict()[
"lambdatype"]
169 f
"Metavariable '{function_name}' in file {filepath}:\n"
170 f
"Lambda function has return type {lambdatype} "
171 f
"but is registered with Manager::VariableDataType::c_{enumtype}.\n"
172 "VariableDataType and lambda return type have to match.",
175 raise AssertionError(
176 f
"Metavariable '{function_name}' in {filepath}:\n"
177 "VariableDataType and lambda definition have to match.\n"
178 "Either lambda function is missing return type information"
179 ", or lambda definition could not be found.\n"
180 "Please add return type annotation '(const Particle * particle) -> double/int/bool' to lambda.\n"
181 "Or add this metavariable as exception, by adding the expected return type information in the 'hardcoded' dictionary of this testcase\n"
186 return len(registering_statements)
189 """Metavariables have to be registered with the correct Manager::Variable::VariableDataType enum value. This test makes sure Metavariable definition and variable registration are correct."""
192 subprocess.run([
"grep",
"-V"], check=
True, capture_output=
True)
193 except subprocess.CalledProcessError:
194 logging.basicConfig(format=
"%(message)s")
196 "TEST SKIPPED: MetavariableDataTypeTest skipped because grep is not available."
201 analysis_module = basf2.find_file(
"analysis")
202 files = subprocess.run(
205 "REGISTER_METAVARIABLE",
214 files = files.stdout.decode().split(
"\n")
215 files = list(filter(
lambda file: file.endswith(
".cc"), files))
217 num_files = len(files)
218 print(f
"Number of files including meta-variables is {num_files}")
221 self.assertGreaterEqual(num_files, 15)
223 num_metavariables = 0
224 for filepath
in files:
228 print(f
"Number of meta-variables is {num_metavariables}")
229 self.assertGreaterEqual(num_metavariables, 238)