Skip to content

Commit

Permalink
new test case and corner case for array casees
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu committed Sep 30, 2022
1 parent 9bc879d commit 99c5c8e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pyteal/ast/abi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ def type_spec_is_assignable(a: TypeSpec, b: TypeSpec) -> bool:
ArrayTypeSpec,
StaticArrayTypeSpec,
DynamicArrayTypeSpec,
StringTypeSpec,
AddressTypeSpec,
)

match a, b:
Expand All @@ -532,9 +534,20 @@ def type_spec_is_assignable(a: TypeSpec, b: TypeSpec) -> bool:
return a.length_static() == b.length_static()
case DynamicArrayTypeSpec(), DynamicArrayTypeSpec():
return True
case _:
return False
case ArrayTypeSpec(), _:
return False
case (ArrayTypeSpec(), _) | (_, ArrayTypeSpec()):
if isinstance(b, ArrayTypeSpec):
a, b = b, a
match a, b:
case DynamicArrayTypeSpec(), StringTypeSpec():
a, b = cast(DynamicArrayTypeSpec, a), cast(StringTypeSpec, b)
return a.value_type_spec() == b.value_type_spec()
case StaticArrayTypeSpec(), AddressTypeSpec():
a, b = cast(StaticArrayTypeSpec, a), cast(AddressTypeSpec, b)
return (
a.value_type_spec() == b.value_type_spec()
and a.length_static() == b.length_static()
)
return False

if isinstance(a, type(b)):
Expand Down
75 changes: 75 additions & 0 deletions pyteal/ast/abi/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,21 @@ class NamedTDecl(abi.NamedTuple):
c: abi.Field[abi.Transaction]


class NamedTComp0(abi.NamedTuple):
a0: abi.Field[abi.String]
a1: abi.Field[abi.StaticArray[abi.Byte, Literal[32]]]


class NamedTComp1(abi.NamedTuple):
b0: abi.Field[abi.DynamicBytes]
b1: abi.Field[abi.Address]


class NamedTComp2(abi.NamedTuple):
b1: abi.Field[abi.Address]
b0: abi.Field[abi.DynamicBytes]


TYPE_SPEC_ASSIGNABLE_CASES = [
(abi.PaymentTransactionTypeSpec(), abi.TransactionTypeSpec(), True),
(
Expand Down Expand Up @@ -841,6 +856,66 @@ class NamedTDecl(abi.NamedTuple):
abi.type_spec_from_annotation(NamedTDecl),
True,
),
(
abi.type_spec_from_annotation(abi.StaticBytes[Literal[7]]),
abi.type_spec_from_annotation(abi.StaticArray[abi.Byte, Literal[11]]),
False,
),
(
abi.type_spec_from_annotation(NamedTDecl),
abi.type_spec_from_annotation(NamedTDecl),
True,
),
(
abi.type_spec_from_annotation(abi.String),
abi.type_spec_from_annotation(abi.DynamicBytes),
True,
),
(
abi.type_spec_from_annotation(abi.DynamicArray[abi.Byte]),
abi.type_spec_from_annotation(abi.String),
True,
),
(
abi.type_spec_from_annotation(abi.DynamicArray[abi.Uint32]),
abi.type_spec_from_annotation(abi.String),
False,
),
(
abi.type_spec_from_annotation(abi.Address),
abi.type_spec_from_annotation(abi.StaticArray[abi.Byte, Literal[32]]),
True,
),
(
abi.type_spec_from_annotation(abi.StaticBytes[Literal[32]]),
abi.type_spec_from_annotation(abi.Address),
True,
),
(
abi.type_spec_from_annotation(abi.StaticBytes[Literal[33]]),
abi.type_spec_from_annotation(abi.Address),
False,
),
(
abi.type_spec_from_annotation(NamedTComp0),
abi.type_spec_from_annotation(NamedTComp1),
True,
),
(
abi.type_spec_from_annotation(NamedTComp1),
abi.type_spec_from_annotation(NamedTComp0),
True,
),
(
abi.type_spec_from_annotation(NamedTDecl),
abi.type_spec_from_annotation(NamedTComp0),
False,
),
(
abi.type_spec_from_annotation(NamedTComp2),
abi.type_spec_from_annotation(NamedTComp0),
False,
),
]


Expand Down

0 comments on commit 99c5c8e

Please sign in to comment.