Skip to content

Commit

Permalink
add tests for namedtuple structs
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Jan 9, 2025
1 parent 56842d1 commit a30932c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion boa/contracts/abi/abi_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def _parse_complex(abi: dict, value: Any, name=None) -> str:
components = abi["components"]
typname = name or abi["name"] or "user_struct"
component_names = [item["name"] for item in components]
typ = namedtuple(typname, component_names)
typ = namedtuple(typname, component_names, rename=True)

def _leaf(tuple_vals):
components_parsed = [
Expand Down
45 changes: 45 additions & 0 deletions tests/unitary/contracts/vvm/test_vvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,48 @@ def foo(x: uint256, y: address):

for log in logs:
assert log.address == c.address


def test_structs():
code = """
# pragma version 0.3.10
struct MyStruct1:
x: uint256
struct MyStruct2:
_from: address
x: uint256
@external
def foo(x: uint256) -> MyStruct1:
return MyStruct1({x: x})
@external
def bar(_from: address, x: uint256) -> MyStruct2:
return MyStruct2({_from: _from, x: x})
@external
def baz(x: uint256, _from: address, y: uint256) -> (MyStruct1, MyStruct2):
return MyStruct1({x: x}), MyStruct2({_from: _from, x: y})
"""

c = boa.loads(code)

t = c.foo(1)
# don't get correct struct names from the abi.
# assert type(t).__name__ == "MyStruct1"
assert t.x == 1

addy = boa.env.generate_address()
s = c.bar(addy, 2)
# assert type(s).__name__ == "MyStruct2"
assert s._0 == addy # test renames
assert s.x == 2

u, v = c.baz(3, addy, 4)
# assert type(u).__name__ == "MyStruct1"
assert u.x == 3
# assert type(v).__name__ == "MyStruct2"
assert v._0 == addy
assert v.x == 4

0 comments on commit a30932c

Please sign in to comment.