コード例 #1
0
def test_can_clear_visible_keys():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    env.clear()

    assert inner == {'__hidden__': 0}
コード例 #2
0
def test_safe_env_can_get_the_value_associated_with_a_key():
    env = SafeEnv({'a': 0, '__hidden__': 0})

    assert env['a'] == 0

    with pytest.raises(KeyError):
        env['b']

    assert env.get('a') == 0
    assert env.get('b') == None
    assert env.get('c', 'default') == 'default'
コード例 #3
0
def test_safe_env_fails_to_get_value_associated_with_double_underscore_keys():
    env = SafeEnv({'a': 0, '__hidden__': 0})

    with pytest.raises(KeyError):
        env['__hidden__']

    with pytest.raises(KeyError):
        env['__another_hidden__']

    assert env.get('__hidden__') == None
    assert env.get('__another_hidden__') == None
コード例 #4
0
def test_safe_env_can_be_updated():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    env.update([('a', 1)])
    env.update({'b': 2}, c=3)

    assert inner == {'a': 1, 'b': 2, 'c': 3, '__hidden__': 0}

    env.update({'__hidden__': 'not-0'})
    env.update({'__another_hidden__': 0})

    assert inner == {'a': 1, 'b': 2, 'c': 3, '__hidden__': 0}
コード例 #5
0
def test_safe_env_implements_popitem():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    item = env.popitem()

    assert item == ('a', 0)
    assert 'a' not in inner

    assert inner == {'__hidden__': 0}

    with pytest.raises(KeyError):
        env.popitem()
コード例 #6
0
def test_safe_env_setdefault():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    env.setdefault('a', 'not-0')
    env.setdefault('b', 0)

    assert inner == {'a': 0, '__hidden__': 0, 'b': 0}

    with pytest.raises(KeyError):
        env.setdefault('__hidden__', 'not-0')

    with pytest.raises(KeyError):
        env.setdefault('__another_hidden__', 'not-0')
コード例 #7
0
def test_safe_env_delegates_to_inner_dict_when_deleting_item():
    inner = {'a': 1}
    env = SafeEnv(inner)

    del env['a']

    assert inner == {}
コード例 #8
0
def test_safe_env_popitem_preserves_order():
    # This is only relevant for python 3.7 and above, where dictionary order has
    # been stabilized

    keys = generate_keys(5, 100)

    inner = {'a': 0}
    for key in keys:
        inner[key] = key

    env = SafeEnv(inner)

    assert env.popitem() == ('a', 0)

    for key in reversed(keys):
        assert inner.popitem() == (key, key)
コード例 #9
0
def test_safe_env_can_test_for_keys():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    assert 'a' in env
    assert 'b' not in env
    assert '__hidden__' not in env
    assert '__another_hidden__' not in env
コード例 #10
0
def test_safe_env_fails_when_deleting_items_with_double_underscore_keys():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    with pytest.raises(KeyError):
        del env['__hidden__']

    with pytest.raises(KeyError):
        del env['__another_hidden__']
コード例 #11
0
def test_safe_env_can_pop():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    value = env.pop('a')

    assert value == 0
    assert inner == {'__hidden__': 0}
    assert env.pop('a', 'default') == 'default'
    assert env.pop('__hidden__', 'default') == 'default'
    assert inner == {'__hidden__': 0}

    with pytest.raises(KeyError):
        env.pop('__hidden__')
コード例 #12
0
def test_safe_env_can_be_represented():
    inner = {'a': 0}
    env = SafeEnv(inner)

    assert repr(env) == 'SafeEnv({\'a\': 0})'

    inner['__self__'] = 0

    assert repr(env) == 'SafeEnv({\'a\': 0})'

    inner['__self__'] = inner

    assert repr(env) == 'SafeEnv({\'a\': 0})'

    env['self'] = env

    assert repr(env) == 'SafeEnv({\'a\': 0, \'self\': SafeEnv({...})})'
コード例 #13
0
def test_safe_env_fails_when_putting_double_underscore_keys():
    inner = {'__hidden__': 0}
    env = SafeEnv(inner)

    with pytest.raises(KeyError):
        env['__hidden__'] = None

    with pytest.raises(KeyError):
        env.setdefault('__hidden__', None)

    with pytest.raises(KeyError):
        env['__another_hidden__'] = None

    with pytest.raises(KeyError):
        env.setdefault('__another_hidden__', None)

    assert inner == {'__hidden__': 0}
コード例 #14
0
ファイル: parser.py プロジェクト: jdferreira/safe-parser
 def prepare_environment(self):
     self.env['__builtins__'] = {}
     self.env['__env__'] = SafeEnv(self.env)
コード例 #15
0
def test_can_get_item_iterator():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    assert list(env.items()) == [('a', 0)]
コード例 #16
0
def test_safe_env_has_len():
    env = SafeEnv({'a': 0, '__hidden__': 0})

    assert len(env) == 1
コード例 #17
0
def test_can_iterate_over_keys():
    inner = {'a': 0, '__hidden__': 0}
    env = SafeEnv(inner)

    assert list(env) == ['a']
コード例 #18
0
def test_safe_env_can_delete_items():
    env = SafeEnv({'a': 0})

    del env['a']

    assert len(env) == 0
コード例 #19
0
def test_can_create_safe_env_from_dict():
    SafeEnv({})
コード例 #20
0
def test_safe_env_can_put_items():
    env = SafeEnv({})

    env['a'] = 0

    assert env['a'] == 0