diff --git a/crates/nargo/src/cli/fs/inputs.rs b/crates/nargo/src/cli/fs/inputs.rs index fc1f661eae..884de30ec4 100644 --- a/crates/nargo/src/cli/fs/inputs.rs +++ b/crates/nargo/src/cli/fs/inputs.rs @@ -73,3 +73,66 @@ pub(crate) fn write_inputs_to_file>( Ok(()) } + +#[cfg(test)] +mod tests { + use std::{collections::BTreeMap, vec}; + + use acvm::FieldElement; + use noirc_abi::{ + input_parser::{Format, InputValue}, + Abi, AbiParameter, AbiType, AbiVisibility, + }; + use tempdir::TempDir; + + use super::{read_inputs_from_file, write_inputs_to_file}; + use crate::constants::VERIFIER_INPUT_FILE; + + #[test] + fn write_and_read_recovers_inputs_and_return_value() { + let input_dir = TempDir::new("input_dir").unwrap().into_path(); + + // We purposefully test a simple ABI here as we're focussing on `fs`. + // Tests for serializing complex types should exist in `noirc_abi`. + let abi = Abi { + parameters: vec![ + AbiParameter { + name: "foo".into(), + typ: AbiType::Field, + visibility: AbiVisibility::Public, + }, + AbiParameter { + name: "bar".into(), + typ: AbiType::String { length: 11 }, + visibility: AbiVisibility::Private, + }, + ], + return_type: Some(AbiType::Field), + + // Input serialization is only dependent on types, not position in witness map. + // Neither of these should be relevant so we leave them empty. + param_witnesses: BTreeMap::new(), + return_witnesses: Vec::new(), + }; + let input_map = BTreeMap::from([ + ("foo".to_owned(), InputValue::Field(42u128.into())), + ("bar".to_owned(), InputValue::String("hello world".to_owned())), + ]); + let return_value = Some(InputValue::Field(FieldElement::zero())); + + write_inputs_to_file( + &input_map, + &return_value, + &input_dir, + VERIFIER_INPUT_FILE, + Format::Toml, + ) + .unwrap(); + + let (loaded_inputs, loaded_return_value) = + read_inputs_from_file(input_dir, VERIFIER_INPUT_FILE, Format::Toml, &abi).unwrap(); + + assert_eq!(loaded_inputs, input_map); + assert_eq!(loaded_return_value, return_value); + } +} diff --git a/crates/noirc_abi/src/input_parser/mod.rs b/crates/noirc_abi/src/input_parser/mod.rs index 352694821e..cf9e0909f5 100644 --- a/crates/noirc_abi/src/input_parser/mod.rs +++ b/crates/noirc_abi/src/input_parser/mod.rs @@ -10,8 +10,7 @@ use crate::{Abi, AbiType}; /// This is what all formats eventually transform into /// For example, a toml file will parse into TomlTypes /// and those TomlTypes will be mapped to Value -#[cfg_attr(test, derive(PartialEq))] -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, PartialEq)] pub enum InputValue { Field(FieldElement), Vec(Vec),