Skip to content

Commit

Permalink
Rework FromJsValue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kajacx committed Feb 23, 2024
1 parent cb30f86 commit e5cfe95
Showing 1 changed file with 51 additions and 60 deletions.
111 changes: 51 additions & 60 deletions crates/wasm-bridge-js/src/conversions/from_js_value_tests.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,67 @@
use super::*;
use wasm_bindgen::JsValue;

macro_rules! test_simple {
($ty: ty, $name: ident) => {
#[wasm_bindgen_test::wasm_bindgen_test]
fn $name() {
for num in [
<$ty>::MIN + 2,
2,
<$ty>::MAX / 2 - 2,
<$ty>::MAX / 2 + 2,
<$ty>::MAX - 2,
] {
let number: JsValue = (num as f64).into();
assert_eq!(<$ty>::from_js_value(&number).unwrap(), num);
}
}
};
fn test_eq<T: FromJsValue + PartialEq + std::fmt::Debug>(js_str: &str, val: T) {
let js_val = js_sys::eval(js_str).unwrap();
let result = T::from_js_value(&js_val).unwrap();
assert_eq!(result, val);
}

test_simple!(i8, test_i8);
test_simple!(i16, test_i16);
test_simple!(i32, test_i32);

test_simple!(u8, test_u8);
test_simple!(u16, test_u16);
test_simple!(u32, test_u32);
#[wasm_bindgen_test::wasm_bindgen_test]
fn test_i8() {
test_eq("5", 5i8);
test_eq("-10", -10i8);
}

macro_rules! test_unsigned {
($ty: ty, $name: ident) => {
#[wasm_bindgen_test::wasm_bindgen_test]
fn $name() {
let num = <$ty>::MAX - 2;
let number: JsValue = (num as f64 - <$ty>::MAX as f64 - 1f64).into();
assert_eq!(<$ty>::from_js_value(&number).unwrap(), num);
}
};
#[wasm_bindgen_test::wasm_bindgen_test]
fn test_i16() {
test_eq("5", 5i16);
test_eq("-10", -10i16);
}

test_unsigned!(u8, unsigned_u8);
test_unsigned!(u16, unsigned_u16);
test_unsigned!(u32, unsigned_u32);
#[wasm_bindgen_test::wasm_bindgen_test]
fn test_i32() {
test_eq("5", 5i32);
test_eq("-10", -10i32);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_i64() {
for num in [
i64::MIN + 2,
2,
i64::MAX / 2 - 2,
i64::MAX / 2 + 2,
i64::MAX - 2,
] {
let number: JsValue = num.into();
assert_eq!(i64::from_js_value(&number).unwrap(), num);
}
test_eq("5n", 5i64);
test_eq("-10n", -10i64);
test_eq("1000000000000000000n", 1_000_000_000_000_000_000i64);
test_eq("-1000000000000000000n", -1_000_000_000_000_000_000i64);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_u8() {
test_eq("5", 5u8);
test_eq("200", 200u8);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_u16() {
test_eq("5", 5u16);
test_eq("60000", 60_000u16);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_u32() {
test_eq("5", 5u32);
test_eq("4000000000", 4_000_000_000u32);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_u64() {
for num in [
i64::MIN + 2,
2,
i64::MAX / 2 - 2,
i64::MAX / 2 + 2,
i64::MAX - 2,
] {
let number: JsValue = num.into();
assert_eq!(u64::from_js_value(&number).unwrap(), num as u64);
}
test_eq("5n", 5u64);
test_eq("18000000000000000000n", 18_000_000_000_000_000_000u64);
}

for num in [2, u64::MAX / 2 - 2, u64::MAX / 2 + 2, u64::MAX - 2] {
let number: JsValue = num.into();
assert_eq!(u64::from_js_value(&number).unwrap(), num);
}
#[wasm_bindgen_test::wasm_bindgen_test]
fn test_f32() {
test_eq("3.5", 3.5f32);
}

#[wasm_bindgen_test::wasm_bindgen_test]
fn test_f64() {
test_eq("3.14159265359", 3.14159265359f64)
}

0 comments on commit e5cfe95

Please sign in to comment.