|
2 | 2 | """ |
3 | 3 | Unit tests for mathics.builtin.trace |
4 | 4 | """ |
5 | | -from inspect import isfunction |
6 | | -from test.helper import evaluate |
7 | | -from typing import Callable |
| 5 | +from inspect import isfunction, ismethod |
| 6 | +from test.helper import evaluate, session |
| 7 | +from typing import Any, Callable |
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | import mathics.eval.tracing |
12 | 12 | from mathics import version_info |
13 | 13 | from mathics.core.evaluation import Evaluation |
14 | 14 | from mathics.core.interrupt import AbortInterrupt |
| 15 | +from mathics.eval.tracing import TraceEvent |
15 | 16 |
|
16 | 17 | trace_evaluation_calls = 0 |
17 | 18 |
|
@@ -68,3 +69,79 @@ def counting_print_evaluate( |
68 | 69 | mathics.eval.tracing.print_evaluate = old_evaluation_hook |
69 | 70 | old_recursion_limit = evaluate(f"$RecursionLimit = {old_recursion_limit.value}") |
70 | 71 | assert mathics.eval.tracing.print_evaluate == old_evaluation_hook |
| 72 | + |
| 73 | + |
| 74 | +event_queue = [] |
| 75 | + |
| 76 | + |
| 77 | +def test_skip_trivial_evaluation(): |
| 78 | + """ |
| 79 | + Test of TraceEvaluate[] to filter events |
| 80 | + """ |
| 81 | + |
| 82 | + def empty_queue(): |
| 83 | + global event_queue |
| 84 | + event_queue = [] |
| 85 | + |
| 86 | + def call_event_func(event: TraceEvent, fn: Callable, *args) -> bool: |
| 87 | + """ |
| 88 | + Capture filtered calls in event_queue. |
| 89 | + """ |
| 90 | + if isinstance(type(fn), type) or ismethod(fn) or isfunction(fn): |
| 91 | + name = f"{fn.__module__}.{fn.__qualname__}" |
| 92 | + else: |
| 93 | + name = str(fn) |
| 94 | + event_queue.append(f"{event.name} call : {name}{args[:3]}") |
| 95 | + return False |
| 96 | + |
| 97 | + def return_event_func(event: TraceEvent, result: Any) -> Any: |
| 98 | + """ |
| 99 | + A somewhat generic function to print a traced call's |
| 100 | + return value. |
| 101 | + """ |
| 102 | + event_queue.append(f"{event.name} result: {result}") |
| 103 | + return result |
| 104 | + |
| 105 | + def capture_print(s: str): |
| 106 | + """ |
| 107 | + A somewhat generic function to print a traced call's |
| 108 | + return value. |
| 109 | + """ |
| 110 | + event_queue.append(s) |
| 111 | + |
| 112 | + session.reset() |
| 113 | + old_print_out = session.evaluation.print_out |
| 114 | + session.evaluation.print_out = capture_print |
| 115 | + empty_queue() |
| 116 | + |
| 117 | + try: |
| 118 | + session.evaluate("TraceEvaluation[2 3 + 4]") |
| 119 | + assert [ |
| 120 | + " Evaluating: System`Plus[System`Times[2, 3], 4]", |
| 121 | + " Evaluating: System`Times[2, 3]", |
| 122 | + " Returning: System`Times[2, 3] = (<Integer: 6>, False)", |
| 123 | + " Returning: System`Times[2, 3] = 6", |
| 124 | + " Returning: System`Plus[System`Times[2, 3], 4] = (<Integer: 10>, False)", |
| 125 | + " Returning: System`Plus[System`Times[2, 3], 4] = 10", |
| 126 | + ] == event_queue |
| 127 | + # print() |
| 128 | + # for line in event_queue: |
| 129 | + # print(line) |
| 130 | + |
| 131 | + empty_queue() |
| 132 | + session.evaluate("TraceEvaluation[(2 + 3) 4]") |
| 133 | + assert [ |
| 134 | + " Evaluating: System`Times[System`Plus[2, 3], 4]" |
| 135 | + " Evaluating: System`Plus[2, 3]", |
| 136 | + " Returning: System`Plus[2, 3] = (<Integer: 5>, False)", |
| 137 | + " Returning: System`Plus[2, 3] = 5", |
| 138 | + " Returning: System`Times[System`Plus[2, 3], 4] = (<Integer: 20>, False)", |
| 139 | + ] |
| 140 | + # print() |
| 141 | + # for line in event_queue: |
| 142 | + # print(line) |
| 143 | + |
| 144 | + finally: |
| 145 | + # Just in case, restore everything back to what it was before running this test. |
| 146 | + session.evaluation.print_out = old_print_out |
| 147 | + session.reset() |
0 commit comments