123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2021-2022 Trili Tech, <contact@trili.tech> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openProtocolleterror~locvf=letopenLwt_result_syntaxinmatchvwith|ErrorerrwhenList.existsferr->return_unit|Ok_->failwith"Unexpected successful result (%s)"loc|Errorerr->failwith"@[Unexpected error (%s): %a@]"locpp_print_traceerrletjoin_errorse1e2=letopenLwt_result_syntaxinmatch(e1,e2)with|Ok(),Ok()->return_unit|Errore,Ok()|Ok(),Errore->faile|Errore1,Errore2->fail(e1@e2)lettest_error_encodingse=letmoduleE=Environment.Error_monadinignore(E.ppFormat.str_formattere);lete'=E.json_of_errore|>E.error_of_jsoninassert(e=e')letproto_error~locvf=error~locv(function|Environment.Ecoproto_errorerr->test_error_encodingserr;ferr|_->false)letproto_error_with_info?(error_info_field=`Title)~locvexpected_error_info=letinfoerr=leti=Error_monad.find_info_of_error(Environment.wrap_tzerrorerr)inmatcherror_info_fieldwith|`Title->i.title|`Id->i.id|`Description->i.description|`Message->Format.asprintf"%a"Environment.Error_monad.pperrinproto_error~locv(functionerr->Format.printf"@[<v 4>THE ERROR IS: %s@,EXPECTED: %s@]@."(infoerr)expected_error_info;letinfo=infoerrinString.equalinfoexpected_error_info)letequal~loc(cmp:'a->'a->bool)msgppab=letopenLwt_result_syntaxinifnot(cmpab)thenfailwith"@[@[[%s]@] - @[%s : %a is not equal to %a@]@]"locmsgppappbelsereturn_unitletleq~loc(cmp:'a->'a->int)msgppab=letopenLwt_result_syntaxinifcmpab>0thenfailwith"@[@[[%s]@] - @[%s : %a is not less or equal to %a@]@]"locmsgppappbelsereturn_unitletlt~loc(cmp:'a->'a->int)msgppab=letopenLwt_result_syntaxinifcmpab>=0thenfailwith"@[@[[%s]@] - @[%s : %a is not less than %a@]@]"locmsgppappbelsereturn_unitletnot_equal~loc(cmp:'a->'a->bool)msgppab=letopenLwt_result_syntaxinifcmpabthenfailwith"@[@[[%s]@] - @[%s : %a is equal to %a@]@]"locmsgppappbelsereturn_unitmoduleInt32=structincludeInt32letppppv=Format.pp_print_intpp(Int32.to_intv)endmoduleInt64=structincludeInt64letppppv=Format.pp_print_intpp(Int64.to_intv)end(* char *)letequal_char~locab=equal~locChar.equal"Characters aren't equal"Format.pp_print_charab(* int *)letequal_int~loc(a:int)(b:int)=equal~locInt.equal"Integers aren't equal"Format.pp_print_intabletnot_equal_int~loc(a:int)(b:int)=not_equal~locInt.equal"Integers are equal"Format.pp_print_intabletleq_int~loc(a:int)(b:int)=leq~locCompare.Int.compare"Integer comparison"Format.pp_print_intab(* int32 *)letequal_int32~loc(a:int32)(b:int32)=equal~locInt32.equal"Int32 aren't equal"Int32.ppabletleq_int32~loc(a:int32)(b:int32)=leq~locCompare.Int32.compare"Int32 comparison"Int32.ppabletlt_int32~loc(a:int32)(b:int32)=lt~locCompare.Int32.compare"Int32 comparison"Int32.ppab(* int64 *)letequal_int64~loc(a:int64)(b:int64)=equal~locCompare.Int64.(=)"Int64 aren't equal"Int64.ppabletnot_equal_int64~loc(a:int64)(b:int64)=not_equal~locInt64.equal"Int64 are equal"Int64.ppabletleq_int64~loc(a:int64)(b:int64)=leq~locCompare.Int64.compare"Int64 comparison"Int64.ppabletequal_z~loc(a:Z.t)(b:Z.t)=equal~locCompare.Z.(=)"Z are not equal"Z.pp_printabletequal_q~loc(a:Q.t)(b:Q.t)=equal~locCompare.Q.(=)"Q are not equal"Q.pp_printab(* bool *)letequal_bool~loc(a:bool)(b:bool)=equal~locBool.equal"Booleans aren't equal"Format.pp_print_boolabletnot_equal_bool~loc(a:bool)(b:bool)=not_equal~locBool.equal"Booleans are equal"Format.pp_print_boolabletis_true~loc(a:bool)=equal~locBool.equal"Boolean is not true"Format.pp_print_boolatrue(* string *)letequal_string~loc(a:string)(b:string)=equal~locString.equal"Strings aren't equal"Format.pp_print_stringabletnot_equal_string~loc(a:string)(b:string)=not_equal~locString.equal"Strings are equal"Format.pp_print_stringab(* tez *)letequal_tez~loc(a:Alpha_context.Tez.t)(b:Alpha_context.Tez.t)=letopenAlpha_contextinequal~locTez.(=)"Tez aren't equal"Tez.ppabletnot_equal_tez~loc(a:Alpha_context.Tez.t)(b:Alpha_context.Tez.t)=letopenAlpha_contextinnot_equal~locTez.(=)"Tez are equal"Tez.ppab(* pkh *)letequal_pkh~loc(a:Signature.Public_key_hash.t)(b:Signature.Public_key_hash.t)=letmodulePKH=Signature.Public_key_hashinequal~locPKH.equal"Public key hashes aren't equal"PKH.ppabletnot_equal_pkh~loc(a:Signature.Public_key_hash.t)(b:Signature.Public_key_hash.t)=letmodulePKH=Signature.Public_key_hashinnot_equal~locPKH.equal"Public key hashes are equal"PKH.ppab(* protocol hash *)letequal_protocol_hash~loc(a:Protocol_hash.t)(b:Protocol_hash.t)=equal~locProtocol_hash.equal"Protocol hashes aren't equal"Protocol_hash.ppabletnot_equal_protocol_hash~loc(a:Protocol_hash.t)(b:Protocol_hash.t)=not_equal~locProtocol_hash.equal"Protocol hashes are equal"Protocol_hash.ppabletget_some~loc=letopenLwt_result_syntaxinfunctionSomex->returnx|None->failwith"Unexpected None (%s)"locletis_none~loc~pp=letopenLwt_result_syntaxinfunction|Somex->failwith"Unexpected (Some %a) (%s)"ppxloc|None->return_unitletequal_result~loc~pp_ok~pp_erroreq_okeq_errorab=equal~loc(Result.equal~ok:eq_ok~error:eq_error)"Results are not equal"(Format.pp_print_result~ok:pp_ok~error:pp_error)abletis_error~loc~pp=letopenLwt_result_syntaxinfunction|Okx->failwith"Unexpected (Ok %a) (%s)"ppxloc|Error_->return_unitletget_ok~__LOC__=letopenLwt_result_syntaxinfunction|Okr->returnr|Errorerr->failwith"@[Unexpected error (%s): %a@]"__LOC__pp_print_traceerropenContext(* Some asserts for account operations *)letcontract_property_isproperty~locbcontractexpected=letopenLwt_result_syntaxinlet*balance=propertybcontractinequal_tez~locbalanceexpected(** [balance_is b c amount] checks that the current balance [b] of contract [c]
is [amount].
*)letbalance_is=contract_property_isContract.balance(** [frozen_bonds_is b c amount] checks that the current frozen bonds of
contract [c] is [amount].
*)letfrozen_bonds_is=contract_property_isContract.frozen_bondsletbalance_or_frozen_bonds_was_operated~is_balance~operand~locbcontractold_balanceamount=letopenLwt_result_wrap_syntaxinlet*?@expected=operandold_balanceamountinletf=ifis_balancethenbalance_iselsefrozen_bonds_isinf~locbcontractexpected(** [balance_was_credited ~loc ctxt contract old_balance amount] checks
that [contract]'s balance was credited [amount] tez in comparison to
[old_balance].
*)letbalance_was_credited=balance_or_frozen_bonds_was_operated~is_balance:true~operand:Alpha_context.Tez.(+?)(** [balance_was_credited ~loc ctxt contract old_balance amount] checks
that [contract]'s balance was debited [amount] tez in comparison to
[old_balance].
*)letbalance_was_debited=balance_or_frozen_bonds_was_operated~is_balance:true~operand:Alpha_context.Tez.(-?)(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks
that [contract]'s frozen bonds was credited [amount] tez in comparison to
[old_balance].
*)letfrozen_bonds_was_credited=balance_or_frozen_bonds_was_operated~is_balance:false~operand:Alpha_context.Tez.(+?)(** [frozen_bonds_was_credited ~loc ctxt contract old_balance amount] checks
that [contract]'s frozen bonds was credited [amount] tez in comparison to
[old_balance].
*)letfrozen_bonds_was_debited=balance_or_frozen_bonds_was_operated~is_balance:false~operand:Alpha_context.Tez.(-?)letpp_print_listppoutxs=letlist_ppfmt=Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt";@.")fmtinFormat.fprintfout"[%a]"(list_pppp)xsletassert_equal_list~loceqmsgpp=equal~loc(List.equaleq)msg(pp_print_listpp)letassert_equal_list_opt~loceqmsgpp=equal~loc(Option.equal(List.equaleq))msg(Format.pp_print_option(pp_print_listpp))(** Checks that both lists have the same elements, not taking the
order of these elements into account, but taking their
multiplicity into account. *)letequal_list_any_order~loc~comparemsgpplist1list2=letordered_list1=List.sortcomparelist1inletordered_list2=List.sortcomparelist2inequal~loc(List.equal(funab->compareab=0))msg(pp_print_listpp)ordered_list1ordered_list2letto_json_stringencodingx=x|>Data_encoding.Json.constructencoding|>Format.asprintf"\n%a\n"Data_encoding.Json.ppletequal_with_encoding~locencodingab=equal_string~loc(to_json_stringencodinga)(to_json_stringencodingb)letnot_equal_with_encoding~locencodingab=not_equal_string~loc(to_json_stringencodinga)(to_json_stringencodingb)