44
55use simplex:: program:: { Program , WitnessTrait } ;
66use simplex:: simplicityhl:: elements:: Script ;
7- use simplex:: transaction:: { FinalTransaction , PartialInput , ProgramInput , RequiredSignature } ;
7+ use simplex:: transaction:: {
8+ FinalTransaction , PartialInput , PartialOutput , ProgramInput , RequiredSignature ,
9+ } ;
810
911#[ derive( Clone , Copy ) ]
1012pub enum Expect {
@@ -41,13 +43,14 @@ pub fn fund(
4143 Ok ( script)
4244}
4345
44- /// Spend the funded UTXO with `witness`. Returns the broadcast result .
45- pub fn spend < W > (
46+ /// Construct the funded UTXO with `witness`.
47+ pub fn construct_final_tx < W > (
4648 context : & simplex:: TestContext ,
4749 program : & impl AsRef < Program > ,
4850 script : & Script ,
4951 witness : W ,
50- ) -> anyhow:: Result < String >
52+ data : Option < & [ u8 ] > ,
53+ ) -> anyhow:: Result < FinalTransaction >
5154where
5255 W : WitnessTrait + ' static ,
5356{
@@ -62,22 +65,34 @@ where
6265 RequiredSignature :: None ,
6366 ) ;
6467
65- Ok ( context. get_default_signer ( ) . broadcast ( & ft) ?. to_string ( ) )
68+ if let Some ( data) = data {
69+ ft. add_output ( PartialOutput :: new_metadata ( data) )
70+ } ;
71+
72+ Ok ( ft)
6673}
6774
68- /// Fund + spend + assert the outcome .
69- pub fn run < W > (
75+ /// Spend the funded UTXO with `witness`. Return the broadcast result .
76+ pub fn spend < W > (
7077 context : & simplex:: TestContext ,
71- program : impl AsRef < Program > ,
78+ program : & impl AsRef < Program > ,
79+ script : & Script ,
7280 witness : W ,
73- expect : Expect ,
74- ) -> anyhow:: Result < ( ) >
81+ data : Option < & [ u8 ] > ,
82+ ) -> anyhow:: Result < String >
7583where
7684 W : WitnessTrait + ' static ,
7785{
78- let script = fund ( context, & program) ?;
79- let result = spend ( context, & program, & script, witness) ;
86+ let ft = construct_final_tx ( context, program, script, witness, data) ?;
87+
88+ Ok ( context. get_default_signer ( ) . broadcast ( & ft) ?. to_string ( ) )
89+ }
8090
91+ /// Assert that the test result is as expected.
92+ pub fn assert_error_msg (
93+ result : Result < String , anyhow:: Error > ,
94+ expect : Expect ,
95+ ) -> anyhow:: Result < ( ) > {
8196 match expect. error_message ( ) {
8297 None => {
8398 result?;
@@ -88,7 +103,41 @@ where
88103 . to_string ( ) ;
89104 assert ! ( err. contains( expected) ) ;
90105 }
91- }
106+ } ;
92107
93108 Ok ( ( ) )
94109}
110+
111+ /// Fund + spend + assert the outcome.
112+ pub fn run < W > (
113+ context : & simplex:: TestContext ,
114+ program : impl AsRef < Program > ,
115+ witness : W ,
116+ expect : Expect ,
117+ ) -> anyhow:: Result < ( ) >
118+ where
119+ W : WitnessTrait + ' static ,
120+ {
121+ let script = fund ( context, & program) ?;
122+ let result = spend ( context, & program, & script, witness, None ) ;
123+
124+ assert_error_msg ( result, expect)
125+ }
126+
127+ /// Fund + spend + assert the outcome.
128+ /// Tx has OP_RETURN data metadata output
129+ pub fn run_with_op_return < W > (
130+ context : & simplex:: TestContext ,
131+ program : impl AsRef < Program > ,
132+ witness : W ,
133+ expect : Expect ,
134+ data : & [ u8 ] ,
135+ ) -> anyhow:: Result < ( ) >
136+ where
137+ W : WitnessTrait + ' static ,
138+ {
139+ let script = fund ( context, & program) ?;
140+ let result = spend ( context, & program, & script, witness, Some ( data) ) ;
141+
142+ assert_error_msg ( result, expect)
143+ }
0 commit comments