test(solc): add capture test for curly bracket imports (#956)

* test(solc): add capture test for curly bracket imports

* chore: rustfmt
This commit is contained in:
Matthias Seitz 2022-02-24 17:43:35 +01:00 committed by GitHub
parent 7afde0ca0a
commit 601cb1b75e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 7 deletions

View File

@ -807,13 +807,7 @@ fn parse_data(content: &str, file: &Path) -> SolData {
.map(|(cap, name)| { .map(|(cap, name)| {
SolDataUnit::new(name.as_str().to_owned(), cap.to_owned().into()) SolDataUnit::new(name.as_str().to_owned(), cap.to_owned().into())
}); });
imports = imports = capture_imports(content);
capture_outer_and_inner(content, &utils::RE_SOL_IMPORT, &["p1", "p2", "p3", "p4"])
.iter()
.map(|(cap, m)| {
SolDataUnit::new(PathBuf::from(m.as_str()), cap.to_owned().into())
})
.collect();
} }
}; };
let license = content.lines().next().and_then(|line| { let license = content.lines().next().and_then(|line| {
@ -846,10 +840,43 @@ fn capture_outer_and_inner<'a>(
.collect() .collect()
} }
fn capture_imports(content: &str) -> Vec<SolDataUnit<PathBuf>> {
capture_outer_and_inner(content, &utils::RE_SOL_IMPORT, &["p1", "p2", "p3", "p4"])
.iter()
.map(|(cap, m)| SolDataUnit::new(PathBuf::from(m.as_str()), cap.to_owned().into()))
.collect()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn can_capture_curly_imports() {
let content = r#"
import { T } from "../Test.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {DsTest} from "ds-test/test.sol";
"#;
let captured_imports =
capture_imports(content).into_iter().map(|s| s.data).collect::<Vec<_>>();
let expected =
utils::find_import_paths(content).map(|m| m.as_str().into()).collect::<Vec<PathBuf>>();
assert_eq!(captured_imports, expected);
assert_eq!(
captured_imports,
vec![
PathBuf::from("../Test.sol"),
"@openzeppelin/contracts/utils/ReentrancyGuard.sol".into(),
"ds-test/test.sol".into(),
]
);
}
#[test] #[test]
fn can_resolve_hardhat_dependency_graph() { fn can_resolve_hardhat_dependency_graph() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/hardhat-sample"); let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/hardhat-sample");