Skip to content

Commit

Permalink
Add assert_matches
Browse files Browse the repository at this point in the history
  • Loading branch information
joelparkerhenderson committed Oct 9, 2024
1 parent cc3323d commit 4ddceb5
Show file tree
Hide file tree
Showing 696 changed files with 2,572 additions and 902 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes highlights for recent major versions.

## Version 8.x

* Add `assert_matches`, `assert_not_matches`.

* Add Iterator macros: `assert_iter_all`, `assert_iter_any`, `assert_iter_eq`, etc.

* Add Result macros: `assert_ok`, `assert_err`, etc.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "assertables"
version = "8.16.0"
version = "8.17.0"
authors = ["Joel Parker Henderson <[email protected]>"]
edition = "2021"
description = "Assertables: assert macros for better testing, debugging, quality assurance, and runtime reliability."
Expand Down
105 changes: 54 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ Examples:
```rust
use assertables::*;
let s = "hello world";
assert_matches!(s, "hello world");
assert_starts_with!(s, "hello");
assert_ends_with!(s, "world");
assert_contains!(s, "o");
assert_len_eq!(s, "***********");
assert_all!(s.chars(), |c: char| c < 'x');
assert_any!(s.chars(), |c: char| c.is_whitespace());
```

To use the macros, add this to your `Cargo.toml` file:

```toml
[dev-dependencies]
assertables = "*"
```

Top benefits:

1. You can write better tests to improve reliability and maintainability.
Expand All @@ -51,117 +60,111 @@ Top comparison crates:
`[cool_asserts](https://crates.io/crates/cool_asserts)`.
`[claims](https://crates.io/crates/claims)`.

To use the macros, add this to your `Cargo.toml` file:

```toml
[dev-dependencies]
assertables = "*"
```

## Highlights

Values:

* `[assert_eq!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_eq) // equal to`
* `[assert_ne!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_ne) // not equal to`
* `[assert_lt!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_lt) // less than`
* `[assert_le!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_le) // less than or equal to`
* `[assert_gt!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_gt) // greater than`
* `[assert_ge!(a, b);](https://docs.rs/assertables/8.16.0/assertables/assert_ge) // greater than or equal to`
* `[assert_eq!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_eq) // equal to`
* `[assert_ne!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_ne) // not equal to`
* `[assert_lt!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_lt) // less than`
* `[assert_le!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_le) // less than or equal to`
* `[assert_gt!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_gt) // greater than`
* `[assert_ge!(a, b);](https://docs.rs/assertables/8.17.0/assertables/assert_ge) // greater than or equal to`

Approximations:

* `[assert_approx_eq!(number, number);](https://docs.rs/assertables/8.16.0/assertables/assert_approx) // |a-b| ≤ 1e-6`
* `[assert_in_delta!(number, number, delta);](https://docs.rs/assertables/8.16.0/assertables/assert_in_delta) // |a-b| ≤ delta`
* `[assert_in_epsilon!(number, number, epsilon);](https://docs.rs/assertables/8.16.0/assertables/assert_in_epsilon) // |a-b| ≤ epsilon * min(a,b)`
* `[assert_approx_eq!(number, number);](https://docs.rs/assertables/8.17.0/assertables/assert_approx) // |a-b| ≤ 1e-6`
* `[assert_in_delta!(number, number, delta);](https://docs.rs/assertables/8.17.0/assertables/assert_in_delta) // |a-b| ≤ delta`
* `[assert_in_epsilon!(number, number, epsilon);](https://docs.rs/assertables/8.17.0/assertables/assert_in_epsilon) // |a-b| ≤ epsilon * min(a,b)`

Groups for iterators, chars, etc.:

* `[assert_all!(group, predicate);](https://docs.rs/assertables/8.16.0/assertables/assert_all) // group.all(predicate)`
* `[assert_any!(group, predicate);](https://docs.rs/assertables/8.16.0/assertables/assert_any) // group.any(predicate)`
* `[assert_all!(group, predicate);](https://docs.rs/assertables/8.17.0/assertables/assert_all) // group.all(predicate)`
* `[assert_any!(group, predicate);](https://docs.rs/assertables/8.17.0/assertables/assert_any) // group.any(predicate)`

Infix for order operators, logic operators, etc.:

* `[assert_infix!(a == b);](https://docs.rs/assertables/8.16.0/assertables/assert_infix) // order: == != < <= > >=`
* `[assert_infix!(a && b);](https://docs.rs/assertables/8.16.0/assertables/assert_infix) // logic: && || ^ & |`
* `[assert_infix!(a == b);](https://docs.rs/assertables/8.17.0/assertables/assert_infix) // order: == != < <= > >=`
* `[assert_infix!(a && b);](https://docs.rs/assertables/8.17.0/assertables/assert_infix) // logic: && || ^ & |`

Parts for strings, vectors, etc.:

* `[assert_starts_with!(whole, part);](https://docs.rs/assertables/8.16.0/assertables/assert_starts_with) // whole.starts_with(part)`
* `[assert_ends_with!(whole, part);](https://docs.rs/assertables/8.16.0/assertables/assert_ends_with) // whole.ends_with(part)`
* `[assert_starts_with!(whole, part);](https://docs.rs/assertables/8.17.0/assertables/assert_starts_with) // whole.starts_with(part)`
* `[assert_ends_with!(whole, part);](https://docs.rs/assertables/8.17.0/assertables/assert_ends_with) // whole.ends_with(part)`

Lengths for strings, vectors, etc.:

* `[assert_len!(item);](https://docs.rs/assertables/8.16.0/assertables/assert_len) // item.len()`
* `[assert_is_empty!(item);](https://docs.rs/assertables/8.16.0/assertables/assert_is_empty) // item.is_empty()`
* `[assert_len!(item);](https://docs.rs/assertables/8.17.0/assertables/assert_len) // item.len()`
* `[assert_is_empty!(item);](https://docs.rs/assertables/8.17.0/assertables/assert_is_empty) // item.is_empty()`

Matching for strings, regex, etc.:

* `[assert_contains!(container, containee);](https://docs.rs/assertables/8.16.0/assertables/assert_contains) // container.contains(containee)`
* `[assert_is_match!(matcher, matchee);](https://docs.rs/assertables/8.16.0/assertables/assert_is_match) // matcher.is_match(matchee)`
* `[assert_matches!(expression, pattern);](module@crate::assert_matches) // matches!(expression, pattern)`
* `[assert_is_match!(matcher, matchee);](https://docs.rs/assertables/8.17.0/assertables/assert_is_match) // matcher.is_match(matchee)`
* `[assert_contains!(container, containee);](https://docs.rs/assertables/8.17.0/assertables/assert_contains) // container.contains(containee)`

Collections for arrays, vectors, iterators, sets, maps:

* `[assert_iter_eq!(arr1, arr2);](https://docs.rs/assertables/8.16.0/assertables/assert_iter) // eq ne lt le gt ge`
* `[assert_set_eq!(vec1, vec2);](https://docs.rs/assertables/8.16.0/assertables/assert_set) // eq ne lt le gt ge etc.`
* `[assert_bag_eq!(map1, map2);](https://docs.rs/assertables/8.16.0/assertables/assert_bag) // eq ne lt le gt ge etc.`
* `[assert_iter_eq!(arr1, arr2);](https://docs.rs/assertables/8.17.0/assertables/assert_iter) // eq ne lt le gt ge`
* `[assert_set_eq!(vec1, vec2);](https://docs.rs/assertables/8.17.0/assertables/assert_set) // eq ne lt le gt ge etc.`
* `[assert_bag_eq!(map1, map2);](https://docs.rs/assertables/8.17.0/assertables/assert_bag) // eq ne lt le gt ge etc.`

Result Ok/Err:

* `[assert_ok!(result);](https://docs.rs/assertables/8.16.0/assertables/assert_ok) // eq ne lt le gt ge`
* `[assert_err!(result);](https://docs.rs/assertables/8.16.0/assertables/assert_err) // eq ne lt le gt ge`
* `[assert_ok!(result);](https://docs.rs/assertables/8.17.0/assertables/assert_ok) // eq ne lt le gt ge`
* `[assert_err!(result);](https://docs.rs/assertables/8.17.0/assertables/assert_err) // eq ne lt le gt ge`

Option Some/None:

* `[assert_some!(option);](https://docs.rs/assertables/8.16.0/assertables/assert_some) // eq ne lt le gt ge`
* `[assert_none!(option);](https://docs.rs/assertables/8.16.0/assertables/assert_none)`
* `[assert_some!(option);](https://docs.rs/assertables/8.17.0/assertables/assert_some) // eq ne lt le gt ge`
* `[assert_none!(option);](https://docs.rs/assertables/8.17.0/assertables/assert_none)`

Poll Ready/Pending:

* `[assert_ready!(poll);](https://docs.rs/assertables/8.16.0/assertables/assert_ready) // eq ne lt le gt ge`
* `[assert_pending!(poll);](https://docs.rs/assertables/8.16.0/assertables/assert_pending)`
* `[assert_ready!(poll);](https://docs.rs/assertables/8.17.0/assertables/assert_ready) // eq ne lt le gt ge`
* `[assert_pending!(poll);](https://docs.rs/assertables/8.17.0/assertables/assert_pending)`

Read file system paths and input/output streams:

* `[assert_fs_read_to_string_eq!(path1, path2);](https://docs.rs/assertables/8.16.0/assertables/assert_fs_read_to_string) // eq ne lt le gt ge`
* `[assert_io_read_to_string_eq!(stream1, stream2);](https://docs.rs/assertables/8.16.0/assertables/assert_io_read_to_string) // eq ne lt le gt ge`
* `[assert_fs_read_to_string_eq!(path1, path2);](https://docs.rs/assertables/8.17.0/assertables/assert_fs_read_to_string) // eq ne lt le gt ge`
* `[assert_io_read_to_string_eq!(stream1, stream2);](https://docs.rs/assertables/8.17.0/assertables/assert_io_read_to_string) // eq ne lt le gt ge`

Run commands and programs then assert on stdout or stderr:

* `[assert_command_stdout_eq!(command1, command2);](https://docs.rs/assertables/8.16.0/assertables/assert_command) // eq ne lt le gt ge etc.`
* `[assert_program_args_stdout_eq!(program1, args1, program2, args2);](https://docs.rs/assertables/8.16.0/assertables/assert_program_args) // eq ne lt le gt ge etc.`
* `[assert_command_stdout_eq!(command1, command2);](https://docs.rs/assertables/8.17.0/assertables/assert_command) // eq ne lt le gt ge etc.`
* `[assert_program_args_stdout_eq!(program1, args1, program2, args2);](https://docs.rs/assertables/8.17.0/assertables/assert_program_args) // eq ne lt le gt ge etc.`

Function comparisons, which are especially good for refactoring:

* `[assert_fn_eq!(fn1, fn2);](https://docs.rs/assertables/8.16.0/assertables/assert_fn) // functions that return values`
* `[assert_fn_ok_eq!(fn1, fn2);](https://docs.rs/assertables/8.16.0/assertables/assert_fn_ok) // functions that return Ok`
* `[assert_fn_err_eq!(fn1, fn2);](https://docs.rs/assertables/8.16.0/assertables/assert_fn_err) // functions that return Err`
* `[assert_fn_eq!(fn1, fn2);](https://docs.rs/assertables/8.17.0/assertables/assert_fn) // functions that return values`
* `[assert_fn_ok_eq!(fn1, fn2);](https://docs.rs/assertables/8.17.0/assertables/assert_fn_ok) // functions that return Ok`
* `[assert_fn_err_eq!(fn1, fn2);](https://docs.rs/assertables/8.17.0/assertables/assert_fn_err) // functions that return Err`


## Forms

All assertables macros have forms for different outcomes:

* `[assert_gt!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.assert_gt.html) // panic during typical test`
* `[assert_gt_as_result!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.assert_gt_as_result.html) // return Ok or Err`
* `[debug_assert_gt!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.debug_assert_gt.html) // panic when in debug mode`
* `[assert_gt!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.assert_gt.html) // panic during typical test`
* `[assert_gt_as_result!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.assert_gt_as_result.html) // return Ok or Err`
* `[debug_assert_gt!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.debug_assert_gt.html) // panic when in debug mode`

All assertables macros have forms for an optional message:

* `[assert_gt!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.assert_gt.html) // automatic error message`
* `[assert_gt!(a, b, "your text");](https://docs.rs/assertables/8.16.0/assertables/macro.assert_gt.html) // custom error message`
* `[assert_gt!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.assert_gt.html) // automatic error message`
* `[assert_gt!(a, b, "your text");](https://docs.rs/assertables/8.17.0/assertables/macro.assert_gt.html) // custom error message`

Many assertables macros have forms for comparing left hand side (LHS) and right hand side (RHS) as the same type or as an arbitrary expression:

* `[assert_ok_eq!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.assert_ok_eq.html) // Ok(…) = Ok(…)`
* `[assert_ok_eq_expr!(a, b);](https://docs.rs/assertables/8.16.0/assertables/macro.assert_ok_eq_expr.html) // Ok(…) = expression`
* `[assert_ok_eq!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.assert_ok_eq.html) // Ok(…) = Ok(…)`
* `[assert_ok_eq_expr!(a, b);](https://docs.rs/assertables/8.17.0/assertables/macro.assert_ok_eq_expr.html) // Ok(…) = expression`


## Tracking

* Package: assertables-rust-crate
* Version: 8.16.0
* Version: 8.17.0
* Created: 2021-03-30T15:47:49Z
* Updated: 2024-10-08T15:07:34Z
* Updated: 2024-10-09T18:40:20Z
* License: MIT or Apache-2.0 or GPL-2.0 or GPL-3.0 or contact us for more
* Contact: Joel Parker Henderson ([email protected])
2 changes: 1 addition & 1 deletion doc/lib/all.html

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions doc/lib/assert_matches/assert_matches/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Assert expression matches a case."><title>lib::assert_matches::assert_matches - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../../static.files/rustdoc-c5d6553a23f1e5a6.css"><meta name="rustdoc-vars" data-root-path="../../../" data-static-root-path="../../../static.files/" data-current-crate="lib" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0 (eeb90cda1 2024-09-04)" data-channel="1.81.0" data-search-js="search-d234aafac6c221dd.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../../static.files/storage-118b08c4c78b968e.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../../static.files/main-d2fab2bf619172d3.js"></script><noscript><link rel="stylesheet" href="../../../static.files/noscript-df360f571f6edeae.css"></noscript><link rel="alternate icon" type="image/png" href="../../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../../lib/index.html">lib</a></h2></div><h2 class="location"><a href="#">Module assert_matches</a></h2><div class="sidebar-elems"><h2><a href="../index.html">In lib::assert_matches</a></h2></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Module <a href="../../index.html">lib</a>::<wbr><a href="../index.html">assert_matches</a>::<wbr><a class="mod" href="#">assert_matches</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../../../src/lib/assert_matches/assert_matches.rs.html#1-218">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Assert expression matches a case.</p>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>assertables::<span class="kw-2">*</span>;

<span class="kw">let </span>a = <span class="string">'a'</span>;
<span class="macro">assert_matches!</span>(a, <span class="string">'a'</span>..=<span class="string">'z'</span>);</code></pre></div>
<p>Note: this implementation of <code>assert_matches</code> is relatively basic.</p>
<ul>
<li>
<p>If you want more capabilities, consider the crate <code>assert_matches</code>.</p>
</li>
<li>
<p>If you’re using Rust nightly, use the std lib macro <code>assert_matches</code>.</p>
</li>
</ul>
<h2 id="module-macros"><a class="doc-anchor" href="#module-macros">§</a>Module macros</h2>
<ul>
<li><a href="../../macro.assert_matches.html" title="macro lib::assert_matches"><code>assert_matches</code></a></li>
<li><a href="../../macro.assert_matches_as_result.html" title="macro lib::assert_matches_as_result"><code>assert_matches_as_result</code></a></li>
<li><a href="../../macro.debug_assert_matches.html" title="macro lib::debug_assert_matches"><code>debug_assert_matches</code></a></li>
</ul>
</div></details></section></div></main></body></html>
1 change: 1 addition & 0 deletions doc/lib/assert_matches/assert_matches/sidebar-items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.SIDEBAR_ITEMS = {};
Loading

0 comments on commit 4ddceb5

Please sign in to comment.