Belle II Software  release-08-01-10
MetavariableDataTypeTest Class Reference
Inheritance diagram for MetavariableDataTypeTest:
Collaboration diagram for MetavariableDataTypeTest:

Public Member Functions

int process_file (self, str filepath)
 
def test_metavariable_data_types (self)
 

Static Public Attributes

dictionary hardcoded
 we have to hardcode some special cases as data types can't be obtained from source code directly More...
 
 registering_regex
 regex for finding the REGISTER_METAVARIABLE statements. More...
 
 extract_regex
 regex for extracting the function name and the enum type from REGISTER_METAVARIABLE statements # noqa More...
 
 lambda_type_regex = re.compile(r"-> (?P<lambdatype>double|bool|int)")
 regex for extracting the type of the lambda function in metavariable function definition # noqa
 

Detailed Description

Determine metavariable types from source code and assert correctness

Definition at line 41 of file test_variables_meta.py.

Member Function Documentation

◆ process_file()

int process_file (   self,
str  filepath 
)
Check all metavariable types for specified file.

Args:
    filepath (str): path to file containing REGISTER_METAVARIABLE

Raises:
    AssertionError: Raised if no expected function definition is found.
    AssertionError: Rased if lambda function has no associated
                    type information, or no lambda function is defined.

Returns:
    int: number of metavariables in file.
    Used for sanity checks of the coverage.

Definition at line 76 of file test_variables_meta.py.

76  def process_file(self, filepath: str) -> int:
77  """Check all metavariable types for specified file.
78 
79  Args:
80  filepath (str): path to file containing REGISTER_METAVARIABLE
81 
82  Raises:
83  AssertionError: Raised if no expected function definition is found.
84  AssertionError: Rased if lambda function has no associated
85  type information, or no lambda function is defined.
86 
87  Returns:
88  int: number of metavariables in file.
89  Used for sanity checks of the coverage.
90  """
91 
92  # Read file contents
93  with open(filepath) as fp:
94  filecontent = fp.read()
95 
96  # List for all found registering statements
97  registering_statements = self.registering_regex.findall(filecontent)
98  # Preprocess all statements by removing spaces and newlines
99  # This makes extraction of function name and enum type easier
100  registering_statements = list(
101  map(
102  lambda line: line.replace(" ", "").replace("\n", ""),
103  registering_statements,
104  ),
105  )
106 
107  for statement in registering_statements:
108  # Extract enum type and name
109  match_content = self.extract_regex.match(statement)
110  function_name = match_content.groupdict()["function_name"]
111  enumtype = match_content.groupdict()["enumtype"]
112 
113  if function_name in self.hardcoded:
114  self.assertEqual(
115  enumtype,
116  self.hardcoded[function_name],
117  f"Metavariable '{function_name}' in file {filepath}:\n"
118  f"Metavariable function return type and Manager::VariableDataType have to match.\n" # noqa
119  f"Expected: Manager::VariableDataType::{self.hardcoded[function_name]}, actual: Manager::VariableDataType::{enumtype}", # noqa
120  )
121  continue
122 
123  # compile regex for finding metavariable function definition
124  function_definition_regex = re.compile(
125  r"Manager::FunctionPtr %s\‍(.*\‍)[^\{]*" % function_name
126  )
127  # compile regex for regular typed function definition
128  regular_definition_regex = re.compile(
129  r"(?P<return_type>double|int|bool) %s\‍(.*?\‍)" % function_name
130  )
131 
132  # Find function body start with regex
133  definition = function_definition_regex.search(filecontent)
134  if definition is not None:
135  func_body_start = definition.end()
136  else: # Manager::FunctionPtr definition not found
137  # search for a regular typed function
138  return_type = (
139  regular_definition_regex.search(filecontent)
140  .groupdict()
141  .get("return_type")
142  ) # noqa
143  if return_type is not None:
144  self.assertEqual(
145  return_type,
146  enumtype,
147  f"Metavariable '{function_name}' in file {filepath}:\n"
148  "Metavariable function return type and Manager::VariableDataType have to match." # noqa
149  f"Return type is {return_type} but it is registered as Manager::VariableDataType::c_{enumtype}.\n", # noqa
150  )
151  continue
152  else:
153  raise AssertionError(
154  f"Metavariable '{function_name}' in file {filepath}:\n"
155  "Metavariable function return type and Manager::VariableDataType have to match." # noqa
156  "Return type of function could not be automatically determined from the source code." # noqa
157  "You can add an exception by adding the expected return type information to " # noqa
158  "the 'hardcoded' dictionary of this testcase."
159  )
160 
161  # grab function body end
162  func_body_end = func_body_start + findMatchedParenthesis(
163  filecontent[func_body_start:], "{", "}"
164  )
165  # Get function body slice from filecontent
166  func_body = filecontent[func_body_start:func_body_end]
167 
168  # grab lambda type definition
169  lambdatype_match = self.lambda_type_regex.search(func_body)
170  if lambdatype_match is not None:
171  lambdatype = lambdatype_match.groupdict()["lambdatype"]
172  self.assertEqual(
173  lambdatype,
174  enumtype,
175  f"Metavariable '{function_name}' in file {filepath}:\n"
176  f"Lambda function has return type {lambdatype} "
177  f"but is registered with Manager::VariableDataType::c_{enumtype}.\n" # noqa
178  "VariableDataType and lambda return type have to match.",
179  )
180  else: # lambda type or definition not found
181  raise AssertionError(
182  f"Metavariable '{function_name}' in {filepath}:\n"
183  "VariableDataType and lambda definition have to match.\n"
184  "Either lambda function is missing return type information"
185  ", or lambda definition could not be found.\n" # noqa
186  "Please add return type annotation '(const Particle * particle) -> double/int/bool' to lambda.\n" # noqa
187  "Or add this metavariable as exception, by adding the expected return type information in the 'hardcoded' dictionary of this testcase\n" # noqa
188  f"{func_body}"
189  )
190 
191  # Return the number of registering statements in this file
192  return len(registering_statements)
193 
unsigned long int findMatchedParenthesis(std::string str, char open='[', char close=']')
Returns position of the matched closing parenthesis if the first character in the given string contai...
Definition: CutHelpers.cc:33

◆ test_metavariable_data_types()

def test_metavariable_data_types (   self)
Metavariables have to be registered with the correct Manager::Variable::VariableDataType enum value. This test makes sure Metavariable definition and variable registration are correct.

Definition at line 194 of file test_variables_meta.py.

Member Data Documentation

◆ extract_regex

extract_regex
static
Initial value:
= re.compile(
r'REGISTER_METAVARIABLE\‍(".*?",(?P<function_name>[^,]*),.*?Manager::VariableDataType::c_(?P<enumtype>double|bool|int)' # noqa
)

regex for extracting the function name and the enum type from REGISTER_METAVARIABLE statements # noqa

Definition at line 70 of file test_variables_meta.py.

◆ hardcoded

dictionary hardcoded
static
Initial value:
= {
"softwareTriggerResult": "double",
"formula": "double",
"softwareTriggerResultNonPrescaled": "double",
"isDaughterOfList": "bool",
"isGrandDaughterOfList": "bool",
"daughterDiffOf": "double",
"daughterDiffOfPhi": "double",
"daughterDiffOfClusterPhi": "double",
"mcDaughterDiffOfPhi": "double",
"grandDaughterDiffOfPhi": "double",
"grandDaughterDiffOfClusterPhi": "double",
"daughterDiffOfPhiCMS": "double",
"daughterDiffOfClusterPhiCMS": "double",
}

we have to hardcode some special cases as data types can't be obtained from source code directly

Definition at line 48 of file test_variables_meta.py.

◆ registering_regex

registering_regex
static
Initial value:
= re.compile(
r"(?s)REGISTER_METAVARIABLE.*?Manager::VariableDataType::(?:c_double|c_int|c_bool)" # noqa
)

regex for finding the REGISTER_METAVARIABLE statements.

Definition at line 66 of file test_variables_meta.py.


The documentation for this class was generated from the following file: