Git Product home page Git Product logo

assert2-rs's Introduction

assert2

All-purpose assert!(...) and check!(...) macros, inspired by Catch2. There is also a debug_assert!(...) macro that is disabled on optimized builds by default. As cherry on top there is a let_assert!(...) macro that lets you test a pattern while capturing parts of it.

Why these macros?

These macros offer some benefits over the assertions from the standard library:

  • The macros parse your expression to detect comparisons and adjust the error message accordingly. No more assert_eq or assert_ne, just write assert!(1 + 1 == 2), or even assert!(1 + 1 > 1)!
  • You can test for pattern matches: assert!(let Err(_) = File::open("/non/existing/file")).
  • You can capture parts of the pattern for further testing by using the let_assert!(...) macro.
  • The check macro can be used to perform multiple checks before panicking.
  • The macros provide more information when the assertion fails.
  • Colored failure messages!

The macros also accept additional arguments for a custom message, so it is fully compatible with std::assert. That means you don't have to worry about overwriting the standard assert with use assert2::assert.

Examples

check!(6 + 1 <= 2 * 3);

Assertion error


check!(true && false);

Assertion error


check!(let Ok(_) = File::open("/non/existing/file"));

Assertion error


let_assert!(Err(e) = File::open("/non/existing/file"));
check!(e.kind() == ErrorKind::PermissionDenied);

Assertion error

assert vs check

The crate provides two macros: check!(...) and assert!(...). The main difference is that check is really intended for test cases and doesn't immediately panic. Instead, it will print the assertion error and fail the test. This allows you to run multiple checks and can help to determine the reason of a test failure more easily. The assert macro on the other hand simply prints the error and panics, and can be used outside of tests just as well.

Currently, check uses a scope guard to delay the panic until the current scope ends. Ideally, check doesn't panic at all, but only signals that a test case has failed. If this becomes possible in the future, the check macro will change, so you should not rely on check to panic.

The let_assert!() macro

You can also use the let_assert!(...). It is very similar to assert!(let ...), but all placeholders will be made available as variables in the calling scope.

This allows you to run additional checks on the captured variables.

For example:

let_assert!(Ok(foo) = Foo::try_new("bar"));
check!(foo.name() == "bar");

let_assert!(Err(Error::InvalidName(e)) = Foo::try_new("bogus name"));
check!(e.name() == "bogus name");
check!(e.to_string() == "invalid name: bogus name");

Controlling colored output.

Colored output can be controlled using environment variables, as per the clicolors spec:

  • CLICOLOR != 0: ANSI colors are supported and should be used when the program isn't piped.
  • CLICOLOR == 0: Don't output ANSI color escape codes.
  • CLICOLOR_FORCE != 0: ANSI colors should be enabled no matter what.

assert2-rs's People

Contributors

de-vri-es avatar m-ou-se avatar pdylanross avatar slinkydeveloper avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.