Visual Studio Code, rebuilt in Rust
Rust
use std::time::Duration;
use super::*;
fn update() -> Update {
Update { version: "bbbbbbbbbb".into(), product_version: "1.148.0".into(), timestamp: 0, url: "https://example.com/a.zip".into(), sha256hash: "ab".repeat(32) }
}
#[test]
fn the_entry_shows_each_states_label_and_look() {
let update = update();
let downloading = |total| State::Downloading { update: update.clone(), explicit: true, downloaded_bytes: 25, total_bytes: total, started_at: std::time::Instant::now() };
assert_eq!(entry_label(&State::Idle { error: None, not_available: false }), ("Update", Look::Plain));
assert_eq!(entry_label(&State::CheckingForUpdates { explicit: true }), ("Checking...", Look::Indefinite));
assert_eq!(entry_label(&State::AvailableForDownload { update: update.clone() }), ("Update", Look::Prominent));
assert_eq!(entry_label(&downloading(Some(100))), ("Downloading...", Look::Percent(25.)));
assert_eq!(entry_label(&downloading(None)), ("Downloading...", Look::Indefinite));
assert_eq!(entry_label(&State::Disabled(DisablementReason::NotBuilt)), ("Update", Look::Disabled));
}
#[test]
fn the_tooltip_follows_the_state_and_update_mode() {
let up_to_date = State::Idle { error: None, not_available: false };
assert_eq!(tooltip_content(&up_to_date, "default").message, Some(("Automatic updates are enabled. Happy Coding!".into(), Some("smiley"))));
assert_eq!(tooltip_content(&up_to_date, "none").message, Some(("Automatic updates are disabled.".into(), Some("warning"))));
let error = tooltip_content(&State::Idle { error: Some("boom".into()), not_available: false }, "default");
assert_eq!((error.title.as_str(), error.message), ("Update Error", Some(("boom".into(), Some("error")))));
let ready = State::Ready { update: update(), app: "/tmp/VSRustium.app".into() };
assert_eq!(tooltip_content(&ready, "manual").title, "Update Installed");
assert_eq!(tooltip_content(&ready, "default").title, "Restart to Update");
}
#[test]
fn download_progress_is_formatted_as_upstream() {
assert_eq!(progress_percent(1, Some(3)), Some(33));
assert_eq!(progress_percent(1, Some(0)), None);
assert_eq!(format_bytes(512.), "512 B");
assert_eq!(format_bytes(1536.), "1.5 KB");
assert_eq!(format_bytes(10. * 1024. * 1024.), "10 MB");
assert_eq!(format_time_remaining(45), "45s");
assert_eq!(format_time_remaining(150), "2 min");
assert_eq!(format_time_remaining(3600), "1 hour");
assert_eq!(format_time_remaining(5400), "1.5 hours");
assert_eq!(time_remaining(50, 100, Duration::from_secs(10)), Some(10));
assert_eq!(download_speed(0, Duration::from_secs(1)), None);
}
#[test]
fn a_version_row_copies_the_whole_commit() {
assert_eq!(version_label("Current Version", "1.148.0", "0123456789"), ("Current Version: 1.148.0 (0123456)".into(), "1.148.0 (0123456789)".into()));
assert_eq!(version_label("Latest Version", "1.148.0", ""), ("Latest Version: 1.148.0".into(), "1.148.0".into()));
}