Example #1
0
def test_StaticBytes_expr(test_case: bytes | bytearray):
    value: abi.StaticBytes = abi.StaticBytes(
        abi.StaticBytesTypeSpec(len(test_case) * 2))
    set_expr = pt.Concat(pt.Bytes(test_case), pt.Bytes(test_case))

    expr = value.set(set_expr)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

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

    expected = pt.TealSimpleBlock([
        pt.TealOp(None, pt.Op.byte, "0x" + BYTE_HEX_TEST_CASE),
        pt.TealOp(None, pt.Op.byte, "0x" + BYTE_HEX_TEST_CASE),
        pt.TealOp(None, pt.Op.concat),
        pt.TealOp(None, pt.Op.store, value.stored_value.slot),
        pt.TealOp(None, pt.Op.int, 32),
        pt.TealOp(None, pt.Op.load, value.stored_value.slot),
        pt.TealOp(None, pt.Op.len),
        pt.TealOp(None, pt.Op.eq),
        pt.TealOp(None, pt.Op.assert_),
    ])

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected
Example #2
0
def test_StaticArrayTypeSpec_eq():
    for elementType in STATIC_TYPES + DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert staticArrayType == staticArrayType
            assert staticArrayType != abi.StaticArrayTypeSpec(
                elementType, length + 1)
            assert staticArrayType != abi.StaticArrayTypeSpec(
                abi.TupleTypeSpec(elementType), length)

    for length in range(256):
        staticBytesType = abi.StaticBytesTypeSpec(length)
        assert staticBytesType == staticBytesType
        assert staticBytesType != abi.StaticBytesTypeSpec(length + 1)
        assert staticBytesType != abi.StaticArrayTypeSpec(
            abi.TupleTypeSpec(abi.ByteTypeSpec()), length)
Example #3
0
def test_StaticArrayTypeSpec_init():
    for elementType in STATIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert staticArrayType.value_type_spec() is elementType
            assert not staticArrayType.is_length_dynamic()
            assert staticArrayType._stride() == elementType.byte_length_static(
            )
            assert staticArrayType.length_static() == length

        with pytest.raises(TypeError):
            abi.StaticArrayTypeSpec(elementType, -1)

    for length in range(256):
        staticBytesType = abi.StaticBytesTypeSpec(length)
        assert isinstance(staticBytesType.value_type_spec(), abi.ByteTypeSpec)
        assert not staticBytesType.is_length_dynamic()
        assert staticBytesType._stride() == 1
        assert staticBytesType.length_static() == length

    for elementType in DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert staticArrayType.value_type_spec() is elementType
            assert not staticArrayType.is_length_dynamic()
            assert staticArrayType._stride() == 2
            assert staticArrayType.length_static() == length

        with pytest.raises(TypeError):
            abi.StaticArrayTypeSpec(elementType, -1)
Example #4
0
def test_StaticArrayTypeSpec_byte_length_static():
    for elementType in STATIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            actual = staticArrayType.byte_length_static()

            if elementType == abi.BoolTypeSpec():
                expected = _bool_sequence_length(length)
            else:
                expected = elementType.byte_length_static() * length

            assert (actual == expected
                    ), "failed with element type {} and length {}".format(
                        elementType, length)

    for length in range(256):
        staticBytesType = abi.StaticBytesTypeSpec(length)
        actual = staticBytesType.byte_length_static()
        assert (
            actual == length
        ), f"failed with element type {staticBytesType.value_type_spec()} and length {length}"

    for elementType in DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            with pytest.raises(ValueError):
                staticArrayType.byte_length_static()
Example #5
0
def test_StaticArrayTypeSpec_str():
    for elementType in STATIC_TYPES + DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert str(staticArrayType) == "{}[{}]".format(elementType, length)

    for length in range(256):
        assert str(abi.StaticBytesTypeSpec(length)) == f"byte[{length}]"
Example #6
0
def test_StaticArrayTypeSpec_is_dynamic():
    for elementType in STATIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert not staticArrayType.is_dynamic()

    for length in range(256):
        assert not abi.StaticBytesTypeSpec(length).is_dynamic()

    for elementType in DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert staticArrayType.is_dynamic()
Example #7
0
def test_StaticArrayTypeSpec_new_instance():
    for elementType in STATIC_TYPES + DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            instance = staticArrayType.new_instance()
            assert isinstance(
                instance,
                abi.StaticArray,
            )
            assert instance.type_spec() == staticArrayType

    for length in range(256):
        staticBytesType = abi.StaticBytesTypeSpec(length)
        instance = staticBytesType.new_instance()
        assert isinstance(instance, abi.StaticBytes)
        assert instance.type_spec() == staticBytesType
Example #8
0
def test_StaticBytes_set_py_bytes(test_case: bytes | bytearray):
    value: abi.StaticBytes = abi.StaticBytes(
        abi.StaticBytesTypeSpec(len(test_case)))

    expr = value.set(test_case)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

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

    expected = pt.TealSimpleBlock([
        pt.TealOp(None, pt.Op.byte, "0x" + BYTE_HEX_TEST_CASE),
        pt.TealOp(None, pt.Op.store, value.stored_value.slot),
    ])

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

    with pytest.raises(pt.TealInputError):
        value.set(test_case[:-1])