-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,76 @@ | ||
#![cfg(test)] | ||
#![cfg(target_family = "wasm")] | ||
|
||
mod util; | ||
|
||
use std::time::{Duration, SystemTime as StdSystemTime}; | ||
|
||
use wasm_bindgen_test::wasm_bindgen_test; | ||
use serde_test::Token; | ||
use web_time::SystemTime; | ||
|
||
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); | ||
test! { | ||
/// De/Serialization of [`SystemTime`]. | ||
async fn system_time_json() { | ||
let time = SystemTime::now(); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(time, deserialized); | ||
} | ||
|
||
/// De/Serialization of [`SystemTime`]. | ||
#[wasm_bindgen_test] | ||
fn system_time() { | ||
let time = SystemTime::now(); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(time, deserialized); | ||
} | ||
/// De/Serialization of [`SystemTime`] with | ||
/// [`UNIX_EPOCH`](SystemTime::UNIX_EPOCH). | ||
async fn unix_epoch_json() { | ||
let time = SystemTime::UNIX_EPOCH; | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(time, deserialized); | ||
} | ||
|
||
/// De/Serialization of [`SystemTime`] with | ||
/// [`UNIX_EPOCH`](SystemTime::UNIX_EPOCH). | ||
#[wasm_bindgen_test] | ||
fn unix_epoch() { | ||
let time = SystemTime::UNIX_EPOCH; | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!(time, deserialized); | ||
} | ||
/// De/Serialization compatibility with [`std::time::SystemTime`]. | ||
async fn std_compatibility_json() { | ||
let time = SystemTime::now(); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: StdSystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!( | ||
time.duration_since(SystemTime::UNIX_EPOCH).unwrap(), | ||
deserialized | ||
.duration_since(StdSystemTime::UNIX_EPOCH) | ||
.unwrap() | ||
); | ||
|
||
#[wasm_bindgen_test] | ||
/// De/Serialization compatibility with [`std::time::SystemTime`]. | ||
fn std_compatibility() { | ||
let time = SystemTime::now(); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: StdSystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!( | ||
time.duration_since(SystemTime::UNIX_EPOCH).unwrap(), | ||
deserialized | ||
.duration_since(StdSystemTime::UNIX_EPOCH) | ||
.unwrap() | ||
); | ||
let time = StdSystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!( | ||
time.duration_since(StdSystemTime::UNIX_EPOCH).unwrap(), | ||
deserialized.duration_since(SystemTime::UNIX_EPOCH).unwrap() | ||
); | ||
} | ||
|
||
/// Deserialization failures. | ||
async fn failure() { | ||
serde_test::assert_de_tokens_error::<SystemTime>( | ||
&[ | ||
Token::Map { len: Some(3) }, | ||
Token::Bytes(b"secs_since_epoch"), | ||
Token::U64(0), | ||
Token::Bytes(b"nanos_since_epoch"), | ||
Token::U32(0), | ||
Token::Bytes(b"test"), | ||
], | ||
"unknown field `test`, expected `secs_since_epoch` or `nanos_since_epoch`", | ||
); | ||
} | ||
} | ||
|
||
let time = StdSystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000); | ||
let serialized = serde_json::to_string(&time).unwrap(); | ||
let deserialized: SystemTime = serde_json::from_str(&serialized).unwrap(); | ||
assert_eq!( | ||
time.duration_since(StdSystemTime::UNIX_EPOCH).unwrap(), | ||
deserialized.duration_since(SystemTime::UNIX_EPOCH).unwrap() | ||
/// Deserialization failures. | ||
fn asdasd() { | ||
serde_test::assert_de_tokens_error::<SystemTime>( | ||
&[ | ||
Token::Map { len: Some(1) }, | ||
Token::Bytes(b"secs_since_epoch"), | ||
Token::I32(0), | ||
Token::MapEnd, | ||
], | ||
"unknown field `x`, expected `a` or `b`", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters