Debian Codegraph @

.cargo/vendor/wasm-bindgen/tests/wasm/import_class.rs

raw
1 //! dox
2
3 #![deny(missing_docs)] // test that documenting public bindings is enough
4 #![allow(clippy::redundant_clone)] // test specifically with cloned objects
5
6 use wasm_bindgen::prelude::*;
7 use wasm_bindgen_test::*;
8
9 #[wasm_bindgen(module = "tests/wasm/import_class.js")]
10 extern "C" {
11 fn math_log(f: f64) -> f64;
12
13 #[wasm_bindgen(js_namespace = StaticFunction)]
14 fn bar() -> u32;
15
16 #[derive(Clone)]
17 type Construct;
18 #[wasm_bindgen(js_namespace = Construct)]
19 fn create() -> Construct;
20 #[wasm_bindgen(method)]
21 fn get_internal_string(this: &Construct) -> String;
22 #[wasm_bindgen(method)]
23 fn append_to_internal_string(this: &Construct, s: &str);
24 #[wasm_bindgen(method)]
25 fn assert_internal_string(this: &Construct, s: &str);
26
27 #[wasm_bindgen(method, js_name = "kebab-case")]
28 fn kebab_case(this: &Construct) -> u32;
29 #[wasm_bindgen(method, getter, js_name = "kebab-case-val")]
30 fn kebab_case_val(this: &Construct) -> u32;
31 #[wasm_bindgen(method, setter, js_name = "kebab-case-val")]
32 fn set_kebab_case_val(this: &Construct, val: u32);
33 #[wasm_bindgen(static_method_of = Construct, js_name = "static-kebab-case")]
34 fn static_kebab_case() -> u32;
35 #[wasm_bindgen(static_method_of = Construct, getter, js_name = "static-kebab-case-val")]
36 fn static_kebab_case_val() -> u32;
37 #[wasm_bindgen(static_method_of = Construct, setter, js_name = "static-kebab-case-val")]
38 fn set_static_kebab_case_val(val: u32);
39
40 type NewConstructors;
41 #[wasm_bindgen(constructor)]
42 fn new(arg: i32) -> NewConstructors;
43 #[wasm_bindgen(method)]
44 fn get(this: &NewConstructors) -> i32;
45
46 #[wasm_bindgen(js_name = default)]
47 type RenamedTypes;
48 #[wasm_bindgen(constructor, js_class = default)]
49 fn new(arg: i32) -> RenamedTypes;
50 #[wasm_bindgen(method, js_class = default)]
51 fn get(this: &RenamedTypes) -> i32;
52
53 fn switch_methods_a();
54 fn switch_methods_b();
55 type SwitchMethods;
56 #[wasm_bindgen(constructor)]
57 #[wasm_bindgen(final)]
58 fn new() -> SwitchMethods;
59 #[wasm_bindgen(js_namespace = SwitchMethods, final)]
60 fn a();
61 fn switch_methods_called() -> bool;
62 #[wasm_bindgen(method, final)]
63 fn b(this: &SwitchMethods);
64
65 type Properties;
66 #[wasm_bindgen(constructor)]
67 fn new() -> Properties;
68 #[wasm_bindgen(getter, method)]
69 fn a(this: &Properties) -> i32;
70 #[wasm_bindgen(setter, method)]
71 fn set_a(this: &Properties, a: i32);
72
73 type RenameProperties;
74 #[wasm_bindgen(constructor)]
75 fn new() -> RenameProperties;
76 #[wasm_bindgen(getter = a, method)]
77 fn test(this: &RenameProperties) -> i32;
78 #[wasm_bindgen(getter, method, js_name = a)]
79 fn test2(this: &RenameProperties) -> i32;
80 #[wasm_bindgen(setter = a, method)]
81 fn another(this: &RenameProperties, a: i32);
82 #[wasm_bindgen(setter, method, js_name = a)]
83 fn another2(this: &RenameProperties, a: i32);
84
85 /// dox
86 pub type AssertImportDenyDocsWorks;
87 /// dox
88 #[wasm_bindgen(constructor)]
89 pub fn new() -> AssertImportDenyDocsWorks;
90 /// dox
91 #[wasm_bindgen(getter = a, method)]
92 pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
93 /// dox
94 pub fn foo();
95
96 pub type Options;
97 #[wasm_bindgen(constructor)]
98 fn new() -> Options;
99 fn take_none(val: Option<Options>);
100 fn take_some(val: Option<Options>);
101 fn return_null() -> Option<Options>;
102 fn return_undefined() -> Option<Options>;
103 fn return_some() -> Option<Options>;
104 fn run_rust_option_tests();
105
106 type CatchConstructors;
107 #[wasm_bindgen(constructor, catch)]
108 fn new(x: u32) -> Result<CatchConstructors, JsValue>;
109
110 type StaticStructural;
111 #[wasm_bindgen(static_method_of = StaticStructural, structural)]
112 fn static_structural(a: u32) -> u32;
113
114 #[derive(Clone)]
115 type InnerClass;
116 #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
117 fn inner_static_function(a: u32) -> u32;
118 #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
119 fn create_inner_instance() -> InnerClass;
120 #[wasm_bindgen(method)]
121 fn get_internal_int(this: &InnerClass) -> u32;
122 #[wasm_bindgen(method)]
123 fn append_to_internal_int(this: &InnerClass, i: u32);
124 #[wasm_bindgen(method)]
125 fn assert_internal_int(this: &InnerClass, i: u32);
126 }
127
128 #[wasm_bindgen(js_namespace = Math)]
129 extern "C" {
130 #[wasm_bindgen]
131 fn random() -> f64;
132 #[wasm_bindgen]
133 fn log(a: f64) -> f64;
134 }
135
136 #[wasm_bindgen_test]
137 fn simple() {
138 random();
139 assert_eq!(log(1.0), math_log(1.0));
140 }
141
142 #[wasm_bindgen_test]
143 fn import_class() {
144 assert_eq!(bar(), 2);
145 }
146
147 #[wasm_bindgen_test]
148 fn construct() {
149 let f = Construct::create();
150 assert_eq!(f.get_internal_string(), "this");
151 assert_eq!(f.clone().get_internal_string(), "this");
152 f.append_to_internal_string(" foo");
153 f.assert_internal_string("this foo");
154
155 assert_eq!(f.kebab_case(), 42);
156 f.set_kebab_case_val(0);
157 // our setter does nothing so this is 42 anyway
158 assert_eq!(f.kebab_case_val(), 42);
159 assert_eq!(Construct::static_kebab_case(), 42);
160 Construct::set_static_kebab_case_val(0);
161 assert_eq!(Construct::static_kebab_case_val(), 42);
162 }
163
164 #[wasm_bindgen_test]
165 fn new_constructors() {
166 let f = NewConstructors::new(1);
167 assert_eq!(f.get(), 2);
168 }
169
170 #[wasm_bindgen_test]
171 fn rename_type() {
172 let f = RenamedTypes::new(1);
173 assert_eq!(f.get(), 2);
174 }
175
176 #[wasm_bindgen_test]
177 fn switch_methods() {
178 assert!(!switch_methods_called());
179 SwitchMethods::a();
180 assert!(switch_methods_called());
181
182 switch_methods_a();
183
184 assert!(!switch_methods_called());
185 SwitchMethods::a();
186 assert!(switch_methods_called());
187
188 assert!(!switch_methods_called());
189 SwitchMethods::new().b();
190 assert!(switch_methods_called());
191
192 switch_methods_b();
193
194 assert!(!switch_methods_called());
195 SwitchMethods::new().b();
196 assert!(!switch_methods_called());
197 }
198
199 #[wasm_bindgen_test]
200 fn properties() {
201 let a = Properties::new();
202 assert_eq!(a.a(), 1);
203 a.set_a(2);
204 assert_eq!(a.a(), 2);
205 }
206
207 #[wasm_bindgen_test]
208 fn rename_setter_getter() {
209 let x: fn() -> RenameProperties = RenameProperties::new;
210 let a = x();
211 assert_eq!(a.test(), 1);
212 a.another(2);
213 assert_eq!(a.test(), 2);
214 a.another2(3);
215 assert_eq!(a.test2(), 3);
216 }
217
218 /// dox
219 #[wasm_bindgen]
220 pub struct AssertDenyDocsWorks {
221 /// dox
222 pub a: u32,
223 _b: i64,
224 }
225
226 /// dox
227 #[wasm_bindgen]
228 pub fn assert_deny_docs_works() {}
229
230 #[wasm_bindgen_test]
231 fn options() {
232 take_none(None);
233 take_some(Some(Options::new()));
234 assert!(return_null().is_none());
235 assert!(return_undefined().is_none());
236 assert!(return_some().is_some());
237 run_rust_option_tests();
238 }
239
240 /// doc
241 #[wasm_bindgen]
242 pub fn rust_take_none(a: Option<Options>) {
243 assert!(a.is_none());
244 }
245
246 /// doc
247 #[wasm_bindgen]
248 pub fn rust_take_some(a: Option<Options>) {
249 assert!(a.is_some());
250 }
251
252 /// doc
253 #[wasm_bindgen]
254 pub fn rust_return_none() -> Option<Options> {
255 None
256 }
257
258 /// doc
259 #[wasm_bindgen]
260 pub fn rust_return_some() -> Option<Options> {
261 Some(Options::new())
262 }
263
264 #[wasm_bindgen_test]
265 fn catch_constructors() {
266 assert!(CatchConstructors::new(0).is_err());
267 assert!(CatchConstructors::new(1).is_ok());
268 }
269
270 #[wasm_bindgen_test]
271 fn static_structural() {
272 assert_eq!(StaticStructural::static_structural(30), 33);
273 }
274
275 #[wasm_bindgen_test]
276 fn nested_namespace() {
277 assert_eq!(InnerClass::inner_static_function(15), 20);
278
279 let f = InnerClass::create_inner_instance();
280 assert_eq!(f.get_internal_int(), 3);
281 assert_eq!(f.clone().get_internal_int(), 3);
282 f.append_to_internal_int(5);
283 f.assert_internal_int(8);
284 }