Tests Workflow Status (main) Crates.io docs.rs

xpct

xpct is an assertions library for Rust. It's designed to be ergonomic, batteries-included, and test framework agnostic.

About

xpct is extensible. In addition to allowing you to write custom matchers, it separates the logic of matchers from how they format their output, meaning you can:

  1. Hook into existing formatters to write custom matchers with pretty output without having to worry about formatting.
  2. Customize the formatting of existing matchers without having to reimplement their logic.

Want to get started? Check out the tutorial.

How do you pronounce "xpct"?

However you choose to pronounce it is how it's pronounced! I pronounce it like "expect."

Docs

Examples

A simple equality assertion, like assert_eq:

```rust,should_panic use xpct::{expect, equal};

expect!("disco").to(equal("Disco")); ```

text [src/main.rs:4:5] = "disco" Expected: "disco" to equal: "Disco"

Unwrapping a Some value to make an assertion on the wrapped value:

```rust,shouldpanic use xpct::{begt, be_some, expect};

expect!(Some(41)) .to(besome()) .to(begt(57)); ```

text [src/main.rs:6:5] = Some(41) Expected: 41 to be greater than: 57

Making assertions about individual fields of a struct:

```rust,shouldpanic use xpct::{ beempty, begt, beok, betrue, expect, fields, haveprefix, matchfields, matchregex, not, why, };

struct Person { id: String, name: String, age: u32, is_superstar: bool, }

let person = Person { id: String::from("LTN-2JFR"), name: String::new(), age: 44, is_superstar: false, };

expect!(person).to(matchfields(fields!(Person { id: matchregex(r"^\w{3}(-\dJFR)?$"), name: why(not(beempty()), "this is a required field"), age: begt(0), issuperstar: betrue(), }))); ```

text [src/main.rs:23:5] = person Expected all of these fields to succeed: my_crate::main::Person { id: OK name: FAILED 🛈 this is a required field Expected this to not be empty age: OK is_superstar: FAILED Expected this to be true }

MSRV Policy

The last two stable Rust releases are supported. Older releases may be supported as well.

The MSRV will only be increased when necessary to take advantage of new Rust features—not every time there is a new Rust release. An increase in the MSRV will be accompanied by a minor semver bump if >=1.0.0 or a patch semver bump if <1.0.0.

Semver Policy

Prior to version 1.0.0, breaking changes will be accompanied by a minor version bump, and new features and bug fixes will be accompanied by a patch version bump.