File: test_parse.py

package info (click to toggle)
pypy3 7.3.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212,332 kB
  • sloc: python: 2,100,989; ansic: 540,684; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (108 lines) | stat: -rw-r--r-- 2,179 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pytest

try:
    import rply
except ImportError:
    pytest.skip("rply not installed")

from rpython.jit.metainterp.ruleopt.parse import *

import os

with open(
    os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "real.rules"
    )
) as f:
    ALLRULES = f.read()


def test_parse_int_add_zero():
    s = """\
add_zero: int_add(x, 0)
    => x
"""
    ast = parse(s)
    assert str(ast.rules[0]) == 'add_zero: int_add(x, 0)\n    => x'

def test_parse_source_positions():
    s = """\
add_zero: int_add(x, 0)
    => x
"""
    ast = parse(s)
    rule = ast.rules[0]
    assert rule.sourcepos.lineno == 1
    assert rule.target.sourcepos.lineno == 2
    assert rule.target.sourcepos.colno == 8

def test_parse_function_many_args():
    s = """\
n: op(C)
    C1 = f(C, C, C, C)
    => C
    """
    ast = parse(s)


def test_sorry():
    s = """\
eq_different_knownbits: int_eq(x, y)
    SORRY_Z3
    => 0
    """
    ast = parse(s)
    assert ast.rules[0].cantproof == True

def test_parse_lshift_rshift():
    s = """\
int_lshift_int_rshift_consts: int_lshift(int_rshift(x, C1), C1)
    C = (-1 >> C1) << C1
    => int_and(x, C)
    """
    ast = parse(s)


def test_parse_all():
    ast = parse(ALLRULES)  # also typechecks


def test_undefined_name():
    s = """\
n: op(C)
    => x
    """
    with pytest.raises(TypeCheckError) as info:
        ast = parse(s)
    assert str(info.value) == "variable 'x' is not defined"
    assert info.value.format(s) == '''\
Type error: variable 'x' is not defined
in line 2
        => x
           ^'''

def test_doubly_defined_name():
    s = """\
n: op(C)
    C = C + 1
    => C
    """
    with pytest.raises(TypeCheckError) as info:
        ast = parse(s)
    assert str(info.value) == "'C' is already defined"


def test_check_not_bool():
    s = """\
n: op(C)
    check C
    => C
    """
    with pytest.raises(TypeCheckError) as info:
        ast = parse(s)
    assert str(info.value) == "expected check expression to return a bool, got int"
    assert info.value.format(s) == '''\
Type error: expected check expression to return a bool, got int
in line 2
        check C
              ^'''