Skip to content

Commit

Permalink
chore(nargo): add test for reading/writing inputs to fs (#996)
Browse files Browse the repository at this point in the history
chore: add test for reading/writing inputs to fs
  • Loading branch information
TomAFrench authored Mar 16, 2023
1 parent 8a60650 commit 284c5e9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
63 changes: 63 additions & 0 deletions crates/nargo/src/cli/fs/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,66 @@ pub(crate) fn write_inputs_to_file<P: AsRef<Path>>(

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);
}
}
3 changes: 1 addition & 2 deletions crates/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FieldElement>),
Expand Down

0 comments on commit 284c5e9

Please sign in to comment.