Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wasm tests running with wasm-bindgen-test #73

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a wasm32 Node/Web/worker test running with wasm-bindgen-test.
jakubadamw committed Jan 31, 2024
commit 2ae3e4d419c3328482e3230b34fd483378358eb7
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@ web-time = "1"
bencher = "0.1"
serde_derive = "1.0"

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"

[[bench]]
name = "bench"
path = "benches/bench.rs"
78 changes: 78 additions & 0 deletions tests/wasm32-datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#![cfg(all(target_arch = "wasm32", target_os = "unknown"))]

use ulid::Ulid;

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

use std::time::{Duration, SystemTime};

fn now() -> std::time::SystemTime {
return web_time::SystemTime::now().to_std();
}

#[wasm_bindgen_test]
fn test_dynamic() {
let ulid = Ulid::new();
let encoded = ulid.to_string();
let ulid2 = Ulid::from_string(&encoded).expect("failed to deserialize");

println!("{}", encoded);
println!("{:?}", ulid);
println!("{:?}", ulid2);
assert_eq!(ulid, ulid2);
}

#[wasm_bindgen_test]
fn test_source() {
use rand::rngs::mock::StepRng;
let mut source = StepRng::new(123, 0);

let u1 = Ulid::with_source(&mut source);
let dt = now() + Duration::from_millis(1);
let u2 = Ulid::from_datetime_with_source(dt, &mut source);
let u3 = Ulid::from_datetime_with_source(dt, &mut source);

assert!(u1 < u2);
assert_eq!(u2, u3);
}

#[wasm_bindgen_test]
fn test_order() {
let dt = now();
let ulid1 = Ulid::from_datetime(dt);
let ulid2 = Ulid::from_datetime(dt + Duration::from_millis(1));
assert!(ulid1 < ulid2);
}

#[wasm_bindgen_test]
fn test_datetime() {
let dt = now();
let ulid = Ulid::from_datetime(dt);

println!("{:?}, {:?}", dt, ulid.datetime());
assert!(ulid.datetime() <= dt);
assert!(ulid.datetime() + Duration::from_millis(1) >= dt);
}

#[wasm_bindgen_test]
fn test_timestamp() {
let dt = now();
let ulid = Ulid::from_datetime(dt);
let ts = dt
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis();

assert_eq!(u128::from(ulid.timestamp_ms()), ts);
}

#[wasm_bindgen_test]
fn default_is_nil() {
assert_eq!(Ulid::default(), Ulid::nil());
}

#[wasm_bindgen_test]
fn nil_is_at_unix_epoch() {
assert_eq!(Ulid::nil().datetime(), SystemTime::UNIX_EPOCH);
}