def test_build_program_clear_state_invalid_config(): for config in (pt.CallConfig.CREATE, pt.CallConfig.ALL): bareCalls = pt.BareCallActions( clear_state=pt.OnCompleteAction(action=pt.Approve(), call_config=config) ) with pytest.raises( pt.TealInputError, match=r"Only CallConfig.CALL or CallConfig.NEVER are valid for a clear state CallConfig, since clear state can never be invoked during creation$", ): pt.Router("test", bareCalls) router = pt.Router("test") @pt.ABIReturnSubroutine def clear_state_method(): return pt.Approve() with pytest.raises( pt.TealInputError, match=r"Only CallConfig.CALL or CallConfig.NEVER are valid for a clear state CallConfig, since clear state can never be invoked during creation$", ): router.add_method_handler( clear_state_method, method_config=pt.MethodConfig(clear_state=config), )
def test_build_program_clear_state_valid_config(): action = pt.If(pt.Txn.fee() == pt.Int(4)).Then(pt.Approve()).Else(pt.Reject()) config = pt.CallConfig.CALL router_with_bare_call = pt.Router( "test", pt.BareCallActions( clear_state=pt.OnCompleteAction(action=action, call_config=config) ), ) _, actual_clear_state_with_bare_call, _ = router_with_bare_call.build_program() expected_clear_state_with_bare_call = assemble_helper( pt.Cond([pt.Txn.application_args.length() == pt.Int(0), action]) ) with pt.TealComponent.Context.ignoreExprEquality(): assert ( assemble_helper(actual_clear_state_with_bare_call) == expected_clear_state_with_bare_call ) router_with_method = pt.Router("test") @pt.ABIReturnSubroutine def clear_state_method(): return action router_with_method.add_method_handler( clear_state_method, method_config=pt.MethodConfig(clear_state=config) ) _, actual_clear_state_with_method, _ = router_with_method.build_program() expected_clear_state_with_method = assemble_helper( pt.Cond( [ pt.Txn.application_args[0] == pt.MethodSignature("clear_state_method()void"), pt.Seq(clear_state_method(), pt.Approve()), ] ) ) with pt.TealComponent.Context.ignoreExprEquality(): assert ( assemble_helper(actual_clear_state_with_method) == expected_clear_state_with_method )
def test_build_program_clear_state_empty(): router = pt.Router( "test", pt.BareCallActions(no_op=pt.OnCompleteAction.always(pt.Approve())) ) approval, clear_state, contract = router.build_program() expected_empty_program = pt.TealSimpleBlock( [ pt.TealOp(None, pt.Op.int, 0), pt.TealOp(None, pt.Op.return_), ] ) with pt.TealComponent.Context.ignoreExprEquality(): assert assemble_helper(approval) != expected_empty_program assert assemble_helper(clear_state) == expected_empty_program expected_contract = sdk_abi.Contract("test", []) assert contract == expected_contract
def test_contract_json_obj(): abi_subroutines = list( filter(lambda x: isinstance(x, pt.ABIReturnSubroutine), GOOD_SUBROUTINE_CASES) ) contract_name = "contract_name" on_complete_actions = pt.BareCallActions( clear_state=pt.OnCompleteAction.call_only(safe_clear_state_delete) ) router = pt.Router(contract_name, on_complete_actions) method_list: list[sdk_abi.Method] = [] for subroutine in abi_subroutines: doc = subroutine.subroutine.implementation.__doc__ desc = None if doc is not None and doc.strip() == "replace me": desc = "dope description" router.add_method_handler(subroutine, description=desc) ms = subroutine.method_spec() # Manually replace it since the override is applied in the method handler # not attached to the ABIReturnSubroutine itself ms.desc = desc if desc is not None else ms.desc sig_method = sdk_abi.Method.from_signature(subroutine.method_signature()) assert ms.name == sig_method.name for idx, arg in enumerate(ms.args): assert arg.type == sig_method.args[idx].type method_list.append(ms) sdk_contract = sdk_abi.Contract(contract_name, method_list) contract = router.contract_construct() assert contract == sdk_contract