Skip to content

Commit a09770a

Browse files
tzakianTimothy Zakian
and
Timothy Zakian
authored
[json-rpc-types] Add test for SuiMoveTypes and json format (#21669)
## Description This adds a snapshot test to ensure stability of the json-formatted types. ## Test plan It's just tests! Co-authored-by: Timothy Zakian <timothyzakian@mac.lan>
1 parent 86feabc commit a09770a

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sui-json-rpc-types/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ mysten-metrics.workspace = true
3434
sui-types.workspace = true
3535
sui-json.workspace = true
3636
sui-package-resolver.workspace = true
37+
38+
[dev-dependencies]
39+
insta.workspace = true

crates/sui-json-rpc-types/src/unit_tests/rpc_types_tests.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,117 @@ fn test_serde() {
145145
}
146146
}
147147

148+
#[test]
149+
fn test_move_type_serde() {
150+
use crate::sui_move as SM;
151+
use crate::sui_move::SuiMoveNormalizedType as SNT;
152+
let test_types = vec![
153+
SNT::Bool,
154+
SNT::U8,
155+
SNT::U16,
156+
SNT::U32,
157+
SNT::U64,
158+
SNT::U128,
159+
SNT::U256,
160+
SNT::Address,
161+
SNT::Signer,
162+
SNT::Vector(Box::new(SNT::U8)),
163+
SNT::Struct {
164+
address: SUI_FRAMEWORK_ADDRESS.to_string(),
165+
module: "coin".to_owned(),
166+
name: "Coin".to_owned(),
167+
type_arguments: vec![SNT::Address],
168+
},
169+
SNT::Vector(Box::new(SNT::U16)),
170+
SNT::Vector(Box::new(SNT::Vector(Box::new(SNT::U8)))),
171+
SNT::TypeParameter(0),
172+
SNT::Reference(Box::new(SNT::U8)),
173+
SNT::MutableReference(Box::new(SNT::Struct {
174+
address: SUI_FRAMEWORK_ADDRESS.to_string(),
175+
module: "coin".to_owned(),
176+
name: "Coin".to_owned(),
177+
type_arguments: vec![SNT::Address],
178+
})),
179+
];
180+
181+
let mut acc = vec![];
182+
183+
for value in test_types {
184+
let json = serde_json::to_string(&value).unwrap();
185+
acc.push(json);
186+
}
187+
188+
let s = SM::SuiMoveNormalizedStruct {
189+
abilities: SM::SuiMoveAbilitySet {
190+
abilities: vec![SM::SuiMoveAbility::Copy],
191+
},
192+
type_parameters: vec![SM::SuiMoveStructTypeParameter {
193+
constraints: SM::SuiMoveAbilitySet {
194+
abilities: vec![SM::SuiMoveAbility::Drop],
195+
},
196+
is_phantom: false,
197+
}],
198+
fields: vec![
199+
SM::SuiMoveNormalizedField {
200+
name: "field1".to_string(),
201+
type_: SNT::U8,
202+
},
203+
SM::SuiMoveNormalizedField {
204+
name: "field2".to_string(),
205+
type_: SNT::U16,
206+
},
207+
],
208+
};
209+
210+
let json = serde_json::to_string(&s).unwrap();
211+
acc.push(json);
212+
213+
// NB: variants declaration and lexicographic ordering are different here
214+
let variants = vec![
215+
("b", vec![SNT::U16]),
216+
("a", vec![]),
217+
(
218+
"c",
219+
vec![
220+
SNT::U32,
221+
SNT::Struct {
222+
address: SUI_FRAMEWORK_ADDRESS.to_string(),
223+
module: "coin".to_owned(),
224+
name: "Coin".to_owned(),
225+
type_arguments: vec![SNT::Address],
226+
},
227+
],
228+
),
229+
]
230+
.into_iter()
231+
.map(|(name, type_)| {
232+
(
233+
name.to_string(),
234+
type_
235+
.into_iter()
236+
.enumerate()
237+
.map(|(i, t)| SM::SuiMoveNormalizedField {
238+
name: format!("field{}", i),
239+
type_: t,
240+
})
241+
.collect(),
242+
)
243+
})
244+
.collect();
245+
246+
let e = SM::SuiMoveNormalizedEnum {
247+
abilities: SM::SuiMoveAbilitySet {
248+
abilities: vec![SM::SuiMoveAbility::Copy],
249+
},
250+
type_parameters: vec![],
251+
variants,
252+
};
253+
254+
acc.push(serde_json::to_string(&e).unwrap());
255+
256+
insta::assert_snapshot!(acc.join("\n"));
257+
}
258+
148259
#[test]
149260
fn test_serde_bytearray() {
150261
// ensure that we serialize byte arrays as number array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/sui-json-rpc-types/src/unit_tests/rpc_types_tests.rs
3+
expression: "acc.join(\"\\n\")"
4+
---
5+
"Bool"
6+
"U8"
7+
"U16"
8+
"U32"
9+
"U64"
10+
"U128"
11+
"U256"
12+
"Address"
13+
"Signer"
14+
{"Vector":"U8"}
15+
{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}
16+
{"Vector":"U16"}
17+
{"Vector":{"Vector":"U8"}}
18+
{"TypeParameter":0}
19+
{"Reference":"U8"}
20+
{"MutableReference":{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}}
21+
{"abilities":{"abilities":["Copy"]},"typeParameters":[{"constraints":{"abilities":["Drop"]},"isPhantom":false}],"fields":[{"name":"field1","type":"U8"},{"name":"field2","type":"U16"}]}
22+
{"abilities":{"abilities":["Copy"]},"typeParameters":[],"variants":{"a":[],"b":[{"name":"field0","type":"U16"}],"c":[{"name":"field0","type":"U32"},{"name":"field1","type":{"Struct":{"address":"0000000000000000000000000000000000000000000000000000000000000002","module":"coin","name":"Coin","typeArguments":["Address"]}}}]}}

0 commit comments

Comments
 (0)