Skip to content

Commit

Permalink
Code refactoring based on latest reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Nov 28, 2024
1 parent 5dec756 commit 968a976
Showing 1 changed file with 115 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,15 @@ fn wasm_file_path() -> &'static str {
const CHAIN_SPEC_BUILDER_PATH: &str = "../../../../../target/release/chain-spec-builder";

macro_rules! bash(
( chain-spec-builder $x:tt, $($a:tt)* ) => {{
( chain-spec-builder $($a:tt)* ) => {{
let path = get_chain_spec_builder_path();
let x: serde_json::Value = json!($x);
let output = spawn_with_output!(
spawn_with_output!(
$path $($a)*
)
.expect("a process running. qed")
.wait_with_output()
.expect("to get output. qed.");
let output0: serde_json::Value = serde_json::from_slice(&output.as_bytes()).unwrap();
assert_eq!(output0, x, "Output did not match expected");
}}
);
.expect("to get output. qed.")

macro_rules! bash2(
( chain-spec-builder $x:tt, $($a:tt)* ) => {{
let path = get_chain_spec_builder_path();
let x: serde_json::Value = json!($x);
let output = spawn_with_output!(
$path $($a)*
)
.expect("a process running. qed")
.wait_with_output()
.expect("to get output. qed.");
let mut output0: serde_json::Value = serde_json::from_slice(&output.as_bytes()).unwrap();
//remove code field for better readability
if let Some(code) = output0["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code")
{
*code = Value::String("0x123".to_string());
}
assert_eq!(output0, x, "Output did not match expected");
}}
);

Expand All @@ -52,30 +30,46 @@ fn get_chain_spec_builder_path() -> &'static str {
CHAIN_SPEC_BUILDER_PATH
}

#[docify::export]
fn cmd_list_presets(path: &str) {
#[docify::export_content]
fn cmd_list_presets(path: &str) -> String {
bash!(
chain-spec-builder {
chain-spec-builder list-presets -r $path
)
}

#[test]
fn list_presets() {
let output: serde_json::Value =
serde_json::from_slice(cmd_list_presets(wasm_file_path()).as_bytes()).unwrap();
assert_eq!(
output,
json!({
"presets":[
"preset_1",
"preset_2",
"preset_3",
"preset_4",
"preset_invalid"
]
}, list-presets -r $path
}),
"Output did not match expected"
);
}

#[test]
fn list_presets() {
cmd_list_presets(wasm_file_path());
}

#[docify::export]
fn cmd_get_preset(path: &str) {
fn cmd_get_preset(path: &str) -> String {
bash!(
chain-spec-builder {
chain-spec-builder display-preset -r $path -p preset_2
)
}

#[test]
fn get_preset() {
let output: serde_json::Value =
serde_json::from_slice(cmd_get_preset(wasm_file_path()).as_bytes()).unwrap();
assert_eq!(
output,
json!({
"bar": {
"initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL",
},
Expand All @@ -87,93 +81,108 @@ fn cmd_get_preset(path: &str) {
},
"someInteger": 200
},
}, display-preset -r $path -p preset_2
}),
"Output did not match expected"
);
}

#[test]
fn get_preset() {
cmd_get_preset(wasm_file_path());
#[docify::export]
fn cmd_generate_chain_spec(path: &str) -> String {
bash!(
chain-spec-builder -c /dev/stdout create -r $path named-preset preset_2
)
}

#[docify::export]
fn cmd_generate_chain_spec(path: &str) {
bash2!(
chain-spec-builder {
"name": "Custom",
"id": "custom",
"chainType": "Live",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" },
"codeSubstitutes": {},
"genesis": {
"runtimeGenesis": {
"code": "0x123",
"patch": {
"bar": {
"initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL"
#[test]
fn generate_chain_spec() {
let mut output: serde_json::Value =
serde_json::from_slice(cmd_generate_chain_spec(wasm_file_path()).as_bytes()).unwrap();
if let Some(code) = output["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code")
{
*code = Value::String("0x123".to_string());
}
assert_eq!(
output,
json!({
"name": "Custom",
"id": "custom",
"chainType": "Live",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" },
"codeSubstitutes": {},
"genesis": {
"runtimeGenesis": {
"code": "0x123",
"patch": {
"bar": {
"initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL"
},
"foo": {
"someEnum": {
"Data2": {
"values": "0x0c10"
}
},
"foo": {
"someEnum": {
"Data2": {
"values": "0x0c10"
}
},
"someInteger": 200
}
"someInteger": 200
}
}
}
}, -c /dev/stdout create -r $path named-preset preset_2
}
}),
"Output did not match expected"
);
}

#[test]
fn generate_chain_spec() {
cmd_generate_chain_spec(wasm_file_path());
}

#[docify::export]
fn cmd_generate_para_chain_spec(path: &str) {
bash2!(
chain-spec-builder {
"name": "Custom",
"id": "custom",
"chainType": "Live",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"relay_chain": "polkadot",
"para_id": 1000,
"properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" },
"codeSubstitutes": {},
"genesis": {
"runtimeGenesis": {
"code": "0x123",
"patch": {
"bar": {
"initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL"
},
"foo": {
"someEnum": {
"Data2": {
"values": "0x0c10"
}
},
"someInteger": 200
}
}
}
}}
, -c /dev/stdout create -c polkadot -p 1000 -r $path named-preset preset_2
);
fn cmd_generate_para_chain_spec(path: &str) -> String {
bash!(
chain-spec-builder -c /dev/stdout create -c polkadot -p 1000 -r $path named-preset preset_2
)
}

#[test]
fn generate_para_chain_spec() {
cmd_generate_para_chain_spec(wasm_file_path());
let mut output: serde_json::Value =
serde_json::from_slice(cmd_generate_para_chain_spec(wasm_file_path()).as_bytes()).unwrap();
if let Some(code) = output["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code")
{
*code = Value::String("0x123".to_string());
}
assert_eq!(
output,
json!({
"name": "Custom",
"id": "custom",
"chainType": "Live",
"bootNodes": [],
"telemetryEndpoints": null,
"protocolId": null,
"relay_chain": "polkadot",
"para_id": 1000,
"properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" },
"codeSubstitutes": {},
"genesis": {
"runtimeGenesis": {
"code": "0x123",
"patch": {
"bar": {
"initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL"
},
"foo": {
"someEnum": {
"Data2": {
"values": "0x0c10"
}
},
"someInteger": 200
}
}
}
}}),
"Output did not match expected"
);
}

#[test]
Expand Down

0 comments on commit 968a976

Please sign in to comment.