pub struct RuleTester { /* private fields */ }Expand description
A rule under test, with a throwaway project to run it in.
The project is removed when the tester is dropped.
Implementations§
Source§impl RuleTester
impl RuleTester
Sourcepub fn new(name: &str, rule_source: &str) -> Result<Self, TestError>
pub fn new(name: &str, rule_source: &str) -> Result<Self, TestError>
Build a tester for a rule’s source.
name labels the temporary directory and need not be unique — every tester gets its
own directory regardless, so a test file with a fn tester() helper shared across
cases works. It has to: two testers sharing a directory would delete each other’s
project mid-run, and the resulting error would point at the config rather than at
the collision.
§Errors
Returns TestError::Setup if the temporary project cannot be written.
Sourcepub fn with_extension(
name: &str,
rule_source: &str,
extension: &str,
) -> Result<Self, TestError>
pub fn with_extension( name: &str, rule_source: &str, extension: &str, ) -> Result<Self, TestError>
Build a tester whose subject files use a given extension.
Needed for a rule targeting tsx, since which grammar parses a file is decided by
its extension — a TSX rule tested against a .ts file would never match.
§Errors
As RuleTester::new.
Sourcepub fn configured(
name: &str,
rule_source: &str,
options: &str,
) -> Result<Self, TestError>
pub fn configured( name: &str, rule_source: &str, options: &str, ) -> Result<Self, TestError>
Build a tester for a factory rule — one whose default export returns a rule when called with options — using the given options expression.
options is JavaScript, spliced into the generated config as rule(<options>).
Passing the options as source rather than as a serialized value is deliberate: a
factory takes whatever its author designed, and a harness that only accepted JSON
could not test one taking a function or a regular expression.
let tester = RuleTester::configured(
"restricted",
RULE_SOURCE,
"{ restrictions: [{ module: 'lodash' }] }",
)
.expect("builds");§Errors
As RuleTester::new.
Sourcepub fn run(&self, source: &str) -> Result<Vec<Violation>, TestError>
pub fn run(&self, source: &str) -> Result<Vec<Violation>, TestError>
Run the rule over a single source file and return what it reported.
§Errors
Returns TestError::Load if the rule does not load, or TestError::Run if it
throws or breaches a budget.
Sourcepub fn accepts(&self, source: &str) -> Result<(), TestError>
pub fn accepts(&self, source: &str) -> Result<(), TestError>
Assert the rule reports nothing for this source.
§Errors
Returns TestError::Mismatch listing what was reported, since “expected none,
got some” is only actionable if you can see which.
Sourcepub fn reports_at(
&self,
source: &str,
expected: &[(u32, u32)],
) -> Result<(), TestError>
pub fn reports_at( &self, source: &str, expected: &[(u32, u32)], ) -> Result<(), TestError>
Assert the rule reports at exactly these one-based positions, in order.
Positions rather than a count, because a rule reporting the right number of violations in the wrong places is a rule that is wrong — and a count-only assertion is exactly what lets that through.
§Errors
Returns TestError::Mismatch showing expected and actual side by side.
Sourcepub fn reports_messages(
&self,
source: &str,
expected: &[&str],
) -> Result<(), TestError>
pub fn reports_messages( &self, source: &str, expected: &[&str], ) -> Result<(), TestError>
Assert the rule reports exactly these messages, in order.
For a rule that substitutes its own message per match — the position alone would not show whether the right one was chosen.
§Errors
Returns TestError::Mismatch showing both lists.
Trait Implementations§
Source§impl Debug for RuleTester
impl Debug for RuleTester
Source§impl Drop for RuleTester
impl Drop for RuleTester
Auto Trait Implementations§
impl Freeze for RuleTester
impl RefUnwindSafe for RuleTester
impl Send for RuleTester
impl Sync for RuleTester
impl Unpin for RuleTester
impl UnsafeUnpin for RuleTester
impl UnwindSafe for RuleTester
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more