-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathtest_macros_tests.rs
More file actions
343 lines (343 loc) · 11.4 KB
/
test_macros_tests.rs
File metadata and controls
343 lines (343 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#![feature(prelude_import)]
#![no_std]
#[macro_use]
extern crate core;
#[prelude_import]
use core::prelude::rust_2021::*;
use proc_macros::{parse_item_fn, parse_item_impl};
use soroban_sdk::{contract, contractimpl};
pub struct Contract;
///ContractArgs is a type for building arg lists for functions defined in "Contract".
pub struct ContractArgs;
///ContractClient is a client for calling the contract defined in "Contract".
pub struct ContractClient<'a> {
pub env: soroban_sdk::Env,
pub address: soroban_sdk::Address,
#[doc(hidden)]
set_auths: Option<&'a [soroban_sdk::xdr::SorobanAuthorizationEntry]>,
#[doc(hidden)]
mock_auths: Option<&'a [soroban_sdk::testutils::MockAuth<'a>]>,
#[doc(hidden)]
mock_all_auths: bool,
#[doc(hidden)]
allow_non_root_auth: bool,
}
impl<'a> ContractClient<'a> {
pub fn new(env: &soroban_sdk::Env, address: &soroban_sdk::Address) -> Self {
Self {
env: env.clone(),
address: address.clone(),
set_auths: None,
mock_auths: None,
mock_all_auths: false,
allow_non_root_auth: false,
}
}
/// Set authorizations in the environment which will be consumed by
/// contracts when they invoke `Address::require_auth` or
/// `Address::require_auth_for_args` functions.
///
/// Requires valid signatures for the authorization to be successful.
/// To mock auth without requiring valid signatures, use `mock_auths`.
///
/// See `soroban_sdk::Env::set_auths` for more details and examples.
pub fn set_auths(&self, auths: &'a [soroban_sdk::xdr::SorobanAuthorizationEntry]) -> Self {
Self {
env: self.env.clone(),
address: self.address.clone(),
set_auths: Some(auths),
mock_auths: self.mock_auths.clone(),
mock_all_auths: false,
allow_non_root_auth: false,
}
}
/// Mock authorizations in the environment which will cause matching invokes
/// of `Address::require_auth` and `Address::require_auth_for_args` to
/// pass.
///
/// See `soroban_sdk::Env::set_auths` for more details and examples.
pub fn mock_auths(&self, mock_auths: &'a [soroban_sdk::testutils::MockAuth<'a>]) -> Self {
Self {
env: self.env.clone(),
address: self.address.clone(),
set_auths: self.set_auths.clone(),
mock_auths: Some(mock_auths),
mock_all_auths: false,
allow_non_root_auth: false,
}
}
/// Mock all calls to the `Address::require_auth` and
/// `Address::require_auth_for_args` functions in invoked contracts,
/// having them succeed as if authorization was provided.
///
/// See `soroban_sdk::Env::mock_all_auths` for more details and
/// examples.
pub fn mock_all_auths(&self) -> Self {
Self {
env: self.env.clone(),
address: self.address.clone(),
set_auths: None,
mock_auths: None,
mock_all_auths: true,
allow_non_root_auth: false,
}
}
/// A version of `mock_all_auths` that allows authorizations that
/// are not present in the root invocation.
///
/// Refer to `mock_all_auths` documentation for details and
/// prefer using `mock_all_auths` unless non-root authorization is
/// required.
///
/// See `soroban_sdk::Env::mock_all_auths_allowing_non_root_auth`
/// for more details and examples.
pub fn mock_all_auths_allowing_non_root_auth(&self) -> Self {
Self {
env: self.env.clone(),
address: self.address.clone(),
set_auths: None,
mock_auths: None,
mock_all_auths: true,
allow_non_root_auth: true,
}
}
}
mod __contract_fn_set_registry {
use super::*;
extern crate std;
use std::collections::BTreeMap;
use std::sync::Mutex;
pub type F = soroban_sdk::testutils::ContractFunctionF;
static FUNCS: Mutex<BTreeMap<&'static str, &'static F>> = Mutex::new(BTreeMap::new());
pub fn register(name: &'static str, func: &'static F) {
FUNCS.lock().unwrap().insert(name, func);
}
pub fn call(
name: &str,
env: soroban_sdk::Env,
args: &[soroban_sdk::Val],
) -> Option<soroban_sdk::Val> {
let fopt: Option<&'static F> = FUNCS.lock().unwrap().get(name).map(|f| f.clone());
fopt.map(|f| f(env, args))
}
}
impl soroban_sdk::testutils::ContractFunctionRegister for Contract {
fn register(name: &'static str, func: &'static __contract_fn_set_registry::F) {
__contract_fn_set_registry::register(name, func);
}
}
#[doc(hidden)]
impl soroban_sdk::testutils::ContractFunctionSet for Contract {
fn call(
&self,
func: &str,
env: soroban_sdk::Env,
args: &[soroban_sdk::Val],
) -> Option<soroban_sdk::Val> {
__contract_fn_set_registry::call(func, env, args)
}
}
impl Contract {
pub fn empty() {}
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub mod __Contract__empty__spec {
#[doc(hidden)]
#[allow(non_snake_case)]
#[allow(non_upper_case_globals)]
pub static __SPEC_XDR_FN_EMPTY: [u8; 28usize] = super::Contract::spec_xdr_empty();
}
impl Contract {
#[allow(non_snake_case)]
pub const fn spec_xdr_empty() -> [u8; 28usize] {
*b"\0\0\0\0\0\0\0\0\0\0\0\x05empty\0\0\0\0\0\0\0\0\0\0\0"
}
}
impl<'a> ContractClient<'a> {
pub fn empty(&self) -> () {
use core::ops::Not;
let old_auth_manager = self
.env
.in_contract()
.not()
.then(|| self.env.host().snapshot_auth_manager().unwrap());
{
if let Some(set_auths) = self.set_auths {
self.env.set_auths(set_auths);
}
if let Some(mock_auths) = self.mock_auths {
self.env.mock_auths(mock_auths);
}
if self.mock_all_auths {
if self.allow_non_root_auth {
self.env.mock_all_auths_allowing_non_root_auth();
} else {
self.env.mock_all_auths();
}
}
}
use soroban_sdk::{FromVal, IntoVal};
let res = self.env.invoke_contract(
&self.address,
&{
#[allow(deprecated)]
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("empty");
SYMBOL
},
::soroban_sdk::Vec::new(&self.env),
);
if let Some(old_auth_manager) = old_auth_manager {
self.env.host().set_auth_manager(old_auth_manager).unwrap();
}
res
}
pub fn try_empty(
&self,
) -> Result<
Result<(), <() as soroban_sdk::TryFromVal<soroban_sdk::Env, soroban_sdk::Val>>::Error>,
Result<soroban_sdk::Error, soroban_sdk::InvokeError>,
> {
use core::ops::Not;
let old_auth_manager = self
.env
.in_contract()
.not()
.then(|| self.env.host().snapshot_auth_manager().unwrap());
{
if let Some(set_auths) = self.set_auths {
self.env.set_auths(set_auths);
}
if let Some(mock_auths) = self.mock_auths {
self.env.mock_auths(mock_auths);
}
if self.mock_all_auths {
self.env.mock_all_auths();
}
}
use soroban_sdk::{FromVal, IntoVal};
let res = self.env.try_invoke_contract(
&self.address,
&{
#[allow(deprecated)]
const SYMBOL: soroban_sdk::Symbol = soroban_sdk::Symbol::short("empty");
SYMBOL
},
::soroban_sdk::Vec::new(&self.env),
);
if let Some(old_auth_manager) = old_auth_manager {
self.env.host().set_auth_manager(old_auth_manager).unwrap();
}
res
}
}
impl ContractArgs {
#[inline(always)]
#[allow(clippy::unused_unit)]
pub fn empty<'i>() -> () {
()
}
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub mod __Contract__empty {
use super::*;
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).empty` instead")]
pub fn invoke_raw(env: soroban_sdk::Env) -> soroban_sdk::Val {
<_ as soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::Val>>::into_val(
#[allow(deprecated)]
&<super::Contract>::empty(),
&env,
)
}
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).empty` instead")]
pub fn invoke_raw_slice(env: soroban_sdk::Env, args: &[soroban_sdk::Val]) -> soroban_sdk::Val {
if args.len() != 0usize {
{
::core::panicking::panic_fmt(format_args!(
"invalid number of input arguments: {0} expected, got {1}",
0usize,
args.len(),
));
};
}
#[allow(deprecated)]
invoke_raw(env)
}
#[deprecated(note = "use `ContractClient::new(&env, &contract_id).empty` instead")]
pub extern "C" fn invoke_raw_extern() -> soroban_sdk::Val {
#[allow(deprecated)]
invoke_raw(soroban_sdk::Env::default())
}
use super::*;
}
#[doc(hidden)]
#[allow(non_snake_case)]
#[allow(unused)]
fn __Contract__2e1cfa82b035c26cbbbdae632cea070514eb8b773f616aaeaf668e2f0be8f10d_ctor() {
#[allow(unsafe_code)]
{
#[link_section = ".init_array"]
#[used]
#[allow(non_upper_case_globals, non_snake_case)]
#[doc(hidden)]
static f: extern "C" fn() -> ::ctor::__support::CtorRetType = {
#[link_section = ".text.startup"]
#[allow(non_snake_case)]
extern "C" fn f() -> ::ctor::__support::CtorRetType {
unsafe {
__Contract__2e1cfa82b035c26cbbbdae632cea070514eb8b773f616aaeaf668e2f0be8f10d_ctor();
};
core::default::Default::default()
}
f
};
}
{
<Contract as soroban_sdk::testutils::ContractFunctionRegister>::register(
"empty",
#[allow(deprecated)]
&__Contract__empty::invoke_raw_slice,
);
}
}
mod test {
use crate::{Contract, ContractClient};
use soroban_sdk::Env;
extern crate test;
#[rustc_test_marker = "test::test_empty"]
#[doc(hidden)]
pub const test_empty: test::TestDescAndFn = test::TestDescAndFn {
desc: test::TestDesc {
name: test::StaticTestName("test::test_empty"),
ignore: false,
ignore_message: ::core::option::Option::None,
source_file: "tests/macros/src/lib.rs",
start_line: 26usize,
start_col: 8usize,
end_line: 26usize,
end_col: 18usize,
compile_fail: false,
no_run: false,
should_panic: test::ShouldPanic::No,
test_type: test::TestType::UnitTest,
},
testfn: test::StaticTestFn(
#[coverage(off)]
|| test::assert_test_result(test_empty()),
),
};
fn test_empty() {
let e = Env::default();
let contract_id = e.register(Contract, ());
let client = ContractClient::new(&e, &contract_id);
client.empty();
}
}
#[rustc_main]
#[coverage(off)]
#[doc(hidden)]
pub fn main() -> () {
extern crate test;
test::test_main_static(&[&test_empty])
}