Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Namespace object #2034

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 17 additions & 37 deletions tests/functional/context/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,52 @@
)


def test_get_namespace(namespace):
def test_get_namespace():
ns = get_namespace()
ns2 = get_namespace()
assert ns == ns2 == namespace


def test_clear(namespace):
namespace["foo"] = 42
assert "foo" in namespace

namespace.clear()
assert "foo" not in namespace
assert ns == ns2


def test_builtin_context_manager(namespace):
namespace["foo"] = 42
with namespace.enter_builtin_scope():
with namespace.enter_scope():
namespace["bar"] = 1337

assert namespace["foo"] == 42
assert "bar" not in namespace


def test_builtin_context_manager_types(namespace):
with namespace.enter_builtin_scope():
for key, value in get_types().items():
assert namespace[key] == value
def test_builtin_types(namespace):
for key, value in get_types().items():
assert namespace[key] == value


def test_builtin_types_persist_after_clear(namespace):
namespace.clear()
for key, value in get_types().items():
assert namespace[key] == value


def test_builtin_context_manager_conamespacetant_vars(namespace):
with namespace.enter_builtin_scope():
def test_context_manager_constant_vars(namespace):
with namespace.enter_scope():
for key in environment.CONSTANT_ENVIRONMENT_VARS.keys():
assert key in namespace

for key in environment.CONSTANT_ENVIRONMENT_VARS.keys():
assert key in namespace


def test_builtin_context_manager_mutable_vars(namespace):
with namespace.enter_builtin_scope():
def test_context_manager_mutable_vars(namespace):
with namespace.enter_scope():
for key in environment.MUTABLE_ENVIRONMENT_VARS.keys():
assert key in namespace

for key in environment.MUTABLE_ENVIRONMENT_VARS.keys():
assert key not in namespace


def test_builtin_context_manager_wrong_sequence(namespace):
with namespace.enter_builtin_scope():
# fails because builtin scope may only be entered once
with pytest.raises(CompilerPanic):
with namespace.enter_builtin_scope():
pass


def test_context_manager(namespace):
with namespace.enter_builtin_scope():
with namespace.enter_scope():
namespace["foo"] = 42
with namespace.enter_scope():
namespace["bar"] = 1337
Expand All @@ -80,13 +66,7 @@ def test_context_manager(namespace):
assert "foo" not in namespace


def test_context_manager_wrong_sequence(namespace):
with pytest.raises(CompilerPanic):
with namespace.enter_scope():
pass


def test_incorrect_context_invokation(namespace):
def test_incorrect_context_invocation(namespace):
with pytest.raises(CompilerPanic):
with namespace:
pass
Expand All @@ -99,7 +79,7 @@ def test_namespace_collision(namespace):


def test_namespace_collision_across_scopes(namespace):
with namespace.enter_builtin_scope():
with namespace.enter_scope():
namespace["foo"] = 42
with namespace.enter_scope():
with pytest.raises(NamespaceCollision):
Expand All @@ -112,7 +92,7 @@ def test_undeclared_definition(namespace):


def test_undeclared_definition_across_scopes(namespace):
with namespace.enter_builtin_scope():
with namespace.enter_scope():
with namespace.enter_scope():
namespace["foo"] = 42
with pytest.raises(UndeclaredDefinition):
Expand Down
22 changes: 9 additions & 13 deletions tests/functional/context/types/test_type_from_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,30 @@


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types(namespace, type_str):
def test_base_types(type_str):
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_abi({"type": type_str})
type_definition = get_type_from_abi({"type": type_str})

assert isinstance(type_definition, primitive._type)


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types_as_arrays(namespace, type_str):
def test_base_types_as_arrays(type_str):
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_abi({"type": f"{type_str}[3]"})
type_definition = get_type_from_abi({"type": f"{type_str}[3]"})

assert isinstance(type_definition, ArrayDefinition)
assert type_definition.length == 3
assert isinstance(type_definition.value_type, primitive._type)


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types_as_multidimensional_arrays(namespace, type_str):
def test_base_types_as_multidimensional_arrays(type_str):
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_abi({"type": f"{type_str}[3][5]"})
type_definition = get_type_from_abi({"type": f"{type_str}[3][5]"})

assert isinstance(type_definition, ArrayDefinition)
assert type_definition.length == 5
Expand All @@ -45,7 +42,6 @@ def test_base_types_as_multidimensional_arrays(namespace, type_str):


@pytest.mark.parametrize("idx", ["0", "-1", "0x00", "'1'", "foo", "[1]", "(1,)"])
def test_invalid_index(namespace, idx):
with namespace.enter_builtin_scope():
with pytest.raises(UnknownType):
get_type_from_abi({"type": f"int128[{idx}]"})
def test_invalid_index(idx):
with pytest.raises(UnknownType):
get_type_from_abi({"type": f"int128[{idx}]"})
42 changes: 17 additions & 25 deletions tests/functional/context/types/test_type_from_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,51 @@


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types(build_node, namespace, type_str):
def test_base_types(build_node, type_str):
node = build_node(type_str)
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, primitive._type)


@pytest.mark.parametrize("type_str", ARRAY_VALUE_TYPES)
def test_array_value_types(build_node, namespace, type_str):
def test_array_value_types(build_node, type_str):
node = build_node(f"{type_str}[1]")
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, primitive._type)


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types_as_arrays(build_node, namespace, type_str):
def test_base_types_as_arrays(build_node, type_str):
node = build_node(f"{type_str}[3]")
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, ArrayDefinition)
assert type_definition.length == 3
assert isinstance(type_definition.value_type, primitive._type)


@pytest.mark.parametrize("type_str", ARRAY_VALUE_TYPES)
def test_array_value_types_as_arrays(build_node, namespace, type_str):
def test_array_value_types_as_arrays(build_node, type_str):
node = build_node(f"{type_str}[1][1]")

with namespace.enter_builtin_scope():
with pytest.raises(StructureException):
get_type_from_annotation(node, DataLocation.STORAGE)
with pytest.raises(StructureException):
get_type_from_annotation(node, DataLocation.STORAGE)


@pytest.mark.parametrize("type_str", BASE_TYPES)
def test_base_types_as_multidimensional_arrays(build_node, namespace, type_str):
node = build_node(f"{type_str}[3][5]")
primitive = get_primitive_types()[type_str]

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, ArrayDefinition)
assert type_definition.length == 5
Expand All @@ -76,21 +71,19 @@ def test_base_types_as_multidimensional_arrays(build_node, namespace, type_str):

@pytest.mark.parametrize("type_str", ["int128", "string"])
@pytest.mark.parametrize("idx", ["0", "-1", "0x00", "'1'", "foo", "[1]", "(1,)"])
def test_invalid_index(build_node, namespace, idx, type_str):
def test_invalid_index(build_node, idx, type_str):
node = build_node(f"{type_str}[{idx}]")
with namespace.enter_builtin_scope():
with pytest.raises((ArrayIndexException, InvalidType)):
get_type_from_annotation(node, DataLocation.STORAGE)
with pytest.raises((ArrayIndexException, InvalidType)):
get_type_from_annotation(node, DataLocation.STORAGE)


@pytest.mark.parametrize("type_str", BASE_TYPES)
@pytest.mark.parametrize("type_str2", BASE_TYPES)
def test_mapping(build_node, namespace, type_str, type_str2):
def test_mapping(build_node, type_str, type_str2):
node = build_node(f"map({type_str}, {type_str2})")
primitives = get_primitive_types()

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, MappingDefinition)
assert isinstance(type_definition.key_type, primitives[type_str]._type)
Expand All @@ -99,12 +92,11 @@ def test_mapping(build_node, namespace, type_str, type_str2):

@pytest.mark.parametrize("type_str", BASE_TYPES)
@pytest.mark.parametrize("type_str2", BASE_TYPES)
def test_multidimensional_mapping(build_node, namespace, type_str, type_str2):
def test_multidimensional_mapping(build_node, type_str, type_str2):
node = build_node(f"map({type_str}, map({type_str}, {type_str2}))")
primitives = get_primitive_types()

with namespace.enter_builtin_scope():
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)
type_definition = get_type_from_annotation(node, DataLocation.STORAGE)

assert isinstance(type_definition, MappingDefinition)
assert isinstance(type_definition.key_type, primitives[type_str]._type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def bar():
self.foo()
"""
vyper_module = parse_to_ast(code)
with namespace.enter_builtin_scope():
with namespace.enter_scope():
with pytest.raises(CallViolation):
ModuleNodeVisitor(vyper_module, {}, namespace)

Expand All @@ -40,6 +40,6 @@ def potato():
self.foo()
"""
vyper_module = parse_to_ast(code)
with namespace.enter_builtin_scope():
with namespace.enter_scope():
with pytest.raises(CallViolation):
ModuleNodeVisitor(vyper_module, {}, namespace)
Loading