Add unit tests for linter (#9)

This commit is contained in:
Charlie Marsh 2022-08-13 17:32:40 -04:00 committed by GitHub
parent 52afc02023
commit 4a67c8d44b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 168 additions and 18 deletions

View file

@ -28,3 +28,92 @@ pub fn check_path(path: &Path, mode: &cache::Mode) -> Result<Vec<Message>> {
Ok(messages)
}
#[cfg(test)]
mod tests {
use std::path::Path;
use anyhow::Result;
use rustpython_parser::ast::Location;
use crate::cache;
use crate::checks::CheckKind::{DuplicateArgumentName, IfTuple, ImportStarUsage};
use crate::linter::check_path;
use crate::message::Message;
#[test]
fn duplicate_argument_name() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/duplicate_argument_name.py"),
&cache::Mode::None,
)?;
let expected = vec![
Message {
kind: DuplicateArgumentName,
location: Location::new(1, 25),
filename: "./resources/test/src/duplicate_argument_name.py".to_string(),
},
Message {
kind: DuplicateArgumentName,
location: Location::new(5, 9),
filename: "./resources/test/src/duplicate_argument_name.py".to_string(),
},
Message {
kind: DuplicateArgumentName,
location: Location::new(9, 27),
filename: "./resources/test/src/duplicate_argument_name.py".to_string(),
},
];
assert_eq!(actual.len(), expected.len());
for i in 1..actual.len() {
assert_eq!(actual[i], expected[i]);
}
Ok(())
}
#[test]
fn if_tuple() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/if_tuple.py"),
&cache::Mode::None,
)?;
let expected = vec![
Message {
kind: IfTuple,
location: Location::new(1, 1),
filename: "./resources/test/src/if_tuple.py".to_string(),
},
Message {
kind: IfTuple,
location: Location::new(7, 5),
filename: "./resources/test/src/if_tuple.py".to_string(),
},
];
assert_eq!(actual.len(), expected.len());
for i in 1..actual.len() {
assert_eq!(actual[i], expected[i]);
}
Ok(())
}
#[test]
fn import_star_usage() -> Result<()> {
let actual = check_path(
&Path::new("./resources/test/src/import_star_usage.py"),
&cache::Mode::None,
)?;
let expected = vec![Message {
kind: ImportStarUsage,
location: Location::new(1, 1),
filename: "./resources/test/src/import_star_usage.py".to_string(),
}];
assert_eq!(actual.len(), expected.len());
for i in 1..actual.len() {
assert_eq!(actual[i], expected[i]);
}
Ok(())
}
}