| 1 |
from pytest import raises |
| 2 |
from pypy.tool.pytest.astrewriter import ast_rewrite |
| 3 |
|
| 4 |
def get_assert_explanation(space, src): |
| 5 |
fn = "?" |
| 6 |
w_code = ast_rewrite.rewrite_asserts(space, src, fn) |
| 7 |
w_d = space.newdict(module=True) |
| 8 |
excinfo = space.raises_w(space.w_AssertionError, space.exec_, w_code, w_d, w_d) |
| 9 |
return space.text_w(space.getitem(space.getattr(excinfo.value.get_w_value(space), space.newtext("args")), space.newint(0))) |
| 10 |
|
| 11 |
def test_simple(space): |
| 12 |
src = """ |
| 13 |
x = 1 |
| 14 |
def f(): |
| 15 |
y = 2 |
| 16 |
assert x == y |
| 17 |
f() |
| 18 |
""" |
| 19 |
expl = get_assert_explanation(space, src) |
| 20 |
assert expl == 'assert 1 == 2' |
| 21 |
|
| 22 |
def test_call(space): |
| 23 |
src = """ |
| 24 |
x = 1 |
| 25 |
def g(): |
| 26 |
return 15 |
| 27 |
def f(): |
| 28 |
y = 2 |
| 29 |
assert g() == x + y |
| 30 |
f() |
| 31 |
""" |
| 32 |
expl = get_assert_explanation(space, src) |
| 33 |
assert expl == 'assert 15 == (1 == 2)\n + where 15 = g()' |
| 34 |
|
| 35 |
def test_list(space): |
| 36 |
src = """ |
| 37 |
x = 1 |
| 38 |
y = 2 |
| 39 |
assert [1, 1, x] == [1, 1, y] |
| 40 |
""" |
| 41 |
expl = get_assert_explanation(space, src) |
| 42 |
|
| 43 |
assert expl == 'assert [1, 1, 1] == [1, 1, 2]' |
| 44 |
|
| 45 |
def test_boolop(space): |
| 46 |
src = "x = 1; y = 2; assert x == 1 and y == 3" |
| 47 |
expl = get_assert_explanation(space, src) |
| 48 |
assert expl == 'assert (1 == 1 and 2 == 3)' |
| 49 |
src = "x = 1; y = 2; assert x == 2 and y == 3" |
| 50 |
expl = get_assert_explanation(space, src) |
| 51 |
assert expl == 'assert (1 == 2)' |
| 52 |
|
| 53 |
def test_attribute(space): |
| 54 |
src = """ |
| 55 |
class A: |
| 56 |
x = 1 |
| 57 |
|
| 58 |
def f(): |
| 59 |
a = A |
| 60 |
assert a.x == 2 |
| 61 |
f() |
| 62 |
""" |
| 63 |
expl = get_assert_explanation(space, src) |
| 64 |
assert expl == "assert 1 == 2\n + where 1 = <class 'A'>.x" |