-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow usage of noir
#[test]
syntax in stdlib (#4553)
# Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* Running tests within the Noir stdlib is a little complicated as nargo will not accept it as a regular crate as the stdlib uses features which are disallowed in other crates. This means that in a number of cases we're placing stdlib unit tests inside of the `test_programs` instead of just using the `#[test]` format inside of the stdlib itself. This is suboptimal as it separates the implementation from the tests in the repository and once #4491 is merged, it will be impossible to use this method to test any private functions within the stdlib. This PR then adds a new integration test which is equivalent to running `nargo test` on the stdlib and moves some tests over to the stdlib as an example. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings.
- Loading branch information
1 parent
4cf700b
commit a8b7cdb
Showing
5 changed files
with
140 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
test_programs/compile_success_empty/field_comparisons/Nargo.toml
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
test_programs/compile_success_empty/field_comparisons/Prover.toml
This file was deleted.
Oops, something went wrong.
86 changes: 0 additions & 86 deletions
86
test_programs/compile_success_empty/field_comparisons/src/main.nr
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{collections::BTreeMap, path::PathBuf}; | ||
|
||
use acvm::blackbox_solver::StubbedBlackBoxSolver; | ||
use noirc_driver::{check_crate, file_manager_with_stdlib, CompileOptions}; | ||
use noirc_frontend::hir::FunctionNameMatch; | ||
|
||
use nargo::{ | ||
ops::{report_errors, run_test, TestStatus}, | ||
package::{Package, PackageType}, | ||
parse_all, prepare_package, | ||
}; | ||
|
||
#[test] | ||
fn stdlib_noir_tests() { | ||
let mut file_manager = file_manager_with_stdlib(&PathBuf::from(".")); | ||
file_manager.add_file_with_source_canonical_path(&PathBuf::from("main.nr"), "".to_owned()); | ||
let parsed_files = parse_all(&file_manager); | ||
|
||
// We need a dummy package as we cannot compile the stdlib on its own. | ||
let dummy_package = Package { | ||
version: None, | ||
compiler_required_version: None, | ||
root_dir: PathBuf::from("."), | ||
package_type: PackageType::Binary, | ||
entry_path: PathBuf::from("main.nr"), | ||
name: "dummy".parse().unwrap(), | ||
dependencies: BTreeMap::new(), | ||
}; | ||
|
||
let (mut context, dummy_crate_id) = | ||
prepare_package(&file_manager, &parsed_files, &dummy_package); | ||
|
||
let result = check_crate(&mut context, dummy_crate_id, true, false); | ||
report_errors(result, &context.file_manager, true, false) | ||
.expect("Error encountered while compiling standard library"); | ||
|
||
// We can now search within the stdlib for any test functions to compile. | ||
|
||
let test_functions = context.get_all_test_functions_in_crate_matching( | ||
context.stdlib_crate_id(), | ||
FunctionNameMatch::Anything, | ||
); | ||
|
||
let test_report: Vec<(String, TestStatus)> = test_functions | ||
.into_iter() | ||
.map(|(test_name, test_function)| { | ||
let status = run_test( | ||
&StubbedBlackBoxSolver, | ||
&mut context, | ||
&test_function, | ||
false, | ||
None, | ||
&CompileOptions::default(), | ||
); | ||
|
||
(test_name, status) | ||
}) | ||
.collect(); | ||
|
||
assert!(!test_report.is_empty(), "Could not find any tests within the stdlib"); | ||
assert!(test_report.iter().all(|(_, status)| !status.failed())); | ||
} |