Skip to content

Commit

Permalink
Merge pull request #73 from grafbase/ulid-wasm-tests
Browse files Browse the repository at this point in the history
Add Wasm tests running with wasm-bindgen-test
  • Loading branch information
dylanhart authored Feb 1, 2024
2 parents 5e13606 + fd447b7 commit c220598
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/curl_options
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--fail
--location
--retry 3
--retry-connrefused
--show-error
--silent
21 changes: 21 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: ci-build
on: [push, pull_request]

env:
WASM_PACK_VERSION: 0.12.1

jobs:
do-build:
runs-on: ubuntu-latest
Expand All @@ -14,3 +18,20 @@ jobs:
- run: cargo test --no-default-features --features=std
- run: cargo test --no-default-features

- name: Create a directory for binary dependencies
shell: bash
run: |
mkdir -p $GITHUB_WORKSPACE/bin
echo "$GITHUB_WORKSPACE/bin" >> $GITHUB_PATH
- name: Install wasm-pack
working-directory: ${{ github.workspace }}/bin
shell: bash
run: |
file_name="wasm-pack-v${WASM_PACK_VERSION}-x86_64-unknown-linux-musl"
curl \
--config ${GITHUB_WORKSPACE}/.github/curl_options \
https://github.com/rustwasm/wasm-pack/releases/download/v${WASM_PACK_VERSION}/${file_name}.tar.gz \
| tar --strip-components=1 -xzvf- "${file_name}/wasm-pack"
- run: wasm-pack test --node
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/time_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub(crate) fn now() -> std::time::SystemTime {
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
{
use web_time::web::SystemTimeExt;
return web_time::SystemTime::now().to_std();
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
return std::time::SystemTime::now();
}
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);
}

0 comments on commit c220598

Please sign in to comment.