From 2e5cc810c8b3d25a3bbe30c708521e92b21e34da Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 20 Jul 2018 17:45:00 -0700 Subject: [PATCH] Migrate tests for global functions to `wasm` --- crates/js-sys/tests/all/main.rs | 211 ------------------------- crates/js-sys/tests/wasm/global_fns.rs | 82 ++++++++++ crates/js-sys/tests/wasm/main.rs | 1 + 3 files changed, 83 insertions(+), 211 deletions(-) create mode 100644 crates/js-sys/tests/wasm/global_fns.rs diff --git a/crates/js-sys/tests/all/main.rs b/crates/js-sys/tests/all/main.rs index 0b0b5ce9d..1c6983dea 100644 --- a/crates/js-sys/tests/all/main.rs +++ b/crates/js-sys/tests/all/main.rs @@ -12,214 +12,3 @@ fn project() -> project_builder::Project { mod ArrayIterator; mod Reflect; - -#[test] -fn decode_uri() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let x = js_sys::decode_uri("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B") - .ok() - .expect("should decode URI OK"); - assert_eq!(String::from(x), "https://mozilla.org/?x=шеллы"); - - assert!(js_sys::decode_uri("%E0%A4%A").is_err()); - } - "#, - ) - .test(); -} - -#[test] -fn decode_uri_component() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let x = js_sys::decode_uri_component("%3Fx%3Dtest") - .ok() - .expect("should decode URI OK"); - assert_eq!(String::from(x), "?x=test"); - - assert!(js_sys::decode_uri_component("%E0%A4%A").is_err()); - } - "#, - ) - .test(); -} - -#[test] -fn encode_uri() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let x = js_sys::encode_uri("ABC abc 123"); - assert_eq!(String::from(x), "ABC%20abc%20123"); - } - "#, - ) - .test(); -} - -#[test] -fn encode_uri_component() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let x = js_sys::encode_uri_component("?x=шеллы"); - assert_eq!(String::from(x), "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"); - } - "#, - ) - .test(); -} - -#[test] -fn eval() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let x = js_sys::eval("42").ok().expect("should eval OK"); - assert_eq!(x.as_f64().unwrap(), 42.0); - - let err = js_sys::eval("(function () { throw 42; }())") - .err() - .expect("eval should throw"); - assert_eq!(err.as_f64().unwrap(), 42.0); - } - "#, - ) - .test(); -} - -#[test] -fn is_finite() { - project() - .file( - "src/lib.rs", - r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn is_finite(value: &JsValue) -> bool { - js_sys::is_finite(value) - } - "#, - ) - .file( - "test.js", - r#" - import * as assert from "assert"; - import * as wasm from "./out"; - - export function test() { - assert.equal(wasm.is_finite(42), true); - assert.equal(wasm.is_finite(42.1), true); - assert.equal(wasm.is_finite('42'), true); - assert.equal(wasm.is_finite(NaN), false); - assert.equal(wasm.is_finite(Infinity), false); - } - "#, - ) - .test(); -} - -#[test] -fn parse_int_float() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - let i = js_sys::parse_int("42", 10); - assert_eq!(i as i64, 42); - - let i = js_sys::parse_int("42", 16); - assert_eq!(i as i64, 66); // 0x42 == 66 - - let i = js_sys::parse_int("invalid int", 10); - assert!(i.is_nan()); - - let f = js_sys::parse_float("123456.789"); - assert_eq!(f, 123456.789); - - let f = js_sys::parse_float("invalid float"); - assert!(f.is_nan()); - } - "#) - .test(); -} - -#[test] -fn escape() { - project() - .file("src/lib.rs", r#" - #![feature(use_extern_macros)] - - extern crate wasm_bindgen; - extern crate js_sys; - use wasm_bindgen::prelude::*; - - #[wasm_bindgen] - pub fn test() { - assert_eq!(String::from(js_sys::escape("test")), "test"); - assert_eq!(String::from(js_sys::escape("äöü")), "%E4%F6%FC"); - assert_eq!(String::from(js_sys::escape("ć")), "%u0107"); - assert_eq!(String::from(js_sys::escape("@*_+-./")), "@*_+-./"); - } - "#) - .test(); -} diff --git a/crates/js-sys/tests/wasm/global_fns.rs b/crates/js-sys/tests/wasm/global_fns.rs new file mode 100644 index 000000000..0e1c99030 --- /dev/null +++ b/crates/js-sys/tests/wasm/global_fns.rs @@ -0,0 +1,82 @@ +use std::f64::{NAN, INFINITY}; + +use wasm_bindgen_test::*; +use js_sys::*; + +#[wasm_bindgen_test] +fn test_decode_uri() { + let x = decode_uri("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B") + .ok() + .expect("should decode URI OK"); + assert_eq!(String::from(x), "https://mozilla.org/?x=шеллы"); + + assert!(decode_uri("%E0%A4%A").is_err()); +} + +#[wasm_bindgen_test] +fn test_decode_uri_component() { + let x = decode_uri_component("%3Fx%3Dtest") + .ok() + .expect("should decode URI OK"); + assert_eq!(String::from(x), "?x=test"); + + assert!(decode_uri_component("%E0%A4%A").is_err()); +} + +#[wasm_bindgen_test] +fn test_encode_uri() { + let x = encode_uri("ABC abc 123"); + assert_eq!(String::from(x), "ABC%20abc%20123"); +} + +#[wasm_bindgen_test] +fn test_encode_uri_component() { + let x = encode_uri_component("?x=шеллы"); + assert_eq!(String::from(x), "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"); +} + +#[wasm_bindgen_test] +fn test_eval() { + let x = eval("42").ok().expect("should eval OK"); + assert_eq!(x.as_f64().unwrap(), 42.0); + + let err = eval("(function () { throw 42; }())") + .err() + .expect("eval should throw"); + assert_eq!(err.as_f64().unwrap(), 42.0); +} + +#[wasm_bindgen_test] +fn test_is_finite() { + assert!(is_finite(&42.into())); + assert!(is_finite(&42.1.into())); + assert!(is_finite(&"42".into())); + assert!(!is_finite(&NAN.into())); + assert!(!is_finite(&INFINITY.into())); +} + +#[wasm_bindgen_test] +fn test_parse_int_float() { + let i = parse_int("42", 10); + assert_eq!(i as i64, 42); + + let i = parse_int("42", 16); + assert_eq!(i as i64, 66); // 0x42 == 66 + + let i = parse_int("invalid int", 10); + assert!(i.is_nan()); + + let f = parse_float("123456.789"); + assert_eq!(f, 123456.789); + + let f = parse_float("invalid float"); + assert!(f.is_nan()); +} + +#[wasm_bindgen_test] +fn test_escape() { + assert_eq!(String::from(escape("test")), "test"); + assert_eq!(String::from(escape("äöü")), "%E4%F6%FC"); + assert_eq!(String::from(escape("ć")), "%u0107"); + assert_eq!(String::from(escape("@*_+-./")), "@*_+-./"); +} diff --git a/crates/js-sys/tests/wasm/main.rs b/crates/js-sys/tests/wasm/main.rs index ffac76b09..3ce1952d8 100644 --- a/crates/js-sys/tests/wasm/main.rs +++ b/crates/js-sys/tests/wasm/main.rs @@ -6,6 +6,7 @@ extern crate js_sys; extern crate wasm_bindgen; extern crate wasm_bindgen_test; +pub mod global_fns; pub mod Array; pub mod ArrayBuffer; pub mod ArrayIterator;