forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Group bindings tests and generate runner invocations at build time
- Loading branch information
Showing
16 changed files
with
714 additions
and
248 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
use std::fmt::Write; | ||
use std::path::Path; | ||
|
||
use anyhow::{bail, Result}; | ||
use codegen_language_definition::model::Language; | ||
use inflector::Inflector; | ||
use infra_utils::codegen::CodegenFileSystem; | ||
use infra_utils::paths::FileWalker; | ||
|
||
pub fn generate_bindings_tests( | ||
_language: &Language, | ||
data_dir: &Path, | ||
output_dir: &Path, | ||
) -> Result<()> { | ||
let bindings_tests = collect_bindings_tests(data_dir)?; | ||
|
||
let mut fs = CodegenFileSystem::new(data_dir)?; | ||
|
||
generate_mod_file(&mut fs, &output_dir.join("mod.rs"), &bindings_tests)?; | ||
|
||
for (group_name, test_files) in &bindings_tests { | ||
generate_unit_test_file( | ||
&mut fs, | ||
group_name, | ||
test_files, | ||
&output_dir.join(format!("{0}.rs", group_name.to_snake_case())), | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn collect_bindings_tests(data_dir: &Path) -> Result<BTreeMap<String, BTreeSet<String>>> { | ||
let mut bindings_tests = BTreeMap::<String, BTreeSet<String>>::new(); | ||
|
||
for file in FileWalker::from_directory(data_dir).find(["**/*.sol"])? { | ||
let parts: Vec<_> = file | ||
.strip_prefix(data_dir)? | ||
.iter() | ||
.map(|p| p.to_str().unwrap()) | ||
.collect(); | ||
|
||
match parts[..] { | ||
[group_name, test_file] => { | ||
bindings_tests | ||
.entry(group_name.to_owned()) | ||
.or_default() | ||
.insert(test_file.to_owned()); | ||
} | ||
_ => { | ||
bail!("Invalid test input. Should be in the form of '<tests-dir>/GROUP_NAME/TEST_FILE.sol', but found: {file:?}"); | ||
} | ||
}; | ||
} | ||
|
||
Ok(bindings_tests) | ||
} | ||
|
||
fn generate_mod_file( | ||
fs: &mut CodegenFileSystem, | ||
mod_file_path: &Path, | ||
bindings_tests: &BTreeMap<String, BTreeSet<String>>, | ||
) -> Result<()> { | ||
let module_declarations_str = | ||
bindings_tests | ||
.keys() | ||
.fold(String::new(), |mut buffer, group_name| { | ||
writeln!(buffer, "mod {0};", group_name.to_snake_case()).unwrap(); | ||
buffer | ||
}); | ||
|
||
let contents = format!( | ||
" | ||
{module_declarations_str} | ||
", | ||
); | ||
|
||
fs.write_file(mod_file_path, contents) | ||
} | ||
|
||
fn generate_unit_test_file( | ||
fs: &mut CodegenFileSystem, | ||
group_name: &str, | ||
test_files: &BTreeSet<String>, | ||
unit_test_file_path: &Path, | ||
) -> Result<()> { | ||
let unit_tests_str = test_files | ||
.iter() | ||
.fold(String::new(), |mut buffer, test_file| { | ||
let test_name = test_file.strip_suffix(".sol").unwrap(); | ||
writeln!( | ||
buffer, | ||
r#" | ||
#[test] | ||
fn {test_name}() -> Result<()> {{ | ||
run("{group_name}", "{test_file}") | ||
}} | ||
"# | ||
) | ||
.unwrap(); | ||
buffer | ||
}); | ||
|
||
let contents = format!( | ||
" | ||
use anyhow::Result; | ||
use crate::bindings::runner::run; | ||
{unit_tests_str} | ||
" | ||
); | ||
|
||
fs.write_file(unit_test_file_path, contents) | ||
} |
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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
mod bindings; | ||
mod cst_output; | ||
|
||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use codegen_language_definition::model::Language; | ||
|
||
use crate::bindings::generate_bindings_tests; | ||
use crate::cst_output::generate_cst_output_tests; | ||
|
||
pub trait TestingGeneratorExtensions { | ||
fn generate_cst_output_tests(&self, snapshots_dir: &Path, output_dir: &Path) -> Result<()>; | ||
fn generate_bindings_tests(&self, snapshots_dir: &Path, output_dir: &Path) -> Result<()>; | ||
} | ||
|
||
impl TestingGeneratorExtensions for Language { | ||
fn generate_cst_output_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> { | ||
generate_cst_output_tests(self, data_dir, output_dir) | ||
} | ||
fn generate_bindings_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> { | ||
generate_bindings_tests(self, data_dir, output_dir) | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
crates/solidity/outputs/cargo/tests/src/bindings/generated/lexical.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/solidity/outputs/cargo/tests/src/bindings/generated/mod.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
mod generated; | ||
mod rules; | ||
mod runner; |
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,7 @@ | ||
use slang_solidity::bindings::Bindings; | ||
|
||
#[test] | ||
fn test_bindings_rules_parsing() { | ||
let result = Bindings::get_graph_builder(); | ||
assert!(result.is_ok()); | ||
} |
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
Oops, something went wrong.