|
| 1 | +from .helper import session, check_evaluation |
| 2 | +import sys |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mathics.core.expression import (Symbol, Atom, String, Expression) |
| 6 | +from mathics.builtin.base import BoxConstruct, AtomBuiltin, Predefined |
| 7 | + |
| 8 | +class CustomBoxConstruct(BoxConstruct): |
| 9 | + def __init__(self, evaluation): |
| 10 | + super().__init__(evaluation=evaluation) |
| 11 | + self._leaves = [1,2,3] |
| 12 | + |
| 13 | + def boxes_to_text(self, leaves=None, **options): |
| 14 | + if not leaves: |
| 15 | + leaves = self._leaves |
| 16 | + return 'CustomBoxConstruct<<' + self._leaves.__str__() + '>>' |
| 17 | + |
| 18 | + def boxes_to_xml(self, leaves=None, **options): |
| 19 | + if not leaves: |
| 20 | + leaves = self._leaves |
| 21 | + return 'CustomBoxConstruct<<' + self._leaves.__str__() + '>>' |
| 22 | + |
| 23 | + def boxes_to_tex(self, leaves=None, **options): |
| 24 | + if not leaves: |
| 25 | + leaves = self._leaves |
| 26 | + return 'CustomBoxConstruct<<' + int(self_leaves) + '>>' |
| 27 | + |
| 28 | +class CustomAtom(Predefined): |
| 29 | + """ |
| 30 | + just a test |
| 31 | + """ |
| 32 | + context = "System`" |
| 33 | + rules = {"N[System`CustomAtom]": "37", } |
| 34 | + |
| 35 | + def apply_to_boxes(self, evaluation): |
| 36 | + 'System`MakeBoxes[System`CustomAtom, StandardForm|TraditionalForm|OutputForm|InputForm]' |
| 37 | + return CustomBoxConstruct(evaluation=evaluation) |
| 38 | + |
| 39 | +def test_custom_boxconstruct(): |
| 40 | + defs = session.evaluation.definitions |
| 41 | + instance_custom_atom = CustomAtom(expression=False) |
| 42 | + instance_custom_atom.contribute(defs, is_pymodule=True) |
| 43 | + expr = session.evaluate("MakeBoxes[CustomAtom, InputForm]") |
| 44 | + formatted = session.format_result().boxes_to_xml() |
| 45 | + assert formatted == "CustomBoxConstruct<<[1, 2, 3]>>" |
| 46 | + |
0 commit comments