claims-0.8.0/.cargo_vcs_info.json0000644000000001360000000000100123110ustar { "git": { "sha1": "ab4f800525475e9c951584a6000ea6cc13fa30ea" }, "path_in_vcs": "" }claims-0.8.0/.github/workflows/ci.yml000064400000000000000000000047761046102023000156320ustar 00000000000000name: Continuous Integration on: push: pull_request: permissions: contents: read env: CARGO_TERM_COLOR: always jobs: test_debug: runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: - 1.38.0 - 1.53.0 - stable - beta - nightly steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} components: rust-src - run: cargo test test_release: runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: - 1.38.0 - 1.53.0 - stable - beta - nightly steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} components: rust-src - run: cargo test --release no-std: runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: - 1.38.0 - stable - beta - nightly steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} targets: thumbv6m-none-eabi - run: cargo build --target thumbv6m-none-eabi fmt: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly with: components: rustfmt - run: cargo fmt -- --check check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly with: components: rust-src - run: cargo check clippy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly with: components: clippy, rust-src - run: cargo clippy -- --deny warnings doc: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - run: cargo doc --no-deps env: RUSTDOCFLAGS: -D warnings msrv: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly - run: curl -LsSf https://github.com/foresterre/cargo-msrv/releases/download/v0.15.1/cargo-msrv_v0.15.1_Linux_x86_64.tar | tar xf - -C ~/.cargo/bin - run: cargo msrv verify claims-0.8.0/.gitignore000064400000000000000000000000251046102023000130660ustar 00000000000000/target Cargo.lock claims-0.8.0/CHANGELOG.md000064400000000000000000000057571046102023000127300ustar 00000000000000# Changelog ## 0.8.0 - 2024-11-16 ### Changed - `assert_err_eq`, `assert_ok_eq`, and `assert_some_eq` now properly display custom messages. - MSRV raised to `1.38.0`. - Rust edition bumped to 2018. - Improved error messages. - Removed `Debug` requirement for `Ok(T)` in `assert_ready_ok!` macro. - Removed `Debug` requirement for `Err(E)` in `assert_ready_err!` macro. ### Fixed - Corrected the name of `debug_assert_ready_ok_eq!` to `debug_assert_ready_eq!`. - `assert_pending!` and `debug_assert_pending!` now correctly display custom messages. - `assert_ready!` and `debug_assert_ready!` now correctly display custom messages. - `debug_assert_pending!` no longer emits warnings for unused values. - `assert_matches!` and `debug_assert_matches!` no longer emit warnings if the given pattern covers all cases. - `assert_matches!` and `debug_assert_matches!` now include `if` guard in the pattern in the error message. - Fixed macro hygiene issues for all macros. ### Removed - Dependency on `autocfg`. ## 0.7.1 - 2022-08-31 ### Changed - Fixed release CI job. ## 0.7.0 - 2022-08-31 ### Added - `assert_err_eq!` and `debug_assert_err_eq!` macros. Thanks @Anders429! ## 0.6.0 - 2022-08-30 ### Changed - Forked the [original project](https://github.com/svartalf/rust-claim) as the maintainer is unreachable at the moment. Renamed crate to `claims`. - Fixed versioning conflict with `autocfg` v1.x, thanks @Turbo87! ## 0.5.0 - 2021-02-04 ### Changed - Fixed compatibility for Rust versions <= `1.6.0`. - Fixed compatibility with Rust version `1.30.0`. - MSRV for `assert_matches!` macro was changed from Rust `1.32.0` to `1.26.0`. ## 0.4.0 - 2020-10-26 ### Changed - Removed `Debug` requirement for `Ok(T)` in `assert_ok!` macro. - Removed `Debug` requirement for `Err(e)` in `assert_err!` macro. ## 0.3.1 - 2020-03-13 ### Changed - Minimal required Rust version for `assert_matches!` macro downgraded from the `1.37` to `1.32`. ## 0.3.0 - 2020-03-13 ### Added - `assert_matches!` and `debug_assert_matches!` macros. ## 0.2.0 - 2020-03-09 ### Added - `assert_some_eq!` and `debug_assert_some_eq!` macros. - `assert_ok_eq!` and `debug_assert_ok_eq!` macros. ### Changed - Ensuring support for older Rust versions. ## 0.1.1 - 2020-03-08 ### Changed - Improved documentation. ## 0.1.0 - 2020-03-07 ### Added - `assert_err!` and `debug_assert_err!` macros. - `assert_ge!` and `debug_assert_ge!` macros. - `assert_gt!` and `debug_assert_gt!` macros. - `assert_le!` and `debug_assert_le!` macros. - `assert_lt!` and `debug_assert_lt!` macros. - `assert_none!` and `debug_assert_none!` macros. - `assert_ok!` and `debug_assert_ok!` macros. - `assert_pending!` and `debug_assert_pending!` macros. - `assert_ready!` and `debug_assert_ready!` macros. - `assert_ready_eq!` and `debug_assert_ready_eq!` macros. - `assert_ready_err!` and `debug_assert_ready_err!` macros. - `assert_ready_ok!` and `debug_assert_ready_ok!` macros. - `assert_some!` and `debug_assert_some!` macros. claims-0.8.0/Cargo.toml0000644000000023770000000000100103200ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "claims" version = "0.8.0" authors = [ "svartalf ", "mattwilkinsonn ", "Anders Evensen", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Additional assertion macros for testing." readme = "README.md" keywords = [ "assert", "test", "testing", "macros", "assertion", ] categories = [ "development-tools", "development-tools::debugging", "development-tools::testing", "no-std", "no-std::no-alloc", ] license = "Apache-2.0 OR MIT" repository = "https://github.com/Anders429/claims" [package.metadata] msrv = "1.38.0" [lib] name = "claims" path = "src/lib.rs" [dev-dependencies.rustversion] version = "1.0.18" claims-0.8.0/Cargo.toml.orig000064400000000000000000000011431046102023000137670ustar 00000000000000[package] name = "claims" description = "Additional assertion macros for testing." version = "0.8.0" authors = ["svartalf ", "mattwilkinsonn ", "Anders Evensen"] license = "Apache-2.0 OR MIT" categories = ["development-tools", "development-tools::debugging", "development-tools::testing", "no-std", "no-std::no-alloc"] keywords = ["assert", "test", "testing", "macros", "assertion"] repository = "https://github.com/Anders429/claims" readme = "README.md" edition = "2018" [package.metadata] msrv = "1.38.0" [dev-dependencies] rustversion = "1.0.18" claims-0.8.0/LICENSE-APACHE000064400000000000000000000264571046102023000130430ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2019-NOW svartalf Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. claims-0.8.0/LICENSE-MIT000064400000000000000000000021151046102023000125340ustar 00000000000000Copyright (c) 2019 svartalf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. claims-0.8.0/README.md000064400000000000000000000055251046102023000123670ustar 00000000000000# claims [![crates.io](https://img.shields.io/crates/v/claims.svg)](https://crates.io/crates/claims) [![docs.rs](https://docs.rs/claims/badge.svg)](https://docs.rs/claims) [![Build Status](https://github.com/Anders429/claims/workflows/Continuous%20Integration/badge.svg)](https://github.com/Anders429/claims/actions) [![License](https://img.shields.io/crates/l/claims)](#license) [![MSRV](https://img.shields.io/badge/rustc-1.38.0+-yellow.svg)](#minimum-supported-rust-version) Additional assertion macros for testing. This crate provides assertion macros that are missing in the Rust standard library: * Comparison: [`assert_ge`](https://docs.rs/claims/latest/claims/macro.assert_ge.html), [`assert_gt`](https://docs.rs/claims/latest/claims/macro.assert_gt.html), [`assert_le`](https://docs.rs/claims/latest/claims/macro.assert_le.html), and [`assert_lt`](https://docs.rs/claims/latest/claims/macro.assert_lt.html). * Matching: [`assert_matches`](https://docs.rs/claims/latest/claims/macro.assert_matches.html). * `Result`: [`assert_ok`](https://docs.rs/claims/latest/claims/macro.assert_ok.html), [`assert_err`](https://docs.rs/claims/latest/claims/macro.assert_err.html), [`assert_ok_eq`](https://docs.rs/claims/latest/claims/macro.assert_ok_eq.html), and [`assert_err_eq`](https://docs.rs/claims/latest/claims/macro.assert_err_eq.html). * `Option`: [`assert_some`](https://docs.rs/claims/latest/claims/macro.assert_some.html), [`assert_none`](https://docs.rs/claims/latest/claims/macro.assert_none.html), and [`assert_some_eq`](https://docs.rs/claims/latest/claims/macro.assert_some_eq.html). * `Poll`: [`assert_pending`](https://docs.rs/claims/latest/claims/macro.assert_pending.html), [`assert_ready`](https://docs.rs/claims/latest/claims/macro.assert_ready.html), [`assert_ready_ok`](https://docs.rs/claims/latest/claims/macro.assert_ready_ok.html), [`assert_ready_err`](https://docs.rs/claims/latest/claims/macro.assert_ready_err.html), and [`assert_ready_eq`](https://docs.rs/claims/latest/claims/macro.assert_ready_eq.html). ## Installation Add the following to your `Cargo.toml` manifest to use this crate for tests, examples, and benchmarks: ```toml [dev-dependencies] claims = "0.8" ``` ## Usage Check out the [documentation](https://docs.rs/claims) for available macros and examples. ## Minimum Supported Rust Version This crate is guaranteed to compile on stable `rustc 1.38.0` and up. ## License Licensed under either of [Apache License 2.0](https://github.com/Anders429/claims/blob/master/LICENSE-APACHE) or [MIT license](https://github.com/Anders429/claims/blob/master/LICENSE-MIT) at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. claims-0.8.0/src/assert_err.rs000064400000000000000000000105111046102023000144050ustar 00000000000000/// Asserts that the expression matches an [`Err(_)`] variant, returning the contained value. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_err!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided /// with or without arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Err(()); /// /// assert_err!(res); /// /// // With a custom message. /// assert_err!(res, "we are checking if there was an error with {:?}", res); /// # } /// ``` /// /// An `Ok(_)` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Ok(42); /// /// assert_err!(res); // Will panic /// # } /// ``` /// /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`Err(_)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err /// [`debug_assert_err!`]: crate::debug_assert_err! #[macro_export] macro_rules! assert_err { ($cond:expr $(,)?) => { match $cond { ::core::result::Result::Err(e) => e, ::core::result::Result::Ok(t) => ::core::panic!("assertion failed, expected Err(_), got Ok({:?})", t), } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::result::Result::Err(e) => e, ::core::result::Result::Ok(t) => ::core::panic!("assertion failed, expected Err(_), got Ok({:?}): {}", t, ::core::format_args!($($arg)+)), } }; } /// Asserts that the expression matches an [`Err(_)`] variant in debug builds. /// /// This macro behaves nearly the same as [`assert_err!`] on debug builds, although it does not /// return the value contained in the `Err` variant. On release builds it is a no-op. /// /// [`Err(_)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err #[macro_export] macro_rules! debug_assert_err { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_err!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn err() { assert_err!(Err::<(), _>(())); } #[test] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(())")] fn not_err() { assert_err!(Ok::<_, ()>(())); } #[test] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(()): foo")] fn not_err_custom_message() { assert_err!(Ok::<_, ()>(()), "foo"); } #[test] fn err_value_returned() { let value = assert_err!(Err::<(), _>(42)); assert_eq!(value, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_err() { debug_assert_err!(Err::<(), _>(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(())")] fn debug_not_err() { debug_assert_err!(Ok::<_, ()>(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(()): foo")] fn debug_not_err_custom_message() { debug_assert_err!(Ok::<_, ()>(()), "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_err() { debug_assert_err!(Ok::<_, ()>(())); } #[test] fn does_not_require_err_to_impl_debug() { enum Foo { Bar, } assert_err!(Err::<(), _>(Foo::Bar)); } #[test] fn debug_does_not_require_err_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_err!(Err::<(), _>(Foo::Bar)); } #[test] fn does_not_require_err_to_impl_debug_custom_message() { enum Foo { Bar, } assert_err!(Err::<(), _>(Foo::Bar), "foo"); } #[test] fn debug_does_not_require_err_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_err!(Err::<(), _>(Foo::Bar), "foo"); } } claims-0.8.0/src/assert_err_eq.rs000064400000000000000000000122711046102023000150770ustar 00000000000000/// Asserts that the left expression contains an [`Err(E)`] variant and its contained value of type /// `E` equals the right expression. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. See /// [`debug_assert_err_eq!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result<(), i32> = Err(1); /// /// assert_err_eq!(res, 1); /// /// // With a custom message /// assert_err_eq!(res, 1, "Everything is good with {:?}", res); /// # } /// ``` /// /// The contained value will be returned from the macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result<(), i32> = Err(1); /// /// let value = assert_err_eq!(res, 1); /// assert_eq!(value, 1); /// # } /// ``` /// /// An `Ok(_)` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result<(), i32> = Ok(()); /// /// assert_err_eq!(res, 1); // Will panic /// # } /// ``` /// /// [`Err(E)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_err_eq!`]: crate::debug_assert_err_eq! #[macro_export] macro_rules! assert_err_eq { ($cond:expr, $expected:expr $(,)?) => { match $cond { ::core::result::Result::Err(t) => { ::core::assert_eq!(t, $expected); t }, ok @ ::core::result::Result::Ok(_) => { ::core::panic!("assertion failed, expected Err(_), got {:?}", ok); } } }; ($cond:expr, $expected:expr, $($arg:tt)+) => { match $cond { ::core::result::Result::Err(t) => { ::core::assert_eq!(t, $expected, $($arg)+); t }, ok @ ::core::result::Result::Ok(_) => { ::core::panic!("assertion failed, expected Err(_), got {:?}: {}", ok, ::core::format_args!($($arg)+)); } } }; } /// Asserts that the left expression contains an [`Err(E)`] variant and its contained value of type /// `E` equals the right expression in debug builds. /// /// This macro behaves nearly the same as [`assert_err_eq!`] on debug builds, although it does not /// return the value contained in the `Err` variant. On release builds it is a no-op. /// /// [`Err(E)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err #[macro_export] macro_rules! debug_assert_err_eq { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_err_eq!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn equal() { assert_err_eq!(Err::<(), _>(42), 42); } #[test] #[should_panic] fn not_equal() { assert_err_eq!(Err::<(), _>(42), 100); } #[test] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(())")] fn not_err() { assert_err_eq!(Ok::<_, usize>(()), 42); } #[test] #[should_panic(expected = "foo")] fn not_equal_custom_message() { assert_err_eq!(Err::<(), _>(1), 2, "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(()): foo")] fn not_err_custom_message() { assert_err_eq!(Ok::<_, usize>(()), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_err_eq!(Err::<(), _>(42), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic] fn debug_not_equal() { debug_assert_err_eq!(Err::<(), _>(42), 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(())")] fn debug_not_err() { debug_assert_err_eq!(Ok::<_, usize>(()), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "foo")] fn debug_not_equal_custom_message() { debug_assert_err_eq!(Err::<(), _>(1), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Err(_), got Ok(()): foo")] fn debug_not_err_custom_message() { debug_assert_err_eq!(Ok::<_, usize>(()), 2, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_equal() { debug_assert_err_eq!(Err::<(), _>(42), 100); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_err() { debug_assert_err_eq!(Ok::<_, usize>(()), 42); } } claims-0.8.0/src/assert_ge.rs000064400000000000000000000103031046102023000142070ustar 00000000000000/// Asserts that the first expression is greater than or equal to the second. /// /// Requires that both expressions be comparable with `>=`. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ge!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_ge!(2, 1); /// /// // With a custom message. /// assert_ge!(2, 1, "Expecting that {} is greater or equal than {}", 2, 1); /// assert_ge!(5, 5, "Expecting that both arguments are equal"); /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_ge!(5, 6); // Will panic /// # } /// ``` /// /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ge!`]: crate::debug_assert_ge! #[macro_export] macro_rules! assert_ge { ($left:expr, $right:expr $(,)?) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val >= *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left >= right)` left: `{:?}`, right: `{:?}`"#, &*left_val, &*right_val) } } } }; ($left:expr, $right:expr, $($arg:tt)+) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val >= *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left >= right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, ::core::format_args!($($arg)+)) } } } }; } /// Asserts that the first expression is greater than or equal to the second on debug builds. /// /// This macro behaves the same as [`assert_ge!`] on debug builds. On release builds it is a no-op. #[macro_export] macro_rules! debug_assert_ge { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ge!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn greater_than() { assert_ge!(5, 3); } #[test] fn equal() { assert_ge!(3, 3); } #[test] #[should_panic( expected = "assertion failed: `(left >= right)`\n left: `1`,\n right: `3`" )] fn less_than() { assert_ge!(1, 3); } #[test] #[should_panic( expected = "assertion failed: `(left >= right)`\n left: `1`,\n right: `3`: foo" )] fn less_than_custom_message() { assert_ge!(1, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_greater_than() { debug_assert_ge!(5, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_ge!(3, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left >= right)`\n left: `1`,\n right: `3`" )] fn debug_less_than() { debug_assert_ge!(1, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left >= right)`\n left: `1`,\n right: `3`: foo" )] fn debug_less_than_custom_message() { debug_assert_ge!(1, 3, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_less_than() { debug_assert_ge!(1, 3); } } claims-0.8.0/src/assert_gt.rs000064400000000000000000000117741046102023000142430ustar 00000000000000/// Asserts that the first expression is greater than the second. /// /// Requires that both expressions be comparable with `>`. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_gt!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_gt!(2, 1); /// /// // With a custom message /// assert_gt!(2, 1, "Expecting that {} is greater or equal than {}", 2, 1); /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_gt!(5, 5); // Will panic /// /// // With a custom message /// assert_gt!(5, 6, "Not expecting {} to be greater than {}", 5, 6); /// # } /// ``` /// /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_gt!`]: crate::debug_assert_gt! #[macro_export] macro_rules! assert_gt { ($left:expr, $right:expr $(,)?) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val > *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left > right)` left: `{:?}`, right: `{:?}`"#, &*left_val, &*right_val) } } } }; ($left:expr, $right:expr, $($arg:tt)+) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val > *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left > right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, ::core::format_args!($($arg)+)) } } } }; } /// Asserts that the first expression is greater than the second in debug builds. /// /// This macro behaves the same as [`assert_gt!`] on debug builds. On release builds it is a no-op. #[macro_export] macro_rules! debug_assert_gt { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_gt!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn greater_than() { assert_gt!(5, 3); } #[test] #[should_panic(expected = "assertion failed: `(left > right)`\n left: `3`,\n right: `3`")] fn equal() { assert_gt!(3, 3); } #[test] #[should_panic(expected = "assertion failed: `(left > right)`\n left: `1`,\n right: `3`")] fn less_than() { assert_gt!(1, 3); } #[test] #[should_panic( expected = "assertion failed: `(left > right)`\n left: `3`,\n right: `3`: foo" )] fn equal_custom_message() { assert_gt!(3, 3, "foo"); } #[test] #[should_panic( expected = "assertion failed: `(left > right)`\n left: `1`,\n right: `3`: foo" )] fn less_than_custom_message() { assert_gt!(1, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_greater_than() { debug_assert_gt!(5, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed: `(left > right)`\n left: `3`,\n right: `3`")] fn debug_equal() { debug_assert_gt!(3, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed: `(left > right)`\n left: `1`,\n right: `3`")] fn debug_less_than() { debug_assert_gt!(1, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left > right)`\n left: `3`,\n right: `3`: foo" )] fn debug_equal_custom_message() { debug_assert_gt!(3, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left > right)`\n left: `1`,\n right: `3`: foo" )] fn debug_less_than_custom_message() { debug_assert_gt!(1, 3, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_equal() { debug_assert_gt!(3, 3); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_less_than() { debug_assert_gt!(1, 3); } } claims-0.8.0/src/assert_le.rs000064400000000000000000000105711046102023000142230ustar 00000000000000/// Asserts that the first expression is less or equal than the second. /// /// Requires that both expressions be comparable with `<=`. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_le!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_le!(1, 2); /// /// // With a custom message /// assert_le!(5, 5, "Expecting that {} is less or equal than {}", 5, 5); /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_le!(6, 5); // Will panic /// /// // With a custom message /// assert_le!(6, 5, "Not expecting {} to be less or equal than {}", 6, 5); /// # } /// ``` /// /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_le!`]: crate::debug_assert_le! #[macro_export] macro_rules! assert_le { ($left:expr, $right:expr $(,)?) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val <= *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left <= right)` left: `{:?}`, right: `{:?}`"#, &*left_val, &*right_val) } } } }; ($left:expr, $right:expr, $($arg:tt)+) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val <= *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left <= right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, ::core::format_args!($($arg)+)) } } } }; } /// Asserts that the first expression is less or equal than the second on debug builds. /// /// This macro behaves the same as [`assert_le!`] on debug builds. On release builds it is a no-op. #[macro_export] macro_rules! debug_assert_le { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_le!($($arg)*); } } #[cfg(test)] mod tests { #[test] #[should_panic( expected = "assertion failed: `(left <= right)`\n left: `5`,\n right: `3`" )] fn greater_than() { assert_le!(5, 3); } #[test] fn equal() { assert_le!(3, 3); } #[test] fn less_than() { assert_le!(1, 3); } #[test] #[should_panic( expected = "assertion failed: `(left <= right)`\n left: `5`,\n right: `3`: foo" )] fn greater_than_custom_message() { assert_le!(5, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left <= right)`\n left: `5`,\n right: `3`" )] fn debug_greater_than() { debug_assert_le!(5, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_le!(3, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_less_than() { debug_assert_le!(1, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left <= right)`\n left: `5`,\n right: `3`: foo" )] fn debug_greater_than_custom_message() { debug_assert_le!(5, 3, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_greater_than() { debug_assert_le!(5, 3); } } claims-0.8.0/src/assert_lt.rs000064400000000000000000000120061046102023000142350ustar 00000000000000/// Asserts that the first expression is less than the second. /// /// Requires that both expressions be comparable with `<`. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_lt!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_lt!(1, 2); /// /// // With a custom message /// assert_lt!(4, 5, "Expecting that {} is less than {}", 4, 5); /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// assert_lt!(5, 5); // Will panic /// assert_lt!(6, 5); /// /// // With a custom message /// assert_lt!(6, 5, "Not expecting {} to be less than {}", 6, 5); /// # } /// ``` /// /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_lt!`]: crate::debug_assert_lt! #[macro_export] macro_rules! assert_lt { ($left:expr, $right:expr $(,)?) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val < *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left < right)` left: `{:?}`, right: `{:?}`"#, &*left_val, &*right_val) } } } }; ($left:expr, $right:expr, $($arg:tt)+) => { match (&$left, &$right) { (left_val, right_val) => { if !(*left_val < *right_val) { // The reborrows below are intentional. Without them, the stack slot for the // borrow is initialized even before the values are compared, leading to a // noticeable slow down. ::core::panic!(r#"assertion failed: `(left < right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, ::core::format_args!($($arg)+)) } } } }; } /// Asserts that the first expression is less than the second on debug builds. /// /// This macro behaves the same as [`assert_lt!`] on debug builds. On release builds it is a no-op. #[macro_export] macro_rules! debug_assert_lt { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_lt!($($arg)*); } } #[cfg(test)] mod tests { #[test] #[should_panic(expected = "assertion failed: `(left < right)`\n left: `5`,\n right: `3`")] fn greater_than() { assert_lt!(5, 3); } #[test] #[should_panic(expected = "assertion failed: `(left < right)`\n left: `3`,\n right: `3`")] fn equal() { assert_lt!(3, 3); } #[test] fn less_than() { assert_lt!(1, 3); } #[test] #[should_panic( expected = "assertion failed: `(left < right)`\n left: `5`,\n right: `3`: foo" )] fn greater_than_custom_message() { assert_lt!(5, 3, "foo"); } #[test] #[should_panic( expected = "assertion failed: `(left < right)`\n left: `3`,\n right: `3`: foo" )] fn equal_custom_message() { assert_lt!(3, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed: `(left < right)`\n left: `5`,\n right: `3`")] fn debug_greater_than() { debug_assert_lt!(5, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed: `(left < right)`\n left: `3`,\n right: `3`")] fn debug_equal() { debug_assert_lt!(3, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_less_than() { debug_assert_lt!(1, 3); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left < right)`\n left: `5`,\n right: `3`: foo" )] fn debug_greater_than_custom_message() { debug_assert_lt!(5, 3, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed: `(left < right)`\n left: `3`,\n right: `3`: foo" )] fn debug_equal_custom_message() { debug_assert_lt!(3, 3, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_greater_than() { debug_assert_lt!(5, 3); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_equal() { debug_assert_lt!(3, 3); } } claims-0.8.0/src/assert_matches.rs000064400000000000000000000175631046102023000152570ustar 00000000000000/// Asserts that the expression matches the provided pattern. /// /// Works like the [`std::matches!`] macro, but panics if there is no match. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_matches!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let foo = 'f'; /// assert_matches!(foo, 'A'..='Z' | 'a'..='z'); /// /// // With a custom message /// assert_matches!(foo, 'A'..='Z' | 'a'..='z', "expecting it to be letter: {}", foo); /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let bar: Option = None; /// assert_matches!(bar, Some(x) if x > 2); // Will panic /// # } /// ``` /// /// [`std::matches!`]: https://doc.rust-lang.org/stable/std/macro.matches.html /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_matches!`]: crate::debug_assert_matches! #[macro_export] macro_rules! assert_matches { ($expression:expr, $($pattern:pat)|+ $(if $guard:expr)? $(,)?) => { #[allow(unreachable_patterns)] match $expression { $($pattern)|+ $(if $guard)? => {}, other => { ::core::panic!(r#"assertion failed, expression does not match the given pattern. expression: {:?} pattern: {}"#, other, ::core::stringify!($($pattern)|+ $(if $guard)?)); } } }; ($expression:expr, $($pattern:pat)|+ $(if $guard:expr)?, $($arg:tt)+) => { #[allow(unreachable_patterns)] match $expression { $($pattern)|+ $(if $guard)? => {}, other => { ::core::panic!(r#"assertion failed, expression does not match the given pattern. expression: {:?} pattern: {}: {}"#, other, ::core::stringify!($($pattern)|+ $(if $guard)?), ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression matches the provided pattern on debug builds. /// /// /// This macro behaves the same as [`assert_matches!`] on debug builds. On release builds it is a /// no-op. #[macro_export] macro_rules! debug_assert_matches { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_matches!($($arg)*); } } #[cfg(test)] mod tests { #[allow(dead_code)] #[derive(Debug)] enum Foo { Bar(usize), Baz(usize), } #[test] fn matches() { assert_matches!(Foo::Bar(42), Foo::Bar(_)); } #[test] fn matches_multiple_variants() { assert_matches!(Foo::Baz(42), Foo::Bar(_) | Foo::Baz(_)); } #[test] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Baz(_)" )] fn not_matches() { assert_matches!(Foo::Bar(42), Foo::Baz(_)); } #[test] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Baz(_): foo" )] fn not_matches_custom_message() { assert_matches!(Foo::Bar(42), Foo::Baz(_), "foo"); } #[test] fn matches_if_guard() { assert_matches!(Foo::Bar(42), Foo::Bar(x) if x < 100); } #[test] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Bar(x) if x > 100" )] fn not_matches_if_guard() { assert_matches!(Foo::Bar(42), Foo::Bar(x) if x > 100); } #[test] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Bar(x) if x > 100: foo" )] fn not_matches_if_guard_custom_message() { assert_matches!(Foo::Bar(42), Foo::Bar(x) if x > 100, "foo"); } #[rustversion::since(1.53)] #[test] fn matches_nested_pattern() { assert_matches!(Some(Foo::Bar(42)), Some(Foo::Bar(_) | Foo::Baz(1 | 2))); } #[rustversion::since(1.53)] #[test] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: None\n pattern: Some(Foo::Bar(_) | Foo::Baz(1 | 2))" )] fn not_matches_nested_pattern() { assert_matches!(None, Some(Foo::Bar(_) | Foo::Baz(1 | 2))); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_matches() { debug_assert_matches!(Foo::Bar(42), Foo::Bar(_)); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_matches_multiple_variants() { debug_assert_matches!(Foo::Baz(42), Foo::Bar(_) | Foo::Baz(_)); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Baz(_)" )] fn debug_not_matches() { debug_assert_matches!(Foo::Bar(42), Foo::Baz(_)); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Baz(_): foo" )] fn debug_not_matches_custom_message() { debug_assert_matches!(Foo::Bar(42), Foo::Baz(_), "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_matches_if_guard() { debug_assert_matches!(Foo::Bar(42), Foo::Bar(x) if x < 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Bar(x) if x > 100" )] fn debug_not_matches_if_guard() { debug_assert_matches!(Foo::Bar(42), Foo::Bar(x) if x > 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: Bar(42)\n pattern: Foo::Bar(x) if x > 100: foo" )] fn debug_not_matches_if_guard_custom_message() { debug_assert_matches!(Foo::Bar(42), Foo::Bar(x) if x > 100, "foo"); } #[rustversion::since(1.53)] #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_matches_nested_pattern() { debug_assert_matches!(Some(Foo::Bar(42)), Some(Foo::Bar(_) | Foo::Baz(1 | 2))); } #[rustversion::since(1.53)] #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic( expected = "assertion failed, expression does not match the given pattern.\n expression: None\n pattern: Some(Foo::Bar(_) | Foo::Baz(1 | 2))" )] fn debug_not_matches_nested_pattern() { debug_assert_matches!(None, Some(Foo::Bar(_) | Foo::Baz(1 | 2))); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_matches() { debug_assert_matches!(Foo::Bar(42), Foo::Baz(_)); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_matches_if_guard() { debug_assert_matches!(Foo::Bar(42), Foo::Bar(x) if x > 100); } #[rustversion::since(1.53)] #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_matches_nested_pattern() { debug_assert_matches!(None, Some(Foo::Bar(_) | Foo::Baz(1 | 2))); } } claims-0.8.0/src/assert_none.rs000064400000000000000000000067551046102023000145730ustar 00000000000000/// Asserts that the expression is [`None`]. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_none!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe: Option = None; /// /// assert_none!(maybe); /// /// // With a custom message /// assert_none!(maybe, "Yep, there is nothing in here"); /// assert_none!(maybe, "we asserting that there are no droids we are looking for at {:?}", maybe); /// # } /// ``` /// /// A `Some(_)` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe = Some(42i32); /// /// assert_none!(maybe); // Will panic /// # } /// ``` /// /// [`None`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.None /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_none!`]: crate::debug_assert_none! #[macro_export] macro_rules! assert_none { ($cond:expr $(,)?) => { match $cond { none @ ::core::option::Option::None => none, some @ ::core::option::Option::Some(_) => { ::core::panic!("assertion failed, expected None, got {:?}", some); } } }; ($cond:expr, $($arg:tt)+) => { match $cond { none @ ::core::option::Option::None => none, some @ ::core::option::Option::Some(_) => { ::core::panic!("assertion failed, expected None, got {:?}: {}", some, ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression is [`None`] on debug builds. /// /// This macro behaves the same as [`assert_none!`] on debug builds. On release builds it is a /// no-op. #[macro_export] macro_rules! debug_assert_none { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_none!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn none() { assert_none!(None::<()>); } #[test] #[should_panic(expected = "assertion failed, expected None, got Some(())")] fn not_none() { assert_none!(Some(())); } #[test] #[should_panic(expected = "assertion failed, expected None, got Some(()): foo")] fn not_none_custom_message() { assert_none!(Some(()), "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_none() { debug_assert_none!(None::<()>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected None, got Some(())")] fn debug_not_none() { debug_assert_none!(Some(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected None, got Some(()): foo")] fn debug_not_none_custom_message() { debug_assert_none!(Some(()), "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_none() { debug_assert_none!(Some(())); } } claims-0.8.0/src/assert_ok.rs000064400000000000000000000112371046102023000142340ustar 00000000000000/// Asserts that the expression matches an [`Ok(_)`] variant, returning the contained value. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ok!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Ok(1); /// /// assert_ok!(res); /// /// // With a custom message /// assert_ok!(res, "Everything is good with {:?}", res); /// # } /// ``` /// /// The contained value will be returned from the macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Ok(1); /// /// let value = assert_ok!(res); /// assert_eq!(value, 1); /// # } /// ``` /// /// An `Err(..)` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let res = Err(()); /// /// assert_ok!(res); // Will panic /// # } /// ``` /// /// [`Ok(_)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ok!`]: crate::debug_assert_ok! #[macro_export] macro_rules! assert_ok { ($cond:expr $(,)?) => { match $cond { ::core::result::Result::Ok(t) => t, ::core::result::Result::Err(e) => { ::core::panic!("assertion failed, expected Ok(_), got Err({:?})", e); } } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::result::Result::Ok(t) => t, ::core::result::Result::Err(e) => { ::core::panic!("assertion failed, expected Ok(_), got Err({:?}): {}", e, ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression matches an [`Ok(_)`] variant on debug builds. /// /// This macro behaves nearly the same as [`assert_ok!`] on debug builds, although it does not /// return the value contained in the `Ok` variant. On release builds it is a no-op. /// /// [`Ok(_)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok #[macro_export] macro_rules! debug_assert_ok { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ok!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn ok() { assert_ok!(Ok::<_, ()>(())); } #[test] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")] fn not_ok() { assert_ok!(Err::<(), _>(())); } #[test] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")] fn not_ok_custom_message() { assert_ok!(Err::<(), _>(()), "foo"); } #[test] fn ok_value_returned() { let value = assert_ok!(Ok::<_, ()>(42)); assert_eq!(value, 42); } #[test] fn debug_ok() { debug_assert_ok!(Ok::<_, ()>(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")] fn debug_not_ok() { debug_assert_ok!(Err::<(), _>(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")] fn debug_not_ok_custom_message() { debug_assert_ok!(Err::<(), _>(()), "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ok() { debug_assert_ok!(Err::<(), _>(())); } #[test] fn does_not_require_ok_to_impl_debug() { enum Foo { Bar, } assert_ok!(Ok::<_, ()>(Foo::Bar)); } #[test] fn debug_does_not_require_ok_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ok!(Ok::<_, ()>(Foo::Bar)); } #[test] fn does_not_require_ok_to_impl_debug_custom_message() { enum Foo { Bar, } assert_ok!(Ok::<_, ()>(Foo::Bar), "foo"); } #[test] fn debug_does_not_require_ok_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ok!(Ok::<_, ()>(Foo::Bar), "foo"); } } claims-0.8.0/src/assert_ok_eq.rs000064400000000000000000000122201046102023000147120ustar 00000000000000/// Asserts that the left expression contains an [`Ok(T)`] variant and its contained value of type /// `T` equals the right expression. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ok_eq!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Ok(1); /// /// assert_ok_eq!(res, 1); /// /// // With a custom message /// assert_ok_eq!(res, 1, "Everything is good with {:?}", res); /// # } /// ``` /// /// The contained value will be returned from the macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Ok(1); /// /// let value = assert_ok_eq!(res, 1); /// assert_eq!(value, 1); /// # } /// ``` /// /// An `Err(_)` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let res: Result = Err(()); /// /// assert_ok_eq!(res, 1); // Will panic /// # } /// ``` /// /// [`Ok(T)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ok_eq!`]: crate::debug_assert_ok_eq! #[macro_export] macro_rules! assert_ok_eq { ($cond:expr, $expected:expr $(,)?) => { match $cond { ::core::result::Result::Ok(t) => { ::core::assert_eq!(t, $expected); t }, e @ ::core::result::Result::Err(_) => { ::core::panic!("assertion failed, expected Ok(_), got {:?}", e); } } }; ($cond:expr, $expected:expr, $($arg:tt)+) => { match $cond { ::core::result::Result::Ok(t) => { ::core::assert_eq!(t, $expected, $($arg)+); t }, e @ ::core::result::Result::Err(_) => { ::core::panic!("assertion failed, expected Ok(_), got {:?}: {}", e, ::core::format_args!($($arg)+)); } } }; } /// Asserts that the left expression contains an [`Ok(T)`] variant and its contained value of type /// `T` equals the right expression on debug builds. /// /// This macro behaves nearly the same as [`assert_ok_eq!`] on debug builds, although it does not /// return the value contained in the `Ok` variant. On release builds it is a no-op. /// /// [`Ok(T)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Ok #[macro_export] macro_rules! debug_assert_ok_eq { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ok_eq!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn equal() { assert_ok_eq!(Ok::<_, ()>(42), 42); } #[test] #[should_panic] fn not_equal() { assert_ok_eq!(Ok::<_, ()>(42), 100); } #[test] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")] fn not_ok() { assert_ok_eq!(Err::(()), 42); } #[test] #[should_panic(expected = "foo")] fn not_equal_custom_message() { assert_ok_eq!(Ok::<_, ()>(1), 2, "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")] fn not_ok_custom_message() { assert_ok_eq!(Err::(()), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_ok_eq!(Ok::<_, ()>(42), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic] fn debug_not_equal() { debug_assert_ok_eq!(Ok::<_, ()>(42), 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(())")] fn debug_not_ok() { debug_assert_ok_eq!(Err::(()), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "foo")] fn debug_not_equal_custom_message() { debug_assert_ok_eq!(Ok::<_, ()>(1), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ok(_), got Err(()): foo")] fn debug_not_ok_custom_message() { debug_assert_ok_eq!(Err::(()), 2, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_equal() { debug_assert_ok_eq!(Ok::<_, ()>(42), 100); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ok() { debug_assert_ok_eq!(Err::(()), 42); } } claims-0.8.0/src/assert_pending.rs000064400000000000000000000103651046102023000152500ustar 00000000000000/// Asserts that the expression matches a [`Poll::Pending`] variant. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_pending!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll = Poll::Pending; /// /// assert_pending!(res); /// /// // With a custom message /// assert_pending!(res, "Future is not ready yet"); /// # } /// ``` /// /// A [`Poll::Pending`] variant will also be returned from this macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll = Poll::Pending; /// /// let value = assert_pending!(res); /// assert_eq!(value, Poll::Pending); /// # } /// ``` /// /// A [`Poll::Ready(_)`] variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res = Poll::Ready(42); /// /// assert_pending!(res); // Will panic /// # } /// ``` /// /// [`Poll::Ready(_)`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_pending!`]: crate::debug_assert_pending! #[macro_export] macro_rules! assert_pending { ($cond:expr $(,)?) => { match $cond { pending @ ::core::task::Poll::Pending => pending, ready @ ::core::task::Poll::Ready(_) => { ::core::panic!("assertion failed, expected Pending, got {:?}", ready); } } }; ($cond:expr, $($arg:tt)+) => { match $cond { pending @ ::core::task::Poll::Pending => pending, ready @ ::core::task::Poll::Ready(_) => { ::core::panic!("assertion failed, expected Pending, got {:?}: {}", ready, ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression matches a [`Poll::Pending`] variant on debug builds. /// /// This macro behaves the same as [`assert_pending!`] on debug builds. On release builds it is a /// no-op. /// /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending #[macro_export] macro_rules! debug_assert_pending { ($($arg:tt)*) => { #[allow(unused_must_use)] #[cfg(debug_assertions)] { $crate::assert_pending!($($arg)*); } } } #[cfg(test)] mod tests { use core::task::Poll::{Pending, Ready}; #[test] fn pending() { let _ = assert_pending!(Pending::<()>); } #[test] #[should_panic(expected = "assertion failed, expected Pending, got Ready(())")] fn not_pending() { let _ = assert_pending!(Ready(())); } #[test] #[should_panic(expected = "assertion failed, expected Pending, got Ready(()): foo")] fn not_pending_custom_message() { let _ = assert_pending!(Ready(()), "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_pending() { debug_assert_pending!(Pending::<()>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Pending, got Ready(())")] fn debug_not_pending() { debug_assert_pending!(Ready(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Pending, got Ready(()): foo")] fn debug_not_pending_custom_message() { debug_assert_pending!(Ready(()), "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_pending() { debug_assert_pending!(Ready(())); } } claims-0.8.0/src/assert_ready.rs000064400000000000000000000116411046102023000147260ustar 00000000000000/// Asserts that the expression matches a [`Poll::Ready(_)`] variant. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ready!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res = Poll::Ready(42); /// /// assert_ready!(res); /// # } /// ``` /// /// The contained value will also be returned from this macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res = Poll::Ready(42); /// /// let value = assert_ready!(res); /// assert_eq!(value, 42); /// # } /// ``` /// /// A [`Poll::Pending`] variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res = Poll::Pending; /// /// assert_ready!(res); // Will panic /// # } /// ``` /// /// [`Poll::Ready(_)`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ready!`]: crate::debug_assert_ready! #[macro_export] macro_rules! assert_ready { ($cond:expr $(,)?) => { match $cond { ::core::task::Poll::Ready(t) => t, ::core::task::Poll::Pending => { ::core::panic!("assertion failed, expected Ready(_), got Pending"); } } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::task::Poll::Ready(t) => t, ::core::task::Poll::Pending => { ::core::panic!("assertion failed, expected Ready(_), got Pending: {}", ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression matches a [`Poll::Ready(_)`] variant on debug builds. /// /// This macro behaves nearly the same as [`assert_ready!`] on debug builds, although it does not /// return the value contained in the `Ready` variant. On release builds it is a no-op. /// /// [`Poll::Ready(_)`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready #[macro_export] macro_rules! debug_assert_ready { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ready!($($arg)*); } } #[cfg(test)] mod tests { use core::task::Poll::{Pending, Ready}; #[test] fn ready() { assert_ready!(Ready(())); } #[test] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending")] fn not_ready() { assert_ready!(Pending::<()>); } #[test] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending: foo")] fn not_ready_custom_message() { assert_ready!(Pending::<()>, "foo"); } #[test] fn ready_value_returned() { let value = assert_ready!(Ready(42)); assert_eq!(value, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_ready() { debug_assert_ready!(Ready(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending")] fn debug_not_ready() { debug_assert_ready!(Pending::<()>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending: foo")] fn debug_not_ready_custom_message() { debug_assert_ready!(Pending::<()>, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ready() { debug_assert_ready!(Pending::<()>); } #[test] fn does_not_require_ready_to_impl_debug() { enum Foo { Bar, } assert_ready!(Ready(Foo::Bar)); } #[test] fn debug_does_not_require_ready_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready!(Ready(Foo::Bar)); } #[test] fn does_not_require_ready_to_impl_debug_custom_message() { enum Foo { Bar, } assert_ready!(Ready(Foo::Bar), "foo"); } #[test] fn debug_does_not_require_ready_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready!(Ready(Foo::Bar), "foo"); } } claims-0.8.0/src/assert_ready_eq.rs000064400000000000000000000126451046102023000154200ustar 00000000000000/// Asserts that the left expression contains a [`Poll::Ready(T)`] variant and its contained value of type /// `T` equals the right expression. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ready_eq!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Ok(42)); /// /// assert_ready_eq!(res, Ok(42)); /// # } /// ``` /// /// The contained value will also be returned from this macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Ok(42)); /// /// let value = assert_ready_eq!(res, Ok(42)); /// assert_eq!(value, Ok(42)); /// # } /// ``` /// /// A [`Poll::Pending`] variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Pending; /// /// assert_ready_eq!(res, Ok(42)); // Will panic /// # } /// ``` /// /// [`Poll::Ready(Ok(T))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ready_eq!`]: crate::debug_assert_ready_eq! #[macro_export] macro_rules! assert_ready_eq { ($cond:expr, $expected:expr $(,)?) => { match $cond { ::core::task::Poll::Ready(t) => { ::core::assert_eq!(t, $expected); t }, ::core::task::Poll::Pending => { ::core::panic!("assertion failed, expected Ready(_), got Pending"); } } }; ($cond:expr, $expected:expr, $($arg:tt)+) => { match $cond { ::core::task::Poll::Ready(t) => { ::core::assert_eq!(t, $expected, $($arg)+); t }, ::core::task::Poll::Pending => { ::core::panic!("assertion failed, expected Ready(_), got Pending: {}", ::core::format_args!($($arg)+)); } } }; } /// Asserts that the left expression contains a [`Poll::Ready(T)`] variant and its contained value of type /// `T` equals the right expression on debug builds. /// /// This macro behaves nearly the same as [`assert_ready_eq!`] on debug builds, although it does not /// return the value contained in the `Poll::Ready` variant. On release builds it is a no-op. /// /// [`Poll::Ready(T)`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready #[macro_export] macro_rules! debug_assert_ready_eq { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ready_eq!($($arg)*); } } #[cfg(test)] mod tests { use core::task::Poll::{Pending, Ready}; #[test] fn equal() { assert_ready_eq!(Ready(42), 42); } #[test] #[should_panic] fn not_equal() { assert_ready_eq!(Ready(42), 100); } #[test] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending")] fn not_ready() { assert_ready_eq!(Pending::, 42); } #[test] #[should_panic(expected = "foo")] fn not_equal_custom_message() { assert_ready_eq!(Ready(1), 2, "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending: foo")] fn not_ready_custom_message() { assert_ready_eq!(Pending::, 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_ready_eq!(Ready(42), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic] fn debug_not_equal() { debug_assert_ready_eq!(Ready(42), 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending")] fn debug_not_ready() { debug_assert_ready_eq!(Pending::, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "foo")] fn debug_not_equal_custom_message() { debug_assert_ready_eq!(Ready(1), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(_), got Pending: foo")] fn debug_not_ready_custom_message() { debug_assert_ready_eq!(Pending::, 2, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_equal() { debug_assert_ready_eq!(Ready(42), 100); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ready() { debug_assert_ready_eq!(Pending::, 42); } } claims-0.8.0/src/assert_ready_err.rs000064400000000000000000000163011046102023000155740ustar 00000000000000/// Asserts that the expression matches a [`Poll::Ready(Err(_))`] variant, returning the contained /// value. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ready_err!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # #[cfg(feature = "std")] extern crate std as core; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Err(())); /// /// assert_ready_err!(res); /// # } /// ``` /// /// The contained value will also be returned from this macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Err("Something went wrong".to_string())); /// /// let message = assert_ready_err!(res); /// assert_eq!("Something went wrong", message); /// # } /// ``` /// /// Both `Poll::Ready(Ok(..))` and [`Poll::Pending`] variants will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Ok(42)); /// /// assert_ready_err!(res); // Will panic /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Pending; /// /// assert_ready_err!(res); // Will panic /// # } /// ``` /// /// [`Poll::Ready(Err(_))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ready_err!`]: crate::debug_assert_ready_err! #[macro_export] macro_rules! assert_ready_err { ($cond:expr $(,)?) => { match $cond { ::core::task::Poll::Ready(::core::result::Result::Err(e)) => e, ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => ::core::panic!("assertion failed, expected Ready(Err(_)), got Ready(Ok({:?}))", t), ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Err(_)), got Pending"), } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::task::Poll::Ready(::core::result::Result::Err(e)) => e, ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => ::core::panic!("assertion failed, expected Ready(Err(_)), got Ready(Ok({:?})): {}", t, ::core::format_args!($($arg)+)), ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Err(_)), got Pending: {}", ::core::format_args!($($arg)+)), } }; } /// Asserts that the expression matches a [`Poll::Ready(Err(_))`] variant on debug builds. /// /// This macro behaves nearly the same as [`assert_ready_err!`] on debug builds, although it does not /// return the value contained in the `Poll::Ready(Err(_))` variant. On release builds it is a no-op. /// /// [`Poll::Ready(Err(_))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready #[macro_export] macro_rules! debug_assert_ready_err { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ready_err!($($arg)*); } } #[cfg(test)] mod tests { use core::task::Poll::{Pending, Ready}; #[test] fn ready_err() { assert_ready_err!(Ready(Err::<(), _>(()))); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(()))")] fn ready_ok() { assert_ready_err!(Ready(Ok::<_, ()>(()))); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending")] fn not_ready() { assert_ready_err!(Pending::>); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(())): foo")] fn ready_ok_custom_message() { assert_ready_err!(Ready(Ok::<_, ()>(())), "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending: foo")] fn not_ready_custom_message() { assert_ready_err!(Pending::>, "foo"); } #[test] fn ready_err_value_returned() { let value = assert_ready_err!(Ready(Err::<(), _>(42))); assert_eq!(value, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_ready_err() { debug_assert_ready_err!(Ready(Err::<(), _>(()))); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(()))")] fn debug_ready_ok() { debug_assert_ready_err!(Ready(Ok::<_, ()>(()))); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending")] fn debug_not_ready() { debug_assert_ready_err!(Pending::>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Ready(Ok(())): foo")] fn debug_ready_ok_custom_message() { debug_assert_ready_err!(Ready(Ok::<_, ()>(())), "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Err(_)), got Pending: foo")] fn debug_not_ready_custom_message() { debug_assert_ready_err!(Pending::>, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_ready_ok() { debug_assert_ready_err!(Ready(Ok::<_, ()>(()))); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ready() { debug_assert_ready_err!(Pending::>); } #[test] fn does_not_require_err_to_impl_debug() { enum Foo { Bar, } assert_ready_err!(Ready(Err::<(), _>(Foo::Bar))); } #[test] fn debug_does_not_require_err_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready_err!(Ready(Err::<(), _>(Foo::Bar))); } #[test] fn does_not_require_err_to_impl_debug_custom_message() { enum Foo { Bar, } assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)), "foo"); } #[test] fn debug_does_not_require_err_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready_err!(Ready(Err::<(), _>(Foo::Bar)), "foo"); } } claims-0.8.0/src/assert_ready_ok.rs000064400000000000000000000160401046102023000154150ustar 00000000000000/// Asserts that the expression matches a [`Poll::Ready(Ok(_))`] variant, returning the contained /// value. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_ready_ok!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Ok(42)); /// /// assert_ready_ok!(res); /// # } /// ``` /// /// The contained value will also be returned from this macro call: /// /// ```rust /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Ok(42)); /// /// let value = assert_ready_ok!(res); /// assert_eq!(value, 42); /// # } /// ``` /// /// Both `Poll::Ready(Err(..))` and [`Poll::Pending`] variants will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Ready(Err(())); /// /// assert_ready_ok!(res); // Will panic /// # } /// ``` /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # use std::task::Poll; /// # fn main() { /// let res: Poll> = Poll::Pending; /// /// assert_ready_ok!(res); // Will panic /// # } /// ``` /// /// [`Poll::Ready(Ok(T))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready /// [`Poll::Pending`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Pending /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_ready_ok!`]: crate::debug_assert_ready_ok #[macro_export] macro_rules! assert_ready_ok { ($cond:expr $(,)?) => { match $cond { ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => t, ::core::task::Poll::Ready(::core::result::Result::Err(e)) => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Ready(Err({:?}))", e), ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Pending"), } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::task::Poll::Ready(::core::result::Result::Ok(t)) => t, ::core::task::Poll::Ready(::core::result::Result::Err(e)) => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Ready(Err({:?})): {}", e, ::core::format_args!($($arg)+)), ::core::task::Poll::Pending => ::core::panic!("assertion failed, expected Ready(Ok(_)), got Pending: {}", ::core::format_args!($($arg)+)), } }; } /// Asserts that the expression matches a [`Poll::Ready(Ok(_))`] variant on debug builds. /// /// This macro behaves nearly the same as [`assert_ready_ok!`] on debug builds, although it does not /// return the value contained in the `Poll::Ready(Ok(_))` variant. On release builds it is a no-op. /// /// [`Poll::Ready(Ok(_))`]: https://doc.rust-lang.org/core/task/enum.Poll.html#variant.Ready #[macro_export] macro_rules! debug_assert_ready_ok { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_ready_ok!($($arg)*); } } #[cfg(test)] mod tests { use core::task::Poll::{Pending, Ready}; #[test] fn ready_ok() { assert_ready_ok!(Ready(Ok::<_, ()>(()))); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(()))")] fn ready_err() { assert_ready_ok!(Ready(Err::<(), _>(()))); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending")] fn not_ready() { assert_ready_ok!(Pending::>); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(())): foo")] fn ready_err_custom_message() { assert_ready_ok!(Ready(Err::<(), _>(())), "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending: foo")] fn not_ready_custom_message() { assert_ready_ok!(Pending::>, "foo"); } #[test] fn ready_ok_value_returned() { let value = assert_ready_ok!(Ready(Ok::<_, ()>(42))); assert_eq!(value, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_ready_ok() { debug_assert_ready_ok!(Ready(Ok::<_, ()>(()))); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(()))")] fn debug_ready_err() { debug_assert_ready_ok!(Ready(Err::<(), _>(()))); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending")] fn debug_not_ready() { debug_assert_ready_ok!(Pending::>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Ready(Err(())): foo")] fn debug_ready_err_custom_message() { debug_assert_ready_ok!(Ready(Err::<(), _>(())), "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Ready(Ok(_)), got Pending: foo")] fn debug_not_ready_custom_message() { debug_assert_ready_ok!(Pending::>, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_ready_err() { debug_assert_ready_ok!(Ready(Err::<(), _>(()))); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_ready() { debug_assert_ready_ok!(Pending::>); } #[test] fn does_not_require_ok_to_impl_debug() { enum Foo { Bar, } assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar))); } #[test] fn debug_does_not_require_ok_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar))); } #[test] fn does_not_require_ok_to_impl_debug_custom_message() { enum Foo { Bar, } assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)), "foo"); } #[test] fn debug_does_not_require_ok_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_ready_ok!(Ready(Ok::<_, ()>(Foo::Bar)), "foo"); } } claims-0.8.0/src/assert_some.rs000064400000000000000000000106711046102023000145670ustar 00000000000000/// Asserts that the expression matches a [`Some(_)`] variant, returning the contained value. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_some!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe = Some(42); /// /// assert_some!(maybe); /// /// // With a custom message /// assert_some!(maybe, "Found it at {:?}", maybe); /// # } /// ``` /// /// A `None` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe = None; /// /// assert_some!(maybe); // Will panic /// # } /// ``` /// /// [`Some(_)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_some!`]: crate::debug_assert_some! #[macro_export] macro_rules! assert_some { ($cond:expr $(,)?) => { match $cond { ::core::option::Option::Some(t) => t, ::core::option::Option::None => { ::core::panic!("assertion failed, expected Some(_), got None"); } } }; ($cond:expr, $($arg:tt)+) => { match $cond { ::core::option::Option::Some(t) => t, ::core::option::Option::None => { ::core::panic!("assertion failed, expected Some(_), got None: {}", ::core::format_args!($($arg)+)); } } }; } /// Asserts that the expression matches a [`Some(_)`] variant on debug builds. /// /// This macro behaves nearly the same as [`assert_some!`] on debug builds, although it does not /// return the value contained in the `Some` variant. On release builds it is a no-op. /// /// [`Some(_)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some #[macro_export] macro_rules! debug_assert_some { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_some!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn some() { assert_some!(Some(())); } #[test] #[should_panic(expected = "assertion failed, expected Some(_), got None")] fn not_some() { assert_some!(None::<()>); } #[test] #[should_panic(expected = "assertion failed, expected Some(_), got None: foo")] fn not_some_custom_message() { assert_some!(None::<()>, "foo"); } #[test] fn some_value_returned() { let value = assert_some!(Some(42)); assert_eq!(value, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_some() { debug_assert_some!(Some(())); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Some(_), got None")] fn debug_not_some() { debug_assert_some!(None::<()>); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Some(_), got None: foo")] fn debug_not_some_custom_message() { debug_assert_some!(None::<()>, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_some() { debug_assert_some!(None::<()>); } #[test] fn does_not_require_some_to_impl_debug() { enum Foo { Bar, } assert_some!(Some(Foo::Bar)); } #[test] fn debug_does_not_require_some_to_impl_debug() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_some!(Some(Foo::Bar)); } #[test] fn does_not_require_some_to_impl_debug_custom_message() { enum Foo { Bar, } assert_some!(Some(Foo::Bar), "foo"); } #[test] fn debug_does_not_require_some_to_impl_debug_custom_message() { #[allow(dead_code)] enum Foo { Bar, } debug_assert_some!(Some(Foo::Bar), "foo"); } } claims-0.8.0/src/assert_some_eq.rs000064400000000000000000000115021046102023000152460ustar 00000000000000/// Asserts that the left expression contains a [`Some(T)`] variant and its contained value of type /// `T` equals the right expression. /// /// ## Uses /// /// Assertions are always checked in both debug and release builds, and cannot be disabled. /// See [`debug_assert_some_eq!`] for assertions that are not enabled in release builds by default. /// /// ## Custom messages /// /// This macro has a second form, where a custom panic message can be provided with or without /// arguments for formatting. See [`std::fmt`] for syntax for this form. /// /// ## Examples /// /// ```rust /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe = Some(42); /// /// assert_some_eq!(maybe, 42); /// /// // With a custom message /// assert_some_eq!(maybe, 42, "Got some value"); /// # } /// ``` /// /// A `None` variant will panic: /// /// ```rust,should_panic /// # #[macro_use] extern crate claims; /// # fn main() { /// let maybe: Option = None; /// /// assert_some_eq!(maybe, 42); // Will panic /// # } /// ``` /// /// [`Some(T)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html /// [`debug_assert_some_eq!`]: crate::debug_assert_some_eq! #[macro_export] macro_rules! assert_some_eq { ($cond:expr, $expected:expr $(,)?) => { match $cond { ::core::option::Option::Some(t) => { ::core::assert_eq!(t, $expected); t }, ::core::option::Option::None => { ::core::panic!("assertion failed, expected Some(_), got None"); } } }; ($cond:expr, $expected:expr, $($arg:tt)+) => { match $cond { ::core::option::Option::Some(t) => { ::core::assert_eq!(t, $expected, $($arg)+); t }, ::core::option::Option::None => { ::core::panic!("assertion failed, expected Some(_), got None: {}", ::core::format_args!($($arg)+)); } } }; } /// Asserts that the left expression contains a [`Some(T)`] variant and its contained value of type /// `T` equals the right expression on debug builds. /// /// This macro behaves nearly the same as [`assert_some_eq!`] on debug builds, although it does not /// return the value contained in the `Some` variant. On release builds it is a no-op. /// /// [`Some(T)`]: https://doc.rust-lang.org/core/option/enum.Option.html#variant.Some #[macro_export] macro_rules! debug_assert_some_eq { ($($arg:tt)*) => { #[cfg(debug_assertions)] $crate::assert_some_eq!($($arg)*); } } #[cfg(test)] mod tests { #[test] fn equal() { assert_some_eq!(Some(42), 42); } #[test] #[should_panic] fn not_equal() { assert_some_eq!(Some(42), 100); } #[test] #[should_panic(expected = "assertion failed, expected Some(_), got None")] fn not_some() { assert_some_eq!(None::, 42); } #[test] #[should_panic(expected = "foo")] fn not_equal_custom_message() { assert_some_eq!(Some(1), 2, "foo"); } #[test] #[should_panic(expected = "assertion failed, expected Some(_), got None: foo")] fn not_some_custom_message() { assert_some_eq!(None::, 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] fn debug_equal() { debug_assert_some_eq!(Some(42), 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic] fn debug_not_equal() { debug_assert_some_eq!(Some(42), 100); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Some(_), got None")] fn debug_not_some() { debug_assert_some_eq!(None::, 42); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "foo")] fn debug_not_equal_custom_message() { debug_assert_some_eq!(Some(1), 2, "foo"); } #[test] #[cfg_attr(not(debug_assertions), ignore = "only run in debug mode")] #[should_panic(expected = "assertion failed, expected Some(_), got None: foo")] fn debug_not_some_custom_message() { debug_assert_some_eq!(None::, 2, "foo"); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_equal() { debug_assert_some_eq!(Some(42), 100); } #[test] #[cfg_attr(debug_assertions, ignore = "only run in release mode")] fn debug_release_not_some() { debug_assert_some_eq!(None::, 42); } } claims-0.8.0/src/lib.rs000064400000000000000000000034051046102023000130060ustar 00000000000000#![no_std] //! Additional assertion macros for testing. //! //! ## Available macros //! //! Note that, like [`core`]/[`std`] macros, all macros in this crate have [`debug_*`](#macros) //! counterparts. //! //! ### Comparison //! //! Assertions similar to [`assert_eq`] or [`assert_ne`]: //! //! * [`assert_ge!`] //! * [`assert_gt!`] //! * [`assert_le!`] //! * [`assert_lt!`] //! //! ### Matching //! //! * [`assert_matches!`] //! //! ### `Result` macros //! //! Assertions for [`Result`] variants: //! //! * [`assert_ok!`] //! * [`assert_err!`] //! * [`assert_ok_eq!`] //! * [`assert_err_eq!`] //! //! ### `Option` macros //! //! Assertions for [`Option`] variants: //! //! * [`assert_some!`] //! * [`assert_none!`] //! * [`assert_some_eq!`] //! //! ### `Poll` macros //! //! Assertions for [`Poll`] variants: //! //! * [`assert_pending!`] //! * [`assert_ready!`] //! * [`assert_ready_ok!`] //! * [`assert_ready_err!`] //! * [`assert_ready_eq!`] //! //! [`core`]: https://doc.rust-lang.org/stable/core/#macros //! [`std`]: https://doc.rust-lang.org/stable/std/#macros //! [`Option`]: https://doc.rust-lang.org/core/option/enum.Option.html //! [`Result`]: https://doc.rust-lang.org/core/result/enum.Result.html //! [`Poll`]: https://doc.rust-lang.org/core/task/enum.Poll.html //! [`assert_eq`]: https://doc.rust-lang.org/core/macro.assert_eq.html //! [`assert_ne`]: https://doc.rust-lang.org/core/macro.assert_ne.html mod assert_err; mod assert_err_eq; mod assert_ge; mod assert_gt; mod assert_le; mod assert_lt; mod assert_matches; mod assert_none; mod assert_ok; mod assert_ok_eq; mod assert_pending; mod assert_ready; mod assert_ready_eq; mod assert_ready_err; mod assert_ready_ok; mod assert_some; mod assert_some_eq;