Debian Codegraph @

.cargo/vendor/js-sys/tests/wasm/Object.rs

raw
1 use js_sys::*;
2 use std::f64::NAN;
3 use wasm_bindgen::prelude::*;
4 use wasm_bindgen_test::*;
5
6 #[wasm_bindgen]
7 extern "C" {
8 type Foo42;
9 #[wasm_bindgen(method, setter, structural)]
10 fn set_foo(this: &Foo42, val: JsValue);
11
12 #[wasm_bindgen(js_name = prototype, js_namespace = Object)]
13 static OBJECT_PROTOTYPE: JsValue;
14 #[wasm_bindgen(js_name = prototype, js_namespace = Array)]
15 static ARRAY_PROTOTYPE: JsValue;
16
17 type DefinePropertyAttrs;
18 #[wasm_bindgen(method, setter, structural)]
19 fn set_value(this: &DefinePropertyAttrs, val: &JsValue);
20
21 type PropertyDescriptor;
22 #[wasm_bindgen(method, getter, structural)]
23 fn value(this: &PropertyDescriptor) -> JsValue;
24 }
25
26 #[wasm_bindgen(module = "tests/wasm/Object.js")]
27 extern "C" {
28 fn map_with_symbol_key() -> Object;
29 fn symbol_key() -> JsValue;
30
31 type Foo;
32 #[wasm_bindgen(constructor)]
33 fn new() -> Foo;
34
35 #[wasm_bindgen(js_name = prototype, js_namespace = Foo)]
36 static FOO_PROTOTYPE: Object;
37 #[wasm_bindgen(js_name = prototype, js_namespace = Bar)]
38 static BAR_PROTOTYPE: Object;
39 }
40
41 fn foo_42() -> Object {
42 let foo = Foo42::from(JsValue::from(Object::new()));
43 foo.set_foo(42.into());
44 JsValue::from(foo).into()
45 }
46
47 #[wasm_bindgen_test]
48 fn new() {
49 assert!(JsValue::from(Object::new()).is_object());
50 }
51
52 #[wasm_bindgen_test]
53 fn assign() {
54 let a = JsValue::from("a");
55 let b = JsValue::from("b");
56 let c = JsValue::from("c");
57
58 let target = Object::new();
59 Reflect::set(target.as_ref(), a.as_ref(), a.as_ref()).unwrap();
60
61 let src1 = Object::new();
62 Reflect::set(src1.as_ref(), &a, &c).unwrap();
63
64 let src2 = Object::new();
65 Reflect::set(src2.as_ref(), &b, &b).unwrap();
66
67 let src3 = Object::new();
68 Reflect::set(src3.as_ref(), &c, &c).unwrap();
69
70 let res = Object::assign3(&target, &src1, &src2, &src3);
71
72 assert!(Object::is(target.as_ref(), res.as_ref()));
73 assert_eq!(Reflect::get(target.as_ref(), &a).unwrap(), c);
74 assert_eq!(Reflect::get(target.as_ref(), &b).unwrap(), b);
75 assert_eq!(Reflect::get(target.as_ref(), &c).unwrap(), c);
76 }
77
78 #[wasm_bindgen_test]
79 fn create() {
80 let array_proto = eval("Array.prototype")
81 .unwrap()
82 .dyn_into::<Object>()
83 .unwrap();
84 let my_array = Object::create(&array_proto);
85 assert!(my_array.is_instance_of::<Array>());
86 }
87
88 #[wasm_bindgen_test]
89 fn define_property() {
90 let value = DefinePropertyAttrs::from(JsValue::from(Object::new()));
91 value.set_value(&43.into());
92 let descriptor = Object::from(JsValue::from(value));
93 let foo = foo_42();
94 let foo = Object::define_property(&foo, &"bar".into(), &descriptor);
95 assert!(foo.has_own_property(&"bar".into()));
96 }
97
98 #[wasm_bindgen_test]
99 fn define_properties() {
100 let props = Object::new();
101 let descriptor = DefinePropertyAttrs::from(JsValue::from(Object::new()));
102 descriptor.set_value(&42.into());
103 let descriptor = JsValue::from(descriptor);
104 Reflect::set(props.as_ref(), &JsValue::from("bar"), &descriptor).unwrap();
105 Reflect::set(props.as_ref(), &JsValue::from("car"), &descriptor).unwrap();
106 let foo = foo_42();
107 let foo = Object::define_properties(&foo, &props);
108 assert!(foo.has_own_property(&"bar".into()));
109 assert!(foo.has_own_property(&"car".into()));
110 }
111
112 #[wasm_bindgen_test]
113 fn entries() {
114 let entries = Object::entries(&foo_42());
115 assert_eq!(entries.length(), 1);
116 entries.for_each(&mut |x, _, _| {
117 assert!(x.is_object());
118 let array: Array = x.into();
119 assert_eq!(array.shift(), "foo");
120 assert_eq!(array.shift(), 42);
121 assert_eq!(array.length(), 0);
122 });
123 }
124
125 #[wasm_bindgen_test]
126 fn from_entries() {
127 let array = Array::new();
128 let entry_one = Array::new();
129 let entry_two = Array::new();
130 entry_one.push(&"foo".into());
131 entry_one.push(&"bar".into());
132 entry_two.push(&"baz".into());
133 entry_two.push(&42.into());
134 array.push(&entry_one);
135 array.push(&entry_two);
136 let object = Object::from_entries(&array).unwrap();
137
138 assert_eq!(Reflect::get(object.as_ref(), &"foo".into()).unwrap(), "bar");
139 assert_eq!(Reflect::get(object.as_ref(), &"baz".into()).unwrap(), 42);
140
141 let not_iterable = Object::new();
142 let error = Object::from_entries(&not_iterable).unwrap_err();
143 assert!(error.is_instance_of::<TypeError>());
144 }
145
146 #[wasm_bindgen_test]
147 fn get_own_property_descriptor() {
148 let foo = foo_42();
149 let desc = Object::get_own_property_descriptor(&foo, &"foo".into());
150 assert_eq!(PropertyDescriptor::from(desc).value(), 42);
151 let desc = Object::get_own_property_descriptor(&foo, &"bar".into());
152 assert!(desc.is_undefined());
153 }
154
155 #[wasm_bindgen_test]
156 fn get_own_property_descriptors() {
157 let foo = foo_42();
158 let descriptors = Object::get_own_property_descriptors(&foo);
159 let foo_desc = Reflect::get(&descriptors, &"foo".into()).unwrap();
160 assert_eq!(PropertyDescriptor::from(foo_desc).value(), 42);
161 }
162
163 #[wasm_bindgen_test]
164 fn get_own_property_names() {
165 let names = Object::get_own_property_names(&foo_42());
166 assert_eq!(names.length(), 1);
167 names.for_each(&mut |x, _, _| {
168 assert_eq!(x, "foo");
169 });
170 }
171
172 #[wasm_bindgen_test]
173 fn get_own_property_symbols() {
174 let symbols = Object::get_own_property_symbols(&map_with_symbol_key());
175 assert_eq!(symbols.length(), 1);
176 }
177
178 #[wasm_bindgen_test]
179 fn get_prototype_of() {
180 let proto = JsValue::from(Object::get_prototype_of(&Object::new().into()));
181 assert_eq!(proto, *OBJECT_PROTOTYPE);
182 let proto = JsValue::from(Object::get_prototype_of(&Array::new().into()));
183 assert_eq!(proto, *ARRAY_PROTOTYPE);
184 }
185
186 #[wasm_bindgen_test]
187 fn has_own_property() {
188 assert!(foo_42().has_own_property(&"foo".into()));
189 assert!(!foo_42().has_own_property(&"bar".into()));
190 assert!(map_with_symbol_key().has_own_property(&symbol_key()));
191 }
192
193 #[wasm_bindgen_test]
194 fn has_own() {
195 assert!(Object::has_own(&foo_42(), &"foo".into()));
196 assert!(!Object::has_own(&foo_42(), &"bar".into()));
197 assert!(Object::has_own(&map_with_symbol_key(), &symbol_key()));
198 }
199
200 #[wasm_bindgen_test]
201 fn to_string() {
202 assert_eq!(Object::new().to_string(), "[object Object]");
203 assert_eq!(foo_42().to_string(), "[object Object]");
204 }
205
206 #[wasm_bindgen_test]
207 fn is() {
208 let object = JsValue::from(Object::new());
209 assert!(Object::is(&object, &object));
210 assert!(Object::is(&JsValue::undefined(), &JsValue::undefined()));
211 assert!(Object::is(&JsValue::null(), &JsValue::null()));
212 assert!(Object::is(&JsValue::TRUE, &JsValue::TRUE));
213 assert!(Object::is(&JsValue::FALSE, &JsValue::FALSE));
214 assert!(Object::is(&"foo".into(), &"foo".into()));
215 assert!(Object::is(&JsValue::from(42), &JsValue::from(42)));
216 assert!(Object::is(&JsValue::from(NAN), &JsValue::from(NAN)));
217
218 let another_object = JsValue::from(Object::new());
219 assert!(!Object::is(&object, &another_object));
220 assert!(!Object::is(&JsValue::TRUE, &JsValue::FALSE));
221 assert!(!Object::is(&"foo".into(), &"bar".into()));
222 assert!(!Object::is(&JsValue::from(23), &JsValue::from(42)));
223 }
224
225 #[wasm_bindgen_test]
226 fn is_extensible() {
227 let object = Object::new();
228 assert!(Object::is_extensible(&object));
229 Object::prevent_extensions(&object);
230 assert!(!Object::is_extensible(&object));
231 }
232
233 #[wasm_bindgen_test]
234 fn is_frozen() {
235 let object = Object::new();
236 assert!(!Object::is_frozen(&object));
237 Object::freeze(&object);
238 assert!(Object::is_frozen(&object));
239 }
240
241 #[wasm_bindgen_test]
242 fn is_sealed() {
243 let object = Object::new();
244 assert!(!Object::is_sealed(&object));
245 Object::seal(&object);
246 assert!(Object::is_sealed(&object));
247 }
248
249 #[wasm_bindgen_test]
250 fn is_prototype_of() {
251 let foo = JsValue::from(Foo::new());
252 assert!(FOO_PROTOTYPE.is_prototype_of(&foo));
253 assert!(!BAR_PROTOTYPE.is_prototype_of(&foo));
254 }
255
256 #[wasm_bindgen_test]
257 fn keys() {
258 let keys = Object::keys(&foo_42());
259 assert_eq!(keys.length(), 1);
260 keys.for_each(&mut |x, _, _| {
261 assert_eq!(x, "foo");
262 });
263 }
264
265 #[wasm_bindgen_test]
266 fn values() {
267 let values = Object::values(&foo_42());
268 assert_eq!(values.length(), 1);
269 values.for_each(&mut |x, _, _| {
270 assert_eq!(x, 42);
271 });
272 }
273
274 #[wasm_bindgen_test]
275 fn property_is_enumerable() {
276 assert!(foo_42().property_is_enumerable(&"foo".into()));
277 assert!(!foo_42().property_is_enumerable(&42.into()));
278 assert!(!Object::new().property_is_enumerable(&"foo".into()));
279 }
280
281 #[wasm_bindgen_test]
282 fn set_prototype_of() {
283 let a = foo_42();
284 let b = foo_42();
285 Object::set_prototype_of(&a, &b);
286 assert!(b.is_prototype_of(&a.into()));
287 }
288
289 #[wasm_bindgen_test]
290 fn to_locale_string() {
291 assert_eq!(Object::new().to_locale_string(), "[object Object]");
292 }
293
294 #[wasm_bindgen_test]
295 fn value_of() {
296 let a = JsValue::from(foo_42());
297 let b = JsValue::from(foo_42());
298 let a2 = JsValue::from(Object::from(a.clone()).value_of());
299 assert_eq!(a, a);
300 assert_eq!(a, a2);
301 assert_ne!(a, b);
302 assert_ne!(a2, b);
303 }