test_eval.py 1.12 KB
Newer Older
1
import os
2

Isuru Fernando's avatar
Isuru Fernando committed
3
import pytest
4
5

import env  # noqa: F401
6
from pybind11_tests import eval_ as m
Dean Moldovan's avatar
Dean Moldovan committed
7
8


9
def test_evals(capture):
Dean Moldovan's avatar
Dean Moldovan committed
10
    with capture:
11
        assert m.test_eval_statements()
12
13
    assert capture == "Hello World!"

14
15
    assert m.test_eval()
    assert m.test_eval_single_statement()
16

Isuru Fernando's avatar
Isuru Fernando committed
17
18
19
    assert m.test_eval_failure()


20
@pytest.mark.xfail("env.PYPY", raises=RuntimeError)
Isuru Fernando's avatar
Isuru Fernando committed
21
def test_eval_file():
22
    filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py")
23
    assert m.test_eval_file(filename)
24

25
    assert m.test_eval_file_failure()
26
27
28
29
30
31
32
33


def test_eval_empty_globals():
    assert "__builtins__" in m.eval_empty_globals(None)

    g = {}
    assert "__builtins__" in m.eval_empty_globals(g)
    assert "__builtins__" in g
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


def test_eval_closure():
    global_, local = m.test_eval_closure()

    assert global_["closure_value"] == 42
    assert local["closure_value"] == 0

    assert "local_value" not in global_
    assert local["local_value"] == 0

    assert "func_global" not in global_
    assert local["func_global"]() == 42

    assert "func_local" not in global_
    with pytest.raises(NameError):
        local["func_local"]()