Skip to content

Latest commit

 

History

History

assert-macro

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

assert! macro and friends for testing

The Rust testing framework provides macros for test assertions, such as:

  • assert!(condition): assert condition is true.

  • assert_eq!(a, b): assert a is equal to b.

  • assert_ne!(a, b): assert a is not equal to b.

Example:

let x = 1;
let y = 2;
assert!(x < y);

Example with an optional message:

let x = 1;
let y = 2;
assert!(x < y, "We want x to be less than y");

Assertables crate

The Assertables crate provides more assert macros, such as:

  • assert_starts_with!(x, y): Does x start with y?

  • assert_contains!(array, element): Does array contains element?

  • assert_is_match(regex, string): Does regex match string?string.

Example:

let a = "hello world";
let b = "hello";
assert_starts_with!(&a, &b);