Skip to content

Commit

Permalink
Merge branch 'master' of github.com:flang-project/bytesize
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunsik committed May 22, 2020
2 parents 22c1189 + 5bd0c23 commit 19b5e9c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {

impl Display for ByteSize {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", to_string(self.0, false))
f.pad(&to_string(self.0, false))
}
}

Expand Down Expand Up @@ -318,6 +318,18 @@ mod tests {
assert_display("609.0 PB", ByteSize::pb(609));
}

#[test]
fn test_display_alignment() {
assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357)));
assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357)));
assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357)));
assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357)));

assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357)));
assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357)));
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
}

fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
assert_eq!(expected.to_string(), b.to_string_as(si));
}
Expand Down

0 comments on commit 19b5e9c

Please sign in to comment.