-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathcontract_fn.rs
More file actions
107 lines (94 loc) · 2.81 KB
/
Copy pathcontract_fn.rs
File metadata and controls
107 lines (94 loc) · 2.81 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
use crate as soroban_sdk;
use soroban_sdk::{contract, contractimpl, Env};
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::{
Limits, ReadXdr, ScSpecEntry, ScSpecFunctionInputV0, ScSpecFunctionV0, ScSpecTypeDef,
};
#[contract]
pub struct Contract;
#[contractimpl]
impl Contract {
pub fn add(_e: &Env, a: i32, b: i32) -> i32 {
a + b
}
pub fn add_with_unused_arg(_e: &Env, a: i32, _b: i32) -> i32 {
a + 2
}
pub fn add_with_mut_arg(_e: &Env, a: i32, mut b: i32) -> i32 {
b *= 1;
a + b
}
pub fn add_with_ref_arg(_e: &Env, a: i32, b: &i32) -> i32 {
a + b
}
}
#[contract]
pub struct Contract2;
#[contractimpl]
impl Contract2 {
pub fn add(_e: &Env, a: i32, b: i32) -> i32 {
a + b
}
}
#[test]
fn test_functional() {
let e = Env::default();
let contract_id = e.register(Contract, ());
let a = 10i32;
let b = 12i32;
let c = ContractClient::new(&e, &contract_id).add(&a, &b);
assert_eq!(c, 22);
let c = ContractClient::new(&e, &contract_id).add_with_mut_arg(&a, &b);
assert_eq!(c, 22);
let c = ContractClient::new(&e, &contract_id).add_with_ref_arg(&a, &b);
assert_eq!(c, 22);
}
#[test]
fn test_spec() {
let entries = ScSpecEntry::from_xdr(Contract::spec_xdr_add(), Limits::none()).unwrap();
let expect = ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
doc: "".try_into().unwrap(),
name: "add".try_into().unwrap(),
inputs: vec![
ScSpecFunctionInputV0 {
doc: "".try_into().unwrap(),
name: "a".try_into().unwrap(),
type_: ScSpecTypeDef::I32,
},
ScSpecFunctionInputV0 {
doc: "".try_into().unwrap(),
name: "b".try_into().unwrap(),
type_: ScSpecTypeDef::I32,
},
]
.try_into()
.unwrap(),
outputs: vec![ScSpecTypeDef::I32].try_into().unwrap(),
});
assert_eq!(entries, expect);
}
#[test]
fn test_spec_with_unused_arg() {
let entries =
ScSpecEntry::from_xdr(Contract::spec_xdr_add_with_unused_arg(), Limits::none()).unwrap();
let expect = ScSpecEntry::FunctionV0(ScSpecFunctionV0 {
doc: "".try_into().unwrap(),
name: "add_with_unused_arg".try_into().unwrap(),
inputs: vec![
ScSpecFunctionInputV0 {
doc: "".try_into().unwrap(),
name: "a".try_into().unwrap(),
type_: ScSpecTypeDef::I32,
},
ScSpecFunctionInputV0 {
doc: "".try_into().unwrap(),
name: "b".try_into().unwrap(),
type_: ScSpecTypeDef::I32,
},
]
.try_into()
.unwrap(),
outputs: vec![ScSpecTypeDef::I32].try_into().unwrap(),
});
assert_eq!(entries, expect);
}