File: test_macro_processor.py

package info (click to toggle)
hy 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,632 kB
  • sloc: python: 7,299; makefile: 38; sh: 27
file content (63 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pytest

from hy.compiler import HyASTCompiler
from hy.errors import HyMacroExpansionError
from hy.macros import macro, macroexpand
from hy.models import Expression, Float, List, String, Symbol
from hy.reader import read


@macro("test")
def tmac(*tree):
    """Turn an expression into a list"""
    return List(tree)


def test_preprocessor_simple():
    """Test basic macro expansion"""
    obj = macroexpand(read('(test "one" "two")'), __name__, HyASTCompiler(__name__))
    assert obj == List([String("one"), String("two")])
    assert type(obj) == List


def test_preprocessor_expression():
    """Test that macro expansion doesn't recurse"""
    obj = macroexpand(
        read('(test (test "one" "two"))'), __name__, HyASTCompiler(__name__)
    )

    assert type(obj) == List
    assert type(obj[0]) == Expression

    assert obj[0] == Expression([Symbol("test"), String("one"), String("two")])

    obj = List([String("one"), String("two")])
    obj = read('(shill ["one" "two"])')[1]
    assert obj == macroexpand(obj, __name__, HyASTCompiler(__name__))


def test_preprocessor_exceptions():
    """Test that macro expansion raises appropriate exceptions"""
    with pytest.raises(HyMacroExpansionError) as excinfo:
        macroexpand(read("(when)"), __name__, HyASTCompiler(__name__))
    assert "TypeError: when()" in excinfo.value.msg


def test_macroexpand_nan():
    # https://github.com/hylang/hy/issues/1574
    import math

    NaN = float("nan")
    x = macroexpand(Float(NaN), __name__, HyASTCompiler(__name__))
    assert type(x) is Float
    assert math.isnan(x)


def test_macroexpand_source_data():
    # https://github.com/hylang/hy/issues/1944
    ast = Expression([Symbol("when"), String("a")])
    ast.start_line = 3
    ast.start_column = 5
    bad = macroexpand(ast, "hy.core.macros", once = True)
    assert bad.start_line == 3
    assert bad.start_column == 5