Struct Assertion

Source
pub struct Assertion<T> {
    pub value: T,
    pub expr_str: &'static str,
    pub negated: bool,
    pub steps: Vec<AssertionStep>,
    pub in_chain: bool,
    pub is_final: bool,
}
Expand description

Represents the complete assertion with all steps

Fields§

§value: T

The value being tested

§expr_str: &'static str

The expression string (variable name)

§negated: bool

Whether the current assertion is negated

§steps: Vec<AssertionStep>

All steps in the assertion chain

§in_chain: bool

Flag to track if this is part of a chain

§is_final: bool

Flag to mark the final step in a chain

Implementations§

Source§

impl<T> Assertion<T>

Source

pub fn new(value: T, expr_str: &'static str) -> Self

Creates a new assertion

Source

pub fn add_step(&self, sentence: AssertionSentence, result: bool) -> Self
where T: Clone,

Add an assertion step and get back a cloned Assertion for chaining

Source

pub fn set_last_logic(&mut self, op: LogicalOp)

Set the logical operation for the last step

Source

pub fn mark_as_intermediate(&mut self)

Mark this assertion as non-final (intermediate step in a chain)

Source

pub fn mark_as_final(&mut self)

Mark this assertion as final (last step in a chain)

Source

pub fn calculate_chain_result(&self) -> bool

Calculate if the entire chain passes

Source

pub fn evaluate(self) -> bool
where T: Clone,

Explicitly evaluate the assertion chain Returns true if the assertion passed, false otherwise

Examples found in repository?
examples/enhanced_output.rs (line 16)
3fn main() {
4    println!("FluentTest Enhanced Output Example\n");
5
6    // Enable enhanced output for this example
7    config().enhanced_output(true).apply();
8
9    // Use various expectations to show the enhanced formatting
10    println!("Running assertions with enhanced output enabled:");
11
12    // Number assertions
13    let number = 42;
14    print_result("expect!(number).to_be_even()", || {
15        // Call evaluate explicitly to get the boolean result
16        expect!(number).to_be_even().evaluate()
17    });
18
19    print_result("expect!(number).to_be_greater_than(30).and().to_be_less_than(50)", || {
20        expect!(number).to_be_greater_than(30).and().to_be_less_than(50).evaluate()
21    });
22
23    // Failing assertion - use catch_unwind for this since it will panic
24    let result = std::panic::catch_unwind(|| {
25        expect!(number).to_be_greater_than(100);
26    });
27    match result {
28        Ok(_) => println!("\n🔍 expect!(number).to_be_greater_than(100)\n  ❌ Failed but did not panic"),
29        Err(e) => {
30            if let Some(s) = e.downcast_ref::<String>() {
31                println!("\n🔍 expect!(number).to_be_greater_than(100)\n  ❌ Failed with: {}", s);
32            } else {
33                println!("\n🔍 expect!(number).to_be_greater_than(100)\n  ❌ Failed with panic");
34            }
35        }
36    }
37
38    // String assertions
39    let text = "Hello, world!";
40    print_result("expect!(text).to_contain(\"world\")", || expect!(text).to_contain("world").evaluate());
41
42    // Collection assertions
43    let items = vec![1, 2, 3, 4, 5];
44    print_result("expect!(items.as_slice()).to_have_length(5)", || expect!(items.as_slice()).to_have_length(5).evaluate());
45
46    println!("\nNOTE: Without enhanced output enabled (the default), these assertions would");
47    println!("      produce standard Rust assertion messages instead of the detailed ones shown above.");
48    println!("\nUse config().enhanced_output(true).apply() to enable enhanced output in your code,");
49    println!("or set the FLUENT_TEST_ENHANCED_OUTPUT=true environment variable.");
50}

Trait Implementations§

Source§

impl<T: Clone> AndModifier<T> for Assertion<T>

Source§

fn and(self) -> Self

Returns a new Assertion with the same value, allowing for chaining assertions

Source§

impl<V> BooleanMatchers for Assertion<V>
where V: AsBoolean + Debug + Clone,

Source§

fn to_be_true(self) -> Self

Source§

fn to_be_false(self) -> Self

Source§

impl<T: Clone> Clone for Assertion<T>

Source§

fn clone(&self) -> Assertion<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, V> CollectionMatchers<T> for Assertion<V>
where T: Debug + Clone + PartialEq, V: AsCollection<Item = T> + Debug + Clone,

Source§

fn to_be_empty(self) -> Self

Source§

fn to_have_length(self, expected: usize) -> Self

Source§

fn to_contain<U: PartialEq<T> + Debug>(self, expected: U) -> Self

Source§

fn to_contain_all_of<U: PartialEq<T> + Debug>(self, expected: &[U]) -> Self

Source§

fn to_equal_collection<U: PartialEq<T> + Debug>(self, expected: &[U]) -> Self

Source§

impl<T: Debug> Debug for Assertion<T>

Source§

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

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

impl<T> Drop for Assertion<T>

For automatic evaluation of assertions when the Assertion drops

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<V, T> EqualityMatchers<T> for Assertion<V>
where T: Debug + PartialEq + Clone, V: AsEqualityComparable<T> + Debug + Clone,

Source§

fn to_equal(self, expected: T) -> Self

Check if the value is equal to the expected value
Source§

fn to_equal_value(self, expected: T) -> Self

Type-specific version of to_equal to avoid trait conflicts
Source§

impl<M, K, V> HashMapMatchers<K, V> for Assertion<M>
where K: Hash + Eq + Debug + Clone, V: Debug + Clone, M: AsHashMap<K, V> + Debug + Clone,

Source§

fn to_be_empty(self) -> Self

Source§

fn to_have_length(self, expected: usize) -> Self

Source§

fn to_contain_key<Q>(self, key: &Q) -> Self
where K: Borrow<Q>, Q: Hash + Eq + Debug + ?Sized,

Source§

fn to_contain_entry<Q, R>(self, key: &Q, value: &R) -> Self
where K: Borrow<Q>, V: Borrow<R>, Q: Hash + Eq + Debug + ?Sized, R: PartialEq + Debug + ?Sized,

Source§

impl<T: Clone> NotModifier<T> for Assertion<T>

Source§

fn not(self) -> Self

Creates a negated assertion This provides a fluent API for negated assertions: expect(value).not().to_equal(x)

Source§

impl<V> NumericMatchers<i32> for Assertion<V>
where V: AsNumeric + Debug + Clone,

Source§

fn to_be_positive(self) -> Self

Source§

fn to_be_negative(self) -> Self

Source§

fn to_be_zero(self) -> Self

Source§

fn to_be_greater_than(self, expected: i32) -> Self

Source§

fn to_be_greater_than_or_equal(self, expected: i32) -> Self

Source§

fn to_be_less_than(self, expected: i32) -> Self

Source§

fn to_be_less_than_or_equal(self, expected: i32) -> Self

Source§

fn to_be_in_range(self, range: Range<i32>) -> Self

Source§

fn to_be_even(self) -> Self

Source§

fn to_be_odd(self) -> Self

Source§

impl<T, V> OptionMatchers<T> for Assertion<V>
where T: Debug + Clone + PartialEq, V: AsOption<Item = T> + Debug + Clone,

Source§

fn to_be_some(self) -> Self

Source§

fn to_be_none(self) -> Self

Source§

fn to_contain(self, expected: &T) -> Self
where T: PartialEq,

Source§

impl<T: Clone> OrModifier<T> for Assertion<T>

Source§

fn or(self) -> Self

Returns a new Assertion with the same value, allowing for OR chaining assertions

Source§

impl<V, T, E> ResultMatchers<T, E> for Assertion<V>
where T: Debug + Clone, E: Debug + Clone, V: AsResult<T, E> + Debug + Clone,

Source§

fn to_be_ok(self) -> Self

Source§

fn to_be_err(self) -> Self

Source§

fn to_contain_ok<U: PartialEq<T> + Debug>(self, expected: &U) -> Self

Source§

fn to_contain_err<U: PartialEq<E> + Debug>(self, expected: &U) -> Self

Source§

impl<V> StringMatchers for Assertion<V>
where V: AsString + Debug + Clone,

Source§

fn to_be_empty(self) -> Self

Source§

fn to_have_length(self, expected: usize) -> Self

Source§

fn to_contain(self, substring: &str) -> Self

Check if the string contains a substring
Source§

fn to_contain_substring(self, substring: &str) -> Self

Type-specific version of to_contain to avoid trait conflicts
Source§

fn to_start_with(self, prefix: &str) -> Self

Source§

fn to_end_with(self, suffix: &str) -> Self

Source§

fn to_match(self, pattern: &str) -> Self

Auto Trait Implementations§

§

impl<T> Freeze for Assertion<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Assertion<T>
where T: RefUnwindSafe,

§

impl<T> Send for Assertion<T>
where T: Send,

§

impl<T> Sync for Assertion<T>
where T: Sync,

§

impl<T> Unpin for Assertion<T>
where T: Unpin,

§

impl<T> UnwindSafe for Assertion<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.