Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move fmt macro out of eng to formatter #2054

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/formatting/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ use super::FormatError;
use crate::config::SharedConfig;
use crate::errors::*;

// A helper macro for testing formatters
#[cfg(test)]
#[macro_export]
macro_rules! new_fmt {
($name:ident) => {{
fmt!($name,)
}};
($name:ident, $($key:ident : $value:tt),* $(,)?) => {
new_formatter(stringify!($name), &[
$( Arg { key: stringify!($key), val: stringify!($value) } ),*
])
};
}

mod bar;
pub use bar::BarFormatter;
mod datetime;
Expand Down
20 changes: 6 additions & 14 deletions src/formatting/formatter/eng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,9 @@ impl Formatter for EngFormatter {
mod tests {
use super::*;

macro_rules! fmt {
($name:ident, $($key:ident : $value:tt),*) => {
new_formatter(stringify!($name), &[
$( Arg { key: stringify!($key), val: stringify!($value) } ),*
]).unwrap()
};
}

#[test]
fn eng_rounding_and_negatives() {
let fmt = fmt!(eng, w: 3);
let fmt = new_fmt!(eng, w: 3).unwrap();
let config = SharedConfig::default();

let result = fmt
Expand Down Expand Up @@ -278,7 +270,7 @@ mod tests {
.unwrap();
assert_eq!(result, " 10");

let fmt = fmt!(eng, w: 5, p: 1);
let fmt = new_fmt!(eng, w: 5, p: 1).unwrap();
let result = fmt
.format(
&Value::Number {
Expand All @@ -300,19 +292,19 @@ mod tests {
unit: Unit::Bytes,
};

let fmt = fmt!(eng, w: 5, p: Mi);
let fmt = new_fmt!(eng, w: 5, p: Mi).unwrap();
let result = fmt.format(&val, &config).unwrap();
assert_eq!(result, "14.96GiB");

let fmt = fmt!(eng, w: 4, p: Mi);
let fmt = new_fmt!(eng, w: 4, p: Mi).unwrap();
let result = fmt.format(&val, &config).unwrap();
assert_eq!(result, "15.0GiB");

let fmt = fmt!(eng, w: 3, p: Mi);
let fmt = new_fmt!(eng, w: 3, p: Mi).unwrap();
let result = fmt.format(&val, &config).unwrap();
assert_eq!(result, " 15GiB");

let fmt = fmt!(eng, w: 2, p: Mi);
let fmt = new_fmt!(eng, w: 2, p: Mi).unwrap();
let result = fmt.format(&val, &config).unwrap();
assert_eq!(result, "15GiB");
}
Expand Down