Skip to main content

char_count

Function char_count 

Source
pub fn char_count<E: Matcher<usize>>(expected: E) -> CharLenMatcher<E>
Expand description

Matches a string whose number of Unicode scalars matches expected.

In other words, the argument must match the output of actual_string.chars().count().

This can have surprising effects when what appears to be a single character is composed of multiple Unicode scalars. See Rust documentation on character representation for more information.

This matches against owned strings and string slices.

let string_slice = "A string";
verify_that!(string_slice, char_count(eq(8)))?;
let non_ascii_string_slice = "Ä ſtřiɲğ";
verify_that!(non_ascii_string_slice, char_count(eq(8)))?;
let owned_string = String::from("A string");
verify_that!(owned_string, char_count(eq(8)))?;

The parameter expected can be any integer numeric matcher.

let string_slice = "A string";
verify_that!(string_slice, char_count(gt(4)))?;