def _verify(self, output: Sequence[str], errors: Sequence[str]) -> Optional[str]: error_message = "Expect:\n{}Encountered:\n{}\n" errors = tuple(map(self._reformat_pylox_error, errors)) if not compare_inner(errors, self._expected_errors): return error_message.format(indent(*self._expected_errors), indent(*errors)) if not compare_inner(output, self._expected_output): return error_message.format(indent(*self._expected_output), indent(*output)) return None
with suppress(LoxExit), redirect_stderr(err_capture), redirect_stdout( out_capture): lox_instance.run(source) out = tuple(line.strip() for line in out_capture.getvalue().splitlines()) err = tuple(line.strip() for line in err_capture.getvalue().splitlines()) if not (message := self._verify(out, err)): print(f"[{green('PASS')}]: {self.path}", file=out_buf) return True else: print(f"[{red('FAIL')}]: {self.path}", file=out_buf) print(indent(message), file=out_buf) return False def _compute_expected_output(self, source: str) -> None: # TODO: fix this string manipulation madness. # TODO: support "Error at "symbol" expectations. expect_runtime_error = list() for line_number, line in enumerate(source.splitlines(), start=1): if match := OUTPUT_EXPECT.search(line): self._expected_output.append(match.group(1)) if match := ERROR_EXPECT.search(line): self._expected_errors.append( f"[line {line_number}] LoxSyntaxError at {match.group(1)}") if match := ERROR_LINE_EXPECT.search(line): self._expected_errors.append( f"[line {match.group(2)}] LoxSyntaxError at {match.group(3)}"
def __str__(self) -> str: body_text = "".join(indent(str(stmt)) for stmt in self.instance_variables) return f"<{type(self).__name__.lower().replace('stmt', '')}: {self.name}\n{indent(body_text)}\n{self.uniq_id}>"
def __str__(self) -> str: inner_text = "".join(indent(str(attr)) for attr in vars(self).values()) return f"<if:\n{inner_text}>"
def __str__(self) -> str: inner_text = "".join(indent(str(stmt)) for stmt in self.body) return f"<{type(self).__name__.lower().replace('stmt', '')}:\n{inner_text}>"