Skip to content

Commit

Permalink
Move fmt macro out of eng to formatter (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 authored May 15, 2024
1 parent fc5a3f6 commit ba873a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit ba873a2

Please sign in to comment.