Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Aug 17, 2024
1 parent 60dfe75 commit 7e3e41c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/time/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use wasm_bindgen::{JsCast, JsValue};
#[wasm_bindgen]
extern "C" {
/// Type for the [global object](https://developer.mozilla.org/en-US/docs/Glossary/Global_object).
#[wasm_bindgen(no_deref)]
type Global;

/// Returns the [`Performance`](https://developer.mozilla.org/en-US/docs/Web/API/Performance) object.
#[wasm_bindgen(method, getter)]
fn performance(this: &Global) -> JsValue;

/// Type for the [`Performance` object](https://developer.mozilla.org/en-US/docs/Web/API/Performance).
#[wasm_bindgen(no_deref)]
pub(super) type Performance;

/// Binding to [`Performance.now()`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now).
Expand Down
7 changes: 7 additions & 0 deletions tests/system_time_success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ test! {
assert!(later >= earlier);
assert!(later.duration_since(earlier).unwrap() <= MAX_DIFF);
}

/// [`SystemTime::elapsed()`] failure.
async fn error() {
let time = SystemTime::now() + DIFF;
let error = time.elapsed().unwrap_err();
assert_eq!(error.to_string(), "second time provided was later than self");
}
}
38 changes: 38 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![cfg(test)]
#![cfg(target_family = "wasm")]

use std::time;
use std::time::Duration;

use wasm_bindgen_test::wasm_bindgen_test;
use web_time::web::SystemTimeExt;

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn to_std() {
assert_eq!(
web_time::SystemTime::UNIX_EPOCH.to_std(),
time::SystemTime::UNIX_EPOCH,
);

let duration = Duration::from_secs(60 * 60 * 24 * 365);
assert_eq!(
(web_time::SystemTime::UNIX_EPOCH + duration).to_std(),
time::SystemTime::UNIX_EPOCH + duration,
);
}

#[wasm_bindgen_test]
fn from_std() {
assert_eq!(
web_time::SystemTime::from_std(time::SystemTime::UNIX_EPOCH),
web_time::SystemTime::UNIX_EPOCH,
);

let duration = Duration::from_secs(60 * 60 * 24 * 365);
assert_eq!(
web_time::SystemTime::from_std(time::SystemTime::UNIX_EPOCH + duration),
web_time::SystemTime::UNIX_EPOCH + duration,
);
}

0 comments on commit 7e3e41c

Please sign in to comment.