1 extern crate wasm_bindgen_test; 2 3 use instant::Instant; 4 use std::time::Duration; 5 use wasm_bindgen_test::*; 6 7 wasm_bindgen_test_configure!(run_in_browser); 8 // run these tests using: wasm-pack test --chrome --headless -- --features wasm-bindgen 9 10 #[wasm_bindgen_test] test_instant_now()11fn test_instant_now() { 12 let now = Instant::now(); 13 assert!(now.elapsed().as_nanos() > 0); 14 } 15 16 #[wasm_bindgen_test] test_duration()17fn test_duration() { 18 let now = Instant::now(); 19 let one_sec = Duration::from_secs(1); 20 assert!(now.elapsed() < one_sec); 21 } 22 23 // Duration::new will overflow when you have u64::MAX seconds and one billion nanoseconds. 24 // <https://doc.rust-lang.org/std/time/struct.Duration.html#method.new> 25 const ONE_BILLION: u32 = 1_000_000_000; 26 27 #[wasm_bindgen_test] test_checked_add()28fn test_checked_add() { 29 let now = Instant::now(); 30 31 assert!(now.checked_add(Duration::from_millis(1)).is_some()); 32 assert_eq!( 33 None, 34 now.checked_add(Duration::new(u64::MAX, ONE_BILLION - 1)) 35 ); 36 } 37 38 #[wasm_bindgen_test] test_checked_sub()39fn test_checked_sub() { 40 let now = Instant::now(); 41 42 assert!(now.checked_sub(Duration::from_millis(1)).is_some()); 43 assert!(now 44 .checked_sub(Duration::new(u64::MAX, ONE_BILLION - 1)) 45 .is_none()); 46 } 47