-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2108 from iamdefinitelyahuman/fix-array-index-typ…
…echeck Array index typecheck
- Loading branch information
Showing
4 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import pytest | ||
|
||
from vyper.ast import parse_to_ast | ||
from vyper.context.validation import validate_semantics | ||
from vyper.exceptions import ( | ||
ArrayIndexException, | ||
InvalidReference, | ||
InvalidType, | ||
TypeMismatch, | ||
UndeclaredDefinition, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("value", ["address", "Bytes[10]", "decimal", "bool"]) | ||
def test_type_mismatch(namespace, value): | ||
code = f""" | ||
a: uint256[3] | ||
@internal | ||
def foo(b: {value}): | ||
self.a[b] = 12 | ||
""" | ||
vyper_module = parse_to_ast(code) | ||
with pytest.raises(TypeMismatch): | ||
validate_semantics(vyper_module, {}) | ||
|
||
|
||
@pytest.mark.parametrize("value", ["1.0", "0.0", "'foo'", "0x00", "b'\x01'", "False"]) | ||
def test_invalid_literal(namespace, value): | ||
code = f""" | ||
a: uint256[3] | ||
@internal | ||
def foo(): | ||
self.a[{value}] = 12 | ||
""" | ||
vyper_module = parse_to_ast(code) | ||
with pytest.raises(InvalidType): | ||
validate_semantics(vyper_module, {}) | ||
|
||
|
||
@pytest.mark.parametrize("value", [-1, 3, -(2 ** 127), 2 ** 127 - 1, 2 ** 256 - 1]) | ||
def test_out_of_bounds(namespace, value): | ||
code = f""" | ||
a: uint256[3] | ||
@internal | ||
def foo(): | ||
self.a[{value}] = 12 | ||
""" | ||
vyper_module = parse_to_ast(code) | ||
with pytest.raises(ArrayIndexException): | ||
validate_semantics(vyper_module, {}) | ||
|
||
|
||
@pytest.mark.parametrize("value", ["b", "self.b"]) | ||
def test_undeclared_definition(namespace, value): | ||
code = f""" | ||
a: uint256[3] | ||
@internal | ||
def foo(): | ||
self.a[{value}] = 12 | ||
""" | ||
vyper_module = parse_to_ast(code) | ||
with pytest.raises(UndeclaredDefinition): | ||
validate_semantics(vyper_module, {}) | ||
|
||
|
||
@pytest.mark.parametrize("value", ["a", "foo", "int128"]) | ||
def test_invalid_reference(namespace, value): | ||
code = f""" | ||
a: uint256[3] | ||
@internal | ||
def foo(): | ||
self.a[{value}] = 12 | ||
""" | ||
vyper_module = parse_to_ast(code) | ||
with pytest.raises(InvalidReference): | ||
validate_semantics(vyper_module, {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters