Skip to content

Commit

Permalink
test(lint): add tests for Linter::from_path
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnychevalier committed Aug 2, 2024
1 parent 1249aa0 commit 92fa77d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ opt-level = 1
lto = true
strip = "symbols"
panic = "abort"

[dev-dependencies]
tempfile = "3.10.1"
33 changes: 31 additions & 2 deletions src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ mod tests {

use super::Linter;

#[test]
fn from_path_unknown_extension() {
assert!(Linter::from_path("file.with_unknown_extension")
.unwrap()
.is_none());
}

#[cfg(feature = "lang-rust")]
#[test]
fn typo_rust_string() {
Expand All @@ -205,8 +212,30 @@ mod tests {
anyhow::bail!("failed to do something for the following reason : foobar foo");
}
"#;
let mut linter =
Linter::new(&Language::rust(), rust.as_bytes().to_vec(), "file.rs").unwrap();
let mut linter = Linter::new(&Language::rust(), rust, "file.rs").unwrap();

let mut typos = linter.iter().collect::<Vec<_>>();
assert_eq!(typos.len(), 1);
let typo = typos.pop().unwrap();
assert_eq!(
format!("{}", typo.code().unwrap()),
"orthotypos::space-before-punctuation-mark"
);
assert_eq!(typo.span(), (141, 1).into());
}

#[cfg(feature = "lang-rust")]
#[test]
fn typo_rust_from_path() {
let rust = r#"
/// Doc comment
fn func() -> anyhow::Result<()> {
anyhow::bail!("failed to do something for the following reason : foobar foo");
}
"#;
let file = tempfile::Builder::new().suffix(".rs").tempfile().unwrap();
std::fs::write(file.path(), rust).unwrap();
let mut linter = Linter::from_path(file.path()).unwrap().unwrap();

let mut typos = linter.iter().collect::<Vec<_>>();
assert_eq!(typos.len(), 1);
Expand Down

0 comments on commit 92fa77d

Please sign in to comment.