-
Notifications
You must be signed in to change notification settings - Fork 17
/
mod.rs
219 lines (182 loc) · 6.24 KB
/
mod.rs
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
use cargo_near_integration_tests::{from_git, generate_abi_fn_with, generate_abi_with};
use function_name::named;
use git2::build::CheckoutBuilder;
use git2::Repository;
use std::collections::HashMap;
use tempfile::TempDir;
use crate::util::AsJsonSchema;
fn clone_git_repo() -> color_eyre::eyre::Result<TempDir> {
let temp_dir = tempfile::tempdir()?;
let repo_dir = temp_dir.path();
let repo = Repository::clone(from_git::SDK_REPO, repo_dir)?;
let commit = repo.revparse_single(from_git::SDK_REVISION)?;
repo.checkout_tree(&commit, Some(&mut CheckoutBuilder::new()))?;
Ok(temp_dir)
}
#[test]
#[named]
fn test_dependency_local_path() -> cargo_near::CliResult {
let near_sdk_dir = clone_git_repo()?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");
// near-sdk = { path = "::path::", features = ["abi"] }
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_local_path.toml";
Vars: HashMap::from([("path", near_sdk_dep_path.to_str().unwrap())]);
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_local_path_with_version() -> cargo_near::CliResult {
let near_sdk_dir = clone_git_repo()?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_local_path_with_version.toml";
Vars: HashMap::from([("path", near_sdk_dep_path.to_str().unwrap())]);
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_default_features() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/_Cargo.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_explicit() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_explicit.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_no_default_features() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_no_default_features.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_multiple_features() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_multiple_features.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_platform_specific() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_platform_specific.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
// Does not work because of NEAR SDK (generates code that depends on `near-sdk` being the package name).
#[ignore]
#[test]
#[named]
fn test_dependency_renamed() -> cargo_near::CliResult {
let abi_root = generate_abi_with! {
Cargo: "/templates/sdk-dependency/_Cargo_renamed.toml";
Code:
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near::near_bindgen;
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
pub struct Contract {}
#[near_bindgen]
impl Contract {
pub fn foo(&self, a: bool, b: u32) {}
}
};
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
#[test]
#[named]
fn test_dependency_patch() -> cargo_near::CliResult {
// [dependencies]
// near-sdk = "4.0.0"
//
// [patch.crates-io]
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "10b0dea3b1a214d789cc90314aa814a4181610ad" }
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_patch.toml";
Code:
pub fn foo(&self, a: bool, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 2);
let function = &abi_root.body.functions[1];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}
/// this is a test of Cargo.toml format
/// TODO: un-ignore when `5.x.x` near-sdk is published
/// and `cargo_near_integration_tests::SDK_VERSION` is changed 4.x.x -> 5.x.x
#[test]
#[ignore]
#[named]
fn test_abi_not_a_table() -> cargo_near::CliResult {
let abi_root = generate_abi_fn_with! {
Cargo: "/templates/sdk-dependency/_Cargo_not_a_table.toml";
Code:
pub fn foo(&self, a: u32, b: u32) {}
};
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
let params = function.params.json_schemas()?;
assert_eq!(params.len(), 2);
Ok(())
}