wasm-bindgen/crates/js-sys/tests/wasm/Number.rs
2018-07-20 14:35:44 -07:00

89 lines
2.8 KiB
Rust

use std::f64::{INFINITY, NAN};
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use js_sys::*;
#[wasm_bindgen_test]
fn is_finite() {
assert!(Number::is_finite(&42.into()));
assert!(Number::is_finite(&42.1.into()));
assert!(!Number::is_finite(&"42".into()));
assert!(!Number::is_finite(&NAN.into()));
assert!(!Number::is_finite(&INFINITY.into()));
}
#[wasm_bindgen_test]
fn is_integer() {
assert!(Number::is_integer(&42.into()));
assert!(!Number::is_integer(&42.1.into()));
}
#[wasm_bindgen_test]
fn is_safe_integer() {
assert_eq!(Number::is_safe_integer(&42.into()), true);
assert_eq!(Number::is_safe_integer(&(Math::pow(2., 53.) - 1.).into()), true);
assert_eq!(Number::is_safe_integer(&Math::pow(2., 53.).into()), false);
assert_eq!(Number::is_safe_integer(&"42".into()), false);
assert_eq!(Number::is_safe_integer(&42.1.into()), false);
assert_eq!(Number::is_safe_integer(&NAN.into()), false);
assert_eq!(Number::is_safe_integer(&INFINITY.into()), false);
}
#[wasm_bindgen_test]
fn new() {
let n = Number::new(JsValue::from(42));
let v = JsValue::from(n);
assert!(v.is_object());
assert_eq!(Number::from(v).value_of(), 42.);
}
#[wasm_bindgen_test]
fn parse_int_float() {
assert_eq!(Number::parse_int("42", 10).value_of(), 42.);
assert_eq!(Number::parse_int("42", 16).value_of(), 66.); // 0x42 == 66
assert!(Number::parse_int("invalid int", 10).value_of().is_nan());
assert_eq!(Number::parse_float("123456.789").value_of(), 123456.789);
assert!(Number::parse_float("invalid float").value_of().is_nan());
}
#[wasm_bindgen_test]
fn to_locale_string() {
let number = Number::new(1234.45.into());
assert_eq!(number.to_locale_string("en-US"), "1,234.45");
// TODO: these tests seems to be system dependent, disable for now
// assert_eq!(wasm.to_locale_string(number, "de-DE"), "1,234.45");
// assert_eq!(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
}
#[wasm_bindgen_test]
fn to_precision() {
assert_eq!(Number::new(0.1.into()).to_precision(3).unwrap(), "0.100");
assert!(Number::new(10.into()).to_precision(101).is_err());
}
#[wasm_bindgen_test]
fn to_string() {
assert_eq!(Number::new(42.into()).to_string(10).unwrap(), "42");
assert_eq!(Number::new(233.into()).to_string(16).unwrap(), "e9");
assert!(Number::new(100.into()).to_string(100).is_err());
}
#[wasm_bindgen_test]
fn value_of() {
assert_eq!(Number::new(42.into()).value_of(), 42.);
}
#[wasm_bindgen_test]
fn to_fixed() {
assert_eq!(Number::new(123.456.into()).to_fixed(2).unwrap(), "123.46");
assert!(Number::new(10.into()).to_fixed(101).is_err());
}
#[wasm_bindgen_test]
fn to_exponential() {
assert_eq!(Number::new(123456.into()).to_exponential(2).unwrap(), "1.23e+5");
assert!(Number::new(10.into()).to_exponential(101).is_err());
}