Skip to main content

Module assert_not_contains

Module assert_not_contains 

Source
Expand description

Assert an expression (such as a string) does not contain an expression (such as a substring).

Pseudocode:
¬ a.contains(b)

These macros work with many kinds of Rust types, such as String, Vec, Range, HashSet. The specifics depend on each type’s implementation of a method contains, and some types require the second argument to be borrowable, so be sure to check the Rust documentation.

§Example

use assertables::*;
use std::collections::HashSet;

// String does not contain substring.
let a = "alfa";
let b = "xx";
assert_not_contains!(a, b);

// Range does not contain value.
// Notice the &b because the macro calls Range.contains(&self, &value).
let a = 1..3;
let b = 4;
assert_not_contains!(a, &b);

// Vector does not contain element.
// Notice the &b because the macro calls Vec.contains(&self, &value).
let a = vec![1, 2, 3];
let b = 4;
assert_not_contains!(a, &b);

// HashSet does not contain element.
// Notice the &b because the macro calls HashSet.contains(&self, &value).
let a: HashSet<String> = [String::from("1")].into();
let b: String = String::from("2");
assert_not_contains!(a, &b);

// HashSet does not contain element with automatic borrow optimization.
// Notice the b because the value type &str is already a reference,
// which HashSet.contains knows how to borrow to compare to String.
let a: HashSet<String> = [String::from("1")].into();
let b = "2";
assert_not_contains!(a, b);

§Module macros