1 use std::f64::{INFINITY, NAN};
2
3 use js_sys::*;
4 use wasm_bindgen_test::*;
5
6 #[wasm_bindgen_test]
test_decode_uri()7 fn test_decode_uri() {
8 let x = decode_uri("https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")
9 .ok()
10 .expect("should decode URI OK");
11 assert_eq!(String::from(x), "https://mozilla.org/?x=шеллы");
12
13 assert!(decode_uri("%E0%A4%A").is_err());
14 }
15
16 #[wasm_bindgen_test]
test_decode_uri_component()17 fn test_decode_uri_component() {
18 let x = decode_uri_component("%3Fx%3Dtest")
19 .ok()
20 .expect("should decode URI OK");
21 assert_eq!(String::from(x), "?x=test");
22
23 assert!(decode_uri_component("%E0%A4%A").is_err());
24 }
25
26 #[wasm_bindgen_test]
test_encode_uri()27 fn test_encode_uri() {
28 let x = encode_uri("ABC abc 123");
29 assert_eq!(String::from(x), "ABC%20abc%20123");
30 }
31
32 #[wasm_bindgen_test]
test_encode_uri_component()33 fn test_encode_uri_component() {
34 let x = encode_uri_component("?x=шеллы");
35 assert_eq!(String::from(x), "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B");
36 }
37
38 #[wasm_bindgen_test]
test_eval()39 fn test_eval() {
40 let x = eval("42").ok().expect("should eval OK");
41 assert_eq!(x.as_f64().unwrap(), 42.0);
42
43 let err = eval("(function () { throw 42; }())")
44 .err()
45 .expect("eval should throw");
46 assert_eq!(err.as_f64().unwrap(), 42.0);
47 }
48
49 #[wasm_bindgen_test]
test_is_finite()50 fn test_is_finite() {
51 assert!(is_finite(&42.into()));
52 assert!(is_finite(&42.1.into()));
53 assert!(is_finite(&"42".into()));
54 assert!(!is_finite(&NAN.into()));
55 assert!(!is_finite(&INFINITY.into()));
56 }
57
58 #[wasm_bindgen_test]
test_parse_int_float()59 fn test_parse_int_float() {
60 let i = parse_int("42", 10);
61 assert_eq!(i as i64, 42);
62
63 let i = parse_int("42", 16);
64 assert_eq!(i as i64, 66); // 0x42 == 66
65
66 let i = parse_int("invalid int", 10);
67 assert!(i.is_nan());
68
69 let f = parse_float("123456.789");
70 assert_eq!(f, 123456.789);
71
72 let f = parse_float("invalid float");
73 assert!(f.is_nan());
74 }
75
76 #[wasm_bindgen_test]
test_escape()77 fn test_escape() {
78 assert_eq!(String::from(escape("test")), "test");
79 assert_eq!(String::from(escape("äöü")), "%E4%F6%FC");
80 assert_eq!(String::from(escape("ć")), "%u0107");
81 assert_eq!(String::from(escape("@*_+-./")), "@*_+-./");
82 }
83
84 #[wasm_bindgen_test]
test_unescape()85 fn test_unescape() {
86 assert_eq!(String::from(unescape("abc123")), "abc123");
87 assert_eq!(String::from(unescape("%E4%F6%FC")), "äöü");
88 assert_eq!(String::from(unescape("%u0107")), "ć");
89 assert_eq!(String::from(unescape("@*_+-./")), "@*_+-./");
90 }
91