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

Move to pyteal as pt in ABI tests with concise abi prefix #286

Merged
merged 1 commit into from
Apr 20, 2022
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
12 changes: 1 addition & 11 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,4 @@ per-file-ignores =
pyteal/__init__.py: F401, F403
pyteal/ir/ops.py: E221
tests/module_test.py: F401, F403
# Temporary ignores while merging
pyteal/ast/abi/array_base_test.py: F403, F405
pyteal/ast/abi/array_dynamic_test.py: F403, F405
pyteal/ast/abi/array_static_test.py: F403, F405
pyteal/ast/abi/bool_test.py: F403, F405
pyteal/ast/abi/method_return_test.py: F403, F405
pyteal/ast/abi/tuple_test.py: F403, F405
pyteal/ast/abi/type_test.py: F403, F405
pyteal/ast/abi/uint_test.py: F403, F405
pyteal/ast/abi/util_test.py: F403, F405
# End temporary ignores while merging

69 changes: 36 additions & 33 deletions pyteal/ast/abi/array_base_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import List, cast
import pytest

from ... import *
import pyteal as pt
from pyteal import abi

options = CompileOptions(version=5)
options = pt.CompileOptions(version=5)

STATIC_TYPES: List[abi.TypeSpec] = [
abi.BoolTypeSpec(),
Expand Down Expand Up @@ -53,31 +54,31 @@
def test_ArrayElement_init():
dynamicArrayType = abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec())
array = dynamicArrayType.new_instance()
index = Int(6)
index = pt.Int(6)

element = abi.ArrayElement(array, index)
assert element.array is array
assert element.index is index

with pytest.raises(TealTypeError):
abi.ArrayElement(array, Bytes("abc"))
with pytest.raises(pt.TealTypeError):
abi.ArrayElement(array, pt.Bytes("abc"))

with pytest.raises(TealTypeError):
abi.ArrayElement(array, Assert(index))
with pytest.raises(pt.TealTypeError):
abi.ArrayElement(array, pt.Assert(index))


def test_ArrayElement_store_into():
for elementType in STATIC_TYPES + DYNAMIC_TYPES:
staticArrayType = abi.StaticArrayTypeSpec(elementType, 100)
staticArray = staticArrayType.new_instance()
index = Int(9)
index = pt.Int(9)

element = abi.ArrayElement(staticArray, index)
output = elementType.new_instance()
expr = element.store_into(output)

encoded = staticArray.encode()
stride = Int(staticArray.type_spec()._stride())
stride = pt.Int(staticArray.type_spec()._stride())
expectedLength = staticArray.length()
if elementType == abi.BoolTypeSpec():
expectedExpr = cast(abi.Bool, output).decodeBit(encoded, index)
Expand All @@ -88,71 +89,73 @@ def test_ArrayElement_store_into():
else:
expectedExpr = output.decode(
encoded,
startIndex=ExtractUint16(encoded, stride * index),
endIndex=If(index + Int(1) == expectedLength)
.Then(Len(encoded))
.Else(ExtractUint16(encoded, stride * index + Int(2))),
startIndex=pt.ExtractUint16(encoded, stride * index),
endIndex=pt.If(index + pt.Int(1) == expectedLength)
.Then(pt.Len(encoded))
.Else(pt.ExtractUint16(encoded, stride * index + pt.Int(2))),
)

expected, _ = expectedExpr.__teal__(options)
expected.addIncoming()
expected = TealBlock.NormalizeBlocks(expected)
expected = pt.TealBlock.NormalizeBlocks(expected)

actual, _ = expr.__teal__(options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)
actual = pt.TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
with pt.TealComponent.Context.ignoreExprEquality():
assert actual == expected

with pytest.raises(TealInputError):
with pytest.raises(pt.TealInputError):
element.store_into(abi.Tuple(elementType))

for elementType in STATIC_TYPES + DYNAMIC_TYPES:
dynamicArrayType = abi.DynamicArrayTypeSpec(elementType)
dynamicArray = dynamicArrayType.new_instance()
index = Int(9)
index = pt.Int(9)

element = abi.ArrayElement(dynamicArray, index)
output = elementType.new_instance()
expr = element.store_into(output)

encoded = dynamicArray.encode()
stride = Int(dynamicArray.type_spec()._stride())
stride = pt.Int(dynamicArray.type_spec()._stride())
expectedLength = dynamicArray.length()
if elementType == abi.BoolTypeSpec():
expectedExpr = cast(abi.Bool, output).decodeBit(encoded, index + Int(16))
expectedExpr = cast(abi.Bool, output).decodeBit(encoded, index + pt.Int(16))
elif not elementType.is_dynamic():
expectedExpr = output.decode(
encoded, startIndex=stride * index + Int(2), length=stride
encoded, startIndex=stride * index + pt.Int(2), length=stride
)
else:
expectedExpr = output.decode(
encoded,
startIndex=ExtractUint16(encoded, stride * index + Int(2)) + Int(2),
endIndex=If(index + Int(1) == expectedLength)
.Then(Len(encoded))
startIndex=pt.ExtractUint16(encoded, stride * index + pt.Int(2))
+ pt.Int(2),
endIndex=pt.If(index + pt.Int(1) == expectedLength)
.Then(pt.Len(encoded))
.Else(
ExtractUint16(encoded, stride * index + Int(2) + Int(2)) + Int(2)
pt.ExtractUint16(encoded, stride * index + pt.Int(2) + pt.Int(2))
+ pt.Int(2)
),
)

expected, _ = expectedExpr.__teal__(options)
expected.addIncoming()
expected = TealBlock.NormalizeBlocks(expected)
expected = pt.TealBlock.NormalizeBlocks(expected)

actual, _ = expr.__teal__(options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)
actual = pt.TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
with TealComponent.Context.ignoreScratchSlotEquality():
with pt.TealComponent.Context.ignoreExprEquality():
with pt.TealComponent.Context.ignoreScratchSlotEquality():
assert actual == expected

assert TealBlock.MatchScratchSlotReferences(
TealBlock.GetReferencedScratchSlots(actual),
TealBlock.GetReferencedScratchSlots(expected),
assert pt.TealBlock.MatchScratchSlotReferences(
pt.TealBlock.GetReferencedScratchSlots(actual),
pt.TealBlock.GetReferencedScratchSlots(expected),
)

with pytest.raises(TealInputError):
with pytest.raises(pt.TealInputError):
element.store_into(abi.Tuple(elementType))
Loading