-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend assertEquals check to all deprecated unittest aliases (#380)
- Loading branch information
1 parent
a2f78ba
commit 71ebd39
Showing
8 changed files
with
116 additions
and
48 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
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
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
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
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
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,56 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use once_cell::sync::Lazy; | ||
use rustpython_ast::{Expr, ExprKind, Location}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::autofix::fixer; | ||
use crate::check_ast::Checker; | ||
use crate::checks::{Check, CheckKind, Fix}; | ||
|
||
static DEPRECATED_ALIASES: Lazy<BTreeMap<&'static str, &'static str>> = Lazy::new(|| { | ||
BTreeMap::from([ | ||
("failUnlessEqual", "assertEqual"), | ||
("assertEquals", "assertEqual"), | ||
("failIfEqual", "assertNotEqual"), | ||
("assertNotEquals", "assertNotEqual"), | ||
("failUnless", "assertTrue"), | ||
("assert_", "assertTrue"), | ||
("failIf", "assertFalse"), | ||
("failUnlessRaises", "assertRaises"), | ||
("failUnlessAlmostEqual", "assertAlmostEqual"), | ||
("assertAlmostEquals", "assertAlmostEqual"), | ||
("failIfAlmostEqual", "assertNotAlmostEqual"), | ||
("assertNotAlmostEquals", "assertNotAlmostEqual"), | ||
("assertRegexpMatches", "assertRegex"), | ||
("assertNotRegexpMatches", "assertNotRegex"), | ||
("assertRaisesRegexp", "assertRaisesRegex"), | ||
]) | ||
}); | ||
|
||
pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) { | ||
if let ExprKind::Attribute { value, attr, .. } = &expr.node { | ||
if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) { | ||
if let ExprKind::Name { id, .. } = &value.node { | ||
if id == "self" { | ||
let mut check = Check::new( | ||
CheckKind::DeprecatedUnittestAlias(attr.to_string(), target.to_string()), | ||
Range::from_located(expr), | ||
); | ||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { | ||
check.amend(Fix { | ||
content: format!("self.{}", target), | ||
location: Location::new(expr.location.row(), expr.location.column()), | ||
end_location: Location::new( | ||
expr.end_location.row(), | ||
expr.end_location.column(), | ||
), | ||
applied: false, | ||
}); | ||
} | ||
checker.add_check(check); | ||
} | ||
} | ||
} | ||
} | ||
} |
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