Skip to content

Commit

Permalink
Migrate missing tests.
Browse files Browse the repository at this point in the history
A lot of tests in 'before_and_after.py' are already present in
test_transformer. Just migrate the ones which were missing.
  • Loading branch information
stephan-hof committed Jan 19, 2017
1 parent 4d01d7e commit 171c4c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 177 deletions.
177 changes: 0 additions & 177 deletions src/RestrictedPython/tests/before_and_after.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,190 +22,13 @@
checkBeforeAndAfter() unit tests, which verifies that the restricted compiler
actually produces the same output as would be output by the normal compiler
for the after function.
"""


# getattr

def simple_getattr_before(x):
return x.y


def simple_getattr_after(x):
return _getattr_(x, 'y')


# set attr

def simple_setattr_before():
x.y = "bar"


def simple_setattr_after():
_write_(x).y = "bar"


# for loop and list comprehensions

def simple_forloop_before(x):
for x in [1, 2, 3]:
pass


def simple_forloop_after(x):
for x in _getiter_([1, 2, 3]):
pass


def nested_forloop_before(x):
for x in [1, 2, 3]:
for y in "abc":
pass


def nested_forloop_after(x):
for x in _getiter_([1, 2, 3]):
for y in _getiter_("abc"):
pass


def simple_list_comprehension_before():
x = [y**2 for y in whatever if y > 3]


def simple_list_comprehension_after():
x = [y**2 for y in _getiter_(whatever) if y > 3]


def nested_list_comprehension_before():
x = [x**2 + y**2 for x in whatever1 if x >= 0
for y in whatever2 if y >= x]


def nested_list_comprehension_after():
x = [x**2 + y**2 for x in _getiter_(whatever1) if x >= 0
for y in _getiter_(whatever2) if y >= x]


# print

def simple_print_before():
print "foo"


def simple_print_after():
_print = _print_()
print >> _print, "foo"


# getitem

def simple_getitem_before():
return x[0]


def simple_getitem_after():
return _getitem_(x, 0)


def simple_get_tuple_key_before():
x = y[1, 2]


def simple_get_tuple_key_after():
x = _getitem_(y, (1, 2))


# set item

def simple_setitem_before():
x[0] = "bar"


def simple_setitem_after():
_write_(x)[0] = "bar"


# delitem

def simple_delitem_before():
del x[0]


def simple_delitem_after():
del _write_(x)[0]


# a collection of function parallels to many of the above

def function_with_print_before():
def foo():
print "foo"
return printed


def function_with_print_after():
def foo():
_print = _print_()
print >> _print, "foo"
return _print()


def function_with_getattr_before():
def foo():
return x.y


def function_with_getattr_after():
def foo():
return _getattr_(x, 'y')


def function_with_setattr_before():
def foo(x):
x.y = "bar"


def function_with_setattr_after():
def foo(x):
_write_(x).y = "bar"


def function_with_getitem_before():
def foo(x):
return x[0]


def function_with_getitem_after():
def foo(x):
return _getitem_(x, 0)


def function_with_forloop_before():
def foo():
for x in [1, 2, 3]:
pass


def function_with_forloop_after():
def foo():
for x in _getiter_([1, 2, 3]):
pass


# this, and all slices, won't work in these tests because the before code
# parses the slice as a slice object, while the after code can't generate a
# slice object in this way. The after code as written below
# is parsed as a call to the 'slice' name, not as a slice object.
# XXX solutions?

# def simple_slice_before():
# x = y[:4]

# def simple_slice_after():
# _getitem = _getitem_
# x = _getitem(y, slice(None, 4))

# Assignment stmts in Python can be very complicated. The "no_unpack"
# test makes sure we're not doing unnecessary rewriting.
def no_unpack_before():
Expand Down
36 changes: 36 additions & 0 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,23 @@ def for_loop(it):
c = c + a
return c
def nested_for_loop(it1, it2):
c = 0
for a in it1:
for b in it2:
c = c + a + b
return c
def dict_comp(it):
return {a: a + a for a in it}
def list_comp(it):
return [a + a for a in it]
def nested_list_comp(it1, it2):
return [a + b for a in it1 if a > 1 for b in it2]
def set_comp(it):
return {a + a for a in it}
Expand All @@ -277,6 +288,14 @@ def test_transformer__RestrictingNodeTransformer__guard_iter(compile, mocker):
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()

ret = glb['nested_for_loop']((1, 2), (3, 4))
assert 20 == ret
_getiter_.assert_has_calls([
mocker.call((1, 2)),
mocker.call((3, 4))
])
_getiter_.reset_mock()

ret = glb['dict_comp'](it)
assert {1: 2, 2: 4, 3: 6} == ret
_getiter_.assert_called_once_with(it)
Expand All @@ -287,6 +306,14 @@ def test_transformer__RestrictingNodeTransformer__guard_iter(compile, mocker):
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()

ret = glb['nested_list_comp']((1, 2), (3, 4))
assert [5, 6] == ret
_getiter_.assert_has_calls([
mocker.call((1, 2)),
mocker.call((3, 4))
])
_getiter_.reset_mock()

ret = glb['set_comp'](it)
assert {2, 4, 6} == ret
_getiter_.assert_called_once_with(it)
Expand Down Expand Up @@ -384,6 +411,9 @@ def test_transformer__RestrictingNodeTransformer__guard_iter2(compile, mocker):
def simple_subscript(a):
return a['b']
def tuple_subscript(a):
return a[1, 2]
def slice_subscript_no_upper_bound(a):
return a[1:]
Expand Down Expand Up @@ -417,6 +447,12 @@ def test_transformer__RestrictingNodeTransformer__visit_Subscript_1(compile, moc
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()

ret = glb['tuple_subscript'](value)
ref = (value, (1, 2))
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()

ret = glb['slice_subscript_no_upper_bound'](value)
ref = (value, slice(1, None, None))
assert ref == ret
Expand Down

0 comments on commit 171c4c7

Please sign in to comment.