|
| 1 | +"""Tests for latexify.transformers.function_expander.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ast |
| 6 | +import math |
| 7 | + |
| 8 | +from latexify import ast_utils, parser, test_utils |
| 9 | +from latexify.transformers.function_expander import FunctionExpander |
| 10 | + |
| 11 | + |
| 12 | +def _make_ast(args: list[str], body: ast.expr) -> ast.Module: |
| 13 | + """Helper function to generate an AST for f(x). |
| 14 | +
|
| 15 | + Args: |
| 16 | + args: The arguments passed to the method. |
| 17 | + body: The body of the return statement. |
| 18 | +
|
| 19 | + Returns: |
| 20 | + Generated AST. |
| 21 | + """ |
| 22 | + return ast.Module( |
| 23 | + body=[ |
| 24 | + ast.FunctionDef( |
| 25 | + name="f", |
| 26 | + args=ast.arguments( |
| 27 | + args=[ast.arg(arg=arg) for arg in args], |
| 28 | + kwonlyargs=[], |
| 29 | + kw_defaults=[], |
| 30 | + defaults=[], |
| 31 | + ), |
| 32 | + body=[ast.Return(body)], |
| 33 | + decorator_list=[], |
| 34 | + ) |
| 35 | + ], |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_hypot_unchanged_without_attribute_access() -> None: |
| 40 | + from math import hypot |
| 41 | + |
| 42 | + def f(x, y): |
| 43 | + return hypot(x, y) |
| 44 | + |
| 45 | + expected = _make_ast( |
| 46 | + ["x", "y"], ast.Call(ast.Name("hypot"), [ast.Name("x"), ast.Name("y")]) |
| 47 | + ) |
| 48 | + transformed = FunctionExpander(set()).visit(parser.parse_function(f)) |
| 49 | + test_utils.assert_ast_equal(transformed, expected) |
| 50 | + |
| 51 | + |
| 52 | +def test_hypot_unchanged() -> None: |
| 53 | + def f(x, y): |
| 54 | + return math.hypot(x, y) |
| 55 | + |
| 56 | + expected = _make_ast( |
| 57 | + ["x", "y"], |
| 58 | + ast.Call( |
| 59 | + ast.Attribute(ast.Name("math"), "hypot", ast.Load()), |
| 60 | + [ast.Name("x"), ast.Name("y")], |
| 61 | + ), |
| 62 | + ) |
| 63 | + transformed = FunctionExpander(set()).visit(parser.parse_function(f)) |
| 64 | + test_utils.assert_ast_equal(transformed, expected) |
| 65 | + |
| 66 | + |
| 67 | +def test_hypot_expanded() -> None: |
| 68 | + def f(x, y): |
| 69 | + return math.hypot(x, y) |
| 70 | + |
| 71 | + expected = _make_ast( |
| 72 | + ["x", "y"], |
| 73 | + ast.Call( |
| 74 | + ast.Name("sqrt"), |
| 75 | + [ |
| 76 | + ast.BinOp( |
| 77 | + ast.BinOp(ast.Name("x"), ast.Pow(), ast_utils.make_constant(2)), |
| 78 | + ast.Add(), |
| 79 | + ast.BinOp(ast.Name("y"), ast.Pow(), ast_utils.make_constant(2)), |
| 80 | + ) |
| 81 | + ], |
| 82 | + ), |
| 83 | + ) |
| 84 | + transformed = FunctionExpander({"hypot"}).visit(parser.parse_function(f)) |
| 85 | + test_utils.assert_ast_equal(transformed, expected) |
| 86 | + |
| 87 | + |
| 88 | +def test_hypot_expanded_no_args() -> None: |
| 89 | + def f(): |
| 90 | + return math.hypot() |
| 91 | + |
| 92 | + expected = _make_ast( |
| 93 | + [], |
| 94 | + ast_utils.make_constant(0), |
| 95 | + ) |
| 96 | + transformed = FunctionExpander({"hypot"}).visit(parser.parse_function(f)) |
| 97 | + test_utils.assert_ast_equal(transformed, expected) |
0 commit comments