Skip to content

Commit

Permalink
Increase code coverage for Eval module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Havlik committed Sep 14, 2017
1 parent f19447f commit 066ec5b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/test_eval.py
Expand Up @@ -40,3 +40,51 @@ def test_eval():
ob = RestrictionCapableEval(exp)
ret = ob.eval({'m': [1, 2]})
assert ret == [2, 1]


def test_Eval__RestrictionCapableEval_1():
"""It raises SyntaxError when there are errors
(by using forbidden stuff) in the code."""
ob = RestrictionCapableEval("_a")
with pytest.raises(SyntaxError):
ob.prepRestrictedCode()


def test_Eval__RestictionCapableEval__prepUnrestrictedCode_1():
"""It does nothing when unrestricted code is already set by init."""
ob = RestrictionCapableEval("a")
assert ob.used == ('a',)
ob.expr = "b"
ob.prepUnrestrictedCode()
assert ob.used == ('a',)


def test_Eval__RestictionCapableEval__prepUnrestrictedCode_2():
"""It does not re-set 'used' if it is already set by an earlier call."""
ob = RestrictionCapableEval("a")
assert ob.used == ('a',)
ob.used = ('b',)
# This is needed to force re-compilation
ob.ucode = None
ob.prepUnrestrictedCode()
# If it was called again, used would be ('a',) again.
assert ob.used == ('b',)


def test_Eval__RestictionCapableEval__prepRestrictedCode_1():
"""It does nothing when restricted code is already set by prepRestrictedCode."""
ob = RestrictionCapableEval("a")
ob.prepRestrictedCode()
assert ob.used == ('a',)
ob.expr = "b"
ob.prepRestrictedCode()
assert ob.used == ('a',)


def test_Eval__RestictionCapableEval__eval_1():
"""It does not add names from the mapping to the
global scope which are already there."""
ob = RestrictionCapableEval("a + b + c")
ob.globals['c'] = 8
result = ob.eval(dict(a=1, b=2, c=4))
assert result == 11

0 comments on commit 066ec5b

Please sign in to comment.