Skip to content

Commit 0a1f422

Browse files
Add tests.
1 parent 055f5fc commit 0a1f422

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustup"
3-
version = "1.18.2"
3+
version = "1.18.3"
44
authors = [ "Diggory Blake <diggsey@googlemail.com>" ]
55
build = "build.rs"
66
documentation = "https://rust-lang.github.io/rustup.rs/rustup/index.html"
@@ -94,7 +94,7 @@ test = false # no unit tests
9494
[[bin]]
9595
name = "rustup-init"
9696
path = "src/cli/main.rs"
97-
test = false # no unit tests
97+
test = true
9898

9999
[profile.release]
100100
lto = true

src/cli/download_tracker.rs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ impl DownloadTracker {
118118
}
119119
self.prepare_for_new_download();
120120
}
121+
// we're doing modular arithmetic, treat as integer
122+
pub fn from_seconds(sec: u32) -> (u32, u32, u32, u32) {
123+
let d = sec / (24 * 3600);
124+
let h = sec % (24 * 3600) / 3600;
125+
let min = sec % 3600 / 60;
126+
let sec = sec % 60;
127+
128+
(d, h, min, sec)
129+
}
121130
/// Resets the state to be ready for a new download.
122131
fn prepare_for_new_download(&mut self) {
123132
self.content_len = None;
@@ -186,27 +195,16 @@ impl fmt::Display for Duration {
186195
let sec = self.0;
187196

188197
if sec.is_infinite() {
189-
write!(f, "Unknown");
198+
write!(f, "Unknown")
190199
} else {
191-
match from_seconds(sec as u32) {
200+
match DownloadTracker::from_seconds(sec as u32) {
192201
(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),
193202
(0, h, m, s) if h > 0 => write!(f, "{:2.0}h {:2.0}m {:2.0}s", h, m, s),
194203
(0, 0, m, s) if m > 0 => write!(f, "{:2.0}m {:2.0}s", m, s),
195204
(_, _, _, s) => write!(f, "{:2.0}s", s),
196205
}
197206
}
198207
}
199-
200-
fn from_seconds(sec: u32) -> (u32, u32, u32, u32) {
201-
// we're doing modular arithmetic, treat as integer
202-
let d = sec / (24 * 3600);
203-
let h = sec % (24 * 3600) / 3600;
204-
let min = sec % 3600 / 60;
205-
let sec = sec % 60;
206-
207-
(d, h, min, sec)
208-
}
209-
210208
}
211209

212210
/// Human readable size (bytes)
@@ -227,3 +225,42 @@ impl fmt::Display for Size {
227225
}
228226
}
229227
}
228+
229+
#[cfg(test)]
230+
mod tests {
231+
232+
#[test]
233+
fn download_tracker_from_seconds_test() {
234+
use crate::download_tracker::DownloadTracker;
235+
assert_eq!(
236+
DownloadTracker::from_seconds(2),
237+
(0, 0, 0, 2)
238+
);
239+
240+
assert_eq!(
241+
DownloadTracker::from_seconds(60),
242+
(0, 0, 1, 0)
243+
);
244+
245+
assert_eq!(
246+
DownloadTracker::from_seconds(3600),
247+
(0, 1, 0, 0)
248+
);
249+
250+
assert_eq!(
251+
DownloadTracker::from_seconds(3600 * 24),
252+
(1, 0, 0, 0)
253+
);
254+
255+
assert_eq!(
256+
DownloadTracker::from_seconds(52292),
257+
(0, 14, 31, 32)
258+
);
259+
260+
assert_eq!(
261+
DownloadTracker::from_seconds(222292),
262+
(2, 13, 44, 52)
263+
);
264+
}
265+
266+
}

0 commit comments

Comments
 (0)