Skip to main content

GoogleTestSupport

Trait GoogleTestSupport 

Source
pub trait GoogleTestSupport {
    // Required methods
    fn and_log_failure(self);
    fn failure_message(self, message: impl Into<String>) -> Self;
    fn with_failure_message(self, provider: impl FnOnce() -> String) -> Self;

    // Provided method
    fn and_log_failure_with_message(self, provider: impl FnOnce() -> String)
       where Self: Sized { ... }
}
Expand description

Adds to Result support for GoogleTest Rust functionality.

Required Methods§

Source

fn and_log_failure(self)

If self is a Result::Err, writes to stdout a failure report and marks the test failed. Otherwise, does nothing.

This can be used for non-fatal test assertions, for example:

let actual = 42;
verify_that!(actual, eq(42)).and_log_failure();
                                 // Test still passing; nothing happens
verify_that!(actual, eq(10)).and_log_failure();
                         // Test now fails and failure output to stdout
verify_that!(actual, eq(100)).and_log_failure();
              // Test still fails and new failure also output to stdout
Source

fn failure_message(self, message: impl Into<String>) -> Self

Adds message to the logged failure message if self is a Result::Err. Otherwise, does nothing.

If this method is called more than once, only message from the last invocation is output.

For example:

let actual = 0;
verify_that!(actual, eq(42)).failure_message("Actual was wrong!")?;

results in the following failure message:

Expected: actual equal to 42
  but was: 0
Actual was wrong!

One can pass a String too:

let actual = 0;
verify_that!(actual, eq(42))
   .failure_message(format!("Actual {} was wrong!", actual))?;

However, consider using GoogleTestSupport::with_failure_message instead in that case to avoid unnecessary memory allocation when the message is not needed.

Source

fn with_failure_message(self, provider: impl FnOnce() -> String) -> Self

Adds the output of the closure provider to the logged failure message if self is a Result::Err. Otherwise, does nothing.

This is analogous to GoogleTestSupport::failure_message but only executes the closure provider if it actually produces the message, thus saving possible memory allocation.

let actual = 0;
verify_that!(actual, eq(42))
   .with_failure_message(|| format!("Actual {} was wrong!", actual))?;

Provided Methods§

Source

fn and_log_failure_with_message(self, provider: impl FnOnce() -> String)
where Self: Sized,

If self is a Result::Err, writes to stdout with a failure report and the message returned by provider.

This is equivalent to combining GoogleTestSupport::and_log_failure with a call to GoogleTestSupport::with_failure_message.

Example:

let actual = 0;
verify_eq!(actual, 42)
   .and_log_failure_with_message(|| format!("Actual {} was wrong!", actual));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§