def test_String_set_expr(): for value_to_set in (pt.Bytes("hi"), pt.Bytes("base16", "0xdeadbeef")): value = abi.String() expr = value.set(value_to_set) assert expr.type_of() == pt.TealType.none assert not expr.has_return() value_start, value_end = value_to_set.__teal__(options) expected_body = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.store, value.stored_value.slot), pt.TealOp(None, pt.Op.load, value.stored_value.slot), pt.TealOp(None, pt.Op.len), pt.TealOp(None, pt.Op.itob), pt.TealOp(None, pt.Op.extract, 6, 0), pt.TealOp(None, pt.Op.load, value.stored_value.slot), pt.TealOp(None, pt.Op.concat), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) value_end.setNextBlock(expected_body) expected = value_start 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
def test_String_encode(): value = abi.String() expr = value.encode() assert expr.type_of() == pt.TealType.bytes assert expr.has_return() is False expected = pt.TealSimpleBlock( [pt.TealOp(expr, pt.Op.load, value.stored_value.slot)]) actual, _ = expr.__teal__(options) assert actual == expected
def test_String_set_copy(): value = abi.String() other = abi.String() expr = value.set(other) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.load, other.stored_value.slot), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): value.set(abi.Address())
def test_String_get(): value = abi.String() expr = value.get() assert expr.type_of() == pt.TealType.bytes assert expr.has_return() is False expected = pt.TealSimpleBlock([ pt.TealOp(expr, pt.Op.load, value.stored_value.slot), pt.TealOp(None, pt.Op.extract, 2, 0), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_String_set_static(value_to_set, value_encoded): value = abi.String() expr = value.set(value_to_set) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, "0x" + value_encoded.hex()), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_String_set_computed(): bv = pt.Bytes("base16", "0x0004DEADBEEF") computed_value = ContainerType(abi.StringTypeSpec(), bv) value = abi.String() expr = value.set(computed_value) assert expr.type_of() == pt.TealType.none assert not expr.has_return() _, byte_ops = bv.__teal__(options) expected = pt.TealSimpleBlock([ byte_ops.ops[0], pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): value.set(ContainerType(abi.ByteTypeSpec(), pt.Int(0x01)))
def test_String_set_static_invalid(): with pytest.raises(pt.TealInputError): abi.String().set(42)