Skip to main content

RuleTester

Struct RuleTester 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for RuleTester

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ParallelSend for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.