Skip to content

Commit

Permalink
Port AugAssign tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Feb 3, 2017
1 parent 081f1af commit 65a27e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
24 changes: 0 additions & 24 deletions src/RestrictedPython/tests/security_in_syntax.py

This file was deleted.

13 changes: 7 additions & 6 deletions tests/__init__.py
Expand Up @@ -5,12 +5,13 @@

def _execute(compile_func):
"""Factory to create an execute function."""
def _execute(source):
code, errors = compile_func(source)[:2]
assert errors == (), errors
assert code is not None
glb = {}
exec(code, glb)
def _execute(source, glb=None):
result = compile_func(source)
assert result.errors == (), result.errors
assert result.code is not None
if glb is None:
glb = {}
exec(result.code, glb)
return glb
return _execute

Expand Down
38 changes: 31 additions & 7 deletions tests/test_transformer.py
Expand Up @@ -639,9 +639,10 @@ def test_transformer__RestrictingNodeTransformer__visit_Subscript_2(
_write_.reset_mock()


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign(
compile, mocker):
@pytest.mark.parametrize(*execute)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign__1(
execute, mocker):
"""It allows augmented assign for variables."""
_inplacevar_ = mocker.stub()
_inplacevar_.side_effect = lambda op, val, expr: val + expr

Expand All @@ -652,24 +653,47 @@ def test_transformer__RestrictingNodeTransformer__visit_AugAssign(
'z': 0
}

result = compile("a += x + z")
assert result.errors == ()
exec(result.code, glb)

execute("a += x + z", glb)
assert glb['a'] == 2
_inplacevar_.assert_called_once_with('+=', 1, 1)
_inplacevar_.reset_mock()


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign__2(compile):
"""It forbids augmented assign of attributes."""
result = compile("a.a += 1")
assert result.errors == (
'Line 1: Augmented assignment of attributes is not allowed.',)


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign__3(compile):
"""It forbids augmented assign of subscripts."""
result = compile("a[a] += 1")
assert result.errors == (
'Line 1: Augmented assignment of object items and slices is not '
'allowed.',)


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign__4(compile):
"""It forbids augmented assign of slices."""
result = compile("a[x:y] += 1")
assert result.errors == (
'Line 1: Augmented assignment of object items and slices is not '
'allowed.',)


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign__5(compile):
"""It forbids augmented assign of slices with steps."""
result = compile("a[x:y:z] += 1")
assert result.errors == (
'Line 1: Augmented assignment of object items and slices is not '
'allowed.',)


# def f(a, b, c): pass
# f(*two_element_sequence, **dict_with_key_c)
#
Expand Down

0 comments on commit 65a27e6

Please sign in to comment.