def test_substring_invalid(): with pytest.raises(pt.TealTypeError): pt.Substring(pt.Int(0), pt.Int(0), pt.Int(2)) with pytest.raises(pt.TealTypeError): pt.Substring(pt.Bytes("my string"), pt.Txn.sender(), pt.Int(2)) with pytest.raises(pt.TealTypeError): pt.Substring(pt.Bytes("my string"), pt.Int(0), pt.Txn.sender()) with pytest.raises(Exception): pt.Substring(pt.Bytes("my string"), pt.Int(1), pt.Int(0)).__teal__(avm5Options)
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=}")
def test_substring_immediate_v5(): args = [pt.Bytes("my string"), pt.Int(1), pt.Int(2)] expr = pt.Substring(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, 1, 1), ] ) actual, _ = expr.__teal__(avm5Options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) assert actual == expected
def test_substring_to_extract(): my_string = "a" * 257 args = [pt.Bytes(my_string), pt.Int(255), pt.Int(257)] expr = pt.Substring(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(expr, pt.Op.extract, 255, 2), ] ) actual, _ = expr.__teal__(avm5Options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
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)