Symbol.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use js_sys::*;
  2. use wasm_bindgen::prelude::*;
  3. use wasm_bindgen_futures::JsFuture;
  4. use wasm_bindgen_test::*;
  5. #[wasm_bindgen(module = "tests/wasm/Symbol.js")]
  6. extern "C" {
  7. fn test_has_instance(sym: &Symbol);
  8. fn test_is_concat_spreadable(sym: &Symbol);
  9. fn test_iterator(sym: &Symbol);
  10. fn test_async_iterator(sym: &Symbol) -> Promise;
  11. fn test_match(sym: &Symbol);
  12. fn test_replace(sym: &Symbol);
  13. fn test_search(sym: &Symbol);
  14. fn test_species(sym: &Symbol);
  15. fn test_split(sym: &Symbol);
  16. fn test_to_primitive(sym: &Symbol);
  17. fn test_to_string_tag(sym: &Symbol);
  18. }
  19. #[wasm_bindgen]
  20. extern "C" {
  21. #[wasm_bindgen(js_name = Symbol)]
  22. fn gensym(val: JsValue) -> Symbol;
  23. }
  24. #[wasm_bindgen_test]
  25. fn has_instance() {
  26. test_has_instance(&Symbol::has_instance());
  27. }
  28. #[wasm_bindgen_test]
  29. fn is_concat_spreadable() {
  30. test_is_concat_spreadable(&Symbol::is_concat_spreadable());
  31. }
  32. #[wasm_bindgen_test]
  33. fn iterator() {
  34. test_iterator(&Symbol::iterator());
  35. }
  36. #[wasm_bindgen_test]
  37. async fn async_iterator() {
  38. JsFuture::from(test_async_iterator(&Symbol::async_iterator()))
  39. .await
  40. .unwrap_throw();
  41. }
  42. #[wasm_bindgen_test]
  43. fn match_() {
  44. test_match(&Symbol::match_());
  45. }
  46. #[wasm_bindgen_test]
  47. fn replace() {
  48. test_replace(&Symbol::replace());
  49. }
  50. #[wasm_bindgen_test]
  51. fn search() {
  52. test_search(&Symbol::search());
  53. }
  54. #[wasm_bindgen_test]
  55. fn species() {
  56. test_species(&Symbol::species());
  57. }
  58. #[wasm_bindgen_test]
  59. fn split() {
  60. test_split(&Symbol::split());
  61. }
  62. #[wasm_bindgen_test]
  63. fn to_primitive() {
  64. test_to_primitive(&Symbol::to_primitive());
  65. }
  66. #[wasm_bindgen_test]
  67. fn to_string_tag() {
  68. test_to_string_tag(&Symbol::to_string_tag());
  69. }
  70. #[wasm_bindgen_test]
  71. fn for_() {
  72. let foo = JsValue::from(Symbol::for_("foo"));
  73. let bar = JsValue::from(Symbol::for_("bar"));
  74. assert_eq!(foo, foo);
  75. assert_eq!(bar, bar);
  76. assert_ne!(foo, bar);
  77. assert_ne!(bar, foo);
  78. assert_eq!(Symbol::for_("mario").to_string(), "Symbol(mario)");
  79. }
  80. #[wasm_bindgen_test]
  81. fn key_for() {
  82. let sym = Symbol::for_("foo");
  83. assert_eq!(Symbol::key_for(&sym), "foo");
  84. assert!(Symbol::key_for(&Symbol::iterator()).is_undefined());
  85. assert!(Symbol::key_for(&Symbol::async_iterator()).is_undefined());
  86. assert!(Symbol::key_for(&gensym(JsValue::undefined())).is_undefined());
  87. }
  88. #[wasm_bindgen_test]
  89. fn to_string() {
  90. assert_eq!(Symbol::iterator().to_string(), "Symbol(Symbol.iterator)");
  91. assert_eq!(
  92. Symbol::async_iterator().to_string(),
  93. "Symbol(Symbol.asyncIterator)"
  94. );
  95. assert_eq!(Symbol::for_("foo").to_string(), "Symbol(foo)");
  96. assert_eq!(gensym("desc".into()).to_string(), "Symbol(desc)");
  97. }
  98. #[wasm_bindgen_test]
  99. fn unscopables() {
  100. assert_eq!(
  101. Symbol::unscopables().to_string(),
  102. "Symbol(Symbol.unscopables)"
  103. );
  104. }
  105. #[wasm_bindgen_test]
  106. fn value_of() {
  107. let a = Symbol::for_("foo");
  108. assert_eq!(JsValue::from(a.value_of()), JsValue::from(a));
  109. let a = gensym(JsValue::undefined());
  110. assert_eq!(JsValue::from(a.value_of()), JsValue::from(a));
  111. }