More readable code around expression generation

FOO(BAR) is an expression, not a name.
Pack expression generation into a method.
No behavior change.
This commit is contained in:
Gilles Peskine 2019-11-21 16:46:51 +01:00
parent 2e6cbcd931
commit 5a994c15f4

View file

@ -145,6 +145,9 @@ class Inputs:
except BaseException as e: except BaseException as e:
raise Exception('distribute_arguments({})'.format(name)) from e raise Exception('distribute_arguments({})'.format(name)) from e
def generate_expressions(self, names):
return itertools.chain(*map(self.distribute_arguments, names))
_argument_split_re = re.compile(r' *, *') _argument_split_re = re.compile(r' *, *')
@classmethod @classmethod
def _argument_split(cls, arguments): def _argument_split(cls, arguments):
@ -252,8 +255,8 @@ def remove_file_if_exists(filename):
except OSError: except OSError:
pass pass
def run_c(options, type_word, names): def run_c(options, type_word, expressions):
"""Generate and run a program to print out numerical values for names.""" """Generate and run a program to print out numerical values for expressions."""
if type_word == 'status': if type_word == 'status':
cast_to = 'long' cast_to = 'long'
printf_format = '%ld' printf_format = '%ld'
@ -278,9 +281,9 @@ def run_c(options, type_word, names):
int main(void) int main(void)
{ {
''') ''')
for name in names: for expr in expressions:
c_file.write(' printf("{}\\n", ({}) {});\n' c_file.write(' printf("{}\\n", ({}) {});\n'
.format(printf_format, cast_to, name)) .format(printf_format, cast_to, expr))
c_file.write(''' return 0; c_file.write(''' return 0;
} }
''') ''')
@ -313,14 +316,14 @@ def do_test(options, inputs, type_word, names):
Use inputs to figure out what arguments to pass to macros that Use inputs to figure out what arguments to pass to macros that
take arguments. take arguments.
""" """
names = sorted(itertools.chain(*map(inputs.distribute_arguments, names))) expressions = sorted(inputs.generate_expressions(names))
values = run_c(options, type_word, names) values = run_c(options, type_word, expressions)
output = subprocess.check_output([options.program, type_word] + values) output = subprocess.check_output([options.program, type_word] + values)
outputs = output.decode('ascii').strip().split('\n') outputs = output.decode('ascii').strip().split('\n')
errors = [(type_word, name, value, output) errors = [(type_word, expr, value, output)
for (name, value, output) in zip(names, values, outputs) for (expr, value, output) in zip(expressions, values, outputs)
if normalize(name) != normalize(output)] if normalize(expr) != normalize(output)]
return len(names), errors return len(expressions), errors
def report_errors(errors): def report_errors(errors):
"""Describe each case where the output is not as expected.""" """Describe each case where the output is not as expected."""