xonsh/tests/test_contexts.py

80 lines
2 KiB
Python
Raw Normal View History

2016-06-05 16:25:43 -04:00
"""Tests xonsh contexts."""
from nose.tools import assert_equal, assert_is
from tools import (mock_xonsh_env, execer_setup, check_exec, check_eval,
check_parse, skip_if)
from xonsh.contexts import Block
2016-06-05 17:02:10 -04:00
#
# helpers
#
2016-06-05 16:25:43 -04:00
def setup():
execer_setup()
2016-06-05 17:02:10 -04:00
def block_checks_glb(name, glbs, body, obs):
block = glbs[name]
for k, v in obs.items():
yield assert_equal, v, glbs[k]
if isinstance(body, str):
body = body.splitlines()
yield assert_equal, body, block.lines
yield assert_is, glbs, block.glbs
yield assert_is, None, block.locs
#
# tests
#
2016-06-05 16:25:43 -04:00
def test_block_noexec():
s = ('x = 1\n'
'with Block():\n'
' x += 42\n')
glbs = {'Block': Block}
check_exec(s, glbs=glbs, locs=None)
assert_equal(1, glbs['x'])
2016-06-05 17:02:10 -04:00
2016-06-05 16:25:43 -04:00
def test_block_oneline():
s = ('x = 1\n'
'with Block() as b:\n'
' x += 42\n')
glbs = {'Block': Block}
check_exec(s, glbs=glbs, locs=None)
2016-06-05 17:02:10 -04:00
yield from block_checks_glb('b', glbs, [' x += 42'], {'x': 1})
def test_block_manylines():
body = (' ![echo wow mom]\n'
'# bad place for a comment\n'
' x += 42')
s = ('x = 1\n'
'with Block() as b:\n' + body + '\n')
glbs = {'Block': Block}
check_exec(s, glbs=glbs, locs=None)
yield from block_checks_glb('b', glbs, body, {'x': 1})
def test_block_leading_comment():
# leading comments do not show up in block lines
s = ('x = 1\n'
'with Block() as b:\n'
' # I am a leading comment\n'
' x += 42\n')
glbs = {'Block': Block}
check_exec(s, glbs=glbs, locs=None)
yield from block_checks_glb('b', glbs, [' x += 42'], {'x': 1})
def test_block_trailing_comment():
# trailing comments do not show up in block lines
s = ('x = 1\n'
'with Block() as b:\n'
' x += 42\n'
' # I am a trailing comment\n')
glbs = {'Block': Block}
check_exec(s, glbs=glbs, locs=None)
yield from block_checks_glb('b', glbs, [' x += 42'], {'x': 1})