Skip to content

Commit

Permalink
extend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Nowak-Liebiediew committed Oct 30, 2023
1 parent 1e46751 commit 97ef832
Showing 1 changed file with 66 additions and 10 deletions.
76 changes: 66 additions & 10 deletions src/dfx-core/src/extension/manifest/custom_canister_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,21 @@ impl CustomCanisterTypeDeclaration {
_ => {}
}
}

// Removing fields should be done last because of the order of the fields in the map.
// It's easier to do in second for loop than to sort Ops beforehand, bacause Op would need to implement PartialOrd,
// which is not possible, because serde_json::Number does not implement it.
for field_name in remove_fields {
final_fields.remove(&field_name);
}

// Override custom canister declaration values by the real canister_declaration
// see: https://github.com/dfinity/sdk/pull/3222#issuecomment-1624073606
for (key, value) in values.iter() {
if key != "type" && key != "canister_name" {
final_fields.insert(key.clone(), value.clone());
}
}

Ok(final_fields)
}
}
Expand Down Expand Up @@ -233,28 +235,64 @@ mod tests {
};}

#[test]
fn test_op_replace() {}
fn test_op_replace_basic() {
test_op!(
custom_canister_template = r#"
{
"main": { "replace": { "input": "{{canister_name}}", "search": "frontend_(.*)", "output": "oowee/$1/main.ts" } }
}
"#,
dfx_json_canister_values = r#"
{
"canister_name": "frontend_xyz"
}
"#,
expected = r#"
{
"main": "oowee/xyz/main.ts"
}
"#
);
}

#[test]
fn test_op_replace_nested() {
test_op!(
custom_canister_template = r#"
{
"main": { "replace": { "input": "{{main}}", "search": "(.*)", "output": "oowee/$1" } }
}
"#,
dfx_json_canister_values = r#"
{
"main": "src/main.ts"
}
"#,
expected = r#"
{
"main": "oowee/src/main.ts"
}
"#
);
}

#[test]
fn test_op_remove() {
test_op!(
custom_canister_template = r#"
{
"type": "{{canister_name}}",
"main": "src/main.ts",
"main": { "remove": true }
}
"#,
dfx_json_canister_values = r#"
{
"canister_name": "something",
"main": "oowee.exe"
}
"#,
expected = r#"
{
"main": "oowee.exe",
"type": "something"
"main": "oowee.exe"
}
"#
);
Expand All @@ -265,19 +303,16 @@ mod tests {
test_op!(
custom_canister_template = r#"
{
"type": "{{canister_name}}",
"main": "src/main.ts"
"type": "{{canister_name}}"
}
"#,
dfx_json_canister_values = r#"
{
"type": "custom",
"canister_name": "something"
}
"#,
expected = r#"
{
"main": "src/main.ts",
"type": "something"
}
"#
Expand Down Expand Up @@ -328,6 +363,27 @@ mod tests {
);
}

#[test]
fn test_overwrite() {
test_op!(
custom_canister_template = r#"
{
"gzip": true
}
"#,
dfx_json_canister_values = r#"
{
"gzip": false
}
"#,
expected = r#"
{
"gzip": false
}
"#
);
}

#[test]
fn test_ops() {
test_op!(
Expand Down

0 comments on commit 97ef832

Please sign in to comment.