Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
a1b96de6
Commit
a1b96de6
authored
Feb 20, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests for safe_exec
parent
37382de3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
0 deletions
+53
-0
codejail/tests/test_safe_exec.py
+53
-0
No files found.
codejail/tests/test_safe_exec.py
0 → 100644
View file @
a1b96de6
"""Test safe_exec.py"""
import
textwrap
import
unittest
from
nose.plugins.skip
import
SkipTest
from
codejail.safe_exec
import
safe_exec
,
not_safe_exec
dedent
=
textwrap
.
dedent
class
SafeExecTests
(
object
):
"""The tests for `safe_exec`, will be mixed into specific test classes below."""
def
test_set_values
(
self
):
g
,
l
=
{},
{}
self
.
safe_exec
(
"a = 17"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
17
)
def
test_division
(
self
):
g
,
l
=
{},
{}
# No future division: 1/2 is 0.
self
.
safe_exec
(
"a = 1/2"
,
g
,
l
)
self
.
assertEqual
(
l
[
'a'
],
0
)
# Future division: 1/2 is 0.5.
self
.
safe_exec
(
"a = 1/2"
,
g
,
l
,
future_division
=
True
)
self
.
assertEqual
(
l
[
'a'
],
0.5
)
def
test_assumed_imports
(
self
):
g
,
l
=
{},
{}
# Using string without importing it is bad.
with
self
.
assertRaises
(
Exception
):
self
.
safe_exec
(
"a = string.ascii_lowercase[0]"
,
g
,
l
)
# Using string with an assumed import is fine.
self
.
safe_exec
(
"a = string.ascii_lowercase[0]"
,
g
,
l
,
assumed_imports
=
[
"string"
])
self
.
assertEqual
(
l
[
'a'
],
'a'
)
# Can also import with a shorthand.
self
.
safe_exec
(
"a = op.join('x', 'y')"
,
g
,
l
,
assumed_imports
=
[(
"op"
,
"os.path"
)])
self
.
assertEqual
(
l
[
'a'
][
0
],
'x'
)
self
.
assertEqual
(
l
[
'a'
][
-
1
],
'y'
)
class
TestSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
"""Run SafeExecTests, with the real safe_exec."""
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
safe_exec
(
*
args
,
**
kwargs
)
class
TestNotSafeExec
(
SafeExecTests
,
unittest
.
TestCase
):
"""Run SafeExecTests, with not_safe_exec."""
def
setUp
(
self
):
if
safe_exec
is
not_safe_exec
:
raise
SkipTest
def
safe_exec
(
self
,
*
args
,
**
kwargs
):
not_safe_exec
(
*
args
,
**
kwargs
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment