This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support trailing commas in macros (#273)
- Loading branch information
Showing
2 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#[macro_use] | ||
extern crate failure; | ||
|
||
// NOTE: | ||
// | ||
// This test is in a separate file due to the fact that ensure! cannot be used | ||
// from within failure. | ||
// | ||
// (you get: 'macro-expanded `macro_export` macros from the current crate cannot | ||
// be referred to by absolute paths') | ||
|
||
// Encloses an early-returning macro in an IIFE so that we | ||
// can treat it as a Result-returning function. | ||
macro_rules! wrap_early_return { | ||
($expr:expr) => {{ | ||
fn func() -> Result<(), failure::Error> { | ||
let _ = $expr; | ||
|
||
#[allow(unreachable_code)] | ||
Ok(()) | ||
} | ||
func().map_err(|e| e.to_string()) | ||
}}; | ||
} | ||
|
||
#[test] | ||
fn bail() { | ||
assert_eq!( | ||
wrap_early_return!(bail!("test")), | ||
wrap_early_return!(bail!("test",))); | ||
assert_eq!( | ||
wrap_early_return!(bail!("test {}", 4)), | ||
wrap_early_return!(bail!("test {}", 4,))); | ||
} | ||
|
||
#[test] | ||
fn ensure() { | ||
assert_eq!( | ||
wrap_early_return!(ensure!(false, "test")), | ||
wrap_early_return!(ensure!(false, "test",))); | ||
assert_eq!( | ||
wrap_early_return!(ensure!(false, "test {}", 4)), | ||
wrap_early_return!(ensure!(false, "test {}", 4,))); | ||
} | ||
|
||
#[test] | ||
fn format_err() { | ||
assert_eq!( | ||
format_err!("test").to_string(), | ||
format_err!("test",).to_string()); | ||
assert_eq!( | ||
format_err!("test {}", 4).to_string(), | ||
format_err!("test {}", 4,).to_string()); | ||
} |