Skip to content

Commit

Permalink
added order condition and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Oct 13, 2023
1 parent 991b3fe commit 729f2ac
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions framework/meta/src/cmd/contract/output_contract/oc_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ fn check_single_constructor(output_contract: &OutputContract) -> Result<(), Stri
/// Note: promise callbacks not included, since they have `#[call_value]` arguments, that are currently not modelled.
fn validate_contract_var_args(abi: &ContractAbi) -> Result<(), String> {
for endpoint_abi in abi.constructors.iter().chain(abi.endpoints.iter()) {
validate_endpoint_var_args(endpoint_abi)?;
validate_endpoint_var_args_number(endpoint_abi)?;
validate_endpoint_var_args_order(endpoint_abi)?;
}
Ok(())
}

fn validate_endpoint_var_args(endpoint_abi: &EndpointAbi) -> Result<(), String> {
fn validate_endpoint_var_args_number(endpoint_abi: &EndpointAbi) -> Result<(), String> {
let num_var_args = endpoint_abi
.inputs
.iter()
Expand All @@ -35,6 +36,22 @@ fn validate_endpoint_var_args(endpoint_abi: &EndpointAbi) -> Result<(), String>
"Multiple var args found in {}. Use #[allow_multiple_var_args] if you want to enable this feature",
&endpoint_abi.rust_method_name));
}

Ok(())
}

fn validate_endpoint_var_args_order(endpoint_abi: &EndpointAbi) -> Result<(), String> {
let mut var_args_encountered = false;
for arg in &endpoint_abi.inputs {
if arg.multi_arg {
var_args_encountered = true;
} else if var_args_encountered {
return Err(format!(
"Found regular arguments after var-args in method {}. This is not allowed, because it makes it impossible to parse the arguments.",
&endpoint_abi.rust_method_name));
}
}

Ok(())
}

Expand All @@ -45,7 +62,7 @@ mod tests {
use super::*;

#[test]
fn validate_endpoint_var_args_test() {
fn validate_endpoint_var_args_number_test() {
let mut endpoint_def = EndpointAbi::default();
let var_arg_1 = InputAbi {
arg_name: "arg_1",
Expand All @@ -63,9 +80,36 @@ mod tests {
assert!(!endpoint_def.allow_multiple_var_args);
assert_eq!(Err(format!(
"Multiple var args found in {}. Use #[allow_multiple_var_args] if you want to enable this feature",
&endpoint_def.rust_method_name)), validate_endpoint_var_args(&endpoint_def));
&endpoint_def.rust_method_name)), validate_endpoint_var_args_number(&endpoint_def));

endpoint_def.allow_multiple_var_args = true;
assert_eq!(Ok(()), validate_endpoint_var_args(&endpoint_def));
assert_eq!(Ok(()), validate_endpoint_var_args_number(&endpoint_def));
}

#[test]
fn validate_endpoint_var_args_order_test() {
let mut endpoint_def = EndpointAbi::default();
let arg = InputAbi {
arg_name: "arg_1",
type_name: TypeName::new(),
multi_arg: false,
};
let var_arg_1 = InputAbi {
arg_name: "arg_2",
type_name: TypeName::new(),
multi_arg: true,
};

endpoint_def.inputs.push(var_arg_1.clone());
endpoint_def.inputs.push(arg.clone());
assert_eq!(Err(format!(
"Found regular arguments after var-args in method {}. This is not allowed, because it makes it impossible to parse the arguments.",
&endpoint_def.rust_method_name)), validate_endpoint_var_args_order(&endpoint_def));

endpoint_def.inputs.clear();

endpoint_def.inputs.push(arg);
endpoint_def.inputs.push(var_arg_1);
assert_eq!(Ok(()), validate_endpoint_var_args_order(&endpoint_def));
}
}

0 comments on commit 729f2ac

Please sign in to comment.