Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(nargo): add test for reading/writing inputs to fs #996

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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