Пример #1
0
def test_assign():
    s = Scope()
    s.assign("a", 1)
    assert s._scopes == [ {"a": 1, }, ]
    s.assign("b", 2)
    assert s._scopes == [ {"a": 1, "b": 2, }, ]
    s.assign("a", "spam")
    assert s._scopes == [ {"a": "spam", "b": 2, }, ]
    s.pushScope()
    s.assign("a", 1)
    s.assign("c", "foo")
    assert s._scopes == [ {"a": "spam", "b": 2, }, {"a": 1, "c": "foo", }, ]
Пример #2
0
def test_lookup():
    s = Scope()
    s.assign("a", "spam")
    s.assign("b", 2)
    s.pushScope()
    s.assign("a", 1)
    s.assign("c", "foo")
    assert s._scopes == [ {"a": "spam", "b": 2, }, {"a": 1, "c": "foo", }, ]
    assert s.lookup("a") == 1
    assert s.lookup("b") == 2
    assert s.lookup("c") == "foo"
    with pytest.raises(ScopeLookupError):
        s.lookup("spam")
Пример #3
0
def test_getGlobal():
    s = Scope()
    s.assign("a", "spam")
    s.assign("b", 2)
    s.pushScope()
    s.assign("a", 1)
    s.assign("c", "foo")
    assert s._scopes == [ {"a": "spam", "b": 2, }, {"a": 1, "c": "foo", }, ]
    assert s.getGlobal("a") == "spam"
    assert s.getGlobal("b") == 2
    with pytest.raises(ScopeLookupError):
        s.getGlobal("c")
    with pytest.raises(ScopeLookupError):
        s.getGlobal("spam")