test_eval.py 1.16 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
import os
3

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

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


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

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

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


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

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


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


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"]()