def test_global_or_local(): a = scope.Scope() b = scope.Scope(a) c = scope.Scope(b) assert a.is_global() assert not b.is_global() assert not c.is_global()
def test_shadowing(): outer = scope.Scope() outer.add_global_declaration(new_token("x"), scope.TupleType(1), None) inner = scope.Scope(outer) inner.add_global_declaration(new_token("x"), scope.TupleType(2), None) assert outer.lookup(new_token("x")) == (scope.TupleType(1), None) assert inner.lookup(new_token("x")) == (scope.TupleType(2), None)
def test_redeclared_error(): s = scope.Scope() s.add_global_declaration(new_token("x"), scope.TupleType(1), None) with pytest.raises(scope.ScopeError): s.add_global_declaration(new_token("x"), scope.TupleType(1), None) # Same type and slot. with pytest.raises(scope.ScopeError): s.add_global_declaration( new_token("x"), scope.TupleType(2), None) # Different type and slot. Still an error.
def test_undeclared_error(): s = scope.Scope() with pytest.raises(scope.ScopeError): s.lookup(new_token("x"))
def test_global_tuple_lookup(): s = scope.Scope() s.add_global_declaration(new_token("x"), scope.TupleType(1), "x_slot") s.add_global_declaration(new_token("y"), scope.TupleType(2), "y_slot") assert s.lookup(new_token("x")) == (scope.TupleType(1), "x_slot") assert s.lookup(new_token("y")) == (scope.TupleType(2), "y_slot")