Beispiel #1
0
def test_extract_invalid():
    with pytest.raises(pt.TealTypeError):
        pt.Extract(pt.Int(0), pt.Int(0), pt.Int(2))

    with pytest.raises(pt.TealTypeError):
        pt.Extract(pt.Bytes("my string"), pt.Txn.sender(), pt.Int(2))

    with pytest.raises(pt.TealTypeError):
        pt.Extract(pt.Bytes("my string"), pt.Int(0), pt.Txn.sender())
Beispiel #2
0
 def generate_expr() -> pt.Expr:
     match op:
         case pt.Op.extract3:
             return pt.Extract(args[0], args[1], args[2])
         case pt.Op.substring3:
             return pt.Substring(args[0], args[1], args[2])
         case _:
             raise Exception(f"Unsupported {op=}")
Beispiel #3
0
def test_extract_immediate():
    args = [pt.Bytes("my string"), pt.Int(0), pt.Int(2)]
    expr = pt.Extract(args[0], args[1], args[2])
    assert expr.type_of() == pt.TealType.bytes

    expected = pt.TealSimpleBlock(
        [
            pt.TealOp(args[0], pt.Op.byte, '"my string"'),
            pt.TealOp(expr, pt.Op.extract, 0, 2),
        ]
    )

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

    assert actual == expected

    with pytest.raises(pt.TealInputError):
        expr.__teal__(avm4Options)
Beispiel #4
0
def test_extract_stack():
    my_string = "*" * 257
    args = [pt.Bytes(my_string), pt.Int(256), pt.Int(257)]
    expr = pt.Extract(args[0], args[1], args[2])
    assert expr.type_of() == pt.TealType.bytes

    expected = pt.TealSimpleBlock(
        [
            pt.TealOp(args[0], pt.Op.byte, '"{my_string}"'.format(my_string=my_string)),
            pt.TealOp(args[1], pt.Op.int, 256),
            pt.TealOp(args[2], pt.Op.int, 257),
            pt.TealOp(expr, pt.Op.extract3),
        ]
    )

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

    assert actual == expected

    with pytest.raises(pt.TealInputError):
        expr.__teal__(avm4Options)
Beispiel #5
0
def test_substringForDecoding():
    class SubstringTest(NamedTuple):
        start_index: Optional[pt.Expr]
        end_index: Optional[pt.Expr]
        length: Optional[pt.Expr]
        expected: Union[pt.Expr, Any]

    encoded = pt.Bytes("encoded")

    tests: List[SubstringTest] = [
        SubstringTest(start_index=None,
                      end_index=None,
                      length=None,
                      expected=encoded),
        SubstringTest(
            start_index=None,
            end_index=None,
            length=pt.Int(4),
            expected=pt.Extract(encoded, pt.Int(0), pt.Int(4)),
        ),
        SubstringTest(
            start_index=None,
            end_index=pt.Int(4),
            length=None,
            expected=pt.Substring(encoded, pt.Int(0), pt.Int(4)),
        ),
        SubstringTest(
            start_index=None,
            end_index=pt.Int(4),
            length=pt.Int(5),
            expected=pt.TealInputError,
        ),
        SubstringTest(
            start_index=pt.Int(4),
            end_index=None,
            length=None,
            expected=pt.Suffix(encoded, pt.Int(4)),
        ),
        SubstringTest(
            start_index=pt.Int(4),
            end_index=None,
            length=pt.Int(5),
            expected=pt.Extract(encoded, pt.Int(4), pt.Int(5)),
        ),
        SubstringTest(
            start_index=pt.Int(4),
            end_index=pt.Int(5),
            length=None,
            expected=pt.Substring(encoded, pt.Int(4), pt.Int(5)),
        ),
        SubstringTest(
            start_index=pt.Int(4),
            end_index=pt.Int(5),
            length=pt.Int(6),
            expected=pt.TealInputError,
        ),
    ]

    for i, test in enumerate(tests):
        if not isinstance(test.expected, pt.Expr):
            with pytest.raises(test.expected):
                substring_for_decoding(
                    encoded,
                    start_index=test.start_index,
                    end_index=test.end_index,
                    length=test.length,
                )
            continue

        expr = substring_for_decoding(
            encoded,
            start_index=test.start_index,
            end_index=test.end_index,
            length=test.length,
        )
        assert expr.type_of() == pt.TealType.bytes
        assert not expr.has_return()

        expected, _ = cast(pt.Expr, test.expected).__teal__(options)
        expected.addIncoming()
        expected = pt.TealBlock.NormalizeBlocks(expected)

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

        with pt.TealComponent.Context.ignoreExprEquality():
            assert actual == expected, "Test at index {} failed".format(i)