| 1 |
use js_sys::*; |
| 2 |
use wasm_bindgen::prelude::*; |
| 3 |
use wasm_bindgen_test::*; |
| 4 |
|
| 5 |
#[wasm_bindgen(module = "tests/wasm/JsString.js")] |
| 6 |
extern "C" { |
| 7 |
fn new_string_object() -> JsValue; |
| 8 |
fn get_replacer_function() -> Function; |
| 9 |
} |
| 10 |
|
| 11 |
#[wasm_bindgen_test] |
| 12 |
fn js_string_inheritance() { |
| 13 |
let string = new_string_object(); |
| 14 |
assert!(string.is_instance_of::<JsString>()); |
| 15 |
assert!(string.is_instance_of::<Object>()); |
| 16 |
} |
| 17 |
|
| 18 |
#[wasm_bindgen_test] |
| 19 |
fn length() { |
| 20 |
fn test(s: &str) { |
| 21 |
assert_eq!(JsString::from(s).length(), s.len() as u32); |
| 22 |
} |
| 23 |
test("Mozilla"); |
| 24 |
test(""); |
| 25 |
} |
| 26 |
|
| 27 |
#[wasm_bindgen_test] |
| 28 |
fn char_at() { |
| 29 |
let s = JsString::from("Brave new world"); |
| 30 |
assert_eq!(JsValue::from(s.char_at(0)), "B"); |
| 31 |
assert_eq!(JsValue::from(s.char_at(999)), ""); |
| 32 |
} |
| 33 |
|
| 34 |
#[wasm_bindgen_test] |
| 35 |
fn char_code_at() { |
| 36 |
let s = "Brave new world"; |
| 37 |
let js = JsString::from(s); |
| 38 |
for (i, b) in s.char_indices() { |
| 39 |
assert_eq!(js.char_code_at(i as u32), b as u32 as f64); |
| 40 |
} |
| 41 |
assert!(js.char_code_at(s.len() as u32).is_nan()); |
| 42 |
} |
| 43 |
|
| 44 |
#[wasm_bindgen_test] |
| 45 |
fn code_point_at() { |
| 46 |
assert_eq!(JsString::from("ABC").code_point_at(1), b'B'); |
| 47 |
assert!(JsString::from("ABC").code_point_at(42).is_undefined()); |
| 48 |
} |
| 49 |
|
| 50 |
#[wasm_bindgen_test] |
| 51 |
fn concat() { |
| 52 |
// TODO: Implement ability to receive multiple optional arguments |
| 53 |
let s = JsString::from("Hello ").concat(&"World".into()); |
| 54 |
assert_eq!(JsValue::from(s), "Hello World"); |
| 55 |
let foo = JsString::from("foo"); |
| 56 |
assert_eq!( |
| 57 |
JsValue::from(foo.concat(&Object::new().into())), |
| 58 |
"foo[object Object]" |
| 59 |
); |
| 60 |
assert_eq!(JsValue::from(foo.concat(&Array::new().into())), "foo"); |
| 61 |
assert_eq!(JsValue::from(foo.concat(&JsValue::null())), "foonull"); |
| 62 |
assert_eq!(JsValue::from(foo.concat(&true.into())), "footrue"); |
| 63 |
assert_eq!(JsValue::from(foo.concat(&1234.into())), "foo1234"); |
| 64 |
} |
| 65 |
|
| 66 |
#[wasm_bindgen_test] |
| 67 |
fn ends_with() { |
| 68 |
let s = "To be, or not to be, that is the question."; |
| 69 |
let js = JsString::from(s); |
| 70 |
|
| 71 |
// TODO: remove third parameter once we have optional parameters |
| 72 |
assert_eq!(js.ends_with("question.", s.len() as i32), true); |
| 73 |
assert_eq!(js.ends_with("to be", s.len() as i32), false); |
| 74 |
assert_eq!(js.ends_with("to be", 19), true); |
| 75 |
} |
| 76 |
|
| 77 |
#[wasm_bindgen_test] |
| 78 |
fn from_char_code() { |
| 79 |
let s = "½+¾="; |
| 80 |
let codes: Vec<u32> = s.chars().map(|char| char as u32).collect(); |
| 81 |
|
| 82 |
assert_eq!(JsString::from_char_code1(codes[0]), "½"); |
| 83 |
assert_eq!(JsString::from_char_code2(codes[0], codes[1]), "½+"); |
| 84 |
assert_eq!( |
| 85 |
JsString::from_char_code3(codes[0], codes[1], codes[2]), |
| 86 |
"½+¾" |
| 87 |
); |
| 88 |
assert_eq!( |
| 89 |
JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]), |
| 90 |
"½+¾=" |
| 91 |
); |
| 92 |
|
| 93 |
let codes_u16: Vec<u16> = codes |
| 94 |
.into_iter() |
| 95 |
.map(|code| { |
| 96 |
assert!(code <= u32::from(u16::max_value())); |
| 97 |
code as u16 |
| 98 |
}) |
| 99 |
.collect(); |
| 100 |
|
| 101 |
assert_eq!(JsString::from_char_code(&codes_u16), "½+¾="); |
| 102 |
} |
| 103 |
|
| 104 |
#[wasm_bindgen_test] |
| 105 |
fn from_code_point() { |
| 106 |
let s = "☃★♲你"; |
| 107 |
let codes: Vec<u32> = s.chars().map(|char| char as u32).collect(); |
| 108 |
|
| 109 |
assert_eq!(JsString::from_code_point1(codes[0]).unwrap(), "☃"); |
| 110 |
assert_eq!( |
| 111 |
JsString::from_code_point2(codes[0], codes[1]).unwrap(), |
| 112 |
"☃★" |
| 113 |
); |
| 114 |
assert_eq!( |
| 115 |
JsString::from_code_point3(codes[0], codes[1], codes[2]).unwrap(), |
| 116 |
"☃★♲" |
| 117 |
); |
| 118 |
assert_eq!( |
| 119 |
JsString::from_code_point4(codes[0], codes[1], codes[2], codes[3]).unwrap(), |
| 120 |
"☃★♲你" |
| 121 |
); |
| 122 |
assert_eq!(JsString::from_code_point(&codes).unwrap(), "☃★♲你"); |
| 123 |
|
| 124 |
assert!(!JsString::from_code_point1(0x10FFFF).is_err()); |
| 125 |
assert!(JsString::from_code_point1(0x110000).is_err()); |
| 126 |
assert!(JsString::from_code_point1(u32::max_value()).is_err()); |
| 127 |
} |
| 128 |
|
| 129 |
#[wasm_bindgen_test] |
| 130 |
fn includes() { |
| 131 |
let str = JsString::from("Blue Whale"); |
| 132 |
|
| 133 |
// TODO: remove second parameter once we have optional parameters |
| 134 |
assert_eq!(str.includes("Blue", 0), true); |
| 135 |
assert_eq!(str.includes("Blute", 0), false); |
| 136 |
assert_eq!(str.includes("Whale", 0), true); |
| 137 |
assert_eq!(str.includes("Whale", 5), true); |
| 138 |
assert_eq!(str.includes("Whale", 7), false); |
| 139 |
assert_eq!(str.includes("", 0), true); |
| 140 |
assert_eq!(str.includes("", 16), true); |
| 141 |
} |
| 142 |
|
| 143 |
#[wasm_bindgen_test] |
| 144 |
fn index_of() { |
| 145 |
let str = JsString::from("Blue Whale"); |
| 146 |
|
| 147 |
// TODO: remove second parameter once we have optional parameters |
| 148 |
assert_eq!(str.index_of("Blue", 0), 0); |
| 149 |
// TODO: remove second parameter once we have optional parameters |
| 150 |
assert_eq!(str.index_of("Blute", 0), -1); |
| 151 |
assert_eq!(str.index_of("Whale", 0), 5); |
| 152 |
assert_eq!(str.index_of("Whale", 5), 5); |
| 153 |
assert_eq!(str.index_of("Whale", 7), -1); |
| 154 |
// TODO: remove second parameter once we have optional parameters |
| 155 |
assert_eq!(str.index_of("", 0), 0); |
| 156 |
assert_eq!(str.index_of("", 9), 9); |
| 157 |
assert_eq!(str.index_of("", 10), 10); |
| 158 |
assert_eq!(str.index_of("", 11), 10); |
| 159 |
} |
| 160 |
|
| 161 |
#[wasm_bindgen_test] |
| 162 |
fn last_index_of() { |
| 163 |
let js = JsString::from("canal"); |
| 164 |
let len = js.length() as i32; |
| 165 |
|
| 166 |
// TODO: remove second parameter once we have optional parameters |
| 167 |
assert_eq!(js.last_index_of("a", len), 3); |
| 168 |
assert_eq!(js.last_index_of("a", 2), 1); |
| 169 |
assert_eq!(js.last_index_of("a", 0), -1); |
| 170 |
// TODO: remove second parameter once we have optional parameters |
| 171 |
assert_eq!(js.last_index_of("x", len), -1); |
| 172 |
assert_eq!(js.last_index_of("c", -5), 0); |
| 173 |
assert_eq!(js.last_index_of("c", 0), 0); |
| 174 |
// TODO: remove second parameter once we have optional parameters |
| 175 |
assert_eq!(js.last_index_of("", len), 5); |
| 176 |
assert_eq!(js.last_index_of("", 2), 2); |
| 177 |
} |
| 178 |
|
| 179 |
#[wasm_bindgen_test] |
| 180 |
fn match_() { |
| 181 |
let s = "The quick brown fox jumped over the lazy dog. It barked."; |
| 182 |
let re = RegExp::new("[A-Z]", "g"); |
| 183 |
let result = JsString::from(s).match_(&re); |
| 184 |
let obj = result.unwrap(); |
| 185 |
|
| 186 |
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "T"); |
| 187 |
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "I"); |
| 188 |
|
| 189 |
let re = RegExp::new("[A-Z]([a-z]*)", "g"); |
| 190 |
let result = JsString::from(s).match_(&re); |
| 191 |
let obj = result.unwrap(); |
| 192 |
|
| 193 |
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "The"); |
| 194 |
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "It"); |
| 195 |
|
| 196 |
let result = JsString::from("foo").match_(&re); |
| 197 |
assert!(result.is_none()); |
| 198 |
|
| 199 |
let s = "For more information, see Chapter 3.4.5.1"; |
| 200 |
let re = RegExp::new("see (chapter \\d+(\\.\\d)*)", "i"); |
| 201 |
let result = JsString::from(s).match_(&re); |
| 202 |
let obj = result.unwrap(); |
| 203 |
|
| 204 |
assert_eq!( |
| 205 |
Reflect::get(obj.as_ref(), &"0".into()).unwrap(), |
| 206 |
"see Chapter 3.4.5.1" |
| 207 |
); |
| 208 |
assert_eq!( |
| 209 |
Reflect::get(obj.as_ref(), &"1".into()).unwrap(), |
| 210 |
"Chapter 3.4.5.1" |
| 211 |
); |
| 212 |
assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".1"); |
| 213 |
assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 22); |
| 214 |
assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s); |
| 215 |
} |
| 216 |
|
| 217 |
#[wasm_bindgen_test] |
| 218 |
fn match_all() { |
| 219 |
let s = "The quick brown fox jumped over the lazy dog. It barked."; |
| 220 |
let re = RegExp::new("[A-Z]([a-z]*)", "g"); |
| 221 |
let result: Vec<_> = JsString::from(s) |
| 222 |
.match_all(&re) |
| 223 |
.into_iter() |
| 224 |
.collect::<Result<_, _>>() |
| 225 |
.unwrap(); |
| 226 |
|
| 227 |
let obj = &result[0]; |
| 228 |
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "The"); |
| 229 |
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "he"); |
| 230 |
|
| 231 |
let obj = &result[1]; |
| 232 |
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "It"); |
| 233 |
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "t"); |
| 234 |
|
| 235 |
let result: Vec<_> = JsString::from("foo") |
| 236 |
.match_all(&re) |
| 237 |
.into_iter() |
| 238 |
.collect::<Result<_, _>>() |
| 239 |
.unwrap(); |
| 240 |
assert_eq!(result.len(), 0); |
| 241 |
|
| 242 |
let s = "For more information, see Chapter 3.4.5.1. Also see Chapter 3.1.4"; |
| 243 |
let re = RegExp::new("see (chapter \\d+(\\.\\d)*)", "gi"); |
| 244 |
let result: Vec<_> = JsString::from(s) |
| 245 |
.match_all(&re) |
| 246 |
.into_iter() |
| 247 |
.collect::<Result<_, _>>() |
| 248 |
.unwrap(); |
| 249 |
|
| 250 |
let obj = &result[0]; |
| 251 |
assert_eq!( |
| 252 |
Reflect::get(obj.as_ref(), &"0".into()).unwrap(), |
| 253 |
"see Chapter 3.4.5.1" |
| 254 |
); |
| 255 |
assert_eq!( |
| 256 |
Reflect::get(obj.as_ref(), &"1".into()).unwrap(), |
| 257 |
"Chapter 3.4.5.1" |
| 258 |
); |
| 259 |
assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".1"); |
| 260 |
assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 22); |
| 261 |
assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s); |
| 262 |
|
| 263 |
let obj = &result[1]; |
| 264 |
assert_eq!( |
| 265 |
Reflect::get(obj.as_ref(), &"0".into()).unwrap(), |
| 266 |
"see Chapter 3.1.4" |
| 267 |
); |
| 268 |
assert_eq!( |
| 269 |
Reflect::get(obj.as_ref(), &"1".into()).unwrap(), |
| 270 |
"Chapter 3.1.4" |
| 271 |
); |
| 272 |
assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".4"); |
| 273 |
assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 48); |
| 274 |
assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s); |
| 275 |
} |
| 276 |
|
| 277 |
#[wasm_bindgen_test] |
| 278 |
fn normalize() { |
| 279 |
let js = JsString::from("\u{1E9B}\u{0323}"); |
| 280 |
|
| 281 |
// TODO: Handle undefined |
| 282 |
assert_eq!(JsValue::from(js.normalize("NFC")), "\u{1E9B}\u{0323}"); |
| 283 |
assert_eq!( |
| 284 |
JsValue::from(js.normalize("NFD")), |
| 285 |
"\u{017F}\u{0323}\u{0307}" |
| 286 |
); |
| 287 |
assert_eq!(JsValue::from(js.normalize("NFKC")), "\u{1E69}"); |
| 288 |
assert_eq!( |
| 289 |
JsValue::from(js.normalize("NFKD")), |
| 290 |
"\u{0073}\u{0323}\u{0307}" |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
#[wasm_bindgen_test] |
| 295 |
fn pad_end() { |
| 296 |
let js = JsString::from("abc"); |
| 297 |
|
| 298 |
// TODO: remove second parameter once we have optional parameters |
| 299 |
assert_eq!(JsValue::from(js.pad_end(10, " ")), "abc "); |
| 300 |
// TODO: remove second parameter once we have optional parameters |
| 301 |
assert_eq!(JsValue::from(js.pad_end(10, " ")), "abc "); |
| 302 |
assert_eq!(JsValue::from(js.pad_end(10, "foo")), "abcfoofoof"); |
| 303 |
assert_eq!(JsValue::from(js.pad_end(6, "123456")), "abc123"); |
| 304 |
// TODO: remove second parameter once we have optional parameters |
| 305 |
assert_eq!(JsValue::from(js.pad_end(1, " ")), "abc"); |
| 306 |
} |
| 307 |
|
| 308 |
#[wasm_bindgen_test] |
| 309 |
fn pad_start() { |
| 310 |
let js = JsString::from("abc"); |
| 311 |
|
| 312 |
// TODO: remove second parameter once we have optional parameters |
| 313 |
assert_eq!(js.pad_start(10, " "), " abc"); |
| 314 |
assert_eq!(js.pad_start(10, "foo"), "foofoofabc"); |
| 315 |
assert_eq!(js.pad_start(6, "123465"), "123abc"); |
| 316 |
assert_eq!(js.pad_start(8, "0"), "00000abc"); |
| 317 |
// TODO: remove second parameter once we have optional parameters |
| 318 |
assert_eq!(js.pad_start(1, " "), "abc"); |
| 319 |
} |
| 320 |
|
| 321 |
#[wasm_bindgen_test] |
| 322 |
fn repeat() { |
| 323 |
assert_eq!(JsString::from("test").repeat(3), "testtesttest"); |
| 324 |
} |
| 325 |
|
| 326 |
#[wasm_bindgen_test] |
| 327 |
fn replace() { |
| 328 |
let js = JsString::from( |
| 329 |
"The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?", |
| 330 |
); |
| 331 |
let result = js.replace("dog", "ferret"); |
| 332 |
|
| 333 |
assert_eq!( |
| 334 |
result, |
| 335 |
"The quick brown fox jumped over the lazy ferret. If the dog reacted, was it really lazy?" |
| 336 |
); |
| 337 |
|
| 338 |
let js = JsString::from("borderTop"); |
| 339 |
let result = js.replace_with_function("T", &get_replacer_function()); |
| 340 |
|
| 341 |
assert_eq!(result, "border-top"); |
| 342 |
|
| 343 |
let js = JsString::from( |
| 344 |
"The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?", |
| 345 |
); |
| 346 |
let re = RegExp::new("dog", "g"); |
| 347 |
let result = js.replace_by_pattern(&re, "ferret"); |
| 348 |
|
| 349 |
assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?"); |
| 350 |
|
| 351 |
let js = JsString::from("borderTop"); |
| 352 |
let re = RegExp::new("[A-Z]", "g"); |
| 353 |
let result = js.replace_by_pattern_with_function(&re, &get_replacer_function()); |
| 354 |
|
| 355 |
assert_eq!(result, "border-top"); |
| 356 |
} |
| 357 |
|
| 358 |
#[wasm_bindgen_test] |
| 359 |
fn replace_all() { |
| 360 |
let js = JsString::from( |
| 361 |
"The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?", |
| 362 |
); |
| 363 |
let result = js.replace_all("dog", "ferret"); |
| 364 |
|
| 365 |
assert_eq!( |
| 366 |
result, |
| 367 |
"The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?" |
| 368 |
); |
| 369 |
|
| 370 |
let js = JsString::from("borderTopTest"); |
| 371 |
let result = js.replace_all_with_function("T", &get_replacer_function()); |
| 372 |
|
| 373 |
assert_eq!(result, "border-top-test"); |
| 374 |
|
| 375 |
let js = JsString::from( |
| 376 |
"The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?", |
| 377 |
); |
| 378 |
let re = RegExp::new("dog", "g"); |
| 379 |
let result = js.replace_all_by_pattern(&re, "ferret"); |
| 380 |
|
| 381 |
assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?"); |
| 382 |
|
| 383 |
let js = JsString::from("borderTopTest"); |
| 384 |
let re = RegExp::new("[A-Z]", "g"); |
| 385 |
let result = js.replace_all_by_pattern_with_function(&re, &get_replacer_function()); |
| 386 |
|
| 387 |
assert_eq!(result, "border-top-test"); |
| 388 |
} |
| 389 |
|
| 390 |
#[wasm_bindgen_test] |
| 391 |
fn search() { |
| 392 |
let js = JsString::from( |
| 393 |
"The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?", |
| 394 |
); |
| 395 |
let re = RegExp::new("[^\\w\\s]", "g"); |
| 396 |
|
| 397 |
assert_eq!(js.search(&re), 44); |
| 398 |
|
| 399 |
let js = JsString::from("hey JudE"); |
| 400 |
let re1 = RegExp::new("[A-Z]", "g"); |
| 401 |
let re2 = RegExp::new("[.]", "g"); |
| 402 |
|
| 403 |
assert_eq!(js.search(&re1), 4); |
| 404 |
assert_eq!(js.search(&re2), -1); |
| 405 |
} |
| 406 |
|
| 407 |
#[wasm_bindgen_test] |
| 408 |
fn slice() { |
| 409 |
let characters = JsString::from("acxn18"); |
| 410 |
assert_eq!(characters.slice(1, 3), "cx"); |
| 411 |
} |
| 412 |
|
| 413 |
#[wasm_bindgen_test] |
| 414 |
fn split() { |
| 415 |
let js = JsString::from("Oh brave new world"); |
| 416 |
let result = js.split(" "); |
| 417 |
|
| 418 |
let mut v = Vec::with_capacity(result.length() as usize); |
| 419 |
result.for_each(&mut |x, _, _| v.push(x)); |
| 420 |
|
| 421 |
assert_eq!(v[0], "Oh"); |
| 422 |
assert_eq!(v[1], "brave"); |
| 423 |
assert_eq!(v[2], "new"); |
| 424 |
assert_eq!(v[3], "world"); |
| 425 |
|
| 426 |
let js = JsString::from("Oct,Nov,Dec"); |
| 427 |
let result = js.split(","); |
| 428 |
|
| 429 |
let mut v = Vec::with_capacity(result.length() as usize); |
| 430 |
result.for_each(&mut |x, _, _| v.push(x)); |
| 431 |
|
| 432 |
assert_eq!(v[0], "Oct"); |
| 433 |
assert_eq!(v[1], "Nov"); |
| 434 |
assert_eq!(v[2], "Dec"); |
| 435 |
|
| 436 |
let result = js.split_limit(",", 2); |
| 437 |
|
| 438 |
let mut v = Vec::with_capacity(result.length() as usize); |
| 439 |
result.for_each(&mut |x, _, _| v.push(x)); |
| 440 |
|
| 441 |
assert_eq!(result.length(), 2); |
| 442 |
assert_eq!(v[0], "Oct"); |
| 443 |
assert_eq!(v[1], "Nov"); |
| 444 |
|
| 445 |
let js = JsString::from("Oh brave new world"); |
| 446 |
let re = RegExp::new("\\s", "g"); |
| 447 |
let result = js.split_by_pattern(&re); |
| 448 |
|
| 449 |
let mut v = Vec::with_capacity(result.length() as usize); |
| 450 |
result.for_each(&mut |x, _, _| v.push(x)); |
| 451 |
|
| 452 |
assert_eq!(v[0], "Oh"); |
| 453 |
assert_eq!(v[1], "brave"); |
| 454 |
assert_eq!(v[2], "new"); |
| 455 |
assert_eq!(v[3], "world"); |
| 456 |
|
| 457 |
let result = js.split_by_pattern_limit(&re, 2); |
| 458 |
|
| 459 |
let mut v = Vec::with_capacity(result.length() as usize); |
| 460 |
result.for_each(&mut |x, _, _| v.push(x)); |
| 461 |
|
| 462 |
assert_eq!(result.length(), 2); |
| 463 |
assert_eq!(v[0], "Oh"); |
| 464 |
assert_eq!(v[1], "brave"); |
| 465 |
} |
| 466 |
|
| 467 |
#[wasm_bindgen_test] |
| 468 |
fn starts_with() { |
| 469 |
let js = JsString::from("To be, or not to be, that is the question."); |
| 470 |
|
| 471 |
// TODO: remove second parameter for both assertions once we have optional parameters |
| 472 |
assert!(js.starts_with("To be", 0)); |
| 473 |
assert!(!js.starts_with("not to be", 0)); |
| 474 |
assert!(js.starts_with("not to be", 10)); |
| 475 |
} |
| 476 |
|
| 477 |
#[wasm_bindgen_test] |
| 478 |
fn substring() { |
| 479 |
let js = JsString::from("Mozilla"); |
| 480 |
|
| 481 |
assert_eq!(js.substring(0, 1), "M"); |
| 482 |
assert_eq!(js.substring(1, 0), "M"); |
| 483 |
|
| 484 |
assert_eq!(js.substring(0, 6), "Mozill"); |
| 485 |
|
| 486 |
// TODO: Add test once we have optional parameters |
| 487 |
// assert_eq!(js.substring(4), "lla"); |
| 488 |
assert_eq!(js.substring(4, 7), "lla"); |
| 489 |
assert_eq!(js.substring(7, 4), "lla"); |
| 490 |
|
| 491 |
assert_eq!(js.substring(0, 7), "Mozilla"); |
| 492 |
assert_eq!(js.substring(0, 10), "Mozilla"); |
| 493 |
} |
| 494 |
|
| 495 |
#[wasm_bindgen_test] |
| 496 |
fn substr() { |
| 497 |
let js = JsString::from("Mozilla"); |
| 498 |
|
| 499 |
assert_eq!(js.substr(0, 1), "M"); |
| 500 |
assert_eq!(js.substr(1, 0), ""); |
| 501 |
assert_eq!(js.substr(-1, 1), "a"); |
| 502 |
assert_eq!(js.substr(1, -1), ""); |
| 503 |
// TODO: Uncomment and test these assertions, once we have support for optional parameters |
| 504 |
// assert_eq!(js.substr(-3), "lla"); |
| 505 |
// assert_eq!(js.substr(1), "ozilla"); |
| 506 |
assert_eq!(js.substr(-20, 2), "Mo"); |
| 507 |
assert_eq!(js.substr(20, 2), ""); |
| 508 |
} |
| 509 |
|
| 510 |
#[wasm_bindgen_test] |
| 511 |
fn to_locale_lower_case() { |
| 512 |
let js = JsString::from("Mozilla"); |
| 513 |
assert_eq!(js.to_locale_lower_case(None), "mozilla"); |
| 514 |
let s = JsString::from("\u{0130}"); |
| 515 |
assert_eq!(s.to_locale_lower_case(Some("tr".into())), "i"); |
| 516 |
assert_ne!(s.to_locale_lower_case(Some("en-US".into())), "i"); |
| 517 |
} |
| 518 |
|
| 519 |
#[wasm_bindgen_test] |
| 520 |
fn to_locale_upper_case() { |
| 521 |
let js = JsString::from("mozilla"); |
| 522 |
assert_eq!(js.to_locale_upper_case(None), "MOZILLA"); |
| 523 |
let s = JsString::from("i\u{0307}"); |
| 524 |
assert_eq!(s.to_locale_upper_case(Some("lt".into())), "I"); |
| 525 |
assert_ne!(s.to_locale_upper_case(Some("en-US".into())), "I"); |
| 526 |
} |
| 527 |
|
| 528 |
#[wasm_bindgen_test] |
| 529 |
fn to_lower_case() { |
| 530 |
assert_eq!(JsString::from("Mozilla").to_lower_case(), "mozilla"); |
| 531 |
} |
| 532 |
|
| 533 |
#[wasm_bindgen_test] |
| 534 |
fn to_string() { |
| 535 |
assert_eq!(JsString::from("foo").to_string(), "foo"); |
| 536 |
} |
| 537 |
|
| 538 |
#[wasm_bindgen_test] |
| 539 |
fn to_upper_case() { |
| 540 |
assert_eq!(JsString::from("Mozilla").to_upper_case(), "MOZILLA"); |
| 541 |
} |
| 542 |
|
| 543 |
#[wasm_bindgen_test] |
| 544 |
fn trim() { |
| 545 |
assert_eq!(JsString::from(" foo ").trim(), "foo"); |
| 546 |
// Another example of .trim() removing whitespace from just one side. |
| 547 |
assert_eq!(JsString::from("foo ").trim(), "foo"); |
| 548 |
} |
| 549 |
|
| 550 |
#[wasm_bindgen_test] |
| 551 |
fn trim_end_and_trim_right() { |
| 552 |
let greeting = JsString::from(" Hello world! "); |
| 553 |
let trimmed = " Hello world!"; |
| 554 |
assert_eq!(greeting.trim_end(), trimmed); |
| 555 |
assert_eq!(greeting.trim_right(), trimmed); |
| 556 |
} |
| 557 |
|
| 558 |
#[wasm_bindgen_test] |
| 559 |
fn trim_start_and_trim_left() { |
| 560 |
let greeting = JsString::from(" Hello world! "); |
| 561 |
let trimmed = "Hello world! "; |
| 562 |
assert_eq!(greeting.trim_start(), trimmed); |
| 563 |
assert_eq!(greeting.trim_left(), trimmed); |
| 564 |
} |
| 565 |
|
| 566 |
#[wasm_bindgen_test] |
| 567 |
fn value_of() { |
| 568 |
let greeting = JsString::from("Hello world!"); |
| 569 |
assert_eq!(greeting.value_of(), "Hello world!"); |
| 570 |
} |
| 571 |
|
| 572 |
#[wasm_bindgen_test] |
| 573 |
fn raw() { |
| 574 |
let call_site = Object::new(); |
| 575 |
let raw = Array::of3(&"foo".into(), &"bar".into(), &"123".into()); |
| 576 |
Reflect::set(&call_site.as_ref(), &"raw".into(), &raw.into()).unwrap(); |
| 577 |
assert_eq!( |
| 578 |
JsString::raw_2(&call_site, "5", "JavaScript").unwrap(), |
| 579 |
"foo5barJavaScript123" |
| 580 |
); |
| 581 |
let substitutions = Array::of2(&"5".into(), &"JavaScript".into()); |
| 582 |
assert_eq!( |
| 583 |
JsString::raw(&call_site, &substitutions).unwrap(), |
| 584 |
"foo5barJavaScript123" |
| 585 |
); |
| 586 |
assert!(JsString::raw_0(&JsValue::null().unchecked_into()).is_err()); |
| 587 |
} |
| 588 |
|
| 589 |
#[wasm_bindgen_test] |
| 590 |
fn is_valid_utf16() { |
| 591 |
assert!(JsString::from("a").is_valid_utf16()); |
| 592 |
assert!(JsString::from("").is_valid_utf16()); |
| 593 |
assert!(JsString::from("🥑").is_valid_utf16()); |
| 594 |
assert!(JsString::from("Why hello there this, 🥑, is 🥑 and is 🥑").is_valid_utf16()); |
| 595 |
|
| 596 |
assert!(JsString::from_char_code1(0x00).is_valid_utf16()); |
| 597 |
assert!(!JsString::from_char_code1(0xd800).is_valid_utf16()); |
| 598 |
assert!(!JsString::from_char_code1(0xdc00).is_valid_utf16()); |
| 599 |
} |
| 600 |
|
| 601 |
#[wasm_bindgen_test] |
| 602 |
fn as_char() { |
| 603 |
assert_eq!(JsString::from('a').as_char(), Some('a')); |
| 604 |
assert_eq!(JsString::from('🥑').as_char(), Some('🥑')); |
| 605 |
assert_eq!(JsString::from("").as_char(), None); |
| 606 |
assert_eq!(JsString::from("ab").as_char(), None); |
| 607 |
assert_eq!(JsString::from_char_code1(0xd800).as_char(), None); |
| 608 |
assert_eq!(JsString::from_char_code1(0xdc00).as_char(), None); |
| 609 |
assert_eq!(JsString::from_char_code1(0xdfff).as_char(), None); |
| 610 |
} |