def test_case(test, tests, index=0, subindex=0, quiet=False, section=None): test, wanted_out, wanted = test.test, test.outs, test.result def fail(why): part, chapter, section = tests.part, tests.chapter, tests.section print("%sTest failed: %s in %s / %s\n%s\n%s\n" % (sep, section, part, chapter, test, why)) return False if not quiet: if section: print("%s %s / %s %s" % (stars, tests.chapter, section, stars)) print("%4d (%2d): TEST %s" % (index, subindex, test)) feeder = SingleLineFeeder(test, "<test>") evaluation = Evaluation(definitions, catch_interrupt=False, output=TestOutput()) try: query = evaluation.parse_feeder(feeder) if query is None: # parsed expression is None result = None out = evaluation.out else: result = evaluation.evaluate(query) out = result.out result = result.result except Exception as exc: fail("Exception %s" % exc) info = sys.exc_info() sys.excepthook(*info) return False if not compare(result, wanted): fail_msg = "Result: %s\nWanted: %s" % (result, wanted) if out: fail_msg += "\nAdditional output:\n" fail_msg += "\n".join(str(o) for o in out) return fail(fail_msg) output_ok = True if len(out) != len(wanted_out): output_ok = False else: for got, wanted in zip(out, wanted_out): if not got == wanted: output_ok = False break if not output_ok: return fail( "Output:\n%s\nWanted:\n%s" % ("\n".join(str(o) for o in out), "\n".join(str(o) for o in wanted_out))) return True
def benchmark_expression(expression_string): print(" '{0}'".format(expression_string)) expr = parse(definitions, SingleLineFeeder(expression_string)) timeit(lambda: expr.evaluate(evaluation))
def benchmark_format(expression_string): print(" '{0}'".format(expression_string)) expr = parse(definitions, SingleLineFeeder(expression_string)) timeit(lambda: expr.default_format(evaluation, "FullForm"))
def benchmark_parse(expression_string): print(" '{0}'".format(truncate_line(expression_string))) timeit(lambda: parse(definitions, SingleLineFeeder(expression_string)))
def parse(self, query): 'Parse a single expression and print the messages.' from mathics.core.parser import SingleLineFeeder return self.parse_feeder(SingleLineFeeder(query))
def _evaluate(str_expression): expr = parse(definitions, SingleLineFeeder(str_expression)) return expr.evaluate(evaluation)
def evaluate(self, str_expression, timeout=None, form=None): expr = parse(self.definitions, SingleLineFeeder(str_expression)) if form is None: form = self.form self.last_result = expr.evaluate(self.evaluation) return self.last_result
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unittest from mathics.core.expression import Expression, Integer, Rational, Symbol from mathics.core.definitions import Definitions from mathics.core.evaluation import Evaluation from mathics.core.parser import SingleLineFeeder, parse definitions = Definitions(add_builtin=True) for i in range(1, 4): evaluation = Evaluation(definitions=definitions, catch_interrupt=False) expr = parse(definitions, SingleLineFeeder(f"<< GS{i}.m")) expr.evaluate(evaluation)