Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
konstankinollc committed May 8, 2019
1 parent 055f5fc commit 534cd14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test = false # no unit tests
[[bin]]
name = "rustup-init"
path = "src/cli/main.rs"
test = false # no unit tests
test = true

[profile.release]
lto = true
Expand Down
45 changes: 32 additions & 13 deletions src/cli/download_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ impl DownloadTracker {
}
self.prepare_for_new_download();
}
// we're doing modular arithmetic, treat as integer
pub fn from_seconds(sec: u32) -> (u32, u32, u32, u32) {
let d = sec / (24 * 3600);
let h = sec % (24 * 3600) / 3600;
let min = sec % 3600 / 60;
let sec = sec % 60;

(d, h, min, sec)
}
/// Resets the state to be ready for a new download.
fn prepare_for_new_download(&mut self) {
self.content_len = None;
Expand Down Expand Up @@ -186,27 +195,16 @@ impl fmt::Display for Duration {
let sec = self.0;

if sec.is_infinite() {
write!(f, "Unknown");
write!(f, "Unknown")
} else {
match from_seconds(sec as u32) {
match DownloadTracker::from_seconds(sec as u32) {
(d, h, m, s) if d > 0 => write!(f, "{:3.0}d {:2.0}h {:2.0}m {:2.0}s", d, h, m, s),
(0, h, m, s) if h > 0 => write!(f, "{:2.0}h {:2.0}m {:2.0}s", h, m, s),
(0, 0, m, s) if m > 0 => write!(f, "{:2.0}m {:2.0}s", m, s),
(_, _, _, s) => write!(f, "{:2.0}s", s),
}
}
}

fn from_seconds(sec: u32) -> (u32, u32, u32, u32) {
// we're doing modular arithmetic, treat as integer
let d = sec / (24 * 3600);
let h = sec % (24 * 3600) / 3600;
let min = sec % 3600 / 60;
let sec = sec % 60;

(d, h, min, sec)
}

}

/// Human readable size (bytes)
Expand All @@ -227,3 +225,24 @@ impl fmt::Display for Size {
}
}
}

#[cfg(test)]
mod tests {

#[test]
fn download_tracker_from_seconds_test() {
use crate::download_tracker::DownloadTracker;
assert_eq!(DownloadTracker::from_seconds(2), (0, 0, 0, 2));

assert_eq!(DownloadTracker::from_seconds(60), (0, 0, 1, 0));

assert_eq!(DownloadTracker::from_seconds(3600), (0, 1, 0, 0));

assert_eq!(DownloadTracker::from_seconds(3600 * 24), (1, 0, 0, 0));

assert_eq!(DownloadTracker::from_seconds(52292), (0, 14, 31, 32));

assert_eq!(DownloadTracker::from_seconds(222292), (2, 13, 44, 52));
}

}

0 comments on commit 534cd14

Please sign in to comment.