|
| 1 | +import pytest |
| 2 | + |
| 3 | +import pyteal as pt |
| 4 | + |
| 5 | +teal6Options = pt.CompileOptions(version=6) |
| 6 | +teal7Options = pt.CompileOptions(version=7) |
| 7 | + |
| 8 | + |
| 9 | +def test_replace_immediate(): |
| 10 | + args = [pt.Bytes("my string"), pt.Int(0), pt.Bytes("abcdefghi")] |
| 11 | + expr = pt.Replace(args[0], args[1], args[2]) |
| 12 | + assert expr.type_of() == pt.TealType.bytes |
| 13 | + |
| 14 | + expected = pt.TealSimpleBlock( |
| 15 | + [ |
| 16 | + pt.TealOp(args[0], pt.Op.byte, '"my string"'), |
| 17 | + pt.TealOp(args[2], pt.Op.byte, '"abcdefghi"'), |
| 18 | + pt.TealOp(expr, pt.Op.replace2, 0), |
| 19 | + ] |
| 20 | + ) |
| 21 | + |
| 22 | + actual, _ = expr.__teal__(teal7Options) |
| 23 | + actual.addIncoming() |
| 24 | + actual = pt.TealBlock.NormalizeBlocks(actual) |
| 25 | + |
| 26 | + assert actual == expected |
| 27 | + |
| 28 | + with pytest.raises(pt.TealInputError): |
| 29 | + expr.__teal__(teal6Options) |
| 30 | + |
| 31 | + |
| 32 | +def test_replace_stack_int(): |
| 33 | + my_string = "*" * 257 |
| 34 | + args = [pt.Bytes(my_string), pt.Int(256), pt.Bytes("ab")] |
| 35 | + expr = pt.Replace(args[0], args[1], args[2]) |
| 36 | + assert expr.type_of() == pt.TealType.bytes |
| 37 | + |
| 38 | + expected = pt.TealSimpleBlock( |
| 39 | + [ |
| 40 | + pt.TealOp(args[0], pt.Op.byte, '"{my_string}"'.format(my_string=my_string)), |
| 41 | + pt.TealOp(args[1], pt.Op.int, 256), |
| 42 | + pt.TealOp(args[2], pt.Op.byte, '"ab"'), |
| 43 | + pt.TealOp(expr, pt.Op.replace3), |
| 44 | + ] |
| 45 | + ) |
| 46 | + |
| 47 | + actual, _ = expr.__teal__(teal7Options) |
| 48 | + actual.addIncoming() |
| 49 | + actual = pt.TealBlock.NormalizeBlocks(actual) |
| 50 | + |
| 51 | + assert actual == expected |
| 52 | + |
| 53 | + with pytest.raises(pt.TealInputError): |
| 54 | + expr.__teal__(teal6Options) |
| 55 | + |
| 56 | + |
| 57 | +# Mirrors `test_replace_stack_int`, but attempts replacement with start != pt.Int. |
| 58 | +def test_replace_stack_not_int(): |
| 59 | + my_string = "*" * 257 |
| 60 | + add = pt.Add(pt.Int(254), pt.Int(2)) |
| 61 | + args = [pt.Bytes(my_string), add, pt.Bytes("ab")] |
| 62 | + expr = pt.Replace(args[0], args[1], args[2]) |
| 63 | + assert expr.type_of() == pt.TealType.bytes |
| 64 | + |
| 65 | + expected = pt.TealSimpleBlock( |
| 66 | + [ |
| 67 | + pt.TealOp(args[0], pt.Op.byte, '"{my_string}"'.format(my_string=my_string)), |
| 68 | + pt.TealOp(pt.Int(254), pt.Op.int, 254), |
| 69 | + pt.TealOp(pt.Int(2), pt.Op.int, 2), |
| 70 | + pt.TealOp(add, pt.Op.add), |
| 71 | + pt.TealOp(args[2], pt.Op.byte, '"ab"'), |
| 72 | + pt.TealOp(expr, pt.Op.replace3), |
| 73 | + ] |
| 74 | + ) |
| 75 | + |
| 76 | + actual, _ = expr.__teal__(teal7Options) |
| 77 | + actual.addIncoming() |
| 78 | + actual = pt.TealBlock.NormalizeBlocks(actual) |
| 79 | + |
| 80 | + with pt.TealComponent.Context.ignoreExprEquality(): |
| 81 | + assert actual == expected |
| 82 | + |
| 83 | + with pytest.raises(pt.TealInputError): |
| 84 | + expr.__teal__(teal6Options) |
| 85 | + |
| 86 | + |
| 87 | +def test_replace_invalid(): |
| 88 | + with pytest.raises(pt.TealTypeError): |
| 89 | + pt.Replace(pt.Bytes("my string"), pt.Int(0), pt.Int(1)) |
| 90 | + |
| 91 | + with pytest.raises(pt.TealTypeError): |
| 92 | + pt.Replace( |
| 93 | + pt.Bytes("my string"), pt.Bytes("should be int"), pt.Bytes("abcdefghi") |
| 94 | + ) |
| 95 | + |
| 96 | + with pytest.raises(pt.TealTypeError): |
| 97 | + pt.Replace(pt.Bytes("my string"), pt.Txn.sender(), pt.Bytes("abcdefghi")) |
0 commit comments