Skip to content

Commit

Permalink
maximum of test coverage by adding tests for each method
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Mar 30, 2017
1 parent ba58f60 commit d0b1685
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 20 deletions.
27 changes: 10 additions & 17 deletions src/RestrictedPython/transformer.py
Expand Up @@ -631,27 +631,21 @@ def visit_UnaryOp(self, node):
"""
UnaryOp (Unary Operations) is the overall element for:
* Not --> which should be allowed
* UAdd
* USub
* UAdd --> Positive Number notation
* USub --> Negative Number notation
"""
return self.node_contents_visit(node)

def visit_UAdd(self, node):
"""
"""
self.not_allowed(node)
"""Positive Numbers notation should be allowed."""
return self.node_contents_visit(node)

def visit_USub(self, node):
"""
"""
self.not_allowed(node)
"""Negativ Numbers notation should be allowed."""
return self.node_contents_visit(node)

def visit_Not(self, node):
"""
The Not Operator should be allowed.
"""
"""The Not Operator should be allowed."""
return self.node_contents_visit(node)

def visit_Invert(self, node):
Expand All @@ -661,9 +655,7 @@ def visit_Invert(self, node):
self.not_allowed(node)

def visit_BinOp(self, node):
"""
"""
"""Binary Operations should be allowed."""
return self.node_contents_visit(node)

def visit_Add(self, node):
Expand Down Expand Up @@ -739,8 +731,9 @@ def visit_BitAnd(self, node):
self.not_allowed(node)

def visit_MatMult(self, node):
"""
"""Matrix Multiplication should not be allowed.
Matrix Multiplication (@) is a Python 3.5+ Feature.
"""
self.not_allowed(node)

Expand Down
37 changes: 37 additions & 0 deletions tests/transformer/test_global_local.py
@@ -0,0 +1,37 @@
from tests import c_exec

import pytest


GLOBAL_EXAMPLE = """
a = 1
global a
"""


@pytest.mark.parametrize(*c_exec)
def test_Global(c_exec):
result = c_exec(GLOBAL_EXAMPLE)
assert result.code is None
assert result.errors == (
'Line 3: Global statements are not allowed.',
)
assert result.warnings == []
assert result.used_names == {}


NONLOCAL_EXAMPLE = """
a = 1
nonlocal a
"""


@pytest.mark.parametrize(*c_exec)
def test_Nonlocal(c_exec):
result = c_exec(NONLOCAL_EXAMPLE)
assert result.code is None
assert result.errors == (
'Line 3: Nonlocal statements are not allowed.',
)
assert result.warnings == []
assert result.used_names == {}
95 changes: 92 additions & 3 deletions tests/transformer/test_operators.py
Expand Up @@ -15,6 +15,9 @@ def test_Add(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 2


@pytest.mark.parametrize(*c_eval)
def test_Sub(c_eval):
Expand All @@ -24,6 +27,9 @@ def test_Sub(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 1


@pytest.mark.parametrize(*c_eval)
def test_Mult(c_eval):
Expand All @@ -33,6 +39,9 @@ def test_Mult(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 4


@pytest.mark.parametrize(*c_eval)
def test_Div(c_eval):
Expand All @@ -42,12 +51,17 @@ def test_Div(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 5


@pytest.mark.parametrize(*c_eval)
def test_Mod(c_eval):
result = c_eval('10 / 2')
assert result.code is not None
assert result.errors == ()
result = c_eval('10 % 2')
assert result.code is None
assert result.errors == (
'Line None: Mod statements are not allowed.',
)
assert result.warnings == []
assert result.used_names == {}

Expand Down Expand Up @@ -98,6 +112,9 @@ def test_Eq(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_NotEq(c_eval):
Expand All @@ -107,6 +124,9 @@ def test_NotEq(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_Gt(c_eval):
Expand All @@ -116,6 +136,9 @@ def test_Gt(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_Lt(c_eval):
Expand All @@ -125,6 +148,9 @@ def test_Lt(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_GtE(c_eval):
Expand All @@ -134,6 +160,9 @@ def test_GtE(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_LtE(c_eval):
Expand All @@ -143,6 +172,9 @@ def test_LtE(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


# Bool Operators

Expand All @@ -155,6 +187,9 @@ def test_Or(c_eval):
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_And(c_eval):
Expand All @@ -164,6 +199,21 @@ def test_And(c_eval):
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_Not(c_eval):
result = c_eval('not False')
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


# Bit wise Operators

Expand Down Expand Up @@ -244,6 +294,9 @@ def test_In(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_NotIn(c_eval):
Expand All @@ -253,6 +306,9 @@ def test_NotIn(c_eval):
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


# Identity Operator

Expand All @@ -265,6 +321,9 @@ def test_Is(c_eval):
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


@pytest.mark.parametrize(*c_eval)
def test_NotIs(c_eval):
Expand All @@ -273,3 +332,33 @@ def test_NotIs(c_eval):
assert result.errors == ()
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result is True


# Unary Operators


@pytest.mark.parametrize(*c_eval)
def test_UAdd(c_eval):
result = c_eval('+1')
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 1


@pytest.mark.parametrize(*c_eval)
def test_USub(c_eval):
result = c_eval('-1')
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
# assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == -1

0 comments on commit d0b1685

Please sign in to comment.